aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Physics/BulletXPlugin/BulletXPlugin.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Region/Physics/BulletXPlugin/BulletXPlugin.cs145
1 files changed, 114 insertions, 31 deletions
diff --git a/OpenSim/Region/Physics/BulletXPlugin/BulletXPlugin.cs b/OpenSim/Region/Physics/BulletXPlugin/BulletXPlugin.cs
index c733adb..2db7a54 100644
--- a/OpenSim/Region/Physics/BulletXPlugin/BulletXPlugin.cs
+++ b/OpenSim/Region/Physics/BulletXPlugin/BulletXPlugin.cs
@@ -64,6 +64,7 @@ using System;
64using System.Collections.Generic; 64using System.Collections.Generic;
65using MonoXnaCompactMaths; 65using MonoXnaCompactMaths;
66using OpenSim.Framework; 66using OpenSim.Framework;
67using OpenSim.Framework.Console;
67using OpenSim.Region.Physics.Manager; 68using OpenSim.Region.Physics.Manager;
68using XnaDevRu.BulletX; 69using XnaDevRu.BulletX;
69using XnaDevRu.BulletX.Dynamics; 70using XnaDevRu.BulletX.Dynamics;
@@ -74,15 +75,6 @@ using BoxShape=XnaDevRu.BulletX.BoxShape;
74 75
75namespace OpenSim.Region.Physics.BulletXPlugin 76namespace OpenSim.Region.Physics.BulletXPlugin
76{ 77{
77 /// <summary>
78 /// This class is only here for compilations reasons
79 /// </summary>
80 public class Mesh
81 {
82 public Mesh()
83 {
84 }
85 }
86 78
87 /// <summary> 79 /// <summary>
88 /// BulletXConversions are called now BulletXMaths 80 /// BulletXConversions are called now BulletXMaths
@@ -268,6 +260,65 @@ namespace OpenSim.Region.Physics.BulletXPlugin
268 } 260 }
269 } 261 }
270 262
263
264 // Class to detect and debug collisions
265 // Mainly used for debugging purposes
266 class CollisionDispatcherLocal : CollisionDispatcher
267 {
268
269 BulletXScene relatedScene;
270
271 public CollisionDispatcherLocal(BulletXScene s)
272 : base()
273 {
274 relatedScene=s;
275 }
276
277 public override bool NeedsCollision(CollisionObject bodyA, CollisionObject bodyB)
278 {
279 RigidBody rb;
280 BulletXCharacter bxcA=null;
281 BulletXPrim bxpA = null;
282 Type t = bodyA.GetType();
283 if (t==typeof(RigidBody)) {
284 rb = (RigidBody)bodyA;
285 relatedScene._characters.TryGetValue(rb, out bxcA);
286 relatedScene._prims.TryGetValue(rb, out bxpA);
287 }
288 String nameA;
289 if (bxcA != null)
290 nameA = bxcA._name;
291 else if (bxpA != null)
292 nameA = bxpA._name;
293 else
294 nameA = "null";
295
296 BulletXCharacter bxcB = null;
297 BulletXPrim bxpB = null;
298 t = bodyB.GetType();
299 if (t == typeof(RigidBody))
300 {
301 rb = (RigidBody)bodyB;
302 relatedScene._characters.TryGetValue(rb, out bxcB);
303 relatedScene._prims.TryGetValue(rb, out bxpB);
304 }
305 String nameB;
306 if (bxcB != null)
307 nameB = bxcB._name;
308 else if (bxpB != null)
309 nameB = bxpB._name;
310 else
311 nameB = "null";
312
313 bool needsCollision=base.NeedsCollision(bodyA, bodyB);
314
315 MainLog.Instance.Debug("BulletX", "A collision was detected between {0} and {1} --> {2}", nameA, nameB, needsCollision);
316
317
318 return needsCollision;
319 }
320 }
321
271 /// <summary> 322 /// <summary>
272 /// PhysicsScene Class for BulletX 323 /// PhysicsScene Class for BulletX
273 /// </summary> 324 /// </summary>
@@ -294,8 +345,11 @@ namespace OpenSim.Region.Physics.BulletXPlugin
294 private const int simulationSubSteps = 10; 345 private const int simulationSubSteps = 10;
295 //private float[] _heightmap; 346 //private float[] _heightmap;
296 private BulletXPlanet _simFlatPlanet; 347 private BulletXPlanet _simFlatPlanet;
297 private List<BulletXCharacter> _characters = new List<BulletXCharacter>(); 348 internal Dictionary<RigidBody, BulletXCharacter> _characters = new Dictionary<RigidBody, BulletXCharacter>();
298 private List<BulletXPrim> _prims = new List<BulletXPrim>(); 349 internal Dictionary<RigidBody, BulletXPrim> _prims = new Dictionary<RigidBody, BulletXPrim>();
350
351 public IMesher mesher;
352
299 353
300 public static float Gravity 354 public static float Gravity
301 { 355 {
@@ -334,7 +388,7 @@ namespace OpenSim.Region.Physics.BulletXPlugin
334 388
335 public BulletXScene() 389 public BulletXScene()
336 { 390 {
337 cDispatcher = new CollisionDispatcher(); 391 cDispatcher = new CollisionDispatcherLocal(this);
338 Vector3 worldMinDim = new Vector3((float) minXY, (float) minXY, (float) minZ); 392 Vector3 worldMinDim = new Vector3((float) minXY, (float) minXY, (float) minZ);
339 Vector3 worldMaxDim = new Vector3((float) maxXY, (float) maxXY, (float) maxZ); 393 Vector3 worldMaxDim = new Vector3((float) maxXY, (float) maxXY, (float) maxZ);
340 opCache = new AxisSweep3(worldMinDim, worldMaxDim, maxHandles); 394 opCache = new AxisSweep3(worldMinDim, worldMaxDim, maxHandles);
@@ -348,6 +402,12 @@ namespace OpenSim.Region.Physics.BulletXPlugin
348 //this._heightmap = new float[65536]; 402 //this._heightmap = new float[65536];
349 } 403 }
350 404
405 public override void Initialise(IMesher meshmerizer)
406 {
407 mesher = meshmerizer;
408 }
409
410
351 public override PhysicsActor AddAvatar(string avName, PhysicsVector position) 411 public override PhysicsActor AddAvatar(string avName, PhysicsVector position)
352 { 412 {
353 PhysicsVector pos = new PhysicsVector(); 413 PhysicsVector pos = new PhysicsVector();
@@ -358,7 +418,7 @@ namespace OpenSim.Region.Physics.BulletXPlugin
358 lock (BulletXLock) 418 lock (BulletXLock)
359 { 419 {
360 newAv = new BulletXCharacter(avName, this, pos); 420 newAv = new BulletXCharacter(avName, this, pos);
361 _characters.Add(newAv); 421 _characters.Add(newAv.RigidBody, newAv);
362 } 422 }
363 return newAv; 423 return newAv;
364 } 424 }
@@ -379,7 +439,7 @@ namespace OpenSim.Region.Physics.BulletXPlugin
379 ((BulletXCharacter) actor).RigidBody.ActivationState = ActivationState.DisableSimulation; 439 ((BulletXCharacter) actor).RigidBody.ActivationState = ActivationState.DisableSimulation;
380 AddForgottenRigidBody(((BulletXCharacter) actor).RigidBody); 440 AddForgottenRigidBody(((BulletXCharacter) actor).RigidBody);
381 } 441 }
382 _characters.Remove((BulletXCharacter) actor); 442 _characters.Remove(((BulletXCharacter)actor).RigidBody);
383 } 443 }
384 GC.Collect(); 444 GC.Collect();
385 } 445 }
@@ -405,7 +465,7 @@ namespace OpenSim.Region.Physics.BulletXPlugin
405 } 465 }
406 else 466 else
407 { 467 {
408 Mesh mesh = null; 468 IMesh mesh = mesher.CreateMesh(primName, pbs, size);
409 result = AddPrim(primName, position, size, rotation, mesh, pbs, isPhysical); 469 result = AddPrim(primName, position, size, rotation, mesh, pbs, isPhysical);
410 } 470 }
411 break; 471 break;
@@ -419,13 +479,13 @@ namespace OpenSim.Region.Physics.BulletXPlugin
419 } 479 }
420 480
421 public PhysicsActor AddPrim(String name, PhysicsVector position, PhysicsVector size, AxiomQuaternion rotation, 481 public PhysicsActor AddPrim(String name, PhysicsVector position, PhysicsVector size, AxiomQuaternion rotation,
422 Mesh mesh, PrimitiveBaseShape pbs, bool isPhysical) 482 IMesh mesh, PrimitiveBaseShape pbs, bool isPhysical)
423 { 483 {
424 BulletXPrim newPrim = null; 484 BulletXPrim newPrim = null;
425 lock (BulletXLock) 485 lock (BulletXLock)
426 { 486 {
427 newPrim = new BulletXPrim(name, this, position, size, rotation, mesh, pbs, isPhysical); 487 newPrim = new BulletXPrim(name, this, position, size, rotation, mesh, pbs, isPhysical);
428 _prims.Add(newPrim); 488 _prims.Add(newPrim.RigidBody, newPrim);
429 } 489 }
430 return newPrim; 490 return newPrim;
431 } 491 }
@@ -446,7 +506,7 @@ namespace OpenSim.Region.Physics.BulletXPlugin
446 ((BulletXPrim) prim).RigidBody.ActivationState = ActivationState.DisableSimulation; 506 ((BulletXPrim) prim).RigidBody.ActivationState = ActivationState.DisableSimulation;
447 AddForgottenRigidBody(((BulletXPrim) prim).RigidBody); 507 AddForgottenRigidBody(((BulletXPrim) prim).RigidBody);
448 } 508 }
449 _prims.Remove((BulletXPrim) prim); 509 _prims.Remove(((BulletXPrim) prim).RigidBody);
450 } 510 }
451 GC.Collect(); 511 GC.Collect();
452 } 512 }
@@ -470,11 +530,11 @@ namespace OpenSim.Region.Physics.BulletXPlugin
470 530
471 private void MoveAllObjects(float timeStep) 531 private void MoveAllObjects(float timeStep)
472 { 532 {
473 foreach (BulletXCharacter actor in _characters) 533 foreach (BulletXCharacter actor in _characters.Values)
474 { 534 {
475 actor.Move(timeStep); 535 actor.Move(timeStep);
476 } 536 }
477 foreach (BulletXPrim prim in _prims) 537 foreach (BulletXPrim prim in _prims.Values)
478 { 538 {
479 } 539 }
480 } 540 }
@@ -482,14 +542,14 @@ namespace OpenSim.Region.Physics.BulletXPlugin
482 private void ValidateHeightForAll() 542 private void ValidateHeightForAll()
483 { 543 {
484 float _height; 544 float _height;
485 foreach (BulletXCharacter actor in _characters) 545 foreach (BulletXCharacter actor in _characters.Values)
486 { 546 {
487 //_height = HeightValue(actor.RigidBodyPosition); 547 //_height = HeightValue(actor.RigidBodyPosition);
488 _height = _simFlatPlanet.HeightValue(actor.RigidBodyPosition); 548 _height = _simFlatPlanet.HeightValue(actor.RigidBodyPosition);
489 actor.ValidateHeight(_height); 549 actor.ValidateHeight(_height);
490 //if (_simFlatPlanet.heightIsNotValid(actor.RigidBodyPosition, out _height)) actor.ValidateHeight(_height); 550 //if (_simFlatPlanet.heightIsNotValid(actor.RigidBodyPosition, out _height)) actor.ValidateHeight(_height);
491 } 551 }
492 foreach (BulletXPrim prim in _prims) 552 foreach (BulletXPrim prim in _prims.Values)
493 { 553 {
494 //_height = HeightValue(prim.RigidBodyPosition); 554 //_height = HeightValue(prim.RigidBodyPosition);
495 _height = _simFlatPlanet.HeightValue(prim.RigidBodyPosition); 555 _height = _simFlatPlanet.HeightValue(prim.RigidBodyPosition);
@@ -510,11 +570,11 @@ namespace OpenSim.Region.Physics.BulletXPlugin
510 { 570 {
511 //UpdatePosition > UpdateKinetics. 571 //UpdatePosition > UpdateKinetics.
512 //Not only position will be updated, also velocity cause acceleration. 572 //Not only position will be updated, also velocity cause acceleration.
513 foreach (BulletXCharacter actor in _characters) 573 foreach (BulletXCharacter actor in _characters.Values)
514 { 574 {
515 actor.UpdateKinetics(); 575 actor.UpdateKinetics();
516 } 576 }
517 foreach (BulletXPrim prim in _prims) 577 foreach (BulletXPrim prim in _prims.Values)
518 { 578 {
519 prim.UpdateKinetics(); 579 prim.UpdateKinetics();
520 } 580 }
@@ -646,9 +706,11 @@ namespace OpenSim.Region.Physics.BulletXPlugin
646 protected PhysicsVector m_rotationalVelocity = PhysicsVector.Zero; 706 protected PhysicsVector m_rotationalVelocity = PhysicsVector.Zero;
647 protected RigidBody rigidBody; 707 protected RigidBody rigidBody;
648 private Boolean iscolliding = false; 708 private Boolean iscolliding = false;
709 internal string _name;
649 710
650 public BulletXActor() 711 public BulletXActor(String name)
651 { 712 {
713 _name = name;
652 } 714 }
653 715
654 public override PhysicsVector Position 716 public override PhysicsVector Position
@@ -847,6 +909,7 @@ namespace OpenSim.Region.Physics.BulletXPlugin
847 } 909 }
848 public BulletXCharacter(String avName, BulletXScene parent_scene, PhysicsVector pos, PhysicsVector velocity, 910 public BulletXCharacter(String avName, BulletXScene parent_scene, PhysicsVector pos, PhysicsVector velocity,
849 PhysicsVector size, PhysicsVector acceleration, AxiomQuaternion orientation) 911 PhysicsVector size, PhysicsVector acceleration, AxiomQuaternion orientation)
912 : base(avName)
850 { 913 {
851 //This fields will be removed. They're temporal 914 //This fields will be removed. They're temporal
852 float _sizeX = 0.5f; 915 float _sizeX = 0.5f;
@@ -1016,14 +1079,15 @@ namespace OpenSim.Region.Physics.BulletXPlugin
1016 private bool m_lastUpdateSent = false; 1079 private bool m_lastUpdateSent = false;
1017 1080
1018 public BulletXPrim(String primName, BulletXScene parent_scene, PhysicsVector pos, PhysicsVector size, 1081 public BulletXPrim(String primName, BulletXScene parent_scene, PhysicsVector pos, PhysicsVector size,
1019 AxiomQuaternion rotation, Mesh mesh, PrimitiveBaseShape pbs, bool isPhysical) 1082 AxiomQuaternion rotation, IMesh mesh, PrimitiveBaseShape pbs, bool isPhysical)
1020 : this(primName, parent_scene, pos, new PhysicsVector(), size, new PhysicsVector(), rotation, mesh, pbs, isPhysical) 1083 : this(primName, parent_scene, pos, new PhysicsVector(), size, new PhysicsVector(), rotation, mesh, pbs, isPhysical)
1021 { 1084 {
1022 } 1085 }
1023 public BulletXPrim(String primName, BulletXScene parent_scene, PhysicsVector pos, PhysicsVector velocity, 1086 public BulletXPrim(String primName, BulletXScene parent_scene, PhysicsVector pos, PhysicsVector velocity,
1024 PhysicsVector size, 1087 PhysicsVector size,
1025 PhysicsVector acceleration, AxiomQuaternion rotation, Mesh mesh, PrimitiveBaseShape pbs, 1088 PhysicsVector acceleration, AxiomQuaternion rotation, IMesh mesh, PrimitiveBaseShape pbs,
1026 bool isPhysical) 1089 bool isPhysical)
1090 : base(primName)
1027 { 1091 {
1028 if ((size.X == 0) || (size.Y == 0) || (size.Z == 0)) throw new Exception("Size 0"); 1092 if ((size.X == 0) || (size.Y == 0) || (size.Z == 0)) throw new Exception("Size 0");
1029 if (rotation.Norm == 0f) rotation = AxiomQuaternion.Identity; 1093 if (rotation.Norm == 0f) rotation = AxiomQuaternion.Identity;
@@ -1037,7 +1101,7 @@ namespace OpenSim.Region.Physics.BulletXPlugin
1037 1101
1038 _parent_scene = parent_scene; 1102 _parent_scene = parent_scene;
1039 1103
1040 CreateRigidBody(parent_scene, pos, size); 1104 CreateRigidBody(parent_scene, mesh, pos, size);
1041 } 1105 }
1042 1106
1043 public override PhysicsVector Position 1107 public override PhysicsVector Position
@@ -1191,7 +1255,7 @@ namespace OpenSim.Region.Physics.BulletXPlugin
1191 } 1255 }
1192 1256
1193 #region Methods for updating values of RigidBody 1257 #region Methods for updating values of RigidBody
1194 internal protected void CreateRigidBody(BulletXScene parent_scene, PhysicsVector pos, PhysicsVector size) 1258 internal protected void CreateRigidBody(BulletXScene parent_scene, IMesh mesh, PhysicsVector pos, PhysicsVector size)
1195 { 1259 {
1196 //For RigidBody Constructor. The next values might change 1260 //For RigidBody Constructor. The next values might change
1197 float _linearDamping = 0.0f; 1261 float _linearDamping = 0.0f;
@@ -1204,7 +1268,26 @@ namespace OpenSim.Region.Physics.BulletXPlugin
1204 { 1268 {
1205 _startTransform.Translation = BulletXMaths.PhysicsVectorToXnaVector3(pos); 1269 _startTransform.Translation = BulletXMaths.PhysicsVectorToXnaVector3(pos);
1206 //For now all prims are boxes 1270 //For now all prims are boxes
1207 CollisionShape _collisionShape = new XnaDevRu.BulletX.BoxShape(BulletXMaths.PhysicsVectorToXnaVector3(size) / 2.0f); 1271 CollisionShape _collisionShape;
1272 if (mesh == null)
1273 {
1274 _collisionShape = new XnaDevRu.BulletX.BoxShape(BulletXMaths.PhysicsVectorToXnaVector3(size) / 2.0f);
1275 } else {
1276 int iVertexCount = mesh.getVertexList().Count;
1277 int[] indices = mesh.getIndexListAsInt();
1278 Vector3[] v3Vertices = new Vector3[iVertexCount];
1279 for (int i = 0; i < iVertexCount; i++)
1280 {
1281 PhysicsVector v=mesh.getVertexList()[i];
1282 if (v != null) // Note, null has special meaning. See meshing code for details
1283 v3Vertices[i] = BulletXMaths.PhysicsVectorToXnaVector3(v);
1284 else
1285 v3Vertices[i] = MonoXnaCompactMaths.Vector3.Zero;
1286 }
1287 TriangleIndexVertexArray triMesh = new TriangleIndexVertexArray(indices, v3Vertices);
1288
1289 _collisionShape = new XnaDevRu.BulletX.TriangleMeshShape(triMesh);
1290 }
1208 DefaultMotionState _motionState = new DefaultMotionState(_startTransform, _centerOfMassOffset); 1291 DefaultMotionState _motionState = new DefaultMotionState(_startTransform, _centerOfMassOffset);
1209 Vector3 _localInertia = new Vector3(); 1292 Vector3 _localInertia = new Vector3();
1210 if (_physical) _collisionShape.CalculateLocalInertia(Mass, out _localInertia); //Always when mass > 0 1293 if (_physical) _collisionShape.CalculateLocalInertia(Mass, out _localInertia); //Always when mass > 0
@@ -1231,7 +1314,7 @@ namespace OpenSim.Region.Physics.BulletXPlugin
1231 rigidBody.ActivationState = ActivationState.DisableSimulation; 1314 rigidBody.ActivationState = ActivationState.DisableSimulation;
1232 this._parent_scene.AddForgottenRigidBody(rigidBody); 1315 this._parent_scene.AddForgottenRigidBody(rigidBody);
1233 } 1316 }
1234 CreateRigidBody(this._parent_scene, this._position, size); 1317 CreateRigidBody(this._parent_scene, null, this._position, size); // Note, null for the meshing definitely is wrong. It's here for the moment to apease the compiler
1235 if (_physical) Speed();//Static objects don't have linear velocity 1318 if (_physical) Speed();//Static objects don't have linear velocity
1236 ReOrient(); 1319 ReOrient();
1237 GC.Collect(); 1320 GC.Collect();