aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim.Physics
diff options
context:
space:
mode:
authorMW2007-03-30 15:19:15 +0000
committerMW2007-03-30 15:19:15 +0000
commite4316394c300ef470fad04774844779888c7a5ea (patch)
tree43f1063379abf3ba0b520e38b7dea26c48019db3 /OpenSim.Physics
parentClothing now seems to work (though there are likely to still be some problems) (diff)
downloadopensim-SC-e4316394c300ef470fad04774844779888c7a5ea.zip
opensim-SC-e4316394c300ef470fad04774844779888c7a5ea.tar.gz
opensim-SC-e4316394c300ef470fad04774844779888c7a5ea.tar.bz2
opensim-SC-e4316394c300ef470fad04774844779888c7a5ea.tar.xz
Done some fixes to the ODE plugin, but its still not working correctly, believe that the terrain is rotated /placed wrongly.
Diffstat (limited to '')
-rw-r--r--OpenSim.Physics/OdePlugin/OdePlugin.cs788
1 files changed, 409 insertions, 379 deletions
diff --git a/OpenSim.Physics/OdePlugin/OdePlugin.cs b/OpenSim.Physics/OdePlugin/OdePlugin.cs
index 7b0d64a..325e2fd 100644
--- a/OpenSim.Physics/OdePlugin/OdePlugin.cs
+++ b/OpenSim.Physics/OdePlugin/OdePlugin.cs
@@ -31,387 +31,417 @@ using Ode.NET;
31 31
32namespace OpenSim.Physics.OdePlugin 32namespace OpenSim.Physics.OdePlugin
33{ 33{
34 /// <summary> 34 /// <summary>
35 /// ODE plugin 35 /// ODE plugin
36 /// </summary> 36 /// </summary>
37 public class OdePlugin : IPhysicsPlugin 37 public class OdePlugin : IPhysicsPlugin
38 { 38 {
39 private OdeScene _mScene; 39 private OdeScene _mScene;
40 40
41 public OdePlugin() 41 public OdePlugin()
42 { 42 {
43 43
44 } 44 }
45 45
46 public bool Init() 46 public bool Init()
47 { 47 {
48 return true; 48 return true;
49 } 49 }
50 50
51 public PhysicsScene GetScene() 51 public PhysicsScene GetScene()
52 { 52 {
53 if(_mScene == null) 53 if (_mScene == null)
54 { 54 {
55 _mScene = new OdeScene(); 55 _mScene = new OdeScene();
56 } 56 }
57 return(_mScene); 57 return (_mScene);
58 } 58 }
59 59
60 public string GetName() 60 public string GetName()
61 { 61 {
62 return("OpenDynamicsEngine"); 62 return ("OpenDynamicsEngine");
63 } 63 }
64 64
65 public void Dispose() 65 public void Dispose()
66 { 66 {
67 67
68 } 68 }
69 } 69 }
70 70
71 public class OdeScene :PhysicsScene 71 public class OdeScene : PhysicsScene
72 { 72 {
73 static public IntPtr world; 73 static public IntPtr world;
74 static public IntPtr space; 74 static public IntPtr space;
75 static private IntPtr contactgroup; 75 static private IntPtr contactgroup;
76 static private IntPtr LandGeom; 76 static private IntPtr LandGeom;
77 static private IntPtr Land; 77 static private IntPtr Land;
78 private double[] _heightmap; 78 private double[] _heightmap;
79 static private d.NearCallback nearCallback = near; 79 static private d.NearCallback nearCallback = near;
80 private List<OdeCharacter> _characters = new List<OdeCharacter>(); 80 private List<OdeCharacter> _characters = new List<OdeCharacter>();
81 private static d.ContactGeom[] contacts = new d.ContactGeom[500]; 81 private static d.ContactGeom[] contacts = new d.ContactGeom[30];
82 private static d.Contact contact; 82 private static d.Contact contact;
83 83
84 public OdeScene() { 84 public OdeScene()
85 world = d.WorldCreate(); 85 {
86 space = d.HashSpaceCreate(IntPtr.Zero); 86 contact.surface.mode = d.ContactFlags.Bounce | d.ContactFlags.SoftCFM;
87 contactgroup = d.JointGroupCreate(0); 87 contact.surface.mu = d.Infinity;
88 d.WorldSetGravity(world, 0.0f, 0.0f, -0.5f); 88 contact.surface.mu2 = 0.0f;
89 d.WorldSetCFM(world, 1e-5f); 89 contact.surface.bounce = 0.1f;
90 d.WorldSetAutoDisableFlag(world, false); 90 contact.surface.bounce_vel = 0.1f;
91 this._heightmap=new double[65536]; 91 contact.surface.soft_cfm = 0.01f;
92 } 92
93 93 world = d.WorldCreate();
94 // This function blatantly ripped off from BoxStack.cs 94 space = d.HashSpaceCreate(IntPtr.Zero);
95 static private void near(IntPtr space, IntPtr g1, IntPtr g2) 95 contactgroup = d.JointGroupCreate(0);
96 { 96 d.WorldSetGravity(world, 0.0f, 0.0f, -0.5f);
97 IntPtr b1 = d.GeomGetBody(g1); 97 //d.WorldSetCFM(world, 1e-5f);
98 IntPtr b2 = d.GeomGetBody(g2); 98 d.WorldSetAutoDisableFlag(world, false);
99 if (b1 != IntPtr.Zero && b2 != IntPtr.Zero && d.AreConnectedExcluding(b1, b2, d.JointType.Contact)) 99 d.WorldSetContactSurfaceLayer(world, 0.001f);
100 return; 100 // d.CreatePlane(space, 0, 0, 1, 0);
101 101 this._heightmap = new double[65536];
102 int count = d.Collide(g1, g2, 500, contacts, d.ContactGeom.SizeOf); 102 }
103 for (int i = 0; i < count; ++i) 103
104 { 104 // This function blatantly ripped off from BoxStack.cs
105 contact.geom = contacts[i]; 105 static private void near(IntPtr space, IntPtr g1, IntPtr g2)
106 IntPtr joint = d.JointCreateContact(world, contactgroup, ref contact); 106 {
107 d.JointAttach(joint, b1, b2); 107 //Console.WriteLine("collision callback");
108 } 108 IntPtr b1 = d.GeomGetBody(g1);
109 109 IntPtr b2 = d.GeomGetBody(g2);
110 } 110 if (b1 != IntPtr.Zero && b2 != IntPtr.Zero && d.AreConnectedExcluding(b1, b2, d.JointType.Contact))
111 111 return;
112 public override PhysicsActor AddAvatar(PhysicsVector position) 112
113 { 113 int count = d.Collide(g1, g2, 500, contacts, d.ContactGeom.SizeOf);
114 PhysicsVector pos = new PhysicsVector(); 114 for (int i = 0; i < count; ++i)
115 pos.X = position.X; 115 {
116 pos.Y = position.Y; 116 contact.geom = contacts[i];
117 pos.Z = position.Z; 117 IntPtr joint = d.JointCreateContact(world, contactgroup, ref contact);
118 OdeCharacter newAv= new OdeCharacter(this,pos); 118 d.JointAttach(joint, b1, b2);
119 this._characters.Add(newAv); 119 }
120 return newAv; 120
121 } 121 }
122 122
123 public override PhysicsActor AddPrim(PhysicsVector position, PhysicsVector size) 123 public override PhysicsActor AddAvatar(PhysicsVector position)
124 { 124 {
125 PhysicsVector pos = new PhysicsVector(); 125 PhysicsVector pos = new PhysicsVector();
126 pos.X = position.X; 126 pos.X = position.X;
127 pos.Y = position.Y; 127 pos.Y = position.Y;
128 pos.Z = position.Z; 128 pos.Z = position.Z + 20;
129 PhysicsVector siz = new PhysicsVector(); 129 OdeCharacter newAv = new OdeCharacter(this, pos);
130 siz.X = size.X; 130 this._characters.Add(newAv);
131 siz.Y = size.Y; 131 return newAv;
132 siz.Z = size.Z; 132 }
133 return new OdePrim(); 133
134 } 134 public override PhysicsActor AddPrim(PhysicsVector position, PhysicsVector size)
135 135 {
136 public override void Simulate(float timeStep) 136 PhysicsVector pos = new PhysicsVector();
137 { 137 pos.X = position.X;
138 foreach (OdeCharacter actor in _characters) { 138 pos.Y = position.Y;
139 actor.Move(timeStep*5f); 139 pos.Z = position.Z;
140 } 140 PhysicsVector siz = new PhysicsVector();
141 d.SpaceCollide(space, IntPtr.Zero, nearCallback); 141 siz.X = size.X;
142 d.WorldQuickStep(world, timeStep*5f); 142 siz.Y = size.Y;
143 d.JointGroupEmpty(contactgroup); 143 siz.Z = size.Z;
144 foreach (OdeCharacter actor in _characters) 144 return new OdePrim();
145 { 145 }
146 actor.UpdatePosition(); 146
147 } 147 public override void Simulate(float timeStep)
148 148 {
149 } 149 foreach (OdeCharacter actor in _characters)
150 150 {
151 public override void GetResults() 151 actor.Move(timeStep * 5f);
152 { 152 }
153 153 d.SpaceCollide(space, IntPtr.Zero, nearCallback);
154 } 154 d.WorldQuickStep(world, timeStep * 5f);
155 155 d.JointGroupEmpty(contactgroup);
156 public override bool IsThreaded 156 foreach (OdeCharacter actor in _characters)
157 { 157 {
158 get 158 actor.UpdatePosition();
159 { 159 }
160 return(false); // for now we won't be multithreaded 160
161 } 161 }
162 } 162
163 163 public override void GetResults()
164 public override void SetTerrain(float[] heightMap) 164 {
165 { 165
166 for(int i=0; i<65536; i++) { 166 }
167 this._heightmap[i]=(double)heightMap[i]; 167
168 } 168 public override bool IsThreaded
169 IntPtr HeightmapData = d.GeomHeightfieldDataCreate(); 169 {
170 d.GeomHeightfieldDataBuildDouble(HeightmapData,_heightmap,1,256,256,256,256,1.0f,0.0f,2.0f,0); 170 get
171 LandGeom=d.CreateHeightfield(space, HeightmapData, 0); 171 {
172 } 172 return (false); // for now we won't be multithreaded
173 }
174 }
175
176 public override void SetTerrain(float[] heightMap)
177 {
178 for (int i = 0; i < 65536; i++)
179 {
180 this._heightmap[i] = (double)heightMap[i];
181 }
182 IntPtr HeightmapData = d.GeomHeightfieldDataCreate();
183 d.GeomHeightfieldDataBuildDouble(HeightmapData, _heightmap, 0, 256, 256, 256, 256, 1.0f, 0.0f, 2.0f, 0);
184 d.GeomHeightfieldDataSetBounds(HeightmapData, 256, 256);
185 LandGeom = d.CreateHeightfield(space, HeightmapData, 1);
186 d.Matrix3 R = new d.Matrix3();
187
188 Axiom.MathLib.Quaternion q1 =Axiom.MathLib.Quaternion.FromAngleAxis(1.5707f, new Axiom.MathLib.Vector3(1,0,0));
189 Axiom.MathLib.Quaternion q2 =Axiom.MathLib.Quaternion.FromAngleAxis(1.5707f, new Axiom.MathLib.Vector3(0,1,0));
190 //Axiom.MathLib.Quaternion q3 = Axiom.MathLib.Quaternion.FromAngleAxis(3.14f, new Axiom.MathLib.Vector3(0, 0, 1));
191
192 q1 = q1 * q2;
193 //q1 = q1 * q3;
194 Axiom.MathLib.Vector3 v3 = new Axiom.MathLib.Vector3();
195 float angle = 0;
196 q1.ToAngleAxis(ref angle, ref v3);
197
198 d.RFromAxisAndAngle(out R, v3.x, v3.y, v3.z, angle);
199 d.GeomSetRotation(LandGeom, ref R);
200 d.GeomSetPosition(LandGeom, 128, 128, 0);
201 }
173 202
174 public override void DeleteTerrain() 203 public override void DeleteTerrain()
175 { 204 {
176 205
177 } 206 }
178 } 207 }
179 208
180 public class OdeCharacter : PhysicsActor 209 public class OdeCharacter : PhysicsActor
181 { 210 {
182 private PhysicsVector _position; 211 private PhysicsVector _position;
183 private PhysicsVector _velocity; 212 private PhysicsVector _velocity;
184 private PhysicsVector _acceleration; 213 private PhysicsVector _acceleration;
185 private bool flying; 214 private bool flying;
186 private float gravityAccel; 215 private float gravityAccel;
187 private IntPtr BoundingCapsule; 216 private IntPtr BoundingCapsule;
188 IntPtr capsule_geom; 217 IntPtr capsule_geom;
189 d.Mass capsule_mass; 218 d.Mass capsule_mass;
190 219
191 public OdeCharacter(OdeScene parent_scene, PhysicsVector pos) 220 public OdeCharacter(OdeScene parent_scene, PhysicsVector pos)
192 { 221 {
193 _velocity = new PhysicsVector(); 222 _velocity = new PhysicsVector();
194 _position = pos; 223 _position = pos;
195 _acceleration = new PhysicsVector(); 224 _acceleration = new PhysicsVector();
196 d.MassSetCapsule(out capsule_mass, 5.0f, 3, 0.5f, 2f); 225 d.MassSetCapsule(out capsule_mass, 5.0f, 3, 0.5f, 2f);
197 capsule_geom = d.CreateCapsule(OdeScene.space, 0.5f, 2f); 226 capsule_geom = d.CreateCapsule(OdeScene.space, 0.5f, 2f);
198 this.BoundingCapsule=d.BodyCreate(OdeScene.world); 227 this.BoundingCapsule = d.BodyCreate(OdeScene.world);
199 d.BodySetMass(BoundingCapsule, ref capsule_mass); 228 d.BodySetMass(BoundingCapsule, ref capsule_mass);
200 d.BodySetPosition(BoundingCapsule,pos.X,pos.Y,pos.Z); 229 d.BodySetPosition(BoundingCapsule, pos.X, pos.Y, pos.Z);
201 } 230 d.GeomSetBody(capsule_geom, BoundingCapsule);
202 231 }
203 public override bool Flying 232
204 { 233 public override bool Flying
205 get 234 {
206 { 235 get
207 return flying; 236 {
208 } 237 return flying;
209 set 238 }
210 { 239 set
211 flying = value; 240 {
212 } 241 flying = value;
213 } 242 }
214 243 }
215 public override PhysicsVector Position 244
216 { 245 public override PhysicsVector Position
217 get 246 {
218 { 247 get
219 return _position; 248 {
220 } 249 return _position;
221 set 250 }
222 { 251 set
223 _position = value; 252 {
224 } 253 _position = value;
225 } 254 }
226 255 }
227 public override PhysicsVector Velocity 256
228 { 257 public override PhysicsVector Velocity
229 get 258 {
230 { 259 get
231 return _velocity; 260 {
232 } 261 return _velocity;
233 set 262 }
234 { 263 set
235 _velocity = value; 264 {
236 } 265 _velocity = value;
237 } 266 }
238 267 }
239 public override bool Kinematic 268
240 { 269 public override bool Kinematic
241 get 270 {
242 { 271 get
243 return false; 272 {
244 } 273 return false;
245 set 274 }
246 { 275 set
247 276 {
248 } 277
249 } 278 }
250 279 }
251 public override Axiom.MathLib.Quaternion Orientation 280
252 { 281 public override Axiom.MathLib.Quaternion Orientation
253 get 282 {
254 { 283 get
255 return Axiom.MathLib.Quaternion.Identity; 284 {
256 } 285 return Axiom.MathLib.Quaternion.Identity;
257 set 286 }
258 { 287 set
259 288 {
260 } 289
261 } 290 }
262 291 }
263 public override PhysicsVector Acceleration 292
264 { 293 public override PhysicsVector Acceleration
265 get 294 {
266 { 295 get
267 return _acceleration; 296 {
268 } 297 return _acceleration;
269 298 }
270 } 299
271 public void SetAcceleration (PhysicsVector accel) 300 }
272 { 301 public void SetAcceleration(PhysicsVector accel)
273 this._acceleration = accel; 302 {
274 } 303 this._acceleration = accel;
275 304 }
276 public override void AddForce(PhysicsVector force) 305
277 { 306 public override void AddForce(PhysicsVector force)
278 307 {
279 } 308
280 309 }
281 public override void SetMomentum(PhysicsVector momentum) 310
282 { 311 public override void SetMomentum(PhysicsVector momentum)
283 312 {
284 } 313
285 314 }
286 public void Move(float timeStep) 315
287 { 316 public void Move(float timeStep)
288 PhysicsVector vec = new PhysicsVector(); 317 {
289 vec.X = this._velocity.X * timeStep; 318 PhysicsVector vec = new PhysicsVector();
290 vec.Y = this._velocity.Y * timeStep; 319 vec.X = this._velocity.X * timeStep;
291 if(flying) 320 vec.Y = this._velocity.Y * timeStep;
292 { 321 if (flying)
293 vec.Z = ( this._velocity.Z+0.5f) * timeStep; 322 {
294 } 323 vec.Z = (this._velocity.Z + 0.5f) * timeStep;
295 d.BodySetLinearVel(this.BoundingCapsule, vec.X, vec.Y, vec.Z); 324 }
296 } 325 d.BodySetLinearVel(this.BoundingCapsule, vec.X, vec.Y, vec.Z);
297 326 }
298 public void UpdatePosition() 327
299 { 328 public void UpdatePosition()
300 d.Vector3 vec = d.BodyGetPosition(BoundingCapsule); 329 {
301 this._position.X = vec.X; 330 d.Vector3 vec = d.BodyGetPosition(BoundingCapsule);
302 this._position.Y = vec.Y; 331 this._position.X = vec.X;
303 this._position.Z = vec.Z; 332 this._position.Y = vec.Y;
304 } 333 this._position.Z = vec.Z;
305 } 334 }
306 335 }
307 public class OdePrim : PhysicsActor 336
308 { 337 public class OdePrim : PhysicsActor
309 private PhysicsVector _position; 338 {
310 private PhysicsVector _velocity; 339 private PhysicsVector _position;
311 private PhysicsVector _acceleration; 340 private PhysicsVector _velocity;
312 341 private PhysicsVector _acceleration;
313 public OdePrim() 342
314 { 343 public OdePrim()
315 _velocity = new PhysicsVector(); 344 {
316 _position = new PhysicsVector(); 345 _velocity = new PhysicsVector();
317 _acceleration = new PhysicsVector(); 346 _position = new PhysicsVector();
318 } 347 _acceleration = new PhysicsVector();
319 public override bool Flying 348 }
320 { 349 public override bool Flying
321 get 350 {
322 { 351 get
323 return false; //no flying prims for you 352 {
324 } 353 return false; //no flying prims for you
325 set 354 }
326 { 355 set
327 356 {
328 } 357
329 } 358 }
330 public override PhysicsVector Position 359 }
331 { 360 public override PhysicsVector Position
332 get 361 {
333 { 362 get
334 PhysicsVector pos = new PhysicsVector(); 363 {
335 // PhysicsVector vec = this._prim.Position; 364 PhysicsVector pos = new PhysicsVector();
336 //pos.X = vec.X; 365 // PhysicsVector vec = this._prim.Position;
337 //pos.Y = vec.Y; 366 //pos.X = vec.X;
338 //pos.Z = vec.Z; 367 //pos.Y = vec.Y;
339 return pos; 368 //pos.Z = vec.Z;
340 369 return pos;
341 } 370
342 set 371 }
343 { 372 set
344 /*PhysicsVector vec = value; 373 {
345 PhysicsVector pos = new PhysicsVector(); 374 /*PhysicsVector vec = value;
346 pos.X = vec.X; 375 PhysicsVector pos = new PhysicsVector();
347 pos.Y = vec.Y; 376 pos.X = vec.X;
348 pos.Z = vec.Z; 377 pos.Y = vec.Y;
349 this._prim.Position = pos;*/ 378 pos.Z = vec.Z;
350 } 379 this._prim.Position = pos;*/
351 } 380 }
352 381 }
353 public override PhysicsVector Velocity 382
354 { 383 public override PhysicsVector Velocity
355 get 384 {
356 { 385 get
357 return _velocity; 386 {
358 } 387 return _velocity;
359 set 388 }
360 { 389 set
361 _velocity = value; 390 {
362 } 391 _velocity = value;
363 } 392 }
364 393 }
365 public override bool Kinematic 394
366 { 395 public override bool Kinematic
367 get 396 {
368 { 397 get
369 return false; 398 {
370 //return this._prim.Kinematic; 399 return false;
371 } 400 //return this._prim.Kinematic;
372 set 401 }
373 { 402 set
374 //this._prim.Kinematic = value; 403 {
375 } 404 //this._prim.Kinematic = value;
376 } 405 }
377 406 }
378 public override Axiom.MathLib.Quaternion Orientation 407
379 { 408 public override Axiom.MathLib.Quaternion Orientation
380 get 409 {
381 { 410 get
382 Axiom.MathLib.Quaternion res = new Axiom.MathLib.Quaternion(); 411 {
383 return res; 412 Axiom.MathLib.Quaternion res = new Axiom.MathLib.Quaternion();
384 } 413 return res;
385 set 414 }
386 { 415 set
387 416 {
388 } 417
389 } 418 }
390 419 }
391 public override PhysicsVector Acceleration 420
392 { 421 public override PhysicsVector Acceleration
393 get 422 {
394 { 423 get
395 return _acceleration; 424 {
396 } 425 return _acceleration;
397 426 }
398 } 427
399 public void SetAcceleration (PhysicsVector accel) 428 }
400 { 429 public void SetAcceleration(PhysicsVector accel)
401 this._acceleration = accel; 430 {
402 } 431 this._acceleration = accel;
403 432 }
404 public override void AddForce(PhysicsVector force) 433
405 { 434 public override void AddForce(PhysicsVector force)
406 435 {
407 } 436
408 437 }
409 public override void SetMomentum(PhysicsVector momentum) 438
410 { 439 public override void SetMomentum(PhysicsVector momentum)
411 440 {
412 } 441
413 442 }
414 443
415 } 444
445 }
416 446
417} 447}