aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Physics/BulletSPlugin/BSScene.cs
diff options
context:
space:
mode:
authorRobert.Adams2011-07-22 10:22:21 -0700
committerDan Lake2011-07-22 10:23:40 -0700
commit5ffec1cd648bc8dcc333fc528707c09bb8dce27d (patch)
treea9c6be19df834d4d53eeff7dec267b75d4c7bacb /OpenSim/Region/Physics/BulletSPlugin/BSScene.cs
parentMerge branch 'master' into bulletsim (diff)
downloadopensim-SC_OLD-5ffec1cd648bc8dcc333fc528707c09bb8dce27d.zip
opensim-SC_OLD-5ffec1cd648bc8dcc333fc528707c09bb8dce27d.tar.gz
opensim-SC_OLD-5ffec1cd648bc8dcc333fc528707c09bb8dce27d.tar.bz2
opensim-SC_OLD-5ffec1cd648bc8dcc333fc528707c09bb8dce27d.tar.xz
Pass collisions and updates in pinned memory (saves marshaling).
Fix folding feet by using collision normals. Add constraint specification.
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSScene.cs')
-rw-r--r--OpenSim/Region/Physics/BulletSPlugin/BSScene.cs86
1 files changed, 47 insertions, 39 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSScene.cs b/OpenSim/Region/Physics/BulletSPlugin/BSScene.cs
index beffd21..062945f 100644
--- a/OpenSim/Region/Physics/BulletSPlugin/BSScene.cs
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSScene.cs
@@ -38,9 +38,7 @@ using OpenSim.Region.Framework;
38 38
39// TODOs for BulletSim (for BSScene, BSPrim, BSCharacter and BulletSim) 39// TODOs for BulletSim (for BSScene, BSPrim, BSCharacter and BulletSim)
40// Fix folding up feet 40// Fix folding up feet
41// Fix terrain. Only flat terrain works. Terrain with shape is oriented wrong? Origined wrong?
42// Parameterize BulletSim. Pass a structure of parameters to the C++ code. Capsule size, friction, ... 41// Parameterize BulletSim. Pass a structure of parameters to the C++ code. Capsule size, friction, ...
43// Shift drag duplication of objects does not work
44// Adjust character capsule size when height is adjusted (ScenePresence.SetHeight) 42// Adjust character capsule size when height is adjusted (ScenePresence.SetHeight)
45// Test sculpties 43// Test sculpties
46// Compute physics FPS reasonably 44// Compute physics FPS reasonably
@@ -82,6 +80,19 @@ public class BSScene : PhysicsScene
82 private long m_simulationStep = 0; 80 private long m_simulationStep = 0;
83 public long SimulationStep { get { return m_simulationStep; } } 81 public long SimulationStep { get { return m_simulationStep; } }
84 82
83 // A value of the time now so all the collision and update routines do not have to get their own
84 // Set to 'now' just before all the prims and actors are called for collisions and updates
85 private int m_simulationNowTime;
86 public int SimulationNowTime { get { return m_simulationNowTime; } }
87
88 private int m_maxCollisionsPerFrame = 2048;
89 private CollisionDesc[] m_collisionArray;
90 private GCHandle m_collisionArrayPinnedHandle;
91
92 private int m_maxUpdatesPerFrame = 2048;
93 private EntityProperties[] m_updateArray;
94 private GCHandle m_updateArrayPinnedHandle;
95
85 private bool _meshSculptedPrim = true; // cause scuplted prims to get meshed 96 private bool _meshSculptedPrim = true; // cause scuplted prims to get meshed
86 private bool _forceSimplePrimMeshing = false; // if a cube or sphere, let Bullet do internal shapes 97 private bool _forceSimplePrimMeshing = false; // if a cube or sphere, let Bullet do internal shapes
87 public float maximumMassObject = 10000.01f; 98 public float maximumMassObject = 10000.01f;
@@ -129,8 +140,19 @@ public class BSScene : PhysicsScene
129 _taintedObjects = new List<TaintCallback>(); 140 _taintedObjects = new List<TaintCallback>();
130 141
131 mesher = meshmerizer; 142 mesher = meshmerizer;
143 // The bounding box for the simulated world
144 Vector3 worldExtent = new Vector3(Constants.RegionSize, Constants.RegionSize, 4096f);
145
146 // Allocate pinned memory to pass back object property updates and collisions from simulation step
147 m_collisionArray = new CollisionDesc[m_maxCollisionsPerFrame];
148 m_collisionArrayPinnedHandle = GCHandle.Alloc(m_collisionArray, GCHandleType.Pinned);
149 m_updateArray = new EntityProperties[m_maxUpdatesPerFrame];
150 m_updateArrayPinnedHandle = GCHandle.Alloc(m_updateArray, GCHandleType.Pinned);
151
132 // m_log.DebugFormat("{0}: Initialize: Calling BulletSimAPI.Initialize.", LogHeader); 152 // m_log.DebugFormat("{0}: Initialize: Calling BulletSimAPI.Initialize.", LogHeader);
133 m_worldID = BulletSimAPI.Initialize(new Vector3(Constants.RegionSize, Constants.RegionSize, 4096f)); 153 m_worldID = BulletSimAPI.Initialize(worldExtent,
154 m_maxCollisionsPerFrame, m_collisionArrayPinnedHandle.AddrOfPinnedObject(),
155 m_maxUpdatesPerFrame, m_updateArrayPinnedHandle.AddrOfPinnedObject());
134 } 156 }
135 157
136 // Called directly from unmanaged code so don't do much 158 // Called directly from unmanaged code so don't do much
@@ -188,19 +210,7 @@ public class BSScene : PhysicsScene
188 } 210 }
189 211
190 public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, Vector3 position, 212 public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, Vector3 position,
191 Vector3 size, Quaternion rotation) // deprecated 213 Vector3 size, Quaternion rotation, bool isPhysical, uint localID)
192 {
193 return null;
194 }
195 public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, Vector3 position,
196 Vector3 size, Quaternion rotation, bool isPhysical)
197 {
198 m_log.ErrorFormat("{0}: CALL TO AddPrimShape in BSScene. NOT IMPLEMENTED", LogHeader);
199 return null;
200 }
201
202 public override PhysicsActor AddPrimShape(uint localID, string primName, PrimitiveBaseShape pbs, Vector3 position,
203 Vector3 size, Quaternion rotation, bool isPhysical)
204 { 214 {
205 // m_log.DebugFormat("{0}: AddPrimShape2: {1}", LogHeader, primName); 215 // m_log.DebugFormat("{0}: AddPrimShape2: {1}", LogHeader, primName);
206 IMesh mesh = null; 216 IMesh mesh = null;
@@ -225,10 +235,8 @@ public class BSScene : PhysicsScene
225 { 235 {
226 int updatedEntityCount; 236 int updatedEntityCount;
227 IntPtr updatedEntitiesPtr; 237 IntPtr updatedEntitiesPtr;
228 IntPtr[] updatedEntities;
229 int collidersCount; 238 int collidersCount;
230 IntPtr collidersPtr; 239 IntPtr collidersPtr;
231 int[] colliders; // should be uint but Marshal.Copy does not have that overload
232 240
233 // update the prim states while we know the physics engine is not busy 241 // update the prim states while we know the physics engine is not busy
234 ProcessTaints(); 242 ProcessTaints();
@@ -242,32 +250,33 @@ public class BSScene : PhysicsScene
242 int numSubSteps = BulletSimAPI.PhysicsStep(m_worldID, timeStep, m_maxSubSteps, m_fixedTimeStep, 250 int numSubSteps = BulletSimAPI.PhysicsStep(m_worldID, timeStep, m_maxSubSteps, m_fixedTimeStep,
243 out updatedEntityCount, out updatedEntitiesPtr, out collidersCount, out collidersPtr); 251 out updatedEntityCount, out updatedEntitiesPtr, out collidersCount, out collidersPtr);
244 252
245 // if there were collisions, they show up here 253 // Don't have to use the pointers passed back since we know it is the same pinned memory we passed in
254
255 // Get a value for 'now' so all the collision and update routines don't have to get their own
256 m_simulationNowTime = Util.EnvironmentTickCount();
257
258 // If there were collisions, process them by sending the event to the prim.
259 // Collisions must be processed before updates.
246 if (collidersCount > 0) 260 if (collidersCount > 0)
247 { 261 {
248 colliders = new int[collidersCount]; 262 for (int ii = 0; ii < collidersCount; ii++)
249 Marshal.Copy(collidersPtr, colliders, 0, collidersCount);
250 for (int ii = 0; ii < collidersCount; ii+=2)
251 { 263 {
252 uint cA = (uint)colliders[ii]; 264 uint cA = m_collisionArray[ii].aID;
253 uint cB = (uint)colliders[ii+1]; 265 uint cB = m_collisionArray[ii].bID;
254 SendCollision(cA, cB); 266 Vector3 point = m_collisionArray[ii].point;
255 SendCollision(cB, cA); 267 Vector3 normal = m_collisionArray[ii].normal;
268 SendCollision(cA, cB, point, normal, 0.01f);
269 SendCollision(cB, cA, point, -normal, 0.01f);
256 } 270 }
257 } 271 }
258 272
259 // if any of the objects had updated properties, they are returned in the updatedEntity structure 273 // If any of the objects had updated properties, tell the object it has been changed by the physics engine
260 // TODO: figure out how to pass all of the EntityProperties structures in one marshal call.
261 if (updatedEntityCount > 0) 274 if (updatedEntityCount > 0)
262 { 275 {
263 updatedEntities = new IntPtr[updatedEntityCount];
264 // fetch all the pointers to all the EntityProperties structures for these updates
265 Marshal.Copy(updatedEntitiesPtr, updatedEntities, 0, updatedEntityCount);
266 for (int ii = 0; ii < updatedEntityCount; ii++) 276 for (int ii = 0; ii < updatedEntityCount; ii++)
267 { 277 {
268 IntPtr updatePointer = updatedEntities[ii]; 278 EntityProperties entprop = m_updateArray[ii];
269 EntityProperties entprop = (EntityProperties)Marshal.PtrToStructure(updatePointer, typeof(EntityProperties)); 279 // m_log.DebugFormat("{0}: entprop[{1}]: id={2}, pos={3}", LogHeader, ii, entprop.ID, entprop.Position);
270 // m_log.DebugFormat("{0}: entprop: id={1}, pos={2}", LogHeader, entprop.ID, entprop.Position);
271 BSCharacter actor; 280 BSCharacter actor;
272 if (m_avatars.TryGetValue(entprop.ID, out actor)) 281 if (m_avatars.TryGetValue(entprop.ID, out actor))
273 { 282 {
@@ -282,12 +291,12 @@ public class BSScene : PhysicsScene
282 } 291 }
283 } 292 }
284 293
285 // fps calculation wrong. This calculation returns about 1 in normal operation. 294 // fps calculation wrong. This calculation always returns about 1 in normal operation.
286 return timeStep / (numSubSteps * m_fixedTimeStep) * 1000f; 295 return timeStep / (numSubSteps * m_fixedTimeStep) * 1000f;
287 } 296 }
288 297
289 // Something has collided 298 // Something has collided
290 private void SendCollision(uint localID, uint collidingWith) 299 private void SendCollision(uint localID, uint collidingWith, Vector3 collidePoint, Vector3 collideNormal, float penitration)
291 { 300 {
292 if (localID == TERRAIN_ID || localID == GROUNDPLANE_ID) 301 if (localID == TERRAIN_ID || localID == GROUNDPLANE_ID)
293 { 302 {
@@ -303,12 +312,12 @@ public class BSScene : PhysicsScene
303 312
304 BSPrim prim; 313 BSPrim prim;
305 if (m_prims.TryGetValue(localID, out prim)) { 314 if (m_prims.TryGetValue(localID, out prim)) {
306 prim.Collide(collidingWith, type, Vector3.Zero, Vector3.UnitZ, 0.01f); 315 prim.Collide(collidingWith, type, collidePoint, collideNormal, 0.01f);
307 return; 316 return;
308 } 317 }
309 BSCharacter actor; 318 BSCharacter actor;
310 if (m_avatars.TryGetValue(localID, out actor)) { 319 if (m_avatars.TryGetValue(localID, out actor)) {
311 actor.Collide(collidingWith, type, Vector3.Zero, Vector3.UnitZ, 0.01f); 320 actor.Collide(collidingWith, type, collidePoint, collideNormal, 0.01f);
312 return; 321 return;
313 } 322 }
314 return; 323 return;
@@ -317,7 +326,6 @@ public class BSScene : PhysicsScene
317 public override void GetResults() { } 326 public override void GetResults() { }
318 327
319 public override void SetTerrain(float[] heightMap) { 328 public override void SetTerrain(float[] heightMap) {
320 m_log.DebugFormat("{0}: SetTerrain", LogHeader);
321 m_heightMap = heightMap; 329 m_heightMap = heightMap;
322 this.TaintedObject(delegate() 330 this.TaintedObject(delegate()
323 { 331 {