Welcome to mirror list, hosted at ThFree Co, Russian Federation.

OdePhysicsEnvironment.cpp « BlOde « Physics « gameengine « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8023b791be044ce930ed72bb5b5230a39face5ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
 * $Id$
 *
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
 *
 * The contents of this file may be used under the terms of either the GNU
 * General Public License Version 2 or later (the "GPL", see
 * http://www.gnu.org/licenses/gpl.html ), or the Blender License 1.0 or
 * later (the "BL", see http://www.blender.org/BL/ ) which has to be
 * bought from the Blender Foundation to become active, in which case the
 * above mentioned GPL option does not apply.
 *
 * The Original Code is Copyright (C) 2002 by NaN Holding BV.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
 */
#include "OdePhysicsEnvironment.h"
#include "PHY_IMotionState.h"
#include "OdePhysicsController.h"

// Ode
#include <ode/config.h>
#include <ode/ode.h>
#include <../ode/src/joint.h>
#include <ode/odemath.h>


ODEPhysicsEnvironment::ODEPhysicsEnvironment()
{
	m_OdeWorld = dWorldCreate();
	m_OdeSpace = dHashSpaceCreate();
	m_OdeContactGroup = dJointGroupCreate (0);
	dWorldSetCFM (m_OdeWorld,1e-5f);

	m_JointGroup = dJointGroupCreate(0);
	
}



ODEPhysicsEnvironment::~ODEPhysicsEnvironment()
{
	dJointGroupDestroy (m_OdeContactGroup);
	dJointGroupDestroy (m_JointGroup);

	dSpaceDestroy (m_OdeSpace);
	dWorldDestroy (m_OdeWorld);
}

void ODEPhysicsEnvironment::proceed(double timeStep)
{
	// ode collision update
	dSpaceCollide (m_OdeSpace,this,&ODEPhysicsEnvironment::OdeNearCallback);

	int m_odeContacts = GetNumOdeContacts();
	
	//physics integrator + resolver update
	dWorldStep (m_OdeWorld,timeStep);
	
	//clear collision points
	this->ClearOdeContactGroup();
}

void ODEPhysicsEnvironment::setGravity(float x,float y,float z)
{
	dWorldSetGravity (m_OdeWorld,x,y,z);
}



int			ODEPhysicsEnvironment::createConstraint(class PHY_IPhysicsController* ctrl,class PHY_IPhysicsController* ctrl2,PHY_ConstraintType type,
		float pivotX,float pivotY,float pivotZ,float axisX,float axisY,float axisZ)
{

	int constraintid = 0;
	ODEPhysicsController* dynactrl = (ODEPhysicsController*)ctrl;
	ODEPhysicsController* dynactrl2 = (ODEPhysicsController*)ctrl2;

	switch (type)
	{
	case PHY_POINT2POINT_CONSTRAINT:
		{
			if (dynactrl)
			{
				dJointID jointid = dJointCreateBall (m_OdeWorld,m_JointGroup);
				struct	dxBody*	bodyid1 = dynactrl->GetOdeBodyId();
				struct	dxBody*	bodyid2=0;
				const float* pos = dBodyGetPosition(bodyid1);
				const float* R = dBodyGetRotation(bodyid1);
				float offset[3] = {pivotX,pivotY,pivotZ};
				float newoffset[3];
				dMULTIPLY0_331 (newoffset,R,offset);
				newoffset[0] += pos[0];
				newoffset[1] += pos[1];
				newoffset[2] += pos[2];
				

				if (dynactrl2)
					bodyid2 = dynactrl2->GetOdeBodyId();
				
				dJointAttach (jointid, bodyid1, bodyid2);
				
				dJointSetBallAnchor (jointid, newoffset[0], newoffset[1], newoffset[2]);
				
				constraintid = (int) jointid;
			}
			break;
		}
	case PHY_LINEHINGE_CONSTRAINT:
	{
			if (dynactrl)
			{
				dJointID jointid = dJointCreateHinge (m_OdeWorld,m_JointGroup);
				struct	dxBody*	bodyid1 = dynactrl->GetOdeBodyId();
				struct	dxBody*	bodyid2=0;
				const float* pos = dBodyGetPosition(bodyid1);
				const float* R = dBodyGetRotation(bodyid1);
				float offset[3] = {pivotX,pivotY,pivotZ};
				float axisset[3] = {axisX,axisY,axisZ};
				
				float newoffset[3];
				float newaxis[3];
				dMULTIPLY0_331 (newaxis,R,axisset);
				
				dMULTIPLY0_331 (newoffset,R,offset);
				newoffset[0] += pos[0];
				newoffset[1] += pos[1];
				newoffset[2] += pos[2];
				

				if (dynactrl2)
					bodyid2 = dynactrl2->GetOdeBodyId();
				
				dJointAttach (jointid, bodyid1, bodyid2);
				
				dJointSetHingeAnchor (jointid, newoffset[0], newoffset[1], newoffset[2]);
				dJointSetHingeAxis(jointid,newaxis[0],newaxis[1],newaxis[2]);

				constraintid = (int) jointid;
			}
			break;
		}
	default:
		{
			//not yet
		}
	}
	
	return constraintid;

}

void		ODEPhysicsEnvironment::removeConstraint(int constraintid)
{
	if (constraintid)
	{
		dJointDestroy((dJointID) constraintid);
	}
}

PHY_IPhysicsController* ODEPhysicsEnvironment::rayTest(void* ignoreClient,float fromX,float fromY,float fromZ, float toX,float toY,float toZ, 
									float& hitX,float& hitY,float& hitZ,float& normalX,float& normalY,float& normalZ)
{
	//collision detection / raytesting
	return NULL;
}


void ODEPhysicsEnvironment::OdeNearCallback (void *data, dGeomID o1, dGeomID o2)
{
	// \todo if this is a registered collision sensor
	// fire the callback

	int i;
	// if (o1->body && o2->body) return;
	ODEPhysicsEnvironment* env = (ODEPhysicsEnvironment*) data;
	dBodyID b1,b2;
	
	b1 = dGeomGetBody(o1);
	b2 = dGeomGetBody(o2);
	// exit without doing anything if the two bodies are connected by a joint
	if (b1 && b2 && dAreConnected (b1,b2)) return;

	ODEPhysicsController * ctrl1 =(ODEPhysicsController *)dGeomGetData(o1);
	ODEPhysicsController * ctrl2 =(ODEPhysicsController *)dGeomGetData(o2);
	float friction=ctrl1->getFriction();
	float restitution = ctrl1->getRestitution();
	//for friction, take minimum

	friction=(friction < ctrl2->getFriction() ?  
	friction :ctrl2->getFriction());

	//restitution:take minimum
	restitution = restitution < ctrl2->getRestitution()?
	restitution : ctrl2->getRestitution();

	dContact contact[3];			// up to 3 contacts per box
	for (i=0; i<3; i++) {
		contact[i].surface.mode = dContactBounce; //dContactMu2;
		contact[i].surface.mu = friction;//dInfinity;
		contact[i].surface.mu2 = 0;
		contact[i].surface.bounce = restitution;//0.5;
		contact[i].surface.bounce_vel = 0.1f;
		contact[i].surface.slip1=0.0;
	}
	
	if (int numc = dCollide (o1,o2,3,&contact[0].geom,sizeof(dContact))) {
		// dMatrix3 RI;
		// dRSetIdentity (RI);
		// const dReal ss[3] = {0.02,0.02,0.02};
		for (i=0; i<numc; i++) {
			dJointID c = dJointCreateContact (env->m_OdeWorld,env->m_OdeContactGroup,contact+i);
			dJointAttach (c,b1,b2);
		}
	}
}


void	ODEPhysicsEnvironment::ClearOdeContactGroup()
{
	dJointGroupEmpty (m_OdeContactGroup);
}

int	ODEPhysicsEnvironment::GetNumOdeContacts()
{
	return m_OdeContactGroup->num;
}