aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Physics/BulletSPlugin
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin')
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSAPIUnman.cs1885
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSAPIXNA.cs1598
-rw-r--r--OpenSim/Region/Physics/BulletSPlugin/BSApiTemplate.cs668
-rw-r--r--OpenSim/Region/Physics/BulletSPlugin/BSCharacter.cs461
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSConstraint.cs27
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSConstraint6Dof.cs48
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSConstraintCollection.cs4
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSConstraintHinge.cs12
-rw-r--r--OpenSim/Region/Physics/BulletSPlugin/BSDynamics.cs1177
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSLinkset.cs54
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSLinksetCompound.cs352
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSLinksetConstraints.cs79
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSMaterials.cs200
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSMotors.cs571
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSParam.cs634
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSPhysObject.cs194
-rw-r--r--OpenSim/Region/Physics/BulletSPlugin/BSPlugin.cs8
-rw-r--r--OpenSim/Region/Physics/BulletSPlugin/BSPrim.cs853
-rw-r--r--OpenSim/Region/Physics/BulletSPlugin/BSScene.cs945
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSShapeCollection.cs231
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSShapes.cs183
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSTerrainHeightmap.cs55
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSTerrainManager.cs181
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs71
-rw-r--r--OpenSim/Region/Physics/BulletSPlugin/BulletSimAPI.cs1015
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BulletSimData.cs265
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BulletSimTODO.txt290
27 files changed, 8881 insertions, 3180 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSAPIUnman.cs b/OpenSim/Region/Physics/BulletSPlugin/BSAPIUnman.cs
new file mode 100755
index 0000000..ae54499
--- /dev/null
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSAPIUnman.cs
@@ -0,0 +1,1885 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyrightD
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27using System;
28using System.Collections.Generic;
29using System.Reflection;
30using System.Runtime.InteropServices;
31using System.Security;
32using System.Text;
33
34using OpenSim.Framework;
35
36using OpenMetaverse;
37
38namespace OpenSim.Region.Physics.BulletSPlugin
39{
40public sealed class BSAPIUnman : BSAPITemplate
41{
42
43private sealed class BulletWorldUnman : BulletWorld
44{
45 public IntPtr ptr;
46 public BulletWorldUnman(uint id, BSScene physScene, IntPtr xx)
47 : base(id, physScene)
48 {
49 ptr = xx;
50 }
51}
52
53private sealed class BulletBodyUnman : BulletBody
54{
55 public IntPtr ptr;
56 public BulletBodyUnman(uint id, IntPtr xx)
57 : base(id)
58 {
59 ptr = xx;
60 }
61 public override bool HasPhysicalBody
62 {
63 get { return ptr != IntPtr.Zero; }
64 }
65 public override void Clear()
66 {
67 ptr = IntPtr.Zero;
68 }
69 public override string AddrString
70 {
71 get { return ptr.ToString("X"); }
72 }
73}
74
75private sealed class BulletShapeUnman : BulletShape
76{
77 public IntPtr ptr;
78 public BulletShapeUnman(IntPtr xx, BSPhysicsShapeType typ)
79 : base()
80 {
81 ptr = xx;
82 type = typ;
83 }
84 public override bool HasPhysicalShape
85 {
86 get { return ptr != IntPtr.Zero; }
87 }
88 public override void Clear()
89 {
90 ptr = IntPtr.Zero;
91 }
92 public override BulletShape Clone()
93 {
94 return new BulletShapeUnman(ptr, type);
95 }
96 public override bool ReferenceSame(BulletShape other)
97 {
98 BulletShapeUnman otheru = other as BulletShapeUnman;
99 return (otheru != null) && (this.ptr == otheru.ptr);
100
101 }
102 public override string AddrString
103 {
104 get { return ptr.ToString("X"); }
105 }
106}
107private sealed class BulletConstraintUnman : BulletConstraint
108{
109 public BulletConstraintUnman(IntPtr xx) : base()
110 {
111 ptr = xx;
112 }
113 public IntPtr ptr;
114
115 public override void Clear()
116 {
117 ptr = IntPtr.Zero;
118 }
119 public override bool HasPhysicalConstraint { get { return ptr != IntPtr.Zero; } }
120
121 // Used for log messages for a unique display of the memory/object allocated to this instance
122 public override string AddrString
123 {
124 get { return ptr.ToString("X"); }
125 }
126}
127
128// We pin the memory passed between the managed and unmanaged code.
129GCHandle m_paramsHandle;
130private GCHandle m_collisionArrayPinnedHandle;
131private GCHandle m_updateArrayPinnedHandle;
132
133// Handle to the callback used by the unmanaged code to call into the managed code.
134// Used for debug logging.
135// Need to store the handle in a persistant variable so it won't be freed.
136private BSAPICPP.DebugLogCallback m_DebugLogCallbackHandle;
137
138private BSScene PhysicsScene { get; set; }
139
140public override string BulletEngineName { get { return "BulletUnmanaged"; } }
141public override string BulletEngineVersion { get; protected set; }
142
143public BSAPIUnman(string paramName, BSScene physScene)
144{
145 PhysicsScene = physScene;
146
147 // Do something fancy with the paramName to get the right DLL implementation
148 // like "Bullet-2.80-OpenCL-Intel" loading the version for Intel based OpenCL implementation, etc.
149 if (Util.IsWindows())
150 Util.LoadArchSpecificWindowsDll("BulletSim.dll");
151 // If not Windows, loading is performed by the
152 // Mono loader as specified in
153 // "bin/Physics/OpenSim.Region.Physics.BulletSPlugin.dll.config".
154}
155
156// Initialization and simulation
157public override BulletWorld Initialize(Vector3 maxPosition, ConfigurationParameters parms,
158 int maxCollisions, ref CollisionDesc[] collisionArray,
159 int maxUpdates, ref EntityProperties[] updateArray
160 )
161{
162 // Pin down the memory that will be used to pass object collisions and updates back from unmanaged code
163 m_paramsHandle = GCHandle.Alloc(parms, GCHandleType.Pinned);
164 m_collisionArrayPinnedHandle = GCHandle.Alloc(collisionArray, GCHandleType.Pinned);
165 m_updateArrayPinnedHandle = GCHandle.Alloc(updateArray, GCHandleType.Pinned);
166
167 // If Debug logging level, enable logging from the unmanaged code
168 m_DebugLogCallbackHandle = null;
169 if (BSScene.m_log.IsDebugEnabled && PhysicsScene.PhysicsLogging.Enabled)
170 {
171 BSScene.m_log.DebugFormat("{0}: Initialize: Setting debug callback for unmanaged code", BSScene.LogHeader);
172 if (PhysicsScene.PhysicsLogging.Enabled)
173 // The handle is saved in a variable to make sure it doesn't get freed after this call
174 m_DebugLogCallbackHandle = new BSAPICPP.DebugLogCallback(BulletLoggerPhysLog);
175 else
176 m_DebugLogCallbackHandle = new BSAPICPP.DebugLogCallback(BulletLogger);
177 }
178
179 // Get the version of the DLL
180 // TODO: this doesn't work yet. Something wrong with marshaling the returned string.
181 // BulletEngineVersion = BulletSimAPI.GetVersion2();
182 BulletEngineVersion = "";
183
184 // Call the unmanaged code with the buffers and other information
185 return new BulletWorldUnman(0, PhysicsScene, BSAPICPP.Initialize2(maxPosition, m_paramsHandle.AddrOfPinnedObject(),
186 maxCollisions, m_collisionArrayPinnedHandle.AddrOfPinnedObject(),
187 maxUpdates, m_updateArrayPinnedHandle.AddrOfPinnedObject(),
188 m_DebugLogCallbackHandle));
189
190}
191
192// Called directly from unmanaged code so don't do much
193private void BulletLogger(string msg)
194{
195 BSScene.m_log.Debug("[BULLETS UNMANAGED]:" + msg);
196}
197
198// Called directly from unmanaged code so don't do much
199private void BulletLoggerPhysLog(string msg)
200{
201 PhysicsScene.DetailLog("[BULLETS UNMANAGED]:" + msg);
202}
203
204public override int PhysicsStep(BulletWorld world, float timeStep, int maxSubSteps, float fixedTimeStep,
205 out int updatedEntityCount, out int collidersCount)
206{
207 BulletWorldUnman worldu = world as BulletWorldUnman;
208 return BSAPICPP.PhysicsStep2(worldu.ptr, timeStep, maxSubSteps, fixedTimeStep, out updatedEntityCount, out collidersCount);
209}
210
211public override void Shutdown(BulletWorld world)
212{
213 BulletWorldUnman worldu = world as BulletWorldUnman;
214 BSAPICPP.Shutdown2(worldu.ptr);
215
216 if (m_paramsHandle.IsAllocated)
217 {
218 m_paramsHandle.Free();
219 }
220 if (m_collisionArrayPinnedHandle.IsAllocated)
221 {
222 m_collisionArrayPinnedHandle.Free();
223 }
224 if (m_updateArrayPinnedHandle.IsAllocated)
225 {
226 m_updateArrayPinnedHandle.Free();
227 }
228}
229
230public override bool PushUpdate(BulletBody obj)
231{
232 BulletBodyUnman bodyu = obj as BulletBodyUnman;
233 return BSAPICPP.PushUpdate2(bodyu.ptr);
234}
235
236public override bool UpdateParameter(BulletWorld world, uint localID, String parm, float value)
237{
238 BulletWorldUnman worldu = world as BulletWorldUnman;
239 return BSAPICPP.UpdateParameter2(worldu.ptr, localID, parm, value);
240}
241
242// =====================================================================================
243// Mesh, hull, shape and body creation helper routines
244public override BulletShape CreateMeshShape(BulletWorld world,
245 int indicesCount, int[] indices,
246 int verticesCount, float[] vertices)
247{
248 BulletWorldUnman worldu = world as BulletWorldUnman;
249 return new BulletShapeUnman(
250 BSAPICPP.CreateMeshShape2(worldu.ptr, indicesCount, indices, verticesCount, vertices),
251 BSPhysicsShapeType.SHAPE_MESH);
252}
253
254public override BulletShape CreateHullShape(BulletWorld world, int hullCount, float[] hulls)
255{
256 BulletWorldUnman worldu = world as BulletWorldUnman;
257 return new BulletShapeUnman(
258 BSAPICPP.CreateHullShape2(worldu.ptr, hullCount, hulls),
259 BSPhysicsShapeType.SHAPE_HULL);
260}
261
262public override BulletShape BuildHullShapeFromMesh(BulletWorld world, BulletShape meshShape)
263{
264 BulletWorldUnman worldu = world as BulletWorldUnman;
265 BulletShapeUnman shapeu = meshShape as BulletShapeUnman;
266 return new BulletShapeUnman(
267 BSAPICPP.BuildHullShapeFromMesh2(worldu.ptr, shapeu.ptr),
268 BSPhysicsShapeType.SHAPE_HULL);
269}
270
271public override BulletShape BuildNativeShape(BulletWorld world, ShapeData shapeData)
272{
273 BulletWorldUnman worldu = world as BulletWorldUnman;
274 return new BulletShapeUnman(BSAPICPP.BuildNativeShape2(worldu.ptr, shapeData), shapeData.Type);
275}
276
277public override bool IsNativeShape(BulletShape shape)
278{
279 BulletShapeUnman shapeu = shape as BulletShapeUnman;
280 if (shapeu != null && shapeu.HasPhysicalShape)
281 return BSAPICPP.IsNativeShape2(shapeu.ptr);
282 return false;
283}
284
285public override void SetShapeCollisionMargin(BulletShape shape, float margin)
286{
287 BulletShapeUnman shapeu = shape as BulletShapeUnman;
288 if (shapeu != null && shapeu.HasPhysicalShape)
289 BSAPICPP.SetShapeCollisionMargin2(shapeu.ptr, margin);
290}
291
292public override BulletShape BuildCapsuleShape(BulletWorld world, float radius, float height, Vector3 scale)
293{
294 BulletWorldUnman worldu = world as BulletWorldUnman;
295 return new BulletShapeUnman(
296 BSAPICPP.BuildCapsuleShape2(worldu.ptr, radius, height, scale),
297 BSPhysicsShapeType.SHAPE_CAPSULE);
298}
299
300public override BulletShape CreateCompoundShape(BulletWorld world, bool enableDynamicAabbTree)
301{
302 BulletWorldUnman worldu = world as BulletWorldUnman;
303 return new BulletShapeUnman(
304 BSAPICPP.CreateCompoundShape2(worldu.ptr, enableDynamicAabbTree),
305 BSPhysicsShapeType.SHAPE_COMPOUND);
306
307}
308
309public override int GetNumberOfCompoundChildren(BulletShape shape)
310{
311 BulletShapeUnman shapeu = shape as BulletShapeUnman;
312 if (shapeu != null && shapeu.HasPhysicalShape)
313 return BSAPICPP.GetNumberOfCompoundChildren2(shapeu.ptr);
314 return 0;
315}
316
317public override void AddChildShapeToCompoundShape(BulletShape shape, BulletShape addShape, Vector3 pos, Quaternion rot)
318{
319 BulletShapeUnman shapeu = shape as BulletShapeUnman;
320 BulletShapeUnman addShapeu = addShape as BulletShapeUnman;
321 BSAPICPP.AddChildShapeToCompoundShape2(shapeu.ptr, addShapeu.ptr, pos, rot);
322}
323
324public override BulletShape GetChildShapeFromCompoundShapeIndex(BulletShape shape, int indx)
325{
326 BulletShapeUnman shapeu = shape as BulletShapeUnman;
327 return new BulletShapeUnman(BSAPICPP.GetChildShapeFromCompoundShapeIndex2(shapeu.ptr, indx), BSPhysicsShapeType.SHAPE_UNKNOWN);
328}
329
330public override BulletShape RemoveChildShapeFromCompoundShapeIndex(BulletShape shape, int indx)
331{
332 BulletShapeUnman shapeu = shape as BulletShapeUnman;
333 return new BulletShapeUnman(BSAPICPP.RemoveChildShapeFromCompoundShapeIndex2(shapeu.ptr, indx), BSPhysicsShapeType.SHAPE_UNKNOWN);
334}
335
336public override void RemoveChildShapeFromCompoundShape(BulletShape shape, BulletShape removeShape)
337{
338 BulletShapeUnman shapeu = shape as BulletShapeUnman;
339 BulletShapeUnman removeShapeu = removeShape as BulletShapeUnman;
340 BSAPICPP.RemoveChildShapeFromCompoundShape2(shapeu.ptr, removeShapeu.ptr);
341}
342
343public override void UpdateChildTransform(BulletShape pShape, int childIndex, Vector3 pos, Quaternion rot, bool shouldRecalculateLocalAabb)
344{
345 BulletShapeUnman shapeu = pShape as BulletShapeUnman;
346 BSAPICPP.UpdateChildTransform2(shapeu.ptr, childIndex, pos, rot, shouldRecalculateLocalAabb);
347}
348
349public override void RecalculateCompoundShapeLocalAabb(BulletShape shape)
350{
351 BulletShapeUnman shapeu = shape as BulletShapeUnman;
352 BSAPICPP.RecalculateCompoundShapeLocalAabb2(shapeu.ptr);
353}
354
355public override BulletShape DuplicateCollisionShape(BulletWorld world, BulletShape srcShape, uint id)
356{
357 BulletWorldUnman worldu = world as BulletWorldUnman;
358 BulletShapeUnman srcShapeu = srcShape as BulletShapeUnman;
359 return new BulletShapeUnman(BSAPICPP.DuplicateCollisionShape2(worldu.ptr, srcShapeu.ptr, id), srcShape.type);
360}
361
362public override bool DeleteCollisionShape(BulletWorld world, BulletShape shape)
363{
364 BulletWorldUnman worldu = world as BulletWorldUnman;
365 BulletShapeUnman shapeu = shape as BulletShapeUnman;
366 return BSAPICPP.DeleteCollisionShape2(worldu.ptr, shapeu.ptr);
367}
368
369public override CollisionObjectTypes GetBodyType(BulletBody obj)
370{
371 BulletBodyUnman bodyu = obj as BulletBodyUnman;
372 return (CollisionObjectTypes)BSAPICPP.GetBodyType2(bodyu.ptr);
373}
374
375public override BulletBody CreateBodyFromShape(BulletWorld world, BulletShape shape, uint id, Vector3 pos, Quaternion rot)
376{
377 BulletWorldUnman worldu = world as BulletWorldUnman;
378 BulletShapeUnman shapeu = shape as BulletShapeUnman;
379 return new BulletBodyUnman(id, BSAPICPP.CreateBodyFromShape2(worldu.ptr, shapeu.ptr, id, pos, rot));
380}
381
382public override BulletBody CreateBodyWithDefaultMotionState(BulletShape shape, uint id, Vector3 pos, Quaternion rot)
383{
384 BulletShapeUnman shapeu = shape as BulletShapeUnman;
385 return new BulletBodyUnman(id, BSAPICPP.CreateBodyWithDefaultMotionState2(shapeu.ptr, id, pos, rot));
386}
387
388public override BulletBody CreateGhostFromShape(BulletWorld world, BulletShape shape, uint id, Vector3 pos, Quaternion rot)
389{
390 BulletWorldUnman worldu = world as BulletWorldUnman;
391 BulletShapeUnman shapeu = shape as BulletShapeUnman;
392 return new BulletBodyUnman(id, BSAPICPP.CreateGhostFromShape2(worldu.ptr, shapeu.ptr, id, pos, rot));
393}
394
395public override void DestroyObject(BulletWorld world, BulletBody obj)
396{
397 BulletWorldUnman worldu = world as BulletWorldUnman;
398 BulletBodyUnman bodyu = obj as BulletBodyUnman;
399 BSAPICPP.DestroyObject2(worldu.ptr, bodyu.ptr);
400}
401
402// =====================================================================================
403// Terrain creation and helper routines
404public override BulletShape CreateGroundPlaneShape(uint id, float height, float collisionMargin)
405{
406 return new BulletShapeUnman(BSAPICPP.CreateGroundPlaneShape2(id, height, collisionMargin), BSPhysicsShapeType.SHAPE_GROUNDPLANE);
407}
408
409public override BulletShape CreateTerrainShape(uint id, Vector3 size, float minHeight, float maxHeight, float[] heightMap,
410 float scaleFactor, float collisionMargin)
411{
412 return new BulletShapeUnman(BSAPICPP.CreateTerrainShape2(id, size, minHeight, maxHeight, heightMap, scaleFactor, collisionMargin),
413 BSPhysicsShapeType.SHAPE_TERRAIN);
414}
415
416// =====================================================================================
417// Constraint creation and helper routines
418public override BulletConstraint Create6DofConstraint(BulletWorld world, BulletBody obj1, BulletBody obj2,
419 Vector3 frame1loc, Quaternion frame1rot,
420 Vector3 frame2loc, Quaternion frame2rot,
421 bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies)
422{
423 BulletWorldUnman worldu = world as BulletWorldUnman;
424 BulletBodyUnman bodyu1 = obj1 as BulletBodyUnman;
425 BulletBodyUnman bodyu2 = obj2 as BulletBodyUnman;
426 return new BulletConstraintUnman(BSAPICPP.Create6DofConstraint2(worldu.ptr, bodyu1.ptr, bodyu2.ptr, frame1loc, frame1rot,
427 frame2loc, frame2rot, useLinearReferenceFrameA, disableCollisionsBetweenLinkedBodies));
428}
429
430public override BulletConstraint Create6DofConstraintToPoint(BulletWorld world, BulletBody obj1, BulletBody obj2,
431 Vector3 joinPoint,
432 bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies)
433{
434 BulletWorldUnman worldu = world as BulletWorldUnman;
435 BulletBodyUnman bodyu1 = obj1 as BulletBodyUnman;
436 BulletBodyUnman bodyu2 = obj2 as BulletBodyUnman;
437 return new BulletConstraintUnman(BSAPICPP.Create6DofConstraintToPoint2(worldu.ptr, bodyu1.ptr, bodyu2.ptr,
438 joinPoint, useLinearReferenceFrameA, disableCollisionsBetweenLinkedBodies));
439}
440
441public override BulletConstraint CreateHingeConstraint(BulletWorld world, BulletBody obj1, BulletBody obj2,
442 Vector3 pivotinA, Vector3 pivotinB,
443 Vector3 axisInA, Vector3 axisInB,
444 bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies)
445{
446 BulletWorldUnman worldu = world as BulletWorldUnman;
447 BulletBodyUnman bodyu1 = obj1 as BulletBodyUnman;
448 BulletBodyUnman bodyu2 = obj2 as BulletBodyUnman;
449 return new BulletConstraintUnman(BSAPICPP.CreateHingeConstraint2(worldu.ptr, bodyu1.ptr, bodyu2.ptr,
450 pivotinA, pivotinB, axisInA, axisInB, useLinearReferenceFrameA, disableCollisionsBetweenLinkedBodies));
451}
452
453public override void SetConstraintEnable(BulletConstraint constrain, float numericTrueFalse)
454{
455 BulletConstraintUnman constrainu = constrain as BulletConstraintUnman;
456 BSAPICPP.SetConstraintEnable2(constrainu.ptr, numericTrueFalse);
457}
458
459public override void SetConstraintNumSolverIterations(BulletConstraint constrain, float iterations)
460{
461 BulletConstraintUnman constrainu = constrain as BulletConstraintUnman;
462 BSAPICPP.SetConstraintNumSolverIterations2(constrainu.ptr, iterations);
463}
464
465public override bool SetFrames(BulletConstraint constrain,
466 Vector3 frameA, Quaternion frameArot, Vector3 frameB, Quaternion frameBrot)
467{
468 BulletConstraintUnman constrainu = constrain as BulletConstraintUnman;
469 return BSAPICPP.SetFrames2(constrainu.ptr, frameA, frameArot, frameB, frameBrot);
470}
471
472public override bool SetLinearLimits(BulletConstraint constrain, Vector3 low, Vector3 hi)
473{
474 BulletConstraintUnman constrainu = constrain as BulletConstraintUnman;
475 return BSAPICPP.SetLinearLimits2(constrainu.ptr, low, hi);
476}
477
478public override bool SetAngularLimits(BulletConstraint constrain, Vector3 low, Vector3 hi)
479{
480 BulletConstraintUnman constrainu = constrain as BulletConstraintUnman;
481 return BSAPICPP.SetAngularLimits2(constrainu.ptr, low, hi);
482}
483
484public override bool UseFrameOffset(BulletConstraint constrain, float enable)
485{
486 BulletConstraintUnman constrainu = constrain as BulletConstraintUnman;
487 return BSAPICPP.UseFrameOffset2(constrainu.ptr, enable);
488}
489
490public override bool TranslationalLimitMotor(BulletConstraint constrain, float enable, float targetVel, float maxMotorForce)
491{
492 BulletConstraintUnman constrainu = constrain as BulletConstraintUnman;
493 return BSAPICPP.TranslationalLimitMotor2(constrainu.ptr, enable, targetVel, maxMotorForce);
494}
495
496public override bool SetBreakingImpulseThreshold(BulletConstraint constrain, float threshold)
497{
498 BulletConstraintUnman constrainu = constrain as BulletConstraintUnman;
499 return BSAPICPP.SetBreakingImpulseThreshold2(constrainu.ptr, threshold);
500}
501
502public override bool CalculateTransforms(BulletConstraint constrain)
503{
504 BulletConstraintUnman constrainu = constrain as BulletConstraintUnman;
505 return BSAPICPP.CalculateTransforms2(constrainu.ptr);
506}
507
508public override bool SetConstraintParam(BulletConstraint constrain, ConstraintParams paramIndex, float value, ConstraintParamAxis axis)
509{
510 BulletConstraintUnman constrainu = constrain as BulletConstraintUnman;
511 return BSAPICPP.SetConstraintParam2(constrainu.ptr, paramIndex, value, axis);
512}
513
514public override bool DestroyConstraint(BulletWorld world, BulletConstraint constrain)
515{
516 BulletWorldUnman worldu = world as BulletWorldUnman;
517 BulletConstraintUnman constrainu = constrain as BulletConstraintUnman;
518 return BSAPICPP.DestroyConstraint2(worldu.ptr, constrainu.ptr);
519}
520
521// =====================================================================================
522// btCollisionWorld entries
523public override void UpdateSingleAabb(BulletWorld world, BulletBody obj)
524{
525 BulletWorldUnman worldu = world as BulletWorldUnman;
526 BulletBodyUnman bodyu = obj as BulletBodyUnman;
527 BSAPICPP.UpdateSingleAabb2(worldu.ptr, bodyu.ptr);
528}
529
530public override void UpdateAabbs(BulletWorld world)
531{
532 BulletWorldUnman worldu = world as BulletWorldUnman;
533 BSAPICPP.UpdateAabbs2(worldu.ptr);
534}
535
536public override bool GetForceUpdateAllAabbs(BulletWorld world)
537{
538 BulletWorldUnman worldu = world as BulletWorldUnman;
539 return BSAPICPP.GetForceUpdateAllAabbs2(worldu.ptr);
540}
541
542public override void SetForceUpdateAllAabbs(BulletWorld world, bool force)
543{
544 BulletWorldUnman worldu = world as BulletWorldUnman;
545 BSAPICPP.SetForceUpdateAllAabbs2(worldu.ptr, force);
546}
547
548// =====================================================================================
549// btDynamicsWorld entries
550public override bool AddObjectToWorld(BulletWorld world, BulletBody obj)
551{
552 BulletWorldUnman worldu = world as BulletWorldUnman;
553 BulletBodyUnman bodyu = obj as BulletBodyUnman;
554
555 // Bullet resets several variables when an object is added to the world.
556 // Gravity is reset to world default depending on the static/dynamic
557 // type. Of course, the collision flags in the broadphase proxy are initialized to default.
558 Vector3 origGrav = BSAPICPP.GetGravity2(bodyu.ptr);
559
560 bool ret = BSAPICPP.AddObjectToWorld2(worldu.ptr, bodyu.ptr);
561
562 if (ret)
563 {
564 BSAPICPP.SetGravity2(bodyu.ptr, origGrav);
565 obj.ApplyCollisionMask(world.physicsScene);
566 }
567 return ret;
568}
569
570public override bool RemoveObjectFromWorld(BulletWorld world, BulletBody obj)
571{
572 BulletWorldUnman worldu = world as BulletWorldUnman;
573 BulletBodyUnman bodyu = obj as BulletBodyUnman;
574 return BSAPICPP.RemoveObjectFromWorld2(worldu.ptr, bodyu.ptr);
575}
576
577public override bool AddConstraintToWorld(BulletWorld world, BulletConstraint constrain, bool disableCollisionsBetweenLinkedObjects)
578{
579 BulletWorldUnman worldu = world as BulletWorldUnman;
580 BulletConstraintUnman constrainu = constrain as BulletConstraintUnman;
581 return BSAPICPP.AddConstraintToWorld2(worldu.ptr, constrainu.ptr, disableCollisionsBetweenLinkedObjects);
582}
583
584public override bool RemoveConstraintFromWorld(BulletWorld world, BulletConstraint constrain)
585{
586 BulletWorldUnman worldu = world as BulletWorldUnman;
587 BulletConstraintUnman constrainu = constrain as BulletConstraintUnman;
588 return BSAPICPP.RemoveConstraintFromWorld2(worldu.ptr, constrainu.ptr);
589}
590// =====================================================================================
591// btCollisionObject entries
592public override Vector3 GetAnisotripicFriction(BulletConstraint constrain)
593{
594 BulletConstraintUnman constrainu = constrain as BulletConstraintUnman;
595 return BSAPICPP.GetAnisotripicFriction2(constrainu.ptr);
596}
597
598public override Vector3 SetAnisotripicFriction(BulletConstraint constrain, Vector3 frict)
599{
600 BulletConstraintUnman constrainu = constrain as BulletConstraintUnman;
601 return BSAPICPP.SetAnisotripicFriction2(constrainu.ptr, frict);
602}
603
604public override bool HasAnisotripicFriction(BulletConstraint constrain)
605{
606 BulletConstraintUnman constrainu = constrain as BulletConstraintUnman;
607 return BSAPICPP.HasAnisotripicFriction2(constrainu.ptr);
608}
609
610public override void SetContactProcessingThreshold(BulletBody obj, float val)
611{
612 BulletBodyUnman bodyu = obj as BulletBodyUnman;
613 BSAPICPP.SetContactProcessingThreshold2(bodyu.ptr, val);
614}
615
616public override float GetContactProcessingThreshold(BulletBody obj)
617{
618 BulletBodyUnman bodyu = obj as BulletBodyUnman;
619 return BSAPICPP.GetContactProcessingThreshold2(bodyu.ptr);
620}
621
622public override bool IsStaticObject(BulletBody obj)
623{
624 BulletBodyUnman bodyu = obj as BulletBodyUnman;
625 return BSAPICPP.IsStaticObject2(bodyu.ptr);
626}
627
628public override bool IsKinematicObject(BulletBody obj)
629{
630 BulletBodyUnman bodyu = obj as BulletBodyUnman;
631 return BSAPICPP.IsKinematicObject2(bodyu.ptr);
632}
633
634public override bool IsStaticOrKinematicObject(BulletBody obj)
635{
636 BulletBodyUnman bodyu = obj as BulletBodyUnman;
637 return BSAPICPP.IsStaticOrKinematicObject2(bodyu.ptr);
638}
639
640public override bool HasContactResponse(BulletBody obj)
641{
642 BulletBodyUnman bodyu = obj as BulletBodyUnman;
643 return BSAPICPP.HasContactResponse2(bodyu.ptr);
644}
645
646public override void SetCollisionShape(BulletWorld world, BulletBody obj, BulletShape shape)
647{
648 BulletWorldUnman worldu = world as BulletWorldUnman;
649 BulletBodyUnman bodyu = obj as BulletBodyUnman;
650 BulletShapeUnman shapeu = shape as BulletShapeUnman;
651 if (worldu != null && bodyu != null)
652 {
653 // Special case to allow the caller to zero out the reference to any physical shape
654 if (shapeu != null)
655 BSAPICPP.SetCollisionShape2(worldu.ptr, bodyu.ptr, shapeu.ptr);
656 else
657 BSAPICPP.SetCollisionShape2(worldu.ptr, bodyu.ptr, IntPtr.Zero);
658 }
659}
660
661public override BulletShape GetCollisionShape(BulletBody obj)
662{
663 BulletBodyUnman bodyu = obj as BulletBodyUnman;
664 return new BulletShapeUnman(BSAPICPP.GetCollisionShape2(bodyu.ptr), BSPhysicsShapeType.SHAPE_UNKNOWN);
665}
666
667public override int GetActivationState(BulletBody obj)
668{
669 BulletBodyUnman bodyu = obj as BulletBodyUnman;
670 return BSAPICPP.GetActivationState2(bodyu.ptr);
671}
672
673public override void SetActivationState(BulletBody obj, int state)
674{
675 BulletBodyUnman bodyu = obj as BulletBodyUnman;
676 BSAPICPP.SetActivationState2(bodyu.ptr, state);
677}
678
679public override void SetDeactivationTime(BulletBody obj, float dtime)
680{
681 BulletBodyUnman bodyu = obj as BulletBodyUnman;
682 BSAPICPP.SetDeactivationTime2(bodyu.ptr, dtime);
683}
684
685public override float GetDeactivationTime(BulletBody obj)
686{
687 BulletBodyUnman bodyu = obj as BulletBodyUnman;
688 return BSAPICPP.GetDeactivationTime2(bodyu.ptr);
689}
690
691public override void ForceActivationState(BulletBody obj, ActivationState state)
692{
693 BulletBodyUnman bodyu = obj as BulletBodyUnman;
694 BSAPICPP.ForceActivationState2(bodyu.ptr, state);
695}
696
697public override void Activate(BulletBody obj, bool forceActivation)
698{
699 BulletBodyUnman bodyu = obj as BulletBodyUnman;
700 BSAPICPP.Activate2(bodyu.ptr, forceActivation);
701}
702
703public override bool IsActive(BulletBody obj)
704{
705 BulletBodyUnman bodyu = obj as BulletBodyUnman;
706 return BSAPICPP.IsActive2(bodyu.ptr);
707}
708
709public override void SetRestitution(BulletBody obj, float val)
710{
711 BulletBodyUnman bodyu = obj as BulletBodyUnman;
712 BSAPICPP.SetRestitution2(bodyu.ptr, val);
713}
714
715public override float GetRestitution(BulletBody obj)
716{
717 BulletBodyUnman bodyu = obj as BulletBodyUnman;
718 return BSAPICPP.GetRestitution2(bodyu.ptr);
719}
720
721public override void SetFriction(BulletBody obj, float val)
722{
723 BulletBodyUnman bodyu = obj as BulletBodyUnman;
724 BSAPICPP.SetFriction2(bodyu.ptr, val);
725}
726
727public override float GetFriction(BulletBody obj)
728{
729 BulletBodyUnman bodyu = obj as BulletBodyUnman;
730 return BSAPICPP.GetFriction2(bodyu.ptr);
731}
732
733public override Vector3 GetPosition(BulletBody obj)
734{
735 BulletBodyUnman bodyu = obj as BulletBodyUnman;
736 return BSAPICPP.GetPosition2(bodyu.ptr);
737}
738
739public override Quaternion GetOrientation(BulletBody obj)
740{
741 BulletBodyUnman bodyu = obj as BulletBodyUnman;
742 return BSAPICPP.GetOrientation2(bodyu.ptr);
743}
744
745public override void SetTranslation(BulletBody obj, Vector3 position, Quaternion rotation)
746{
747 BulletBodyUnman bodyu = obj as BulletBodyUnman;
748 BSAPICPP.SetTranslation2(bodyu.ptr, position, rotation);
749}
750
751 /*
752public override IntPtr GetBroadphaseHandle(BulletBody obj)
753{
754 BulletBodyUnman bodyu = obj as BulletBodyUnman;
755 return BSAPICPP.GetBroadphaseHandle2(bodyu.ptr);
756}
757
758public override void SetBroadphaseHandle(BulletBody obj, IntPtr handle)
759{
760 BulletBodyUnman bodyu = obj as BulletBodyUnman;
761 BSAPICPP.SetUserPointer2(bodyu.ptr, handle);
762}
763 */
764
765public override void SetInterpolationLinearVelocity(BulletBody obj, Vector3 vel)
766{
767 BulletBodyUnman bodyu = obj as BulletBodyUnman;
768 BSAPICPP.SetInterpolationLinearVelocity2(bodyu.ptr, vel);
769}
770
771public override void SetInterpolationAngularVelocity(BulletBody obj, Vector3 vel)
772{
773 BulletBodyUnman bodyu = obj as BulletBodyUnman;
774 BSAPICPP.SetInterpolationAngularVelocity2(bodyu.ptr, vel);
775}
776
777public override void SetInterpolationVelocity(BulletBody obj, Vector3 linearVel, Vector3 angularVel)
778{
779 BulletBodyUnman bodyu = obj as BulletBodyUnman;
780 BSAPICPP.SetInterpolationVelocity2(bodyu.ptr, linearVel, angularVel);
781}
782
783public override float GetHitFraction(BulletBody obj)
784{
785 BulletBodyUnman bodyu = obj as BulletBodyUnman;
786 return BSAPICPP.GetHitFraction2(bodyu.ptr);
787}
788
789public override void SetHitFraction(BulletBody obj, float val)
790{
791 BulletBodyUnman bodyu = obj as BulletBodyUnman;
792 BSAPICPP.SetHitFraction2(bodyu.ptr, val);
793}
794
795public override CollisionFlags GetCollisionFlags(BulletBody obj)
796{
797 BulletBodyUnman bodyu = obj as BulletBodyUnman;
798 return BSAPICPP.GetCollisionFlags2(bodyu.ptr);
799}
800
801public override CollisionFlags SetCollisionFlags(BulletBody obj, CollisionFlags flags)
802{
803 BulletBodyUnman bodyu = obj as BulletBodyUnman;
804 return BSAPICPP.SetCollisionFlags2(bodyu.ptr, flags);
805}
806
807public override CollisionFlags AddToCollisionFlags(BulletBody obj, CollisionFlags flags)
808{
809 BulletBodyUnman bodyu = obj as BulletBodyUnman;
810 return BSAPICPP.AddToCollisionFlags2(bodyu.ptr, flags);
811}
812
813public override CollisionFlags RemoveFromCollisionFlags(BulletBody obj, CollisionFlags flags)
814{
815 BulletBodyUnman bodyu = obj as BulletBodyUnman;
816 return BSAPICPP.RemoveFromCollisionFlags2(bodyu.ptr, flags);
817}
818
819public override float GetCcdMotionThreshold(BulletBody obj)
820{
821 BulletBodyUnman bodyu = obj as BulletBodyUnman;
822 return BSAPICPP.GetCcdMotionThreshold2(bodyu.ptr);
823}
824
825
826public override void SetCcdMotionThreshold(BulletBody obj, float val)
827{
828 BulletBodyUnman bodyu = obj as BulletBodyUnman;
829 BSAPICPP.SetCcdMotionThreshold2(bodyu.ptr, val);
830}
831
832public override float GetCcdSweptSphereRadius(BulletBody obj)
833{
834 BulletBodyUnman bodyu = obj as BulletBodyUnman;
835 return BSAPICPP.GetCcdSweptSphereRadius2(bodyu.ptr);
836}
837
838public override void SetCcdSweptSphereRadius(BulletBody obj, float val)
839{
840 BulletBodyUnman bodyu = obj as BulletBodyUnman;
841 BSAPICPP.SetCcdSweptSphereRadius2(bodyu.ptr, val);
842}
843
844public override IntPtr GetUserPointer(BulletBody obj)
845{
846 BulletBodyUnman bodyu = obj as BulletBodyUnman;
847 return BSAPICPP.GetUserPointer2(bodyu.ptr);
848}
849
850public override void SetUserPointer(BulletBody obj, IntPtr val)
851{
852 BulletBodyUnman bodyu = obj as BulletBodyUnman;
853 BSAPICPP.SetUserPointer2(bodyu.ptr, val);
854}
855
856// =====================================================================================
857// btRigidBody entries
858public override void ApplyGravity(BulletBody obj)
859{
860 BulletBodyUnman bodyu = obj as BulletBodyUnman;
861 BSAPICPP.ApplyGravity2(bodyu.ptr);
862}
863
864public override void SetGravity(BulletBody obj, Vector3 val)
865{
866 BulletBodyUnman bodyu = obj as BulletBodyUnman;
867 BSAPICPP.SetGravity2(bodyu.ptr, val);
868}
869
870public override Vector3 GetGravity(BulletBody obj)
871{
872 BulletBodyUnman bodyu = obj as BulletBodyUnman;
873 return BSAPICPP.GetGravity2(bodyu.ptr);
874}
875
876public override void SetDamping(BulletBody obj, float lin_damping, float ang_damping)
877{
878 BulletBodyUnman bodyu = obj as BulletBodyUnman;
879 BSAPICPP.SetDamping2(bodyu.ptr, lin_damping, ang_damping);
880}
881
882public override void SetLinearDamping(BulletBody obj, float lin_damping)
883{
884 BulletBodyUnman bodyu = obj as BulletBodyUnman;
885 BSAPICPP.SetLinearDamping2(bodyu.ptr, lin_damping);
886}
887
888public override void SetAngularDamping(BulletBody obj, float ang_damping)
889{
890 BulletBodyUnman bodyu = obj as BulletBodyUnman;
891 BSAPICPP.SetAngularDamping2(bodyu.ptr, ang_damping);
892}
893
894public override float GetLinearDamping(BulletBody obj)
895{
896 BulletBodyUnman bodyu = obj as BulletBodyUnman;
897 return BSAPICPP.GetLinearDamping2(bodyu.ptr);
898}
899
900public override float GetAngularDamping(BulletBody obj)
901{
902 BulletBodyUnman bodyu = obj as BulletBodyUnman;
903 return BSAPICPP.GetAngularDamping2(bodyu.ptr);
904}
905
906public override float GetLinearSleepingThreshold(BulletBody obj)
907{
908 BulletBodyUnman bodyu = obj as BulletBodyUnman;
909 return BSAPICPP.GetLinearSleepingThreshold2(bodyu.ptr);
910}
911
912public override void ApplyDamping(BulletBody obj, float timeStep)
913{
914 BulletBodyUnman bodyu = obj as BulletBodyUnman;
915 BSAPICPP.ApplyDamping2(bodyu.ptr, timeStep);
916}
917
918public override void SetMassProps(BulletBody obj, float mass, Vector3 inertia)
919{
920 BulletBodyUnman bodyu = obj as BulletBodyUnman;
921 BSAPICPP.SetMassProps2(bodyu.ptr, mass, inertia);
922}
923
924public override Vector3 GetLinearFactor(BulletBody obj)
925{
926 BulletBodyUnman bodyu = obj as BulletBodyUnman;
927 return BSAPICPP.GetLinearFactor2(bodyu.ptr);
928}
929
930public override void SetLinearFactor(BulletBody obj, Vector3 factor)
931{
932 BulletBodyUnman bodyu = obj as BulletBodyUnman;
933 BSAPICPP.SetLinearFactor2(bodyu.ptr, factor);
934}
935
936public override void SetCenterOfMassByPosRot(BulletBody obj, Vector3 pos, Quaternion rot)
937{
938 BulletBodyUnman bodyu = obj as BulletBodyUnman;
939 BSAPICPP.SetCenterOfMassByPosRot2(bodyu.ptr, pos, rot);
940}
941
942// Add a force to the object as if its mass is one.
943// Deep down in Bullet: m_totalForce += force*m_linearFactor;
944public override void ApplyCentralForce(BulletBody obj, Vector3 force)
945{
946 BulletBodyUnman bodyu = obj as BulletBodyUnman;
947 BSAPICPP.ApplyCentralForce2(bodyu.ptr, force);
948}
949
950// Set the force being applied to the object as if its mass is one.
951public override void SetObjectForce(BulletBody obj, Vector3 force)
952{
953 BulletBodyUnman bodyu = obj as BulletBodyUnman;
954 BSAPICPP.SetObjectForce2(bodyu.ptr, force);
955}
956
957public override Vector3 GetTotalForce(BulletBody obj)
958{
959 BulletBodyUnman bodyu = obj as BulletBodyUnman;
960 return BSAPICPP.GetTotalForce2(bodyu.ptr);
961}
962
963public override Vector3 GetTotalTorque(BulletBody obj)
964{
965 BulletBodyUnman bodyu = obj as BulletBodyUnman;
966 return BSAPICPP.GetTotalTorque2(bodyu.ptr);
967}
968
969public override Vector3 GetInvInertiaDiagLocal(BulletBody obj)
970{
971 BulletBodyUnman bodyu = obj as BulletBodyUnman;
972 return BSAPICPP.GetInvInertiaDiagLocal2(bodyu.ptr);
973}
974
975public override void SetInvInertiaDiagLocal(BulletBody obj, Vector3 inert)
976{
977 BulletBodyUnman bodyu = obj as BulletBodyUnman;
978 BSAPICPP.SetInvInertiaDiagLocal2(bodyu.ptr, inert);
979}
980
981public override void SetSleepingThresholds(BulletBody obj, float lin_threshold, float ang_threshold)
982{
983 BulletBodyUnman bodyu = obj as BulletBodyUnman;
984 BSAPICPP.SetSleepingThresholds2(bodyu.ptr, lin_threshold, ang_threshold);
985}
986
987// Deep down in Bullet: m_totalTorque += torque*m_angularFactor;
988public override void ApplyTorque(BulletBody obj, Vector3 torque)
989{
990 BulletBodyUnman bodyu = obj as BulletBodyUnman;
991 BSAPICPP.ApplyTorque2(bodyu.ptr, torque);
992}
993
994// Apply force at the given point. Will add torque to the object.
995// Deep down in Bullet: applyCentralForce(force);
996// applyTorque(rel_pos.cross(force*m_linearFactor));
997public override void ApplyForce(BulletBody obj, Vector3 force, Vector3 pos)
998{
999 BulletBodyUnman bodyu = obj as BulletBodyUnman;
1000 BSAPICPP.ApplyForce2(bodyu.ptr, force, pos);
1001}
1002
1003// Apply impulse to the object. Same as "ApplycentralForce" but force scaled by object's mass.
1004// Deep down in Bullet: m_linearVelocity += impulse *m_linearFactor * m_inverseMass;
1005public override void ApplyCentralImpulse(BulletBody obj, Vector3 imp)
1006{
1007 BulletBodyUnman bodyu = obj as BulletBodyUnman;
1008 BSAPICPP.ApplyCentralImpulse2(bodyu.ptr, imp);
1009}
1010
1011// Apply impulse to the object's torque. Force is scaled by object's mass.
1012// Deep down in Bullet: m_angularVelocity += m_invInertiaTensorWorld * torque * m_angularFactor;
1013public override void ApplyTorqueImpulse(BulletBody obj, Vector3 imp)
1014{
1015 BulletBodyUnman bodyu = obj as BulletBodyUnman;
1016 BSAPICPP.ApplyTorqueImpulse2(bodyu.ptr, imp);
1017}
1018
1019// Apply impulse at the point given. For is scaled by object's mass and effects both linear and angular forces.
1020// Deep down in Bullet: applyCentralImpulse(impulse);
1021// applyTorqueImpulse(rel_pos.cross(impulse*m_linearFactor));
1022public override void ApplyImpulse(BulletBody obj, Vector3 imp, Vector3 pos)
1023{
1024 BulletBodyUnman bodyu = obj as BulletBodyUnman;
1025 BSAPICPP.ApplyImpulse2(bodyu.ptr, imp, pos);
1026}
1027
1028public override void ClearForces(BulletBody obj)
1029{
1030 BulletBodyUnman bodyu = obj as BulletBodyUnman;
1031 BSAPICPP.ClearForces2(bodyu.ptr);
1032}
1033
1034public override void ClearAllForces(BulletBody obj)
1035{
1036 BulletBodyUnman bodyu = obj as BulletBodyUnman;
1037 BSAPICPP.ClearAllForces2(bodyu.ptr);
1038}
1039
1040public override void UpdateInertiaTensor(BulletBody obj)
1041{
1042 BulletBodyUnman bodyu = obj as BulletBodyUnman;
1043 BSAPICPP.UpdateInertiaTensor2(bodyu.ptr);
1044}
1045
1046public override Vector3 GetLinearVelocity(BulletBody obj)
1047{
1048 BulletBodyUnman bodyu = obj as BulletBodyUnman;
1049 return BSAPICPP.GetLinearVelocity2(bodyu.ptr);
1050}
1051
1052public override Vector3 GetAngularVelocity(BulletBody obj)
1053{
1054 BulletBodyUnman bodyu = obj as BulletBodyUnman;
1055 return BSAPICPP.GetAngularVelocity2(bodyu.ptr);
1056}
1057
1058public override void SetLinearVelocity(BulletBody obj, Vector3 vel)
1059{
1060 BulletBodyUnman bodyu = obj as BulletBodyUnman;
1061 BSAPICPP.SetLinearVelocity2(bodyu.ptr, vel);
1062}
1063
1064public override void SetAngularVelocity(BulletBody obj, Vector3 angularVelocity)
1065{
1066 BulletBodyUnman bodyu = obj as BulletBodyUnman;
1067 BSAPICPP.SetAngularVelocity2(bodyu.ptr, angularVelocity);
1068}
1069
1070public override Vector3 GetVelocityInLocalPoint(BulletBody obj, Vector3 pos)
1071{
1072 BulletBodyUnman bodyu = obj as BulletBodyUnman;
1073 return BSAPICPP.GetVelocityInLocalPoint2(bodyu.ptr, pos);
1074}
1075
1076public override void Translate(BulletBody obj, Vector3 trans)
1077{
1078 BulletBodyUnman bodyu = obj as BulletBodyUnman;
1079 BSAPICPP.Translate2(bodyu.ptr, trans);
1080}
1081
1082public override void UpdateDeactivation(BulletBody obj, float timeStep)
1083{
1084 BulletBodyUnman bodyu = obj as BulletBodyUnman;
1085 BSAPICPP.UpdateDeactivation2(bodyu.ptr, timeStep);
1086}
1087
1088public override bool WantsSleeping(BulletBody obj)
1089{
1090 BulletBodyUnman bodyu = obj as BulletBodyUnman;
1091 return BSAPICPP.WantsSleeping2(bodyu.ptr);
1092}
1093
1094public override void SetAngularFactor(BulletBody obj, float factor)
1095{
1096 BulletBodyUnman bodyu = obj as BulletBodyUnman;
1097 BSAPICPP.SetAngularFactor2(bodyu.ptr, factor);
1098}
1099
1100public override void SetAngularFactorV(BulletBody obj, Vector3 factor)
1101{
1102 BulletBodyUnman bodyu = obj as BulletBodyUnman;
1103 BSAPICPP.SetAngularFactorV2(bodyu.ptr, factor);
1104}
1105
1106public override Vector3 GetAngularFactor(BulletBody obj)
1107{
1108 BulletBodyUnman bodyu = obj as BulletBodyUnman;
1109 return BSAPICPP.GetAngularFactor2(bodyu.ptr);
1110}
1111
1112public override bool IsInWorld(BulletWorld world, BulletBody obj)
1113{
1114 BulletBodyUnman bodyu = obj as BulletBodyUnman;
1115 return BSAPICPP.IsInWorld2(bodyu.ptr);
1116}
1117
1118public override void AddConstraintRef(BulletBody obj, BulletConstraint constrain)
1119{
1120 BulletBodyUnman bodyu = obj as BulletBodyUnman;
1121 BulletConstraintUnman constrainu = constrain as BulletConstraintUnman;
1122 BSAPICPP.AddConstraintRef2(bodyu.ptr, constrainu.ptr);
1123}
1124
1125public override void RemoveConstraintRef(BulletBody obj, BulletConstraint constrain)
1126{
1127 BulletBodyUnman bodyu = obj as BulletBodyUnman;
1128 BulletConstraintUnman constrainu = constrain as BulletConstraintUnman;
1129 BSAPICPP.RemoveConstraintRef2(bodyu.ptr, constrainu.ptr);
1130}
1131
1132public override BulletConstraint GetConstraintRef(BulletBody obj, int index)
1133{
1134 BulletBodyUnman bodyu = obj as BulletBodyUnman;
1135 return new BulletConstraintUnman(BSAPICPP.GetConstraintRef2(bodyu.ptr, index));
1136}
1137
1138public override int GetNumConstraintRefs(BulletBody obj)
1139{
1140 BulletBodyUnman bodyu = obj as BulletBodyUnman;
1141 return BSAPICPP.GetNumConstraintRefs2(bodyu.ptr);
1142}
1143
1144public override bool SetCollisionGroupMask(BulletBody body, uint filter, uint mask)
1145{
1146 BulletBodyUnman bodyu = body as BulletBodyUnman;
1147 return BSAPICPP.SetCollisionGroupMask2(bodyu.ptr, filter, mask);
1148}
1149
1150// =====================================================================================
1151// btCollisionShape entries
1152
1153public override float GetAngularMotionDisc(BulletShape shape)
1154{
1155 BulletShapeUnman shapeu = shape as BulletShapeUnman;
1156 return BSAPICPP.GetAngularMotionDisc2(shapeu.ptr);
1157}
1158
1159public override float GetContactBreakingThreshold(BulletShape shape, float defaultFactor)
1160{
1161 BulletShapeUnman shapeu = shape as BulletShapeUnman;
1162 return BSAPICPP.GetContactBreakingThreshold2(shapeu.ptr, defaultFactor);
1163}
1164
1165public override bool IsPolyhedral(BulletShape shape)
1166{
1167 BulletShapeUnman shapeu = shape as BulletShapeUnman;
1168 return BSAPICPP.IsPolyhedral2(shapeu.ptr);
1169}
1170
1171public override bool IsConvex2d(BulletShape shape)
1172{
1173 BulletShapeUnman shapeu = shape as BulletShapeUnman;
1174 return BSAPICPP.IsConvex2d2(shapeu.ptr);
1175}
1176
1177public override bool IsConvex(BulletShape shape)
1178{
1179 BulletShapeUnman shapeu = shape as BulletShapeUnman;
1180 return BSAPICPP.IsConvex2(shapeu.ptr);
1181}
1182
1183public override bool IsNonMoving(BulletShape shape)
1184{
1185 BulletShapeUnman shapeu = shape as BulletShapeUnman;
1186 return BSAPICPP.IsNonMoving2(shapeu.ptr);
1187}
1188
1189public override bool IsConcave(BulletShape shape)
1190{
1191 BulletShapeUnman shapeu = shape as BulletShapeUnman;
1192 return BSAPICPP.IsConcave2(shapeu.ptr);
1193}
1194
1195public override bool IsCompound(BulletShape shape)
1196{
1197 BulletShapeUnman shapeu = shape as BulletShapeUnman;
1198 return BSAPICPP.IsCompound2(shapeu.ptr);
1199}
1200
1201public override bool IsSoftBody(BulletShape shape)
1202{
1203 BulletShapeUnman shapeu = shape as BulletShapeUnman;
1204 return BSAPICPP.IsSoftBody2(shapeu.ptr);
1205}
1206
1207public override bool IsInfinite(BulletShape shape)
1208{
1209 BulletShapeUnman shapeu = shape as BulletShapeUnman;
1210 return BSAPICPP.IsInfinite2(shapeu.ptr);
1211}
1212
1213public override void SetLocalScaling(BulletShape shape, Vector3 scale)
1214{
1215 BulletShapeUnman shapeu = shape as BulletShapeUnman;
1216 BSAPICPP.SetLocalScaling2(shapeu.ptr, scale);
1217}
1218
1219public override Vector3 GetLocalScaling(BulletShape shape)
1220{
1221 BulletShapeUnman shapeu = shape as BulletShapeUnman;
1222 return BSAPICPP.GetLocalScaling2(shapeu.ptr);
1223}
1224
1225public override Vector3 CalculateLocalInertia(BulletShape shape, float mass)
1226{
1227 BulletShapeUnman shapeu = shape as BulletShapeUnman;
1228 return BSAPICPP.CalculateLocalInertia2(shapeu.ptr, mass);
1229}
1230
1231public override int GetShapeType(BulletShape shape)
1232{
1233 BulletShapeUnman shapeu = shape as BulletShapeUnman;
1234 return BSAPICPP.GetShapeType2(shapeu.ptr);
1235}
1236
1237public override void SetMargin(BulletShape shape, float val)
1238{
1239 BulletShapeUnman shapeu = shape as BulletShapeUnman;
1240 BSAPICPP.SetMargin2(shapeu.ptr, val);
1241}
1242
1243public override float GetMargin(BulletShape shape)
1244{
1245 BulletShapeUnman shapeu = shape as BulletShapeUnman;
1246 return BSAPICPP.GetMargin2(shapeu.ptr);
1247}
1248
1249// =====================================================================================
1250// Debugging
1251public override void DumpRigidBody(BulletWorld world, BulletBody collisionObject)
1252{
1253 BulletWorldUnman worldu = world as BulletWorldUnman;
1254 BulletBodyUnman bodyu = collisionObject as BulletBodyUnman;
1255 BSAPICPP.DumpRigidBody2(worldu.ptr, bodyu.ptr);
1256}
1257
1258public override void DumpCollisionShape(BulletWorld world, BulletShape collisionShape)
1259{
1260 BulletWorldUnman worldu = world as BulletWorldUnman;
1261 BulletShapeUnman shapeu = collisionShape as BulletShapeUnman;
1262 BSAPICPP.DumpCollisionShape2(worldu.ptr, shapeu.ptr);
1263}
1264
1265public override void DumpConstraint(BulletWorld world, BulletConstraint constrain)
1266{
1267 BulletWorldUnman worldu = world as BulletWorldUnman;
1268 BulletConstraintUnman constrainu = constrain as BulletConstraintUnman;
1269 BSAPICPP.DumpConstraint2(worldu.ptr, constrainu.ptr);
1270}
1271
1272public override void DumpActivationInfo(BulletWorld world)
1273{
1274 BulletWorldUnman worldu = world as BulletWorldUnman;
1275 BSAPICPP.DumpActivationInfo2(worldu.ptr);
1276}
1277
1278public override void DumpAllInfo(BulletWorld world)
1279{
1280 BulletWorldUnman worldu = world as BulletWorldUnman;
1281 BSAPICPP.DumpAllInfo2(worldu.ptr);
1282}
1283
1284public override void DumpPhysicsStatistics(BulletWorld world)
1285{
1286 BulletWorldUnman worldu = world as BulletWorldUnman;
1287 BSAPICPP.DumpPhysicsStatistics2(worldu.ptr);
1288}
1289public override void ResetBroadphasePool(BulletWorld world)
1290{
1291 BulletWorldUnman worldu = world as BulletWorldUnman;
1292 BSAPICPP.ResetBroadphasePool(worldu.ptr);
1293}
1294public override void ResetConstraintSolver(BulletWorld world)
1295{
1296 BulletWorldUnman worldu = world as BulletWorldUnman;
1297 BSAPICPP.ResetConstraintSolver(worldu.ptr);
1298}
1299
1300// =====================================================================================
1301// =====================================================================================
1302// =====================================================================================
1303// =====================================================================================
1304// =====================================================================================
1305// The actual interface to the unmanaged code
1306static class BSAPICPP
1307{
1308// ===============================================================================
1309// Link back to the managed code for outputting log messages
1310[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
1311public delegate void DebugLogCallback([MarshalAs(UnmanagedType.LPStr)]string msg);
1312
1313// ===============================================================================
1314// Initialization and simulation
1315[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1316public static extern IntPtr Initialize2(Vector3 maxPosition, IntPtr parms,
1317 int maxCollisions, IntPtr collisionArray,
1318 int maxUpdates, IntPtr updateArray,
1319 DebugLogCallback logRoutine);
1320
1321[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1322public static extern int PhysicsStep2(IntPtr world, float timeStep, int maxSubSteps, float fixedTimeStep,
1323 out int updatedEntityCount, out int collidersCount);
1324
1325[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1326public static extern void Shutdown2(IntPtr sim);
1327
1328[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1329public static extern bool PushUpdate2(IntPtr obj);
1330
1331[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1332public static extern bool UpdateParameter2(IntPtr world, uint localID, String parm, float value);
1333
1334// =====================================================================================
1335// Mesh, hull, shape and body creation helper routines
1336[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1337public static extern IntPtr CreateMeshShape2(IntPtr world,
1338 int indicesCount, [MarshalAs(UnmanagedType.LPArray)] int[] indices,
1339 int verticesCount, [MarshalAs(UnmanagedType.LPArray)] float[] vertices );
1340
1341[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1342public static extern IntPtr CreateHullShape2(IntPtr world,
1343 int hullCount, [MarshalAs(UnmanagedType.LPArray)] float[] hulls);
1344
1345[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1346public static extern IntPtr BuildHullShapeFromMesh2(IntPtr world, IntPtr meshShape);
1347
1348[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1349public static extern IntPtr BuildNativeShape2(IntPtr world, ShapeData shapeData);
1350
1351[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1352public static extern bool IsNativeShape2(IntPtr shape);
1353
1354[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1355public static extern void SetShapeCollisionMargin2(IntPtr shape, float margin);
1356
1357[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1358public static extern IntPtr BuildCapsuleShape2(IntPtr world, float radius, float height, Vector3 scale);
1359
1360[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1361public static extern IntPtr CreateCompoundShape2(IntPtr sim, bool enableDynamicAabbTree);
1362
1363[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1364public static extern int GetNumberOfCompoundChildren2(IntPtr cShape);
1365
1366[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1367public static extern void AddChildShapeToCompoundShape2(IntPtr cShape, IntPtr addShape, Vector3 pos, Quaternion rot);
1368
1369[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1370public static extern IntPtr GetChildShapeFromCompoundShapeIndex2(IntPtr cShape, int indx);
1371
1372[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1373public static extern IntPtr RemoveChildShapeFromCompoundShapeIndex2(IntPtr cShape, int indx);
1374
1375[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1376public static extern void RemoveChildShapeFromCompoundShape2(IntPtr cShape, IntPtr removeShape);
1377
1378[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1379public static extern void UpdateChildTransform2(IntPtr pShape, int childIndex, Vector3 pos, Quaternion rot, bool shouldRecalculateLocalAabb);
1380
1381[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1382public static extern void RecalculateCompoundShapeLocalAabb2(IntPtr cShape);
1383
1384[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1385public static extern IntPtr DuplicateCollisionShape2(IntPtr sim, IntPtr srcShape, uint id);
1386
1387[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1388public static extern bool DeleteCollisionShape2(IntPtr world, IntPtr shape);
1389
1390[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1391public static extern int GetBodyType2(IntPtr obj);
1392
1393[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1394public static extern IntPtr CreateBodyFromShape2(IntPtr sim, IntPtr shape, uint id, Vector3 pos, Quaternion rot);
1395
1396[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1397public static extern IntPtr CreateBodyWithDefaultMotionState2(IntPtr shape, uint id, Vector3 pos, Quaternion rot);
1398
1399[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1400public static extern IntPtr CreateGhostFromShape2(IntPtr sim, IntPtr shape, uint id, Vector3 pos, Quaternion rot);
1401
1402[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1403public static extern void DestroyObject2(IntPtr sim, IntPtr obj);
1404
1405// =====================================================================================
1406// Terrain creation and helper routines
1407[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1408public static extern IntPtr CreateGroundPlaneShape2(uint id, float height, float collisionMargin);
1409
1410[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1411public static extern IntPtr CreateTerrainShape2(uint id, Vector3 size, float minHeight, float maxHeight,
1412 [MarshalAs(UnmanagedType.LPArray)] float[] heightMap,
1413 float scaleFactor, float collisionMargin);
1414
1415// =====================================================================================
1416// Constraint creation and helper routines
1417[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1418public static extern IntPtr Create6DofConstraint2(IntPtr world, IntPtr obj1, IntPtr obj2,
1419 Vector3 frame1loc, Quaternion frame1rot,
1420 Vector3 frame2loc, Quaternion frame2rot,
1421 bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies);
1422
1423[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1424public static extern IntPtr Create6DofConstraintToPoint2(IntPtr world, IntPtr obj1, IntPtr obj2,
1425 Vector3 joinPoint,
1426 bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies);
1427
1428[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1429public static extern IntPtr CreateHingeConstraint2(IntPtr world, IntPtr obj1, IntPtr obj2,
1430 Vector3 pivotinA, Vector3 pivotinB,
1431 Vector3 axisInA, Vector3 axisInB,
1432 bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies);
1433
1434[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1435public static extern void SetConstraintEnable2(IntPtr constrain, float numericTrueFalse);
1436
1437[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1438public static extern void SetConstraintNumSolverIterations2(IntPtr constrain, float iterations);
1439
1440[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1441public static extern bool SetFrames2(IntPtr constrain,
1442 Vector3 frameA, Quaternion frameArot, Vector3 frameB, Quaternion frameBrot);
1443
1444[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1445public static extern bool SetLinearLimits2(IntPtr constrain, Vector3 low, Vector3 hi);
1446
1447[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1448public static extern bool SetAngularLimits2(IntPtr constrain, Vector3 low, Vector3 hi);
1449
1450[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1451public static extern bool UseFrameOffset2(IntPtr constrain, float enable);
1452
1453[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1454public static extern bool TranslationalLimitMotor2(IntPtr constrain, float enable, float targetVel, float maxMotorForce);
1455
1456[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1457public static extern bool SetBreakingImpulseThreshold2(IntPtr constrain, float threshold);
1458
1459[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1460public static extern bool CalculateTransforms2(IntPtr constrain);
1461
1462[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1463public static extern bool SetConstraintParam2(IntPtr constrain, ConstraintParams paramIndex, float value, ConstraintParamAxis axis);
1464
1465[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1466public static extern bool DestroyConstraint2(IntPtr world, IntPtr constrain);
1467
1468// =====================================================================================
1469// btCollisionWorld entries
1470[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1471public static extern void UpdateSingleAabb2(IntPtr world, IntPtr obj);
1472
1473[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1474public static extern void UpdateAabbs2(IntPtr world);
1475
1476[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1477public static extern bool GetForceUpdateAllAabbs2(IntPtr world);
1478
1479[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1480public static extern void SetForceUpdateAllAabbs2(IntPtr world, bool force);
1481
1482// =====================================================================================
1483// btDynamicsWorld entries
1484[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1485public static extern bool AddObjectToWorld2(IntPtr world, IntPtr obj);
1486
1487[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1488public static extern bool RemoveObjectFromWorld2(IntPtr world, IntPtr obj);
1489
1490[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1491public static extern bool AddConstraintToWorld2(IntPtr world, IntPtr constrain, bool disableCollisionsBetweenLinkedObjects);
1492
1493[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1494public static extern bool RemoveConstraintFromWorld2(IntPtr world, IntPtr constrain);
1495// =====================================================================================
1496// btCollisionObject entries
1497[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1498public static extern Vector3 GetAnisotripicFriction2(IntPtr constrain);
1499
1500[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1501public static extern Vector3 SetAnisotripicFriction2(IntPtr constrain, Vector3 frict);
1502
1503[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1504public static extern bool HasAnisotripicFriction2(IntPtr constrain);
1505
1506[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1507public static extern void SetContactProcessingThreshold2(IntPtr obj, float val);
1508
1509[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1510public static extern float GetContactProcessingThreshold2(IntPtr obj);
1511
1512[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1513public static extern bool IsStaticObject2(IntPtr obj);
1514
1515[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1516public static extern bool IsKinematicObject2(IntPtr obj);
1517
1518[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1519public static extern bool IsStaticOrKinematicObject2(IntPtr obj);
1520
1521[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1522public static extern bool HasContactResponse2(IntPtr obj);
1523
1524[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1525public static extern void SetCollisionShape2(IntPtr sim, IntPtr obj, IntPtr shape);
1526
1527[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1528public static extern IntPtr GetCollisionShape2(IntPtr obj);
1529
1530[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1531public static extern int GetActivationState2(IntPtr obj);
1532
1533[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1534public static extern void SetActivationState2(IntPtr obj, int state);
1535
1536[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1537public static extern void SetDeactivationTime2(IntPtr obj, float dtime);
1538
1539[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1540public static extern float GetDeactivationTime2(IntPtr obj);
1541
1542[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1543public static extern void ForceActivationState2(IntPtr obj, ActivationState state);
1544
1545[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1546public static extern void Activate2(IntPtr obj, bool forceActivation);
1547
1548[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1549public static extern bool IsActive2(IntPtr obj);
1550
1551[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1552public static extern void SetRestitution2(IntPtr obj, float val);
1553
1554[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1555public static extern float GetRestitution2(IntPtr obj);
1556
1557[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1558public static extern void SetFriction2(IntPtr obj, float val);
1559
1560[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1561public static extern float GetFriction2(IntPtr obj);
1562
1563 /* Haven't defined the type 'Transform'
1564[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1565public static extern Transform GetWorldTransform2(IntPtr obj);
1566
1567[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1568public static extern void setWorldTransform2(IntPtr obj, Transform trans);
1569 */
1570
1571[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1572public static extern Vector3 GetPosition2(IntPtr obj);
1573
1574[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1575public static extern Quaternion GetOrientation2(IntPtr obj);
1576
1577[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1578public static extern void SetTranslation2(IntPtr obj, Vector3 position, Quaternion rotation);
1579
1580[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1581public static extern IntPtr GetBroadphaseHandle2(IntPtr obj);
1582
1583[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1584public static extern void SetBroadphaseHandle2(IntPtr obj, IntPtr handle);
1585
1586 /*
1587[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1588public static extern Transform GetInterpolationWorldTransform2(IntPtr obj);
1589
1590[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1591public static extern void SetInterpolationWorldTransform2(IntPtr obj, Transform trans);
1592 */
1593
1594[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1595public static extern void SetInterpolationLinearVelocity2(IntPtr obj, Vector3 vel);
1596
1597[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1598public static extern void SetInterpolationAngularVelocity2(IntPtr obj, Vector3 vel);
1599
1600[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1601public static extern void SetInterpolationVelocity2(IntPtr obj, Vector3 linearVel, Vector3 angularVel);
1602
1603[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1604public static extern float GetHitFraction2(IntPtr obj);
1605
1606[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1607public static extern void SetHitFraction2(IntPtr obj, float val);
1608
1609[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1610public static extern CollisionFlags GetCollisionFlags2(IntPtr obj);
1611
1612[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1613public static extern CollisionFlags SetCollisionFlags2(IntPtr obj, CollisionFlags flags);
1614
1615[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1616public static extern CollisionFlags AddToCollisionFlags2(IntPtr obj, CollisionFlags flags);
1617
1618[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1619public static extern CollisionFlags RemoveFromCollisionFlags2(IntPtr obj, CollisionFlags flags);
1620
1621[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1622public static extern float GetCcdMotionThreshold2(IntPtr obj);
1623
1624[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1625public static extern void SetCcdMotionThreshold2(IntPtr obj, float val);
1626
1627[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1628public static extern float GetCcdSweptSphereRadius2(IntPtr obj);
1629
1630[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1631public static extern void SetCcdSweptSphereRadius2(IntPtr obj, float val);
1632
1633[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1634public static extern IntPtr GetUserPointer2(IntPtr obj);
1635
1636[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1637public static extern void SetUserPointer2(IntPtr obj, IntPtr val);
1638
1639// =====================================================================================
1640// btRigidBody entries
1641[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1642public static extern void ApplyGravity2(IntPtr obj);
1643
1644[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1645public static extern void SetGravity2(IntPtr obj, Vector3 val);
1646
1647[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1648public static extern Vector3 GetGravity2(IntPtr obj);
1649
1650[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1651public static extern void SetDamping2(IntPtr obj, float lin_damping, float ang_damping);
1652
1653[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1654public static extern void SetLinearDamping2(IntPtr obj, float lin_damping);
1655
1656[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1657public static extern void SetAngularDamping2(IntPtr obj, float ang_damping);
1658
1659[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1660public static extern float GetLinearDamping2(IntPtr obj);
1661
1662[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1663public static extern float GetAngularDamping2(IntPtr obj);
1664
1665[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1666public static extern float GetLinearSleepingThreshold2(IntPtr obj);
1667
1668[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1669public static extern float GetAngularSleepingThreshold2(IntPtr obj);
1670
1671[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1672public static extern void ApplyDamping2(IntPtr obj, float timeStep);
1673
1674[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1675public static extern void SetMassProps2(IntPtr obj, float mass, Vector3 inertia);
1676
1677[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1678public static extern Vector3 GetLinearFactor2(IntPtr obj);
1679
1680[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1681public static extern void SetLinearFactor2(IntPtr obj, Vector3 factor);
1682
1683 /*
1684[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1685public static extern void SetCenterOfMassTransform2(IntPtr obj, Transform trans);
1686 */
1687
1688[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1689public static extern void SetCenterOfMassByPosRot2(IntPtr obj, Vector3 pos, Quaternion rot);
1690
1691// Add a force to the object as if its mass is one.
1692[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1693public static extern void ApplyCentralForce2(IntPtr obj, Vector3 force);
1694
1695// Set the force being applied to the object as if its mass is one.
1696[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1697public static extern void SetObjectForce2(IntPtr obj, Vector3 force);
1698
1699[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1700public static extern Vector3 GetTotalForce2(IntPtr obj);
1701
1702[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1703public static extern Vector3 GetTotalTorque2(IntPtr obj);
1704
1705[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1706public static extern Vector3 GetInvInertiaDiagLocal2(IntPtr obj);
1707
1708[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1709public static extern void SetInvInertiaDiagLocal2(IntPtr obj, Vector3 inert);
1710
1711[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1712public static extern void SetSleepingThresholds2(IntPtr obj, float lin_threshold, float ang_threshold);
1713
1714[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1715public static extern void ApplyTorque2(IntPtr obj, Vector3 torque);
1716
1717// Apply force at the given point. Will add torque to the object.
1718[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1719public static extern void ApplyForce2(IntPtr obj, Vector3 force, Vector3 pos);
1720
1721// Apply impulse to the object. Same as "ApplycentralForce" but force scaled by object's mass.
1722[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1723public static extern void ApplyCentralImpulse2(IntPtr obj, Vector3 imp);
1724
1725// Apply impulse to the object's torque. Force is scaled by object's mass.
1726[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1727public static extern void ApplyTorqueImpulse2(IntPtr obj, Vector3 imp);
1728
1729// Apply impulse at the point given. For is scaled by object's mass and effects both linear and angular forces.
1730[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1731public static extern void ApplyImpulse2(IntPtr obj, Vector3 imp, Vector3 pos);
1732
1733[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1734public static extern void ClearForces2(IntPtr obj);
1735
1736[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1737public static extern void ClearAllForces2(IntPtr obj);
1738
1739[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1740public static extern void UpdateInertiaTensor2(IntPtr obj);
1741
1742[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1743public static extern Vector3 GetCenterOfMassPosition2(IntPtr obj);
1744
1745 /*
1746[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1747public static extern Transform GetCenterOfMassTransform2(IntPtr obj);
1748 */
1749
1750[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1751public static extern Vector3 GetLinearVelocity2(IntPtr obj);
1752
1753[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1754public static extern Vector3 GetAngularVelocity2(IntPtr obj);
1755
1756[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1757public static extern void SetLinearVelocity2(IntPtr obj, Vector3 val);
1758
1759[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1760public static extern void SetAngularVelocity2(IntPtr obj, Vector3 angularVelocity);
1761
1762[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1763public static extern Vector3 GetVelocityInLocalPoint2(IntPtr obj, Vector3 pos);
1764
1765[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1766public static extern void Translate2(IntPtr obj, Vector3 trans);
1767
1768[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1769public static extern void UpdateDeactivation2(IntPtr obj, float timeStep);
1770
1771[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1772public static extern bool WantsSleeping2(IntPtr obj);
1773
1774[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1775public static extern void SetAngularFactor2(IntPtr obj, float factor);
1776
1777[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1778public static extern void SetAngularFactorV2(IntPtr obj, Vector3 factor);
1779
1780[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1781public static extern Vector3 GetAngularFactor2(IntPtr obj);
1782
1783[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1784public static extern bool IsInWorld2(IntPtr obj);
1785
1786[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1787public static extern void AddConstraintRef2(IntPtr obj, IntPtr constrain);
1788
1789[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1790public static extern void RemoveConstraintRef2(IntPtr obj, IntPtr constrain);
1791
1792[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1793public static extern IntPtr GetConstraintRef2(IntPtr obj, int index);
1794
1795[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1796public static extern int GetNumConstraintRefs2(IntPtr obj);
1797
1798[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1799public static extern bool SetCollisionGroupMask2(IntPtr body, uint filter, uint mask);
1800
1801// =====================================================================================
1802// btCollisionShape entries
1803
1804[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1805public static extern float GetAngularMotionDisc2(IntPtr shape);
1806
1807[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1808public static extern float GetContactBreakingThreshold2(IntPtr shape, float defaultFactor);
1809
1810[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1811public static extern bool IsPolyhedral2(IntPtr shape);
1812
1813[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1814public static extern bool IsConvex2d2(IntPtr shape);
1815
1816[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1817public static extern bool IsConvex2(IntPtr shape);
1818
1819[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1820public static extern bool IsNonMoving2(IntPtr shape);
1821
1822[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1823public static extern bool IsConcave2(IntPtr shape);
1824
1825[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1826public static extern bool IsCompound2(IntPtr shape);
1827
1828[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1829public static extern bool IsSoftBody2(IntPtr shape);
1830
1831[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1832public static extern bool IsInfinite2(IntPtr shape);
1833
1834[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1835public static extern void SetLocalScaling2(IntPtr shape, Vector3 scale);
1836
1837[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1838public static extern Vector3 GetLocalScaling2(IntPtr shape);
1839
1840[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1841public static extern Vector3 CalculateLocalInertia2(IntPtr shape, float mass);
1842
1843[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1844public static extern int GetShapeType2(IntPtr shape);
1845
1846[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1847public static extern void SetMargin2(IntPtr shape, float val);
1848
1849[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1850public static extern float GetMargin2(IntPtr shape);
1851
1852// =====================================================================================
1853// Debugging
1854[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1855public static extern void DumpRigidBody2(IntPtr sim, IntPtr collisionObject);
1856
1857[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1858public static extern void DumpCollisionShape2(IntPtr sim, IntPtr collisionShape);
1859
1860[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1861public static extern void DumpMapInfo2(IntPtr sim, IntPtr manInfo);
1862
1863[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1864public static extern void DumpConstraint2(IntPtr sim, IntPtr constrain);
1865
1866[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1867public static extern void DumpActivationInfo2(IntPtr sim);
1868
1869[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1870public static extern void DumpAllInfo2(IntPtr sim);
1871
1872[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1873public static extern void DumpPhysicsStatistics2(IntPtr sim);
1874
1875[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1876public static extern void ResetBroadphasePool(IntPtr sim);
1877
1878[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1879public static extern void ResetConstraintSolver(IntPtr sim);
1880
1881}
1882
1883}
1884
1885}
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSAPIXNA.cs b/OpenSim/Region/Physics/BulletSPlugin/BSAPIXNA.cs
new file mode 100755
index 0000000..b6ff52b
--- /dev/null
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSAPIXNA.cs
@@ -0,0 +1,1598 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyrightD
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27using System;
28using System.Collections.Generic;
29using System.IO;
30using System.Text;
31
32using OpenSim.Framework;
33
34using OpenMetaverse;
35
36using BulletXNA;
37using BulletXNA.LinearMath;
38using BulletXNA.BulletCollision;
39using BulletXNA.BulletDynamics;
40using BulletXNA.BulletCollision.CollisionDispatch;
41
42namespace OpenSim.Region.Physics.BulletSPlugin
43{
44public sealed class BSAPIXNA : BSAPITemplate
45{
46private sealed class BulletWorldXNA : BulletWorld
47{
48 public DiscreteDynamicsWorld world;
49 public BulletWorldXNA(uint id, BSScene physScene, DiscreteDynamicsWorld xx)
50 : base(id, physScene)
51 {
52 world = xx;
53 }
54}
55
56private sealed class BulletBodyXNA : BulletBody
57{
58 public CollisionObject body;
59 public RigidBody rigidBody { get { return RigidBody.Upcast(body); } }
60
61 public BulletBodyXNA(uint id, CollisionObject xx)
62 : base(id)
63 {
64 body = xx;
65 }
66 public override bool HasPhysicalBody
67 {
68 get { return body != null; }
69 }
70 public override void Clear()
71 {
72 body = null;
73 }
74 public override string AddrString
75 {
76 get { return "XNARigidBody"; }
77 }
78}
79
80private sealed class BulletShapeXNA : BulletShape
81{
82 public CollisionShape shape;
83 public BulletShapeXNA(CollisionShape xx, BSPhysicsShapeType typ)
84 : base()
85 {
86 shape = xx;
87 type = typ;
88 }
89 public override bool HasPhysicalShape
90 {
91 get { return shape != null; }
92 }
93 public override void Clear()
94 {
95 shape = null;
96 }
97 public override BulletShape Clone()
98 {
99 return new BulletShapeXNA(shape, type);
100 }
101 public override bool ReferenceSame(BulletShape other)
102 {
103 BulletShapeXNA otheru = other as BulletShapeXNA;
104 return (otheru != null) && (this.shape == otheru.shape);
105
106 }
107 public override string AddrString
108 {
109 get { return "XNACollisionShape"; }
110 }
111}
112private sealed class BulletConstraintXNA : BulletConstraint
113{
114 public TypedConstraint constrain;
115 public BulletConstraintXNA(TypedConstraint xx) : base()
116 {
117 constrain = xx;
118 }
119
120 public override void Clear()
121 {
122 constrain = null;
123 }
124 public override bool HasPhysicalConstraint { get { return constrain != null; } }
125
126 // Used for log messages for a unique display of the memory/object allocated to this instance
127 public override string AddrString
128 {
129 get { return "XNAConstraint"; }
130 }
131}
132
133 private static int m_collisionsThisFrame;
134 private BSScene PhysicsScene { get; set; }
135
136 public override string BulletEngineName { get { return "BulletXNA"; } }
137 public override string BulletEngineVersion { get; protected set; }
138
139 public BSAPIXNA(string paramName, BSScene physScene)
140 {
141 PhysicsScene = physScene;
142 }
143
144 /// <summary>
145 ///
146 /// </summary>
147 /// <param name="p"></param>
148 /// <param name="p_2"></param>
149 public override bool RemoveObjectFromWorld(BulletWorld pWorld, BulletBody pBody)
150 {
151 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world;
152 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
153 world.RemoveRigidBody(body);
154 return true;
155 }
156
157 public override bool AddConstraintToWorld(BulletWorld world, BulletConstraint constrain, bool disableCollisionsBetweenLinkedObjects)
158 {
159 /* TODO */
160 return false;
161 }
162
163 public override bool RemoveConstraintFromWorld(BulletWorld world, BulletConstraint constrain)
164 {
165 /* TODO */
166 return false;
167 }
168
169 public override void SetRestitution(BulletBody pBody, float pRestitution)
170 {
171 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
172 body.SetRestitution(pRestitution);
173 }
174
175 public override int GetShapeType(BulletShape pShape)
176 {
177 CollisionShape shape = ((BulletShapeXNA)pShape).shape;
178 return (int)shape.GetShapeType();
179 }
180 public override void SetMargin(BulletShape pShape, float pMargin)
181 {
182 CollisionShape shape = ((BulletShapeXNA)pShape).shape;
183 shape.SetMargin(pMargin);
184 }
185
186 public override float GetMargin(BulletShape pShape)
187 {
188 CollisionShape shape = ((BulletShapeXNA)pShape).shape;
189 return shape.GetMargin();
190 }
191
192 public override void SetLocalScaling(BulletShape pShape, Vector3 pScale)
193 {
194 CollisionShape shape = ((BulletShapeXNA)pShape).shape;
195 IndexedVector3 vec = new IndexedVector3(pScale.X, pScale.Y, pScale.Z);
196 shape.SetLocalScaling(ref vec);
197
198 }
199
200 public override void SetContactProcessingThreshold(BulletBody pBody, float contactprocessingthreshold)
201 {
202 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
203 body.SetContactProcessingThreshold(contactprocessingthreshold);
204 }
205
206 public override void SetCcdMotionThreshold(BulletBody pBody, float pccdMotionThreashold)
207 {
208 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
209 body.SetCcdMotionThreshold(pccdMotionThreashold);
210 }
211
212 public override void SetCcdSweptSphereRadius(BulletBody pBody, float pCcdSweptSphereRadius)
213 {
214 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
215 body.SetCcdSweptSphereRadius(pCcdSweptSphereRadius);
216 }
217
218 public override void SetAngularFactorV(BulletBody pBody, Vector3 pAngularFactor)
219 {
220 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
221 body.SetAngularFactor(new IndexedVector3(pAngularFactor.X, pAngularFactor.Y, pAngularFactor.Z));
222 }
223
224 public override CollisionFlags AddToCollisionFlags(BulletBody pBody, CollisionFlags pcollisionFlags)
225 {
226 CollisionObject body = ((BulletBodyXNA)pBody).body;
227 CollisionFlags existingcollisionFlags = (CollisionFlags)(uint)body.GetCollisionFlags();
228 existingcollisionFlags |= pcollisionFlags;
229 body.SetCollisionFlags((BulletXNA.BulletCollision.CollisionFlags)(uint)existingcollisionFlags);
230 return (CollisionFlags) (uint) existingcollisionFlags;
231 }
232
233 public override bool AddObjectToWorld(BulletWorld pWorld, BulletBody pBody)
234 {
235 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world;
236 CollisionObject cbody = ((BulletBodyXNA)pBody).body;
237 RigidBody rbody = cbody as RigidBody;
238
239 // Bullet resets several variables when an object is added to the world. In particular,
240 // BulletXNA resets position and rotation. Gravity is also reset depending on the static/dynamic
241 // type. Of course, the collision flags in the broadphase proxy are initialized to default.
242 IndexedMatrix origPos = cbody.GetWorldTransform();
243 if (rbody != null)
244 {
245 IndexedVector3 origGrav = rbody.GetGravity();
246 world.AddRigidBody(rbody);
247 rbody.SetGravity(origGrav);
248 }
249 else
250 {
251 world.AddCollisionObject(rbody);
252 }
253 cbody.SetWorldTransform(origPos);
254
255 pBody.ApplyCollisionMask(pWorld.physicsScene);
256
257 //if (body.GetBroadphaseHandle() != null)
258 // world.UpdateSingleAabb(body);
259 return true;
260 }
261
262 public override void ForceActivationState(BulletBody pBody, ActivationState pActivationState)
263 {
264 CollisionObject body = ((BulletBodyXNA)pBody).body;
265 body.ForceActivationState((BulletXNA.BulletCollision.ActivationState)(uint)pActivationState);
266 }
267
268 public override void UpdateSingleAabb(BulletWorld pWorld, BulletBody pBody)
269 {
270 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world;
271 CollisionObject body = ((BulletBodyXNA)pBody).body;
272 world.UpdateSingleAabb(body);
273 }
274
275 public override void UpdateAabbs(BulletWorld world) { /* TODO */ }
276 public override bool GetForceUpdateAllAabbs(BulletWorld world) { /* TODO */ return false; }
277 public override void SetForceUpdateAllAabbs(BulletWorld world, bool force) { /* TODO */ }
278
279 public override bool SetCollisionGroupMask(BulletBody pBody, uint pGroup, uint pMask)
280 {
281 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
282 body.GetBroadphaseHandle().m_collisionFilterGroup = (BulletXNA.BulletCollision.CollisionFilterGroups) pGroup;
283 body.GetBroadphaseHandle().m_collisionFilterGroup = (BulletXNA.BulletCollision.CollisionFilterGroups) pGroup;
284 if ((uint) body.GetBroadphaseHandle().m_collisionFilterGroup == 0)
285 return false;
286 return true;
287 }
288
289 public override void ClearAllForces(BulletBody pBody)
290 {
291 CollisionObject body = ((BulletBodyXNA)pBody).body;
292 IndexedVector3 zeroVector = new IndexedVector3(0, 0, 0);
293 body.SetInterpolationLinearVelocity(ref zeroVector);
294 body.SetInterpolationAngularVelocity(ref zeroVector);
295 IndexedMatrix bodytransform = body.GetWorldTransform();
296
297 body.SetInterpolationWorldTransform(ref bodytransform);
298
299 if (body is RigidBody)
300 {
301 RigidBody rigidbody = body as RigidBody;
302 rigidbody.SetLinearVelocity(zeroVector);
303 rigidbody.SetAngularVelocity(zeroVector);
304 rigidbody.ClearForces();
305 }
306 }
307
308 public override void SetInterpolationAngularVelocity(BulletBody pBody, Vector3 pVector3)
309 {
310 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
311 IndexedVector3 vec = new IndexedVector3(pVector3.X, pVector3.Y, pVector3.Z);
312 body.SetInterpolationAngularVelocity(ref vec);
313 }
314
315 public override void SetAngularVelocity(BulletBody pBody, Vector3 pVector3)
316 {
317 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
318 IndexedVector3 vec = new IndexedVector3(pVector3.X, pVector3.Y, pVector3.Z);
319 body.SetAngularVelocity(ref vec);
320 }
321 public override Vector3 GetTotalForce(BulletBody pBody)
322 {
323 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
324 IndexedVector3 iv3 = body.GetTotalForce();
325 return new Vector3(iv3.X, iv3.Y, iv3.Z);
326 }
327 public override Vector3 GetTotalTorque(BulletBody pBody)
328 {
329 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
330 IndexedVector3 iv3 = body.GetTotalTorque();
331 return new Vector3(iv3.X, iv3.Y, iv3.Z);
332 }
333 public override Vector3 GetInvInertiaDiagLocal(BulletBody pBody)
334 {
335 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
336 IndexedVector3 iv3 = body.GetInvInertiaDiagLocal();
337 return new Vector3(iv3.X, iv3.Y, iv3.Z);
338 }
339 public override void SetInvInertiaDiagLocal(BulletBody pBody, Vector3 inert)
340 {
341 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
342 IndexedVector3 iv3 = new IndexedVector3(inert.X, inert.Y, inert.Z);
343 body.SetInvInertiaDiagLocal(ref iv3);
344 }
345 public override void ApplyForce(BulletBody pBody, Vector3 force, Vector3 pos)
346 {
347 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
348 IndexedVector3 forceiv3 = new IndexedVector3(force.X, force.Y, force.Z);
349 IndexedVector3 posiv3 = new IndexedVector3(pos.X, pos.Y, pos.Z);
350 body.ApplyForce(ref forceiv3, ref posiv3);
351 }
352 public override void ApplyImpulse(BulletBody pBody, Vector3 imp, Vector3 pos)
353 {
354 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
355 IndexedVector3 impiv3 = new IndexedVector3(imp.X, imp.Y, imp.Z);
356 IndexedVector3 posiv3 = new IndexedVector3(pos.X, pos.Y, pos.Z);
357 body.ApplyImpulse(ref impiv3, ref posiv3);
358 }
359
360 public override void ClearForces(BulletBody pBody)
361 {
362 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
363 body.ClearForces();
364 }
365
366 public override void SetTranslation(BulletBody pBody, Vector3 _position, Quaternion _orientation)
367 {
368 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
369 IndexedVector3 vposition = new IndexedVector3(_position.X, _position.Y, _position.Z);
370 IndexedQuaternion vquaternion = new IndexedQuaternion(_orientation.X, _orientation.Y, _orientation.Z,
371 _orientation.W);
372 IndexedMatrix mat = IndexedMatrix.CreateFromQuaternion(vquaternion);
373 mat._origin = vposition;
374 body.SetWorldTransform(mat);
375
376 }
377
378 public override Vector3 GetPosition(BulletBody pBody)
379 {
380 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
381 IndexedVector3 pos = body.GetInterpolationWorldTransform()._origin;
382 return new Vector3(pos.X, pos.Y, pos.Z);
383 }
384
385 public override Vector3 CalculateLocalInertia(BulletShape pShape, float pphysMass)
386 {
387 CollisionShape shape = ((BulletShapeXNA)pShape).shape;
388 IndexedVector3 inertia = IndexedVector3.Zero;
389 shape.CalculateLocalInertia(pphysMass, out inertia);
390 return new Vector3(inertia.X, inertia.Y, inertia.Z);
391 }
392
393 public override void SetMassProps(BulletBody pBody, float pphysMass, Vector3 plocalInertia)
394 {
395 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
396 IndexedVector3 inertia = new IndexedVector3(plocalInertia.X, plocalInertia.Y, plocalInertia.Z);
397 body.SetMassProps(pphysMass, inertia);
398 }
399
400
401 public override void SetObjectForce(BulletBody pBody, Vector3 _force)
402 {
403 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
404 IndexedVector3 force = new IndexedVector3(_force.X, _force.Y, _force.Z);
405 body.SetTotalForce(ref force);
406 }
407
408 public override void SetFriction(BulletBody pBody, float _currentFriction)
409 {
410 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
411 body.SetFriction(_currentFriction);
412 }
413
414 public override void SetLinearVelocity(BulletBody pBody, Vector3 _velocity)
415 {
416 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
417 IndexedVector3 velocity = new IndexedVector3(_velocity.X, _velocity.Y, _velocity.Z);
418 body.SetLinearVelocity(velocity);
419 }
420
421 public override void Activate(BulletBody pBody, bool pforceactivation)
422 {
423 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
424 body.Activate(pforceactivation);
425
426 }
427
428 public override Quaternion GetOrientation(BulletBody pBody)
429 {
430 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
431 IndexedQuaternion mat = body.GetInterpolationWorldTransform().GetRotation();
432 return new Quaternion(mat.X, mat.Y, mat.Z, mat.W);
433 }
434
435 public override CollisionFlags RemoveFromCollisionFlags(BulletBody pBody, CollisionFlags pcollisionFlags)
436 {
437 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
438 CollisionFlags existingcollisionFlags = (CollisionFlags)(uint)body.GetCollisionFlags();
439 existingcollisionFlags &= ~pcollisionFlags;
440 body.SetCollisionFlags((BulletXNA.BulletCollision.CollisionFlags)(uint)existingcollisionFlags);
441 return (CollisionFlags)(uint)existingcollisionFlags;
442 }
443
444 public override float GetCcdMotionThreshold(BulletBody obj) { /* TODO */ return 0f; }
445
446 public override float GetCcdSweptSphereRadius(BulletBody obj) { /* TODO */ return 0f; }
447
448 public override IntPtr GetUserPointer(BulletBody obj) { /* TODO */ return IntPtr.Zero; }
449
450 public override void SetUserPointer(BulletBody obj, IntPtr val) { /* TODO */ }
451
452 public override void SetGravity(BulletBody pBody, Vector3 pGravity)
453 {
454 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
455 IndexedVector3 gravity = new IndexedVector3(pGravity.X, pGravity.Y, pGravity.Z);
456 body.SetGravity(gravity);
457 }
458
459 public override bool DestroyConstraint(BulletWorld pWorld, BulletConstraint pConstraint)
460 {
461 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world;
462 TypedConstraint constraint = ((BulletConstraintXNA)pConstraint).constrain;
463 world.RemoveConstraint(constraint);
464 return true;
465 }
466
467 public override bool SetLinearLimits(BulletConstraint pConstraint, Vector3 low, Vector3 high)
468 {
469 Generic6DofConstraint constraint = ((BulletConstraintXNA)pConstraint).constrain as Generic6DofConstraint;
470 IndexedVector3 lowlimit = new IndexedVector3(low.X, low.Y, low.Z);
471 IndexedVector3 highlimit = new IndexedVector3(high.X, high.Y, high.Z);
472 constraint.SetLinearLowerLimit(lowlimit);
473 constraint.SetLinearUpperLimit(highlimit);
474 return true;
475 }
476
477 public override bool SetAngularLimits(BulletConstraint pConstraint, Vector3 low, Vector3 high)
478 {
479 Generic6DofConstraint constraint = ((BulletConstraintXNA)pConstraint).constrain as Generic6DofConstraint;
480 IndexedVector3 lowlimit = new IndexedVector3(low.X, low.Y, low.Z);
481 IndexedVector3 highlimit = new IndexedVector3(high.X, high.Y, high.Z);
482 constraint.SetAngularLowerLimit(lowlimit);
483 constraint.SetAngularUpperLimit(highlimit);
484 return true;
485 }
486
487 public override void SetConstraintNumSolverIterations(BulletConstraint pConstraint, float cnt)
488 {
489 Generic6DofConstraint constraint = ((BulletConstraintXNA)pConstraint).constrain as Generic6DofConstraint;
490 constraint.SetOverrideNumSolverIterations((int)cnt);
491 }
492
493 public override bool CalculateTransforms(BulletConstraint pConstraint)
494 {
495 Generic6DofConstraint constraint = ((BulletConstraintXNA)pConstraint).constrain as Generic6DofConstraint;
496 constraint.CalculateTransforms();
497 return true;
498 }
499
500 public override void SetConstraintEnable(BulletConstraint pConstraint, float p_2)
501 {
502 Generic6DofConstraint constraint = ((BulletConstraintXNA)pConstraint).constrain as Generic6DofConstraint;
503 constraint.SetEnabled((p_2 == 0) ? false : true);
504 }
505
506
507 //BulletSimAPI.Create6DofConstraint(m_world.ptr, m_body1.ptr, m_body2.ptr,frame1, frame1rot,frame2, frame2rot,useLinearReferenceFrameA, disableCollisionsBetweenLinkedBodies));
508 public override BulletConstraint Create6DofConstraint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2, Vector3 pframe1, Quaternion pframe1rot, Vector3 pframe2, Quaternion pframe2rot, bool puseLinearReferenceFrameA, bool pdisableCollisionsBetweenLinkedBodies)
509
510 {
511 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world;
512 RigidBody body1 = ((BulletBodyXNA)pBody1).rigidBody;
513 RigidBody body2 = ((BulletBodyXNA)pBody2).rigidBody;
514 IndexedVector3 frame1v = new IndexedVector3(pframe1.X, pframe1.Y, pframe1.Z);
515 IndexedQuaternion frame1rot = new IndexedQuaternion(pframe1rot.X, pframe1rot.Y, pframe1rot.Z, pframe1rot.W);
516 IndexedMatrix frame1 = IndexedMatrix.CreateFromQuaternion(frame1rot);
517 frame1._origin = frame1v;
518
519 IndexedVector3 frame2v = new IndexedVector3(pframe2.X, pframe2.Y, pframe2.Z);
520 IndexedQuaternion frame2rot = new IndexedQuaternion(pframe2rot.X, pframe2rot.Y, pframe2rot.Z, pframe2rot.W);
521 IndexedMatrix frame2 = IndexedMatrix.CreateFromQuaternion(frame2rot);
522 frame2._origin = frame1v;
523
524 Generic6DofConstraint consttr = new Generic6DofConstraint(body1, body2, ref frame1, ref frame2,
525 puseLinearReferenceFrameA);
526 consttr.CalculateTransforms();
527 world.AddConstraint(consttr,pdisableCollisionsBetweenLinkedBodies);
528
529 return new BulletConstraintXNA(consttr);
530 }
531
532
533 /// <summary>
534 ///
535 /// </summary>
536 /// <param name="pWorld"></param>
537 /// <param name="pBody1"></param>
538 /// <param name="pBody2"></param>
539 /// <param name="pjoinPoint"></param>
540 /// <param name="puseLinearReferenceFrameA"></param>
541 /// <param name="pdisableCollisionsBetweenLinkedBodies"></param>
542 /// <returns></returns>
543 public override BulletConstraint Create6DofConstraintToPoint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2, Vector3 pjoinPoint, bool puseLinearReferenceFrameA, bool pdisableCollisionsBetweenLinkedBodies)
544 {
545 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world;
546 RigidBody body1 = ((BulletBodyXNA)pBody1).rigidBody;
547 RigidBody body2 = ((BulletBodyXNA)pBody2).rigidBody;
548 IndexedMatrix frame1 = new IndexedMatrix(IndexedBasisMatrix.Identity, new IndexedVector3(0, 0, 0));
549 IndexedMatrix frame2 = new IndexedMatrix(IndexedBasisMatrix.Identity, new IndexedVector3(0, 0, 0));
550
551 IndexedVector3 joinPoint = new IndexedVector3(pjoinPoint.X, pjoinPoint.Y, pjoinPoint.Z);
552 IndexedMatrix mat = IndexedMatrix.Identity;
553 mat._origin = new IndexedVector3(pjoinPoint.X, pjoinPoint.Y, pjoinPoint.Z);
554 frame1._origin = body1.GetWorldTransform().Inverse()*joinPoint;
555 frame2._origin = body2.GetWorldTransform().Inverse()*joinPoint;
556
557 Generic6DofConstraint consttr = new Generic6DofConstraint(body1, body2, ref frame1, ref frame2, puseLinearReferenceFrameA);
558 consttr.CalculateTransforms();
559 world.AddConstraint(consttr, pdisableCollisionsBetweenLinkedBodies);
560
561 return new BulletConstraintXNA(consttr);
562 }
563 //SetFrames(m_constraint.ptr, frameA, frameArot, frameB, frameBrot);
564 public override bool SetFrames(BulletConstraint pConstraint, Vector3 pframe1, Quaternion pframe1rot, Vector3 pframe2, Quaternion pframe2rot)
565 {
566 Generic6DofConstraint constraint = ((BulletConstraintXNA)pConstraint).constrain as Generic6DofConstraint;
567 IndexedVector3 frame1v = new IndexedVector3(pframe1.X, pframe1.Y, pframe1.Z);
568 IndexedQuaternion frame1rot = new IndexedQuaternion(pframe1rot.X, pframe1rot.Y, pframe1rot.Z, pframe1rot.W);
569 IndexedMatrix frame1 = IndexedMatrix.CreateFromQuaternion(frame1rot);
570 frame1._origin = frame1v;
571
572 IndexedVector3 frame2v = new IndexedVector3(pframe2.X, pframe2.Y, pframe2.Z);
573 IndexedQuaternion frame2rot = new IndexedQuaternion(pframe2rot.X, pframe2rot.Y, pframe2rot.Z, pframe2rot.W);
574 IndexedMatrix frame2 = IndexedMatrix.CreateFromQuaternion(frame2rot);
575 frame2._origin = frame1v;
576 constraint.SetFrames(ref frame1, ref frame2);
577 return true;
578 }
579
580 public override Vector3 GetLinearVelocity(BulletBody pBody)
581 {
582 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
583 IndexedVector3 iv3 = body.GetLinearVelocity();
584 return new Vector3(iv3.X, iv3.Y, iv3.Z);
585 }
586 public override Vector3 GetAngularVelocity(BulletBody pBody)
587 {
588 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
589 IndexedVector3 iv3 = body.GetAngularVelocity();
590 return new Vector3(iv3.X, iv3.Y, iv3.Z);
591 }
592 public override Vector3 GetVelocityInLocalPoint(BulletBody pBody, Vector3 pos)
593 {
594 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
595 IndexedVector3 posiv3 = new IndexedVector3(pos.X, pos.Y, pos.Z);
596 IndexedVector3 iv3 = body.GetVelocityInLocalPoint(ref posiv3);
597 return new Vector3(iv3.X, iv3.Y, iv3.Z);
598 }
599 public override void Translate(BulletBody pBody, Vector3 trans)
600 {
601 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
602 }
603 public override void UpdateDeactivation(BulletBody pBody, float timeStep)
604 {
605 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
606 body.UpdateDeactivation(timeStep);
607 }
608
609 public override bool WantsSleeping(BulletBody pBody)
610 {
611 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
612 return body.WantsSleeping();
613 }
614
615 public override void SetAngularFactor(BulletBody pBody, float factor)
616 {
617 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
618 body.SetAngularFactor(factor);
619 }
620
621 public override Vector3 GetAngularFactor(BulletBody pBody)
622 {
623 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
624 IndexedVector3 iv3 = body.GetAngularFactor();
625 return new Vector3(iv3.X, iv3.Y, iv3.Z);
626 }
627
628 public override bool IsInWorld(BulletWorld pWorld, BulletBody pBody)
629 {
630 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world;
631 CollisionObject body = ((BulletBodyXNA)pBody).body;
632 return world.IsInWorld(body);
633 }
634
635 public override void AddConstraintRef(BulletBody pBody, BulletConstraint pConstrain)
636 {
637 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
638 TypedConstraint constrain = ((BulletConstraintXNA)pConstrain).constrain;
639 body.AddConstraintRef(constrain);
640 }
641
642 public override void RemoveConstraintRef(BulletBody pBody, BulletConstraint pConstrain)
643 {
644 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
645 TypedConstraint constrain = ((BulletConstraintXNA)pConstrain).constrain;
646 body.RemoveConstraintRef(constrain);
647 }
648
649 public override BulletConstraint GetConstraintRef(BulletBody pBody, int index)
650 {
651 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
652 return new BulletConstraintXNA(body.GetConstraintRef(index));
653 }
654
655 public override int GetNumConstraintRefs(BulletBody pBody)
656 {
657 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
658 return body.GetNumConstraintRefs();
659 }
660
661 public override void SetInterpolationLinearVelocity(BulletBody pBody, Vector3 VehicleVelocity)
662 {
663 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
664 IndexedVector3 velocity = new IndexedVector3(VehicleVelocity.X, VehicleVelocity.Y, VehicleVelocity.Z);
665 body.SetInterpolationLinearVelocity(ref velocity);
666 }
667
668 public override bool UseFrameOffset(BulletConstraint pConstraint, float onOff)
669 {
670 Generic6DofConstraint constraint = ((BulletConstraintXNA)pConstraint).constrain as Generic6DofConstraint;
671 constraint.SetUseFrameOffset((onOff == 0) ? false : true);
672 return true;
673 }
674 //SetBreakingImpulseThreshold(m_constraint.ptr, threshold);
675 public override bool SetBreakingImpulseThreshold(BulletConstraint pConstraint, float threshold)
676 {
677 Generic6DofConstraint constraint = ((BulletConstraintXNA)pConstraint).constrain as Generic6DofConstraint;
678 constraint.SetBreakingImpulseThreshold(threshold);
679 return true;
680 }
681 //BulletSimAPI.SetAngularDamping(Prim.PhysBody.ptr, angularDamping);
682 public override void SetAngularDamping(BulletBody pBody, float angularDamping)
683 {
684 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
685 float lineardamping = body.GetLinearDamping();
686 body.SetDamping(lineardamping, angularDamping);
687
688 }
689
690 public override void UpdateInertiaTensor(BulletBody pBody)
691 {
692 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
693 body.UpdateInertiaTensor();
694 }
695
696 public override void RecalculateCompoundShapeLocalAabb(BulletShape pCompoundShape)
697 {
698 CompoundShape shape = ((BulletShapeXNA)pCompoundShape).shape as CompoundShape;
699 shape.RecalculateLocalAabb();
700 }
701
702 //BulletSimAPI.GetCollisionFlags(PhysBody.ptr)
703 public override CollisionFlags GetCollisionFlags(BulletBody pBody)
704 {
705 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
706 uint flags = (uint)body.GetCollisionFlags();
707 return (CollisionFlags) flags;
708 }
709
710 public override void SetDamping(BulletBody pBody, float pLinear, float pAngular)
711 {
712 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
713 body.SetDamping(pLinear, pAngular);
714 }
715 //PhysBody.ptr, PhysicsScene.Params.deactivationTime);
716 public override void SetDeactivationTime(BulletBody pBody, float pDeactivationTime)
717 {
718 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
719 body.SetDeactivationTime(pDeactivationTime);
720 }
721 //SetSleepingThresholds(PhysBody.ptr, PhysicsScene.Params.linearSleepingThreshold, PhysicsScene.Params.angularSleepingThreshold);
722 public override void SetSleepingThresholds(BulletBody pBody, float plinearSleepingThreshold, float pangularSleepingThreshold)
723 {
724 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
725 body.SetSleepingThresholds(plinearSleepingThreshold, pangularSleepingThreshold);
726 }
727
728 public override CollisionObjectTypes GetBodyType(BulletBody pBody)
729 {
730 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
731 return (CollisionObjectTypes)(int) body.GetInternalType();
732 }
733
734 public override void ApplyGravity(BulletBody obj) { /* TODO */ }
735
736 public override Vector3 GetGravity(BulletBody obj) { /* TODO */ return Vector3.Zero; }
737
738 public override void SetLinearDamping(BulletBody obj, float lin_damping) { /* TODO */ }
739
740 public override float GetLinearDamping(BulletBody obj) { /* TODO */ return 0f; }
741
742 public override float GetAngularDamping(BulletBody obj) { /* TODO */ return 0f; }
743
744 public override float GetLinearSleepingThreshold(BulletBody obj) { /* TODO */ return 0f; }
745
746 public override void ApplyDamping(BulletBody obj, float timeStep) { /* TODO */ }
747
748 public override Vector3 GetLinearFactor(BulletBody obj) { /* TODO */ return Vector3.Zero; }
749
750 public override void SetLinearFactor(BulletBody obj, Vector3 factor) { /* TODO */ }
751
752 public override void SetCenterOfMassByPosRot(BulletBody obj, Vector3 pos, Quaternion rot) { /* TODO */ }
753
754 //BulletSimAPI.ApplyCentralForce(PhysBody.ptr, fSum);
755 public override void ApplyCentralForce(BulletBody pBody, Vector3 pfSum)
756 {
757 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
758 IndexedVector3 fSum = new IndexedVector3(pfSum.X, pfSum.Y, pfSum.Z);
759 body.ApplyCentralForce(ref fSum);
760 }
761 public override void ApplyCentralImpulse(BulletBody pBody, Vector3 pfSum)
762 {
763 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
764 IndexedVector3 fSum = new IndexedVector3(pfSum.X, pfSum.Y, pfSum.Z);
765 body.ApplyCentralImpulse(ref fSum);
766 }
767 public override void ApplyTorque(BulletBody pBody, Vector3 pfSum)
768 {
769 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
770 IndexedVector3 fSum = new IndexedVector3(pfSum.X, pfSum.Y, pfSum.Z);
771 body.ApplyTorque(ref fSum);
772 }
773 public override void ApplyTorqueImpulse(BulletBody pBody, Vector3 pfSum)
774 {
775 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
776 IndexedVector3 fSum = new IndexedVector3(pfSum.X, pfSum.Y, pfSum.Z);
777 body.ApplyTorqueImpulse(ref fSum);
778 }
779
780 public override void DestroyObject(BulletWorld p, BulletBody p_2)
781 {
782 //TODO:
783 }
784
785 public override void Shutdown(BulletWorld pWorld)
786 {
787 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world;
788 world.Cleanup();
789 }
790
791 public override BulletShape DuplicateCollisionShape(BulletWorld sim, BulletShape srcShape, uint id)
792 {
793 return null;
794 }
795
796 public override bool DeleteCollisionShape(BulletWorld p, BulletShape p_2)
797 {
798 //TODO:
799 return false;
800 }
801 //(sim.ptr, shape.ptr, prim.LocalID, prim.RawPosition, prim.RawOrientation);
802
803 public override BulletBody CreateBodyFromShape(BulletWorld pWorld, BulletShape pShape, uint pLocalID, Vector3 pRawPosition, Quaternion pRawOrientation)
804 {
805 CollisionWorld world = ((BulletWorldXNA)pWorld).world;
806 IndexedMatrix mat =
807 IndexedMatrix.CreateFromQuaternion(new IndexedQuaternion(pRawOrientation.X, pRawOrientation.Y,
808 pRawOrientation.Z, pRawOrientation.W));
809 mat._origin = new IndexedVector3(pRawPosition.X, pRawPosition.Y, pRawPosition.Z);
810 CollisionShape shape = ((BulletShapeXNA)pShape).shape;
811 //UpdateSingleAabb(world, shape);
812 // TODO: Feed Update array into null
813 RigidBody body = new RigidBody(0,new SimMotionState(world,pLocalID,mat,null),shape,IndexedVector3.Zero);
814
815 body.SetUserPointer(pLocalID);
816 return new BulletBodyXNA(pLocalID, body);
817 }
818
819
820 public override BulletBody CreateBodyWithDefaultMotionState( BulletShape pShape, uint pLocalID, Vector3 pRawPosition, Quaternion pRawOrientation)
821 {
822
823 IndexedMatrix mat =
824 IndexedMatrix.CreateFromQuaternion(new IndexedQuaternion(pRawOrientation.X, pRawOrientation.Y,
825 pRawOrientation.Z, pRawOrientation.W));
826 mat._origin = new IndexedVector3(pRawPosition.X, pRawPosition.Y, pRawPosition.Z);
827
828 CollisionShape shape = ((BulletShapeXNA)pShape).shape;
829
830 // TODO: Feed Update array into null
831 RigidBody body = new RigidBody(0, new DefaultMotionState( mat, IndexedMatrix.Identity), shape, IndexedVector3.Zero);
832 body.SetWorldTransform(mat);
833 body.SetUserPointer(pLocalID);
834 return new BulletBodyXNA(pLocalID, body);
835 }
836 //(m_mapInfo.terrainBody.ptr, CollisionFlags.CF_STATIC_OBJECT);
837 public override CollisionFlags SetCollisionFlags(BulletBody pBody, CollisionFlags collisionFlags)
838 {
839 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
840 body.SetCollisionFlags((BulletXNA.BulletCollision.CollisionFlags) (uint) collisionFlags);
841 return (CollisionFlags)body.GetCollisionFlags();
842 }
843
844 public override Vector3 GetAnisotripicFriction(BulletConstraint pconstrain) { /* TODO */ return Vector3.Zero; }
845 public override Vector3 SetAnisotripicFriction(BulletConstraint pconstrain, Vector3 frict) { /* TODO */ return Vector3.Zero; }
846 public override bool HasAnisotripicFriction(BulletConstraint pconstrain) { /* TODO */ return false; }
847 public override float GetContactProcessingThreshold(BulletBody pBody) { /* TODO */ return 0f; }
848 public override bool IsStaticObject(BulletBody pBody) { /* TODO */ return false; }
849 public override bool IsKinematicObject(BulletBody pBody) { /* TODO */ return false; }
850 public override bool IsStaticOrKinematicObject(BulletBody pBody) { /* TODO */ return false; }
851 public override bool HasContactResponse(BulletBody pBody) { /* TODO */ return false; }
852 public override int GetActivationState(BulletBody pBody) { /* TODO */ return 0; }
853 public override void SetActivationState(BulletBody pBody, int state) { /* TODO */ }
854 public override float GetDeactivationTime(BulletBody pBody) { /* TODO */ return 0f; }
855 public override bool IsActive(BulletBody pBody) { /* TODO */ return false; }
856 public override float GetRestitution(BulletBody pBody) { /* TODO */ return 0f; }
857 public override float GetFriction(BulletBody pBody) { /* TODO */ return 0f; }
858 public override void SetInterpolationVelocity(BulletBody pBody, Vector3 linearVel, Vector3 angularVel) { /* TODO */ }
859 public override float GetHitFraction(BulletBody pBody) { /* TODO */ return 0f; }
860
861 //(m_mapInfo.terrainBody.ptr, PhysicsScene.Params.terrainHitFraction);
862 public override void SetHitFraction(BulletBody pBody, float pHitFraction)
863 {
864 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
865 body.SetHitFraction(pHitFraction);
866 }
867 //BuildCapsuleShape(physicsScene.World.ptr, 1f, 1f, prim.Scale);
868 public override BulletShape BuildCapsuleShape(BulletWorld pWorld, float pRadius, float pHeight, Vector3 pScale)
869 {
870 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world;
871 IndexedVector3 scale = new IndexedVector3(pScale.X, pScale.Y, pScale.Z);
872 CapsuleShapeZ capsuleShapeZ = new CapsuleShapeZ(pRadius, pHeight);
873 capsuleShapeZ.SetMargin(world.WorldSettings.Params.collisionMargin);
874 capsuleShapeZ.SetLocalScaling(ref scale);
875
876 return new BulletShapeXNA(capsuleShapeZ, BSPhysicsShapeType.SHAPE_CAPSULE); ;
877 }
878
879 public override BulletWorld Initialize(Vector3 maxPosition, ConfigurationParameters parms,
880 int maxCollisions, ref CollisionDesc[] collisionArray,
881 int maxUpdates, ref EntityProperties[] updateArray
882 )
883 {
884 /* TODO */
885 return new BulletWorldXNA(1, null, null);
886 }
887
888 private static object Initialize2(Vector3 worldExtent,
889 ConfigurationParameters[] o,
890 int mMaxCollisionsPerFrame, ref List<BulletXNA.CollisionDesc> collisionArray,
891 int mMaxUpdatesPerFrame, ref List<BulletXNA.EntityProperties> updateArray,
892 object mDebugLogCallbackHandle)
893 {
894 CollisionWorld.WorldData.ParamData p = new CollisionWorld.WorldData.ParamData();
895
896 p.angularDamping = o[0].XangularDamping;
897 p.defaultFriction = o[0].defaultFriction;
898 p.defaultFriction = o[0].defaultFriction;
899 p.defaultDensity = o[0].defaultDensity;
900 p.defaultRestitution = o[0].defaultRestitution;
901 p.collisionMargin = o[0].collisionMargin;
902 p.gravity = o[0].gravity;
903
904 p.linearDamping = o[0].XlinearDamping;
905 p.angularDamping = o[0].XangularDamping;
906 p.deactivationTime = o[0].XdeactivationTime;
907 p.linearSleepingThreshold = o[0].XlinearSleepingThreshold;
908 p.angularSleepingThreshold = o[0].XangularSleepingThreshold;
909 p.ccdMotionThreshold = o[0].XccdMotionThreshold;
910 p.ccdSweptSphereRadius = o[0].XccdSweptSphereRadius;
911 p.contactProcessingThreshold = o[0].XcontactProcessingThreshold;
912
913 p.terrainImplementation = o[0].XterrainImplementation;
914 p.terrainFriction = o[0].XterrainFriction;
915
916 p.terrainHitFraction = o[0].XterrainHitFraction;
917 p.terrainRestitution = o[0].XterrainRestitution;
918 p.terrainCollisionMargin = o[0].XterrainCollisionMargin;
919
920 p.avatarFriction = o[0].XavatarFriction;
921 p.avatarStandingFriction = o[0].XavatarStandingFriction;
922 p.avatarDensity = o[0].XavatarDensity;
923 p.avatarRestitution = o[0].XavatarRestitution;
924 p.avatarCapsuleWidth = o[0].XavatarCapsuleWidth;
925 p.avatarCapsuleDepth = o[0].XavatarCapsuleDepth;
926 p.avatarCapsuleHeight = o[0].XavatarCapsuleHeight;
927 p.avatarContactProcessingThreshold = o[0].XavatarContactProcessingThreshold;
928
929 p.vehicleAngularDamping = o[0].XvehicleAngularDamping;
930
931 p.maxPersistantManifoldPoolSize = o[0].maxPersistantManifoldPoolSize;
932 p.maxCollisionAlgorithmPoolSize = o[0].maxCollisionAlgorithmPoolSize;
933 p.shouldDisableContactPoolDynamicAllocation = o[0].shouldDisableContactPoolDynamicAllocation;
934 p.shouldForceUpdateAllAabbs = o[0].shouldForceUpdateAllAabbs;
935 p.shouldRandomizeSolverOrder = o[0].shouldRandomizeSolverOrder;
936 p.shouldSplitSimulationIslands = o[0].shouldSplitSimulationIslands;
937 p.shouldEnableFrictionCaching = o[0].shouldEnableFrictionCaching;
938 p.numberOfSolverIterations = o[0].numberOfSolverIterations;
939
940 p.linksetImplementation = o[0].XlinksetImplementation;
941 p.linkConstraintUseFrameOffset = o[0].XlinkConstraintUseFrameOffset;
942 p.linkConstraintEnableTransMotor = o[0].XlinkConstraintEnableTransMotor;
943 p.linkConstraintTransMotorMaxVel = o[0].XlinkConstraintTransMotorMaxVel;
944 p.linkConstraintTransMotorMaxForce = o[0].XlinkConstraintTransMotorMaxForce;
945 p.linkConstraintERP = o[0].XlinkConstraintERP;
946 p.linkConstraintCFM = o[0].XlinkConstraintCFM;
947 p.linkConstraintSolverIterations = o[0].XlinkConstraintSolverIterations;
948 p.physicsLoggingFrames = o[0].XphysicsLoggingFrames;
949 DefaultCollisionConstructionInfo ccci = new DefaultCollisionConstructionInfo();
950
951 DefaultCollisionConfiguration cci = new DefaultCollisionConfiguration();
952 CollisionDispatcher m_dispatcher = new CollisionDispatcher(cci);
953
954
955 if (p.maxPersistantManifoldPoolSize > 0)
956 cci.m_persistentManifoldPoolSize = (int)p.maxPersistantManifoldPoolSize;
957 if (p.shouldDisableContactPoolDynamicAllocation !=0)
958 m_dispatcher.SetDispatcherFlags(DispatcherFlags.CD_DISABLE_CONTACTPOOL_DYNAMIC_ALLOCATION);
959 //if (p.maxCollisionAlgorithmPoolSize >0 )
960
961 DbvtBroadphase m_broadphase = new DbvtBroadphase();
962 //IndexedVector3 aabbMin = new IndexedVector3(0, 0, 0);
963 //IndexedVector3 aabbMax = new IndexedVector3(256, 256, 256);
964
965 //AxisSweep3Internal m_broadphase2 = new AxisSweep3Internal(ref aabbMin, ref aabbMax, Convert.ToInt32(0xfffe), 0xffff, ushort.MaxValue/2, null, true);
966 m_broadphase.GetOverlappingPairCache().SetInternalGhostPairCallback(new GhostPairCallback());
967
968 SequentialImpulseConstraintSolver m_solver = new SequentialImpulseConstraintSolver();
969
970 DiscreteDynamicsWorld world = new DiscreteDynamicsWorld(m_dispatcher, m_broadphase, m_solver, cci);
971 world.UpdatedObjects = updateArray;
972 world.UpdatedCollisions = collisionArray;
973 world.WorldSettings.Params = p;
974 world.SetForceUpdateAllAabbs(p.shouldForceUpdateAllAabbs != 0);
975 world.GetSolverInfo().m_solverMode = SolverMode.SOLVER_USE_WARMSTARTING | SolverMode.SOLVER_SIMD;
976 if (p.shouldRandomizeSolverOrder != 0)
977 world.GetSolverInfo().m_solverMode |= SolverMode.SOLVER_RANDMIZE_ORDER;
978
979 world.GetSimulationIslandManager().SetSplitIslands(p.shouldSplitSimulationIslands != 0);
980 //world.GetDispatchInfo().m_enableSatConvex Not implemented in C# port
981
982 if (p.shouldEnableFrictionCaching != 0)
983 world.GetSolverInfo().m_solverMode |= SolverMode.SOLVER_ENABLE_FRICTION_DIRECTION_CACHING;
984
985 if (p.numberOfSolverIterations > 0)
986 world.GetSolverInfo().m_numIterations = (int) p.numberOfSolverIterations;
987
988
989 world.GetSolverInfo().m_damping = world.WorldSettings.Params.linearDamping;
990 world.GetSolverInfo().m_restitution = world.WorldSettings.Params.defaultRestitution;
991 world.GetSolverInfo().m_globalCfm = 0.0f;
992 world.GetSolverInfo().m_tau = 0.6f;
993 world.GetSolverInfo().m_friction = 0.3f;
994 world.GetSolverInfo().m_maxErrorReduction = 20f;
995 world.GetSolverInfo().m_numIterations = 10;
996 world.GetSolverInfo().m_erp = 0.2f;
997 world.GetSolverInfo().m_erp2 = 0.1f;
998 world.GetSolverInfo().m_sor = 1.0f;
999 world.GetSolverInfo().m_splitImpulse = false;
1000 world.GetSolverInfo().m_splitImpulsePenetrationThreshold = -0.02f;
1001 world.GetSolverInfo().m_linearSlop = 0.0f;
1002 world.GetSolverInfo().m_warmstartingFactor = 0.85f;
1003 world.GetSolverInfo().m_restingContactRestitutionThreshold = 2;
1004 world.SetForceUpdateAllAabbs(true);
1005
1006
1007 world.SetGravity(new IndexedVector3(0,0,p.gravity));
1008
1009 return world;
1010 }
1011 //m_constraint.ptr, ConstraintParams.BT_CONSTRAINT_STOP_CFM, cfm, ConstraintParamAxis.AXIS_ALL
1012 public override bool SetConstraintParam(BulletConstraint pConstraint, ConstraintParams paramIndex, float paramvalue, ConstraintParamAxis axis)
1013 {
1014 Generic6DofConstraint constrain = ((BulletConstraintXNA)pConstraint).constrain as Generic6DofConstraint;
1015 if (axis == ConstraintParamAxis.AXIS_LINEAR_ALL || axis == ConstraintParamAxis.AXIS_ALL)
1016 {
1017 constrain.SetParam((BulletXNA.BulletDynamics.ConstraintParams) (int) paramIndex, paramvalue, 0);
1018 constrain.SetParam((BulletXNA.BulletDynamics.ConstraintParams) (int) paramIndex, paramvalue, 1);
1019 constrain.SetParam((BulletXNA.BulletDynamics.ConstraintParams) (int) paramIndex, paramvalue, 2);
1020 }
1021 if (axis == ConstraintParamAxis.AXIS_ANGULAR_ALL || axis == ConstraintParamAxis.AXIS_ALL)
1022 {
1023 constrain.SetParam((BulletXNA.BulletDynamics.ConstraintParams)(int)paramIndex, paramvalue, 3);
1024 constrain.SetParam((BulletXNA.BulletDynamics.ConstraintParams)(int)paramIndex, paramvalue, 4);
1025 constrain.SetParam((BulletXNA.BulletDynamics.ConstraintParams)(int)paramIndex, paramvalue, 5);
1026 }
1027 if (axis == ConstraintParamAxis.AXIS_LINEAR_ALL)
1028 {
1029 constrain.SetParam((BulletXNA.BulletDynamics.ConstraintParams)(int)paramIndex, paramvalue, (int)axis);
1030 }
1031 return true;
1032 }
1033
1034 public override bool PushUpdate(BulletBody pCollisionObject)
1035 {
1036 bool ret = false;
1037 RigidBody rb = ((BulletBodyXNA)pCollisionObject).rigidBody;
1038 if (rb != null)
1039 {
1040 SimMotionState sms = rb.GetMotionState() as SimMotionState;
1041 if (sms != null)
1042 {
1043 IndexedMatrix wt = IndexedMatrix.Identity;
1044 sms.GetWorldTransform(out wt);
1045 sms.SetWorldTransform(ref wt, true);
1046 ret = true;
1047 }
1048 }
1049 return ret;
1050
1051 }
1052
1053 public override float GetAngularMotionDisc(BulletShape pShape)
1054 {
1055 CollisionShape shape = ((BulletShapeXNA)pShape).shape;
1056 return shape.GetAngularMotionDisc();
1057 }
1058 public override float GetContactBreakingThreshold(BulletShape pShape, float defaultFactor)
1059 {
1060 CollisionShape shape = ((BulletShapeXNA)pShape).shape;
1061 return shape.GetContactBreakingThreshold(defaultFactor);
1062 }
1063 public override bool IsCompound(BulletShape pShape)
1064 {
1065 CollisionShape shape = ((BulletShapeXNA)pShape).shape;
1066 return shape.IsCompound();
1067 }
1068 public override bool IsSoftBody(BulletShape pShape)
1069 {
1070 CollisionShape shape = ((BulletShapeXNA)pShape).shape;
1071 return shape.IsSoftBody();
1072 }
1073 public override bool IsPolyhedral(BulletShape pShape)
1074 {
1075 CollisionShape shape = ((BulletShapeXNA)pShape).shape;
1076 return shape.IsPolyhedral();
1077 }
1078 public override bool IsConvex2d(BulletShape pShape)
1079 {
1080 CollisionShape shape = ((BulletShapeXNA)pShape).shape;
1081 return shape.IsConvex2d();
1082 }
1083 public override bool IsConvex(BulletShape pShape)
1084 {
1085 CollisionShape shape = ((BulletShapeXNA)pShape).shape;
1086 return shape.IsConvex();
1087 }
1088 public override bool IsNonMoving(BulletShape pShape)
1089 {
1090 CollisionShape shape = ((BulletShapeXNA)pShape).shape;
1091 return shape.IsNonMoving();
1092 }
1093 public override bool IsConcave(BulletShape pShape)
1094 {
1095 CollisionShape shape = ((BulletShapeXNA)pShape).shape;
1096 return shape.IsConcave();
1097 }
1098 public override bool IsInfinite(BulletShape pShape)
1099 {
1100 CollisionShape shape = ((BulletShapeXNA)pShape).shape;
1101 return shape.IsInfinite();
1102 }
1103 public override bool IsNativeShape(BulletShape pShape)
1104 {
1105 CollisionShape shape = ((BulletShapeXNA)pShape).shape;
1106 bool ret;
1107 switch (shape.GetShapeType())
1108 {
1109 case BroadphaseNativeTypes.BOX_SHAPE_PROXYTYPE:
1110 case BroadphaseNativeTypes.CONE_SHAPE_PROXYTYPE:
1111 case BroadphaseNativeTypes.SPHERE_SHAPE_PROXYTYPE:
1112 case BroadphaseNativeTypes.CYLINDER_SHAPE_PROXYTYPE:
1113 ret = true;
1114 break;
1115 default:
1116 ret = false;
1117 break;
1118 }
1119 return ret;
1120 }
1121
1122 public override void SetShapeCollisionMargin(BulletShape shape, float margin) { /* TODO */ }
1123
1124 //sim.ptr, shape.ptr,prim.LocalID, prim.RawPosition, prim.RawOrientation
1125 public override BulletBody CreateGhostFromShape(BulletWorld pWorld, BulletShape pShape, uint pLocalID, Vector3 pRawPosition, Quaternion pRawOrientation)
1126 {
1127 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world;
1128 IndexedMatrix bodyTransform = new IndexedMatrix();
1129 bodyTransform._origin = new IndexedVector3(pRawPosition.X, pRawPosition.Y, pRawPosition.Z);
1130 bodyTransform.SetRotation(new IndexedQuaternion(pRawOrientation.X,pRawOrientation.Y,pRawOrientation.Z,pRawOrientation.W));
1131 GhostObject gObj = new PairCachingGhostObject();
1132 gObj.SetWorldTransform(bodyTransform);
1133 CollisionShape shape = ((BulletShapeXNA)pShape).shape;
1134 gObj.SetCollisionShape(shape);
1135 gObj.SetUserPointer(pLocalID);
1136 // TODO: Add to Special CollisionObjects!
1137 return new BulletBodyXNA(pLocalID, gObj);
1138 }
1139
1140 public override void SetCollisionShape(BulletWorld pWorld, BulletBody pObj, BulletShape pShape)
1141 {
1142 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world;
1143 CollisionObject obj = ((BulletBodyXNA)pObj).body;
1144 CollisionShape shape = ((BulletShapeXNA)pShape).shape;
1145 obj.SetCollisionShape(shape);
1146
1147 }
1148 public override BulletShape GetCollisionShape(BulletBody obj) { /* TODO */ return null; }
1149
1150 //(PhysicsScene.World.ptr, nativeShapeData)
1151 public override BulletShape BuildNativeShape(BulletWorld pWorld, ShapeData pShapeData)
1152 {
1153 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world;
1154 CollisionShape shape = null;
1155 switch (pShapeData.Type)
1156 {
1157 case BSPhysicsShapeType.SHAPE_BOX:
1158 shape = new BoxShape(new IndexedVector3(0.5f,0.5f,0.5f));
1159 break;
1160 case BSPhysicsShapeType.SHAPE_CONE:
1161 shape = new ConeShapeZ(0.5f, 1.0f);
1162 break;
1163 case BSPhysicsShapeType.SHAPE_CYLINDER:
1164 shape = new CylinderShapeZ(new IndexedVector3(0.5f, 0.5f, 0.5f));
1165 break;
1166 case BSPhysicsShapeType.SHAPE_SPHERE:
1167 shape = new SphereShape(0.5f);
1168 break;
1169
1170 }
1171 if (shape != null)
1172 {
1173 IndexedVector3 scaling = new IndexedVector3(pShapeData.Scale.X, pShapeData.Scale.Y, pShapeData.Scale.Z);
1174 shape.SetMargin(world.WorldSettings.Params.collisionMargin);
1175 shape.SetLocalScaling(ref scaling);
1176
1177 }
1178 return new BulletShapeXNA(shape, pShapeData.Type);
1179 }
1180 //PhysicsScene.World.ptr, false
1181 public override BulletShape CreateCompoundShape(BulletWorld pWorld, bool enableDynamicAabbTree)
1182 {
1183 return new BulletShapeXNA(new CompoundShape(enableDynamicAabbTree), BSPhysicsShapeType.SHAPE_COMPOUND);
1184 }
1185
1186 public override int GetNumberOfCompoundChildren(BulletShape pCompoundShape)
1187 {
1188 CompoundShape compoundshape = ((BulletShapeXNA)pCompoundShape).shape as CompoundShape;
1189 return compoundshape.GetNumChildShapes();
1190 }
1191 //LinksetRoot.PhysShape.ptr, newShape.ptr, displacementPos, displacementRot
1192 public override void AddChildShapeToCompoundShape(BulletShape pCShape, BulletShape paddShape, Vector3 displacementPos, Quaternion displacementRot)
1193 {
1194 IndexedMatrix relativeTransform = new IndexedMatrix();
1195 CompoundShape compoundshape = ((BulletShapeXNA)pCShape).shape as CompoundShape;
1196 CollisionShape addshape = ((BulletShapeXNA)paddShape).shape;
1197
1198 relativeTransform._origin = new IndexedVector3(displacementPos.X, displacementPos.Y, displacementPos.Z);
1199 relativeTransform.SetRotation(new IndexedQuaternion(displacementRot.X,displacementRot.Y,displacementRot.Z,displacementRot.W));
1200 compoundshape.AddChildShape(ref relativeTransform, addshape);
1201
1202 }
1203
1204 public override BulletShape RemoveChildShapeFromCompoundShapeIndex(BulletShape pCShape, int pii)
1205 {
1206 CompoundShape compoundshape = ((BulletShapeXNA)pCShape).shape as CompoundShape;
1207 CollisionShape ret = null;
1208 ret = compoundshape.GetChildShape(pii);
1209 compoundshape.RemoveChildShapeByIndex(pii);
1210 return new BulletShapeXNA(ret, BSPhysicsShapeType.SHAPE_UNKNOWN);
1211 }
1212
1213 public override BulletShape GetChildShapeFromCompoundShapeIndex(BulletShape cShape, int indx) { /* TODO */ return null; }
1214 public override void RemoveChildShapeFromCompoundShape(BulletShape cShape, BulletShape removeShape) { /* TODO */ }
1215 public override void UpdateChildTransform(BulletShape pShape, int childIndex, Vector3 pos, Quaternion rot, bool shouldRecalculateLocalAabb) { /* TODO */ }
1216
1217 public override BulletShape CreateGroundPlaneShape(uint pLocalId, float pheight, float pcollisionMargin)
1218 {
1219 StaticPlaneShape m_planeshape = new StaticPlaneShape(new IndexedVector3(0,0,1),(int)pheight );
1220 m_planeshape.SetMargin(pcollisionMargin);
1221 m_planeshape.SetUserPointer(pLocalId);
1222 return new BulletShapeXNA(m_planeshape, BSPhysicsShapeType.SHAPE_GROUNDPLANE);
1223 }
1224
1225 public override BulletConstraint CreateHingeConstraint(BulletWorld pWorld, BulletBody pBody1, BulletBody ppBody2, Vector3 ppivotInA, Vector3 ppivotInB, Vector3 paxisInA, Vector3 paxisInB, bool puseLinearReferenceFrameA, bool pdisableCollisionsBetweenLinkedBodies)
1226 {
1227 HingeConstraint constrain = null;
1228 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world;
1229 RigidBody rb1 = ((BulletBodyXNA)pBody1).rigidBody;
1230 RigidBody rb2 = ((BulletBodyXNA)ppBody2).rigidBody;
1231 if (rb1 != null && rb2 != null)
1232 {
1233 IndexedVector3 pivotInA = new IndexedVector3(ppivotInA.X, ppivotInA.Y, ppivotInA.Z);
1234 IndexedVector3 pivotInB = new IndexedVector3(ppivotInB.X, ppivotInB.Y, ppivotInB.Z);
1235 IndexedVector3 axisInA = new IndexedVector3(paxisInA.X, paxisInA.Y, paxisInA.Z);
1236 IndexedVector3 axisInB = new IndexedVector3(paxisInB.X, paxisInB.Y, paxisInB.Z);
1237 world.AddConstraint(constrain, pdisableCollisionsBetweenLinkedBodies);
1238 }
1239 return new BulletConstraintXNA(constrain);
1240 }
1241
1242 public override BulletShape CreateHullShape(BulletWorld pWorld, int pHullCount, float[] pConvHulls)
1243 {
1244 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world;
1245 CompoundShape compoundshape = new CompoundShape(false);
1246
1247 compoundshape.SetMargin(world.WorldSettings.Params.collisionMargin);
1248 int ii = 1;
1249
1250 for (int i = 0; i < pHullCount; i++)
1251 {
1252 int vertexCount = (int) pConvHulls[ii];
1253
1254 IndexedVector3 centroid = new IndexedVector3(pConvHulls[ii + 1], pConvHulls[ii + 2], pConvHulls[ii + 3]);
1255 IndexedMatrix childTrans = IndexedMatrix.Identity;
1256 childTrans._origin = centroid;
1257
1258 List<IndexedVector3> virts = new List<IndexedVector3>();
1259 int ender = ((ii + 4) + (vertexCount*3));
1260 for (int iii = ii + 4; iii < ender; iii+=3)
1261 {
1262
1263 virts.Add(new IndexedVector3(pConvHulls[iii], pConvHulls[iii + 1], pConvHulls[iii +2]));
1264 }
1265 ConvexHullShape convexShape = new ConvexHullShape(virts, vertexCount);
1266 convexShape.SetMargin(world.WorldSettings.Params.collisionMargin);
1267 compoundshape.AddChildShape(ref childTrans, convexShape);
1268 ii += (vertexCount*3 + 4);
1269 }
1270
1271 return new BulletShapeXNA(compoundshape, BSPhysicsShapeType.SHAPE_HULL);
1272 }
1273
1274 public override BulletShape BuildHullShapeFromMesh(BulletWorld world, BulletShape meshShape) { /* TODO */ return null; }
1275
1276 public override BulletShape CreateMeshShape(BulletWorld pWorld, int pIndicesCount, int[] indices, int pVerticesCount, float[] verticesAsFloats)
1277 {
1278 //DumpRaw(indices,verticesAsFloats,pIndicesCount,pVerticesCount);
1279
1280 for (int iter = 0; iter < pVerticesCount; iter++)
1281 {
1282 if (verticesAsFloats[iter] > 0 && verticesAsFloats[iter] < 0.0001) verticesAsFloats[iter] = 0;
1283 if (verticesAsFloats[iter] < 0 && verticesAsFloats[iter] > -0.0001) verticesAsFloats[iter] = 0;
1284 }
1285
1286 ObjectArray<int> indicesarr = new ObjectArray<int>(indices);
1287 ObjectArray<float> vertices = new ObjectArray<float>(verticesAsFloats);
1288 DumpRaw(indicesarr,vertices,pIndicesCount,pVerticesCount);
1289 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world;
1290 IndexedMesh mesh = new IndexedMesh();
1291 mesh.m_indexType = PHY_ScalarType.PHY_INTEGER;
1292 mesh.m_numTriangles = pIndicesCount/3;
1293 mesh.m_numVertices = pVerticesCount;
1294 mesh.m_triangleIndexBase = indicesarr;
1295 mesh.m_vertexBase = vertices;
1296 mesh.m_vertexStride = 3;
1297 mesh.m_vertexType = PHY_ScalarType.PHY_FLOAT;
1298 mesh.m_triangleIndexStride = 3;
1299
1300 TriangleIndexVertexArray tribuilder = new TriangleIndexVertexArray();
1301 tribuilder.AddIndexedMesh(mesh, PHY_ScalarType.PHY_INTEGER);
1302 BvhTriangleMeshShape meshShape = new BvhTriangleMeshShape(tribuilder, true,true);
1303 meshShape.SetMargin(world.WorldSettings.Params.collisionMargin);
1304 // world.UpdateSingleAabb(meshShape);
1305 return new BulletShapeXNA(meshShape, BSPhysicsShapeType.SHAPE_MESH);
1306
1307 }
1308 public static void DumpRaw(ObjectArray<int>indices, ObjectArray<float> vertices, int pIndicesCount,int pVerticesCount )
1309 {
1310
1311 String fileName = "objTest3.raw";
1312 String completePath = System.IO.Path.Combine(Util.configDir(), fileName);
1313 StreamWriter sw = new StreamWriter(completePath);
1314 IndexedMesh mesh = new IndexedMesh();
1315
1316 mesh.m_indexType = PHY_ScalarType.PHY_INTEGER;
1317 mesh.m_numTriangles = pIndicesCount / 3;
1318 mesh.m_numVertices = pVerticesCount;
1319 mesh.m_triangleIndexBase = indices;
1320 mesh.m_vertexBase = vertices;
1321 mesh.m_vertexStride = 3;
1322 mesh.m_vertexType = PHY_ScalarType.PHY_FLOAT;
1323 mesh.m_triangleIndexStride = 3;
1324
1325 TriangleIndexVertexArray tribuilder = new TriangleIndexVertexArray();
1326 tribuilder.AddIndexedMesh(mesh, PHY_ScalarType.PHY_INTEGER);
1327
1328
1329
1330 for (int i = 0; i < pVerticesCount; i++)
1331 {
1332
1333 string s = vertices[indices[i * 3]].ToString("0.0000");
1334 s += " " + vertices[indices[i * 3 + 1]].ToString("0.0000");
1335 s += " " + vertices[indices[i * 3 + 2]].ToString("0.0000");
1336
1337 sw.Write(s + "\n");
1338 }
1339
1340 sw.Close();
1341 }
1342 public static void DumpRaw(int[] indices, float[] vertices, int pIndicesCount, int pVerticesCount)
1343 {
1344
1345 String fileName = "objTest6.raw";
1346 String completePath = System.IO.Path.Combine(Util.configDir(), fileName);
1347 StreamWriter sw = new StreamWriter(completePath);
1348 IndexedMesh mesh = new IndexedMesh();
1349
1350 mesh.m_indexType = PHY_ScalarType.PHY_INTEGER;
1351 mesh.m_numTriangles = pIndicesCount / 3;
1352 mesh.m_numVertices = pVerticesCount;
1353 mesh.m_triangleIndexBase = indices;
1354 mesh.m_vertexBase = vertices;
1355 mesh.m_vertexStride = 3;
1356 mesh.m_vertexType = PHY_ScalarType.PHY_FLOAT;
1357 mesh.m_triangleIndexStride = 3;
1358
1359 TriangleIndexVertexArray tribuilder = new TriangleIndexVertexArray();
1360 tribuilder.AddIndexedMesh(mesh, PHY_ScalarType.PHY_INTEGER);
1361
1362
1363 sw.WriteLine("Indices");
1364 sw.WriteLine(string.Format("int[] indices = new int[{0}];",pIndicesCount));
1365 for (int iter = 0; iter < indices.Length; iter++)
1366 {
1367 sw.WriteLine(string.Format("indices[{0}]={1};",iter,indices[iter]));
1368 }
1369 sw.WriteLine("VerticesFloats");
1370 sw.WriteLine(string.Format("float[] vertices = new float[{0}];", pVerticesCount));
1371 for (int iter = 0; iter < vertices.Length; iter++)
1372 {
1373 sw.WriteLine(string.Format("Vertices[{0}]={1};", iter, vertices[iter].ToString("0.0000")));
1374 }
1375
1376 // for (int i = 0; i < pVerticesCount; i++)
1377 // {
1378 //
1379 // string s = vertices[indices[i * 3]].ToString("0.0000");
1380 // s += " " + vertices[indices[i * 3 + 1]].ToString("0.0000");
1381 // s += " " + vertices[indices[i * 3 + 2]].ToString("0.0000");
1382 //
1383 // sw.Write(s + "\n");
1384 //}
1385
1386 sw.Close();
1387 }
1388
1389 public override BulletShape CreateTerrainShape(uint id, Vector3 size, float minHeight, float maxHeight, float[] heightMap,
1390 float scaleFactor, float collisionMargin)
1391 {
1392 const int upAxis = 2;
1393 HeightfieldTerrainShape terrainShape = new HeightfieldTerrainShape((int)size.X, (int)size.Y,
1394 heightMap, scaleFactor,
1395 minHeight, maxHeight, upAxis,
1396 false);
1397 terrainShape.SetMargin(collisionMargin + 0.5f);
1398 terrainShape.SetUseDiamondSubdivision(true);
1399 terrainShape.SetUserPointer(id);
1400 return new BulletShapeXNA(terrainShape, BSPhysicsShapeType.SHAPE_TERRAIN);
1401 }
1402
1403 public override bool TranslationalLimitMotor(BulletConstraint pConstraint, float ponOff, float targetVelocity, float maxMotorForce)
1404 {
1405 TypedConstraint tconstrain = ((BulletConstraintXNA)pConstraint).constrain;
1406 bool onOff = ponOff != 0;
1407 bool ret = false;
1408
1409 switch (tconstrain.GetConstraintType())
1410 {
1411 case TypedConstraintType.D6_CONSTRAINT_TYPE:
1412 Generic6DofConstraint constrain = tconstrain as Generic6DofConstraint;
1413 constrain.GetTranslationalLimitMotor().m_enableMotor[0] = onOff;
1414 constrain.GetTranslationalLimitMotor().m_targetVelocity[0] = targetVelocity;
1415 constrain.GetTranslationalLimitMotor().m_maxMotorForce[0] = maxMotorForce;
1416 ret = true;
1417 break;
1418 }
1419
1420
1421 return ret;
1422
1423 }
1424
1425 public override int PhysicsStep(BulletWorld world, float timeStep, int maxSubSteps, float fixedTimeStep,
1426 out int updatedEntityCount, out int collidersCount)
1427 {
1428 /* TODO */
1429 updatedEntityCount = 0;
1430 collidersCount = 0;
1431 return 1;
1432 }
1433
1434 private int PhysicsStep2(BulletWorld pWorld, float timeStep, int m_maxSubSteps, float m_fixedTimeStep,
1435 out int updatedEntityCount, out List<BulletXNA.EntityProperties> updatedEntities,
1436 out int collidersCount, out List<BulletXNA.CollisionDesc>colliders)
1437 {
1438 int epic = PhysicsStepint(pWorld, timeStep, m_maxSubSteps, m_fixedTimeStep, out updatedEntityCount, out updatedEntities,
1439 out collidersCount, out colliders);
1440 return epic;
1441 }
1442
1443 private static int PhysicsStepint(BulletWorld pWorld,float timeStep, int m_maxSubSteps, float m_fixedTimeStep, out int updatedEntityCount, out List<BulletXNA.EntityProperties> updatedEntities, out int collidersCount, out List<BulletXNA.CollisionDesc> colliders)
1444 {
1445 int numSimSteps = 0;
1446
1447
1448 //if (updatedEntities is null)
1449 // updatedEntities = new List<BulletXNA.EntityProperties>();
1450
1451 //if (colliders is null)
1452 // colliders = new List<BulletXNA.CollisionDesc>();
1453
1454
1455 if (pWorld is BulletWorldXNA)
1456 {
1457 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world;
1458
1459 numSimSteps = world.StepSimulation(timeStep, m_maxSubSteps, m_fixedTimeStep);
1460 int updates = 0;
1461
1462 updatedEntityCount = world.UpdatedObjects.Count;
1463 updatedEntities = new List<BulletXNA.EntityProperties>(world.UpdatedObjects);
1464 updatedEntityCount = updatedEntities.Count;
1465 world.UpdatedObjects.Clear();
1466
1467
1468 collidersCount = world.UpdatedCollisions.Count;
1469 colliders = new List<BulletXNA.CollisionDesc>(world.UpdatedCollisions);
1470
1471 world.UpdatedCollisions.Clear();
1472 m_collisionsThisFrame = 0;
1473 int numManifolds = world.GetDispatcher().GetNumManifolds();
1474 for (int j = 0; j < numManifolds; j++)
1475 {
1476 PersistentManifold contactManifold = world.GetDispatcher().GetManifoldByIndexInternal(j);
1477 int numContacts = contactManifold.GetNumContacts();
1478 if (numContacts == 0)
1479 continue;
1480
1481 CollisionObject objA = contactManifold.GetBody0() as CollisionObject;
1482 CollisionObject objB = contactManifold.GetBody1() as CollisionObject;
1483
1484 ManifoldPoint manifoldPoint = contactManifold.GetContactPoint(0);
1485 IndexedVector3 contactPoint = manifoldPoint.GetPositionWorldOnB();
1486 IndexedVector3 contactNormal = -manifoldPoint.m_normalWorldOnB; // make relative to A
1487
1488 RecordCollision(world, objA, objB, contactPoint, contactNormal);
1489 m_collisionsThisFrame ++;
1490 if (m_collisionsThisFrame >= 9999999)
1491 break;
1492
1493
1494 }
1495
1496
1497 }
1498 else
1499 {
1500 //if (updatedEntities is null)
1501 updatedEntities = new List<BulletXNA.EntityProperties>();
1502 updatedEntityCount = 0;
1503 //if (colliders is null)
1504 colliders = new List<BulletXNA.CollisionDesc>();
1505 collidersCount = 0;
1506 }
1507 return numSimSteps;
1508 }
1509
1510 private static void RecordCollision(CollisionWorld world, CollisionObject objA, CollisionObject objB, IndexedVector3 contact, IndexedVector3 norm)
1511 {
1512
1513 IndexedVector3 contactNormal = norm;
1514 if ((objA.GetCollisionFlags() & BulletXNA.BulletCollision.CollisionFlags.BS_WANTS_COLLISIONS) == 0 &&
1515 (objB.GetCollisionFlags() & BulletXNA.BulletCollision.CollisionFlags.BS_WANTS_COLLISIONS) == 0)
1516 {
1517 return;
1518 }
1519 uint idA = (uint)objA.GetUserPointer();
1520 uint idB = (uint)objB.GetUserPointer();
1521 if (idA > idB)
1522 {
1523 uint temp = idA;
1524 idA = idB;
1525 idB = temp;
1526 contactNormal = -contactNormal;
1527 }
1528
1529 ulong collisionID = ((ulong) idA << 32) | idB;
1530
1531 BulletXNA.CollisionDesc cDesc = new BulletXNA.CollisionDesc()
1532 {
1533 aID = idA,
1534 bID = idB,
1535 point = contact,
1536 normal = contactNormal
1537 };
1538 world.UpdatedCollisions.Add(cDesc);
1539 m_collisionsThisFrame++;
1540
1541
1542 }
1543 private static EntityProperties GetDebugProperties(BulletWorld pWorld, BulletBody pBody)
1544 {
1545 EntityProperties ent = new EntityProperties();
1546 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world;
1547 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
1548 IndexedMatrix transform = body.GetWorldTransform();
1549 IndexedVector3 LinearVelocity = body.GetInterpolationLinearVelocity();
1550 IndexedVector3 AngularVelocity = body.GetInterpolationAngularVelocity();
1551 IndexedQuaternion rotation = transform.GetRotation();
1552 ent.Acceleration = Vector3.Zero;
1553 ent.ID = (uint)body.GetUserPointer();
1554 ent.Position = new Vector3(transform._origin.X,transform._origin.Y,transform._origin.Z);
1555 ent.Rotation = new Quaternion(rotation.X,rotation.Y,rotation.Z,rotation.W);
1556 ent.Velocity = new Vector3(LinearVelocity.X, LinearVelocity.Y, LinearVelocity.Z);
1557 ent.RotationalVelocity = new Vector3(AngularVelocity.X, AngularVelocity.Y, AngularVelocity.Z);
1558 return ent;
1559 }
1560
1561 public override bool UpdateParameter(BulletWorld world, uint localID, String parm, float value) { /* TODO */ return false; }
1562
1563 public override Vector3 GetLocalScaling(BulletShape pShape)
1564 {
1565 CollisionShape shape = ((BulletShapeXNA)pShape).shape;
1566 IndexedVector3 scale = shape.GetLocalScaling();
1567 return new Vector3(scale.X,scale.Y,scale.Z);
1568 }
1569
1570 public bool RayCastGround(BulletWorld pWorld, Vector3 _RayOrigin, float pRayHeight, BulletBody NotMe)
1571 {
1572 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world;
1573 if (world != null)
1574 {
1575 if (NotMe is BulletBodyXNA && NotMe.HasPhysicalBody)
1576 {
1577 CollisionObject AvoidBody = ((BulletBodyXNA)NotMe).body;
1578
1579 IndexedVector3 rOrigin = new IndexedVector3(_RayOrigin.X, _RayOrigin.Y, _RayOrigin.Z);
1580 IndexedVector3 rEnd = new IndexedVector3(_RayOrigin.X, _RayOrigin.Y, _RayOrigin.Z - pRayHeight);
1581 using (
1582 ClosestNotMeRayResultCallback rayCallback =
1583 new ClosestNotMeRayResultCallback(rOrigin, rEnd, AvoidBody)
1584 )
1585 {
1586 world.RayTest(ref rOrigin, ref rEnd, rayCallback);
1587 if (rayCallback.HasHit())
1588 {
1589 IndexedVector3 hitLocation = rayCallback.m_hitPointWorld;
1590 }
1591 return rayCallback.HasHit();
1592 }
1593 }
1594 }
1595 return false;
1596 }
1597}
1598}
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSApiTemplate.cs b/OpenSim/Region/Physics/BulletSPlugin/BSApiTemplate.cs
new file mode 100644
index 0000000..bc163eb
--- /dev/null
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSApiTemplate.cs
@@ -0,0 +1,668 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27using System;
28using System.Collections.Generic;
29using System.Runtime.InteropServices;
30using System.Security;
31using System.Text;
32using OpenMetaverse;
33
34namespace OpenSim.Region.Physics.BulletSPlugin {
35
36 // Constraint type values as defined by Bullet
37public enum ConstraintType : int
38{
39 POINT2POINT_CONSTRAINT_TYPE = 3,
40 HINGE_CONSTRAINT_TYPE,
41 CONETWIST_CONSTRAINT_TYPE,
42 D6_CONSTRAINT_TYPE,
43 SLIDER_CONSTRAINT_TYPE,
44 CONTACT_CONSTRAINT_TYPE,
45 D6_SPRING_CONSTRAINT_TYPE,
46 MAX_CONSTRAINT_TYPE
47}
48
49// ===============================================================================
50[StructLayout(LayoutKind.Sequential)]
51public struct ConvexHull
52{
53 Vector3 Offset;
54 int VertexCount;
55 Vector3[] Vertices;
56}
57public enum BSPhysicsShapeType
58{
59 SHAPE_UNKNOWN = 0,
60 SHAPE_CAPSULE = 1,
61 SHAPE_BOX = 2,
62 SHAPE_CONE = 3,
63 SHAPE_CYLINDER = 4,
64 SHAPE_SPHERE = 5,
65 SHAPE_MESH = 6,
66 SHAPE_HULL = 7,
67 // following defined by BulletSim
68 SHAPE_GROUNDPLANE = 20,
69 SHAPE_TERRAIN = 21,
70 SHAPE_COMPOUND = 22,
71 SHAPE_HEIGHTMAP = 23,
72 SHAPE_AVATAR = 24,
73};
74
75// The native shapes have predefined shape hash keys
76public enum FixedShapeKey : ulong
77{
78 KEY_NONE = 0,
79 KEY_BOX = 1,
80 KEY_SPHERE = 2,
81 KEY_CONE = 3,
82 KEY_CYLINDER = 4,
83 KEY_CAPSULE = 5,
84 KEY_AVATAR = 6,
85}
86
87[StructLayout(LayoutKind.Sequential)]
88public struct ShapeData
89{
90 public uint ID;
91 public BSPhysicsShapeType Type;
92 public Vector3 Position;
93 public Quaternion Rotation;
94 public Vector3 Velocity;
95 public Vector3 Scale;
96 public float Mass;
97 public float Buoyancy;
98 public System.UInt64 HullKey;
99 public System.UInt64 MeshKey;
100 public float Friction;
101 public float Restitution;
102 public float Collidable; // true of things bump into this
103 public float Static; // true if a static object. Otherwise gravity, etc.
104 public float Solid; // true if object cannot be passed through
105 public Vector3 Size;
106
107 // note that bools are passed as floats since bool size changes by language and architecture
108 public const float numericTrue = 1f;
109 public const float numericFalse = 0f;
110}
111[StructLayout(LayoutKind.Sequential)]
112public struct SweepHit
113{
114 public uint ID;
115 public float Fraction;
116 public Vector3 Normal;
117 public Vector3 Point;
118}
119[StructLayout(LayoutKind.Sequential)]
120public struct RaycastHit
121{
122 public uint ID;
123 public float Fraction;
124 public Vector3 Normal;
125}
126[StructLayout(LayoutKind.Sequential)]
127public struct CollisionDesc
128{
129 public uint aID;
130 public uint bID;
131 public Vector3 point;
132 public Vector3 normal;
133}
134[StructLayout(LayoutKind.Sequential)]
135public struct EntityProperties
136{
137 public uint ID;
138 public Vector3 Position;
139 public Quaternion Rotation;
140 public Vector3 Velocity;
141 public Vector3 Acceleration;
142 public Vector3 RotationalVelocity;
143}
144
145// Format of this structure must match the definition in the C++ code
146// NOTE: adding the X causes compile breaks if used. These are unused symbols
147// that can be removed from both here and the unmanaged definition of this structure.
148[StructLayout(LayoutKind.Sequential)]
149public struct ConfigurationParameters
150{
151 public float defaultFriction;
152 public float defaultDensity;
153 public float defaultRestitution;
154 public float collisionMargin;
155 public float gravity;
156
157 public float XlinearDamping;
158 public float XangularDamping;
159 public float XdeactivationTime;
160 public float XlinearSleepingThreshold;
161 public float XangularSleepingThreshold;
162 public float XccdMotionThreshold;
163 public float XccdSweptSphereRadius;
164 public float XcontactProcessingThreshold;
165
166 public float XterrainImplementation;
167 public float XterrainFriction;
168 public float XterrainHitFraction;
169 public float XterrainRestitution;
170 public float XterrainCollisionMargin;
171
172 public float XavatarFriction;
173 public float XavatarStandingFriction;
174 public float XavatarDensity;
175 public float XavatarRestitution;
176 public float XavatarCapsuleWidth;
177 public float XavatarCapsuleDepth;
178 public float XavatarCapsuleHeight;
179 public float XavatarContactProcessingThreshold;
180
181 public float XvehicleAngularDamping;
182
183 public float maxPersistantManifoldPoolSize;
184 public float maxCollisionAlgorithmPoolSize;
185 public float shouldDisableContactPoolDynamicAllocation;
186 public float shouldForceUpdateAllAabbs;
187 public float shouldRandomizeSolverOrder;
188 public float shouldSplitSimulationIslands;
189 public float shouldEnableFrictionCaching;
190 public float numberOfSolverIterations;
191
192 public float XlinksetImplementation;
193 public float XlinkConstraintUseFrameOffset;
194 public float XlinkConstraintEnableTransMotor;
195 public float XlinkConstraintTransMotorMaxVel;
196 public float XlinkConstraintTransMotorMaxForce;
197 public float XlinkConstraintERP;
198 public float XlinkConstraintCFM;
199 public float XlinkConstraintSolverIterations;
200
201 public float XphysicsLoggingFrames;
202
203 public const float numericTrue = 1f;
204 public const float numericFalse = 0f;
205}
206
207
208// The states a bullet collision object can have
209public enum ActivationState : uint
210{
211 ACTIVE_TAG = 1,
212 ISLAND_SLEEPING,
213 WANTS_DEACTIVATION,
214 DISABLE_DEACTIVATION,
215 DISABLE_SIMULATION,
216}
217
218public enum CollisionObjectTypes : int
219{
220 CO_COLLISION_OBJECT = 1 << 0,
221 CO_RIGID_BODY = 1 << 1,
222 CO_GHOST_OBJECT = 1 << 2,
223 CO_SOFT_BODY = 1 << 3,
224 CO_HF_FLUID = 1 << 4,
225 CO_USER_TYPE = 1 << 5,
226}
227
228// Values used by Bullet and BulletSim to control object properties.
229// Bullet's "CollisionFlags" has more to do with operations on the
230// object (if collisions happen, if gravity effects it, ...).
231public enum CollisionFlags : uint
232{
233 CF_STATIC_OBJECT = 1 << 0,
234 CF_KINEMATIC_OBJECT = 1 << 1,
235 CF_NO_CONTACT_RESPONSE = 1 << 2,
236 CF_CUSTOM_MATERIAL_CALLBACK = 1 << 3,
237 CF_CHARACTER_OBJECT = 1 << 4,
238 CF_DISABLE_VISUALIZE_OBJECT = 1 << 5,
239 CF_DISABLE_SPU_COLLISION_PROCESS = 1 << 6,
240 // Following used by BulletSim to control collisions and updates
241 BS_SUBSCRIBE_COLLISION_EVENTS = 1 << 10,
242 BS_FLOATS_ON_WATER = 1 << 11,
243 BS_VEHICLE_COLLISIONS = 1 << 12,
244 BS_NONE = 0,
245 BS_ALL = 0xFFFFFFFF
246};
247
248// Values f collisions groups and masks
249public enum CollisionFilterGroups : uint
250{
251 // Don't use the bit definitions!! Define the use in a
252 // filter/mask definition below. This way collision interactions
253 // are more easily found and debugged.
254 BNoneGroup = 0,
255 BDefaultGroup = 1 << 0, // 0001
256 BStaticGroup = 1 << 1, // 0002
257 BKinematicGroup = 1 << 2, // 0004
258 BDebrisGroup = 1 << 3, // 0008
259 BSensorTrigger = 1 << 4, // 0010
260 BCharacterGroup = 1 << 5, // 0020
261 BAllGroup = 0x000FFFFF,
262 // Filter groups defined by BulletSim
263 BGroundPlaneGroup = 1 << 10, // 0400
264 BTerrainGroup = 1 << 11, // 0800
265 BRaycastGroup = 1 << 12, // 1000
266 BSolidGroup = 1 << 13, // 2000
267 // BLinksetGroup = xx // a linkset proper is either static or dynamic
268 BLinksetChildGroup = 1 << 14, // 4000
269};
270
271// CFM controls the 'hardness' of the constraint. 0=fixed, 0..1=violatable. Default=0
272// ERP controls amount of correction per tick. Usable range=0.1..0.8. Default=0.2.
273public enum ConstraintParams : int
274{
275 BT_CONSTRAINT_ERP = 1, // this one is not used in Bullet as of 20120730
276 BT_CONSTRAINT_STOP_ERP,
277 BT_CONSTRAINT_CFM,
278 BT_CONSTRAINT_STOP_CFM,
279};
280public enum ConstraintParamAxis : int
281{
282 AXIS_LINEAR_X = 0,
283 AXIS_LINEAR_Y,
284 AXIS_LINEAR_Z,
285 AXIS_ANGULAR_X,
286 AXIS_ANGULAR_Y,
287 AXIS_ANGULAR_Z,
288 AXIS_LINEAR_ALL = 20, // these last three added by BulletSim so we don't have to do zillions of calls
289 AXIS_ANGULAR_ALL,
290 AXIS_ALL
291};
292
293public abstract class BSAPITemplate
294{
295// Returns the name of the underlying Bullet engine
296public abstract string BulletEngineName { get; }
297public abstract string BulletEngineVersion { get; protected set;}
298
299// Initialization and simulation
300public abstract BulletWorld Initialize(Vector3 maxPosition, ConfigurationParameters parms,
301 int maxCollisions, ref CollisionDesc[] collisionArray,
302 int maxUpdates, ref EntityProperties[] updateArray
303 );
304
305public abstract int PhysicsStep(BulletWorld world, float timeStep, int maxSubSteps, float fixedTimeStep,
306 out int updatedEntityCount, out int collidersCount);
307
308public abstract bool UpdateParameter(BulletWorld world, uint localID, String parm, float value);
309
310public abstract void Shutdown(BulletWorld sim);
311
312public abstract bool PushUpdate(BulletBody obj);
313
314// =====================================================================================
315// Mesh, hull, shape and body creation helper routines
316public abstract BulletShape CreateMeshShape(BulletWorld world,
317 int indicesCount, int[] indices,
318 int verticesCount, float[] vertices );
319
320public abstract BulletShape CreateHullShape(BulletWorld world,
321 int hullCount, float[] hulls);
322
323public abstract BulletShape BuildHullShapeFromMesh(BulletWorld world, BulletShape meshShape);
324
325public abstract BulletShape BuildNativeShape(BulletWorld world, ShapeData shapeData);
326
327public abstract bool IsNativeShape(BulletShape shape);
328
329public abstract void SetShapeCollisionMargin(BulletShape shape, float margin);
330
331public abstract BulletShape BuildCapsuleShape(BulletWorld world, float radius, float height, Vector3 scale);
332
333public abstract BulletShape CreateCompoundShape(BulletWorld sim, bool enableDynamicAabbTree);
334
335public abstract int GetNumberOfCompoundChildren(BulletShape cShape);
336
337public abstract void AddChildShapeToCompoundShape(BulletShape cShape, BulletShape addShape, Vector3 pos, Quaternion rot);
338
339public abstract BulletShape GetChildShapeFromCompoundShapeIndex(BulletShape cShape, int indx);
340
341public abstract BulletShape RemoveChildShapeFromCompoundShapeIndex(BulletShape cShape, int indx);
342
343public abstract void RemoveChildShapeFromCompoundShape(BulletShape cShape, BulletShape removeShape);
344
345public abstract void UpdateChildTransform(BulletShape pShape, int childIndex, Vector3 pos, Quaternion rot, bool shouldRecalculateLocalAabb);
346
347public abstract void RecalculateCompoundShapeLocalAabb(BulletShape cShape);
348
349public abstract BulletShape DuplicateCollisionShape(BulletWorld sim, BulletShape srcShape, uint id);
350
351public abstract bool DeleteCollisionShape(BulletWorld world, BulletShape shape);
352
353public abstract CollisionObjectTypes GetBodyType(BulletBody obj);
354
355public abstract BulletBody CreateBodyFromShape(BulletWorld sim, BulletShape shape, uint id, Vector3 pos, Quaternion rot);
356
357public abstract BulletBody CreateBodyWithDefaultMotionState(BulletShape shape, uint id, Vector3 pos, Quaternion rot);
358
359public abstract BulletBody CreateGhostFromShape(BulletWorld sim, BulletShape shape, uint id, Vector3 pos, Quaternion rot);
360
361public abstract void DestroyObject(BulletWorld sim, BulletBody obj);
362
363// =====================================================================================
364public abstract BulletShape CreateGroundPlaneShape(uint id, float height, float collisionMargin);
365
366public abstract BulletShape CreateTerrainShape(uint id, Vector3 size, float minHeight, float maxHeight, float[] heightMap,
367 float scaleFactor, float collisionMargin);
368
369// =====================================================================================
370// Constraint creation and helper routines
371public abstract BulletConstraint Create6DofConstraint(BulletWorld world, BulletBody obj1, BulletBody obj2,
372 Vector3 frame1loc, Quaternion frame1rot,
373 Vector3 frame2loc, Quaternion frame2rot,
374 bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies);
375
376public abstract BulletConstraint Create6DofConstraintToPoint(BulletWorld world, BulletBody obj1, BulletBody obj2,
377 Vector3 joinPoint,
378 bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies);
379
380public abstract BulletConstraint CreateHingeConstraint(BulletWorld world, BulletBody obj1, BulletBody obj2,
381 Vector3 pivotinA, Vector3 pivotinB,
382 Vector3 axisInA, Vector3 axisInB,
383 bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies);
384
385public abstract void SetConstraintEnable(BulletConstraint constrain, float numericTrueFalse);
386
387public abstract void SetConstraintNumSolverIterations(BulletConstraint constrain, float iterations);
388
389public abstract bool SetFrames(BulletConstraint constrain,
390 Vector3 frameA, Quaternion frameArot, Vector3 frameB, Quaternion frameBrot);
391
392public abstract bool SetLinearLimits(BulletConstraint constrain, Vector3 low, Vector3 hi);
393
394public abstract bool SetAngularLimits(BulletConstraint constrain, Vector3 low, Vector3 hi);
395
396public abstract bool UseFrameOffset(BulletConstraint constrain, float enable);
397
398public abstract bool TranslationalLimitMotor(BulletConstraint constrain, float enable, float targetVel, float maxMotorForce);
399
400public abstract bool SetBreakingImpulseThreshold(BulletConstraint constrain, float threshold);
401
402public abstract bool CalculateTransforms(BulletConstraint constrain);
403
404public abstract bool SetConstraintParam(BulletConstraint constrain, ConstraintParams paramIndex, float value, ConstraintParamAxis axis);
405
406public abstract bool DestroyConstraint(BulletWorld world, BulletConstraint constrain);
407
408// =====================================================================================
409// btCollisionWorld entries
410public abstract void UpdateSingleAabb(BulletWorld world, BulletBody obj);
411
412public abstract void UpdateAabbs(BulletWorld world);
413
414public abstract bool GetForceUpdateAllAabbs(BulletWorld world);
415
416public abstract void SetForceUpdateAllAabbs(BulletWorld world, bool force);
417
418// =====================================================================================
419// btDynamicsWorld entries
420// public abstract bool AddObjectToWorld(BulletWorld world, BulletBody obj, Vector3 pos, Quaternion rot);
421public abstract bool AddObjectToWorld(BulletWorld world, BulletBody obj);
422
423public abstract bool RemoveObjectFromWorld(BulletWorld world, BulletBody obj);
424
425public abstract bool AddConstraintToWorld(BulletWorld world, BulletConstraint constrain, bool disableCollisionsBetweenLinkedObjects);
426
427public abstract bool RemoveConstraintFromWorld(BulletWorld world, BulletConstraint constrain);
428// =====================================================================================
429// btCollisionObject entries
430public abstract Vector3 GetAnisotripicFriction(BulletConstraint constrain);
431
432public abstract Vector3 SetAnisotripicFriction(BulletConstraint constrain, Vector3 frict);
433
434public abstract bool HasAnisotripicFriction(BulletConstraint constrain);
435
436public abstract void SetContactProcessingThreshold(BulletBody obj, float val);
437
438public abstract float GetContactProcessingThreshold(BulletBody obj);
439
440public abstract bool IsStaticObject(BulletBody obj);
441
442public abstract bool IsKinematicObject(BulletBody obj);
443
444public abstract bool IsStaticOrKinematicObject(BulletBody obj);
445
446public abstract bool HasContactResponse(BulletBody obj);
447
448public abstract void SetCollisionShape(BulletWorld sim, BulletBody obj, BulletShape shape);
449
450public abstract BulletShape GetCollisionShape(BulletBody obj);
451
452public abstract int GetActivationState(BulletBody obj);
453
454public abstract void SetActivationState(BulletBody obj, int state);
455
456public abstract void SetDeactivationTime(BulletBody obj, float dtime);
457
458public abstract float GetDeactivationTime(BulletBody obj);
459
460public abstract void ForceActivationState(BulletBody obj, ActivationState state);
461
462public abstract void Activate(BulletBody obj, bool forceActivation);
463
464public abstract bool IsActive(BulletBody obj);
465
466public abstract void SetRestitution(BulletBody obj, float val);
467
468public abstract float GetRestitution(BulletBody obj);
469
470public abstract void SetFriction(BulletBody obj, float val);
471
472public abstract float GetFriction(BulletBody obj);
473
474public abstract Vector3 GetPosition(BulletBody obj);
475
476public abstract Quaternion GetOrientation(BulletBody obj);
477
478public abstract void SetTranslation(BulletBody obj, Vector3 position, Quaternion rotation);
479
480// public abstract IntPtr GetBroadphaseHandle(BulletBody obj);
481
482// public abstract void SetBroadphaseHandle(BulletBody obj, IntPtr handle);
483
484public abstract void SetInterpolationLinearVelocity(BulletBody obj, Vector3 vel);
485
486public abstract void SetInterpolationAngularVelocity(BulletBody obj, Vector3 vel);
487
488public abstract void SetInterpolationVelocity(BulletBody obj, Vector3 linearVel, Vector3 angularVel);
489
490public abstract float GetHitFraction(BulletBody obj);
491
492public abstract void SetHitFraction(BulletBody obj, float val);
493
494public abstract CollisionFlags GetCollisionFlags(BulletBody obj);
495
496public abstract CollisionFlags SetCollisionFlags(BulletBody obj, CollisionFlags flags);
497
498public abstract CollisionFlags AddToCollisionFlags(BulletBody obj, CollisionFlags flags);
499
500public abstract CollisionFlags RemoveFromCollisionFlags(BulletBody obj, CollisionFlags flags);
501
502public abstract float GetCcdMotionThreshold(BulletBody obj);
503
504public abstract void SetCcdMotionThreshold(BulletBody obj, float val);
505
506public abstract float GetCcdSweptSphereRadius(BulletBody obj);
507
508public abstract void SetCcdSweptSphereRadius(BulletBody obj, float val);
509
510public abstract IntPtr GetUserPointer(BulletBody obj);
511
512public abstract void SetUserPointer(BulletBody obj, IntPtr val);
513
514// =====================================================================================
515// btRigidBody entries
516public abstract void ApplyGravity(BulletBody obj);
517
518public abstract void SetGravity(BulletBody obj, Vector3 val);
519
520public abstract Vector3 GetGravity(BulletBody obj);
521
522public abstract void SetDamping(BulletBody obj, float lin_damping, float ang_damping);
523
524public abstract void SetLinearDamping(BulletBody obj, float lin_damping);
525
526public abstract void SetAngularDamping(BulletBody obj, float ang_damping);
527
528public abstract float GetLinearDamping(BulletBody obj);
529
530public abstract float GetAngularDamping(BulletBody obj);
531
532public abstract float GetLinearSleepingThreshold(BulletBody obj);
533
534public abstract void ApplyDamping(BulletBody obj, float timeStep);
535
536public abstract void SetMassProps(BulletBody obj, float mass, Vector3 inertia);
537
538public abstract Vector3 GetLinearFactor(BulletBody obj);
539
540public abstract void SetLinearFactor(BulletBody obj, Vector3 factor);
541
542public abstract void SetCenterOfMassByPosRot(BulletBody obj, Vector3 pos, Quaternion rot);
543
544// Add a force to the object as if its mass is one.
545public abstract void ApplyCentralForce(BulletBody obj, Vector3 force);
546
547// Set the force being applied to the object as if its mass is one.
548public abstract void SetObjectForce(BulletBody obj, Vector3 force);
549
550public abstract Vector3 GetTotalForce(BulletBody obj);
551
552public abstract Vector3 GetTotalTorque(BulletBody obj);
553
554public abstract Vector3 GetInvInertiaDiagLocal(BulletBody obj);
555
556public abstract void SetInvInertiaDiagLocal(BulletBody obj, Vector3 inert);
557
558public abstract void SetSleepingThresholds(BulletBody obj, float lin_threshold, float ang_threshold);
559
560public abstract void ApplyTorque(BulletBody obj, Vector3 torque);
561
562// Apply force at the given point. Will add torque to the object.
563public abstract void ApplyForce(BulletBody obj, Vector3 force, Vector3 pos);
564
565// Apply impulse to the object. Same as "ApplycentralForce" but force scaled by object's mass.
566public abstract void ApplyCentralImpulse(BulletBody obj, Vector3 imp);
567
568// Apply impulse to the object's torque. Force is scaled by object's mass.
569public abstract void ApplyTorqueImpulse(BulletBody obj, Vector3 imp);
570
571// Apply impulse at the point given. For is scaled by object's mass and effects both linear and angular forces.
572public abstract void ApplyImpulse(BulletBody obj, Vector3 imp, Vector3 pos);
573
574public abstract void ClearForces(BulletBody obj);
575
576public abstract void ClearAllForces(BulletBody obj);
577
578public abstract void UpdateInertiaTensor(BulletBody obj);
579
580public abstract Vector3 GetLinearVelocity(BulletBody obj);
581
582public abstract Vector3 GetAngularVelocity(BulletBody obj);
583
584public abstract void SetLinearVelocity(BulletBody obj, Vector3 val);
585
586public abstract void SetAngularVelocity(BulletBody obj, Vector3 angularVelocity);
587
588public abstract Vector3 GetVelocityInLocalPoint(BulletBody obj, Vector3 pos);
589
590public abstract void Translate(BulletBody obj, Vector3 trans);
591
592public abstract void UpdateDeactivation(BulletBody obj, float timeStep);
593
594public abstract bool WantsSleeping(BulletBody obj);
595
596public abstract void SetAngularFactor(BulletBody obj, float factor);
597
598public abstract void SetAngularFactorV(BulletBody obj, Vector3 factor);
599
600public abstract Vector3 GetAngularFactor(BulletBody obj);
601
602public abstract bool IsInWorld(BulletWorld world, BulletBody obj);
603
604public abstract void AddConstraintRef(BulletBody obj, BulletConstraint constrain);
605
606public abstract void RemoveConstraintRef(BulletBody obj, BulletConstraint constrain);
607
608public abstract BulletConstraint GetConstraintRef(BulletBody obj, int index);
609
610public abstract int GetNumConstraintRefs(BulletBody obj);
611
612public abstract bool SetCollisionGroupMask(BulletBody body, uint filter, uint mask);
613
614// =====================================================================================
615// btCollisionShape entries
616
617public abstract float GetAngularMotionDisc(BulletShape shape);
618
619public abstract float GetContactBreakingThreshold(BulletShape shape, float defaultFactor);
620
621public abstract bool IsPolyhedral(BulletShape shape);
622
623public abstract bool IsConvex2d(BulletShape shape);
624
625public abstract bool IsConvex(BulletShape shape);
626
627public abstract bool IsNonMoving(BulletShape shape);
628
629public abstract bool IsConcave(BulletShape shape);
630
631public abstract bool IsCompound(BulletShape shape);
632
633public abstract bool IsSoftBody(BulletShape shape);
634
635public abstract bool IsInfinite(BulletShape shape);
636
637public abstract void SetLocalScaling(BulletShape shape, Vector3 scale);
638
639public abstract Vector3 GetLocalScaling(BulletShape shape);
640
641public abstract Vector3 CalculateLocalInertia(BulletShape shape, float mass);
642
643public abstract int GetShapeType(BulletShape shape);
644
645public abstract void SetMargin(BulletShape shape, float val);
646
647public abstract float GetMargin(BulletShape shape);
648
649// =====================================================================================
650// Debugging
651public virtual void DumpRigidBody(BulletWorld sim, BulletBody collisionObject) { }
652
653public virtual void DumpCollisionShape(BulletWorld sim, BulletShape collisionShape) { }
654
655public virtual void DumpConstraint(BulletWorld sim, BulletConstraint constrain) { }
656
657public virtual void DumpActivationInfo(BulletWorld sim) { }
658
659public virtual void DumpAllInfo(BulletWorld sim) { }
660
661public virtual void DumpPhysicsStatistics(BulletWorld sim) { }
662
663public virtual void ResetBroadphasePool(BulletWorld sim) { }
664
665public virtual void ResetConstraintSolver(BulletWorld sim) { }
666
667};
668}
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSCharacter.cs b/OpenSim/Region/Physics/BulletSPlugin/BSCharacter.cs
index 4c195e1..a5fec87 100644
--- a/OpenSim/Region/Physics/BulletSPlugin/BSCharacter.cs
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSCharacter.cs
@@ -58,17 +58,16 @@ public sealed class BSCharacter : BSPhysObject
58 private bool _flying; 58 private bool _flying;
59 private bool _setAlwaysRun; 59 private bool _setAlwaysRun;
60 private bool _throttleUpdates; 60 private bool _throttleUpdates;
61 private bool _isColliding;
62 private bool _collidingObj;
63 private bool _floatOnWater; 61 private bool _floatOnWater;
64 private OMV.Vector3 _rotationalVelocity; 62 private OMV.Vector3 _rotationalVelocity;
65 private bool _kinematic; 63 private bool _kinematic;
66 private float _buoyancy; 64 private float _buoyancy;
67 65
68 // The friction and velocity of the avatar is modified depending on whether walking or not. 66 // The friction and velocity of the avatar is modified depending on whether walking or not.
69 private OMV.Vector3 _appliedVelocity; // the last velocity applied to the avatar
70 private float _currentFriction; // the friction currently being used (changed by setVelocity). 67 private float _currentFriction; // the friction currently being used (changed by setVelocity).
71 68
69 private BSVMotor _velocityMotor;
70
72 private OMV.Vector3 _PIDTarget; 71 private OMV.Vector3 _PIDTarget;
73 private bool _usePID; 72 private bool _usePID;
74 private float _PIDTau; 73 private float _PIDTau;
@@ -83,34 +82,36 @@ public sealed class BSCharacter : BSPhysObject
83 _physicsActorType = (int)ActorTypes.Agent; 82 _physicsActorType = (int)ActorTypes.Agent;
84 _position = pos; 83 _position = pos;
85 84
86 // Old versions of ScenePresence passed only the height. If width and/or depth are zero,
87 // replace with the default values.
88 _size = size;
89 if (_size.X == 0f) _size.X = PhysicsScene.Params.avatarCapsuleDepth;
90 if (_size.Y == 0f) _size.Y = PhysicsScene.Params.avatarCapsuleWidth;
91
92 _flying = isFlying; 85 _flying = isFlying;
93 _orientation = OMV.Quaternion.Identity; 86 _orientation = OMV.Quaternion.Identity;
94 _velocity = OMV.Vector3.Zero; 87 _velocity = OMV.Vector3.Zero;
95 _appliedVelocity = OMV.Vector3.Zero;
96 _buoyancy = ComputeBuoyancyFromFlying(isFlying); 88 _buoyancy = ComputeBuoyancyFromFlying(isFlying);
97 _currentFriction = PhysicsScene.Params.avatarStandingFriction; 89 _currentFriction = BSParam.AvatarStandingFriction;
98 _avatarDensity = PhysicsScene.Params.avatarDensity; 90 _avatarDensity = BSParam.AvatarDensity;
99 91
100 // The dimensions of the avatar capsule are kept in the scale. 92 // Old versions of ScenePresence passed only the height. If width and/or depth are zero,
93 // replace with the default values.
94 _size = size;
95 if (_size.X == 0f) _size.X = BSParam.AvatarCapsuleDepth;
96 if (_size.Y == 0f) _size.Y = BSParam.AvatarCapsuleWidth;
97
98 // The dimensions of the physical capsule are kept in the scale.
101 // Physics creates a unit capsule which is scaled by the physics engine. 99 // Physics creates a unit capsule which is scaled by the physics engine.
102 ComputeAvatarScale(_size); 100 Scale = ComputeAvatarScale(_size);
103 // set _avatarVolume and _mass based on capsule size, _density and Scale 101 // set _avatarVolume and _mass based on capsule size, _density and Scale
104 ComputeAvatarVolumeAndMass(); 102 ComputeAvatarVolumeAndMass();
103
104 SetupMovementMotor();
105
105 DetailLog("{0},BSCharacter.create,call,size={1},scale={2},density={3},volume={4},mass={5}", 106 DetailLog("{0},BSCharacter.create,call,size={1},scale={2},density={3},volume={4},mass={5}",
106 LocalID, _size, Scale, _avatarDensity, _avatarVolume, RawMass); 107 LocalID, _size, Scale, _avatarDensity, _avatarVolume, RawMass);
107 108
108 // do actual create at taint time 109 // do actual creation in taint time
109 PhysicsScene.TaintedObject("BSCharacter.create", delegate() 110 PhysicsScene.TaintedObject("BSCharacter.create", delegate()
110 { 111 {
111 DetailLog("{0},BSCharacter.create,taint", LocalID); 112 DetailLog("{0},BSCharacter.create,taint", LocalID);
112 // New body and shape into PhysBody and PhysShape 113 // New body and shape into PhysBody and PhysShape
113 PhysicsScene.Shapes.GetBodyAndShape(true, PhysicsScene.World, this, null, null); 114 PhysicsScene.Shapes.GetBodyAndShape(true, PhysicsScene.World, this);
114 115
115 SetPhysicalProperties(); 116 SetPhysicalProperties();
116 }); 117 });
@@ -120,54 +121,155 @@ public sealed class BSCharacter : BSPhysObject
120 // called when this character is being destroyed and the resources should be released 121 // called when this character is being destroyed and the resources should be released
121 public override void Destroy() 122 public override void Destroy()
122 { 123 {
124 base.Destroy();
125
123 DetailLog("{0},BSCharacter.Destroy", LocalID); 126 DetailLog("{0},BSCharacter.Destroy", LocalID);
124 PhysicsScene.TaintedObject("BSCharacter.destroy", delegate() 127 PhysicsScene.TaintedObject("BSCharacter.destroy", delegate()
125 { 128 {
126 PhysicsScene.Shapes.DereferenceBody(PhysBody, true, null); 129 PhysicsScene.Shapes.DereferenceBody(PhysBody, true /* inTaintTime */, null /* bodyCallback */);
127 PhysicsScene.Shapes.DereferenceShape(PhysShape, true, null); 130 PhysBody.Clear();
131 PhysicsScene.Shapes.DereferenceShape(PhysShape, true /* inTaintTime */, null /* bodyCallback */);
132 PhysShape.Clear();
128 }); 133 });
129 } 134 }
130 135
131 private void SetPhysicalProperties() 136 private void SetPhysicalProperties()
132 { 137 {
133 BulletSimAPI.RemoveObjectFromWorld2(PhysicsScene.World.ptr, PhysBody.ptr); 138 PhysicsScene.PE.RemoveObjectFromWorld(PhysicsScene.World, PhysBody);
134 139
135 ZeroMotion(true); 140 ZeroMotion(true);
136 ForcePosition = _position; 141 ForcePosition = _position;
142
137 // Set the velocity and compute the proper friction 143 // Set the velocity and compute the proper friction
144 _velocityMotor.Reset();
145 _velocityMotor.SetTarget(_velocity);
146 _velocityMotor.SetCurrent(_velocity);
138 ForceVelocity = _velocity; 147 ForceVelocity = _velocity;
139 148
140 // This will enable or disable the flying buoyancy of the avatar. 149 // This will enable or disable the flying buoyancy of the avatar.
141 // Needs to be reset especially when an avatar is recreated after crossing a region boundry. 150 // Needs to be reset especially when an avatar is recreated after crossing a region boundry.
142 Flying = _flying; 151 Flying = _flying;
143 152
144 BulletSimAPI.SetRestitution2(PhysBody.ptr, PhysicsScene.Params.avatarRestitution); 153 PhysicsScene.PE.SetRestitution(PhysBody, BSParam.AvatarRestitution);
145 BulletSimAPI.SetMargin2(PhysShape.ptr, PhysicsScene.Params.collisionMargin); 154 PhysicsScene.PE.SetMargin(PhysShape, PhysicsScene.Params.collisionMargin);
146 BulletSimAPI.SetLocalScaling2(PhysShape.ptr, Scale); 155 PhysicsScene.PE.SetLocalScaling(PhysShape, Scale);
147 BulletSimAPI.SetContactProcessingThreshold2(PhysBody.ptr, PhysicsScene.Params.contactProcessingThreshold); 156 PhysicsScene.PE.SetContactProcessingThreshold(PhysBody, BSParam.ContactProcessingThreshold);
148 if (PhysicsScene.Params.ccdMotionThreshold > 0f) 157 if (BSParam.CcdMotionThreshold > 0f)
149 { 158 {
150 BulletSimAPI.SetCcdMotionThreshold2(PhysBody.ptr, PhysicsScene.Params.ccdMotionThreshold); 159 PhysicsScene.PE.SetCcdMotionThreshold(PhysBody, BSParam.CcdMotionThreshold);
151 BulletSimAPI.SetCcdSweptSphereRadius2(PhysBody.ptr, PhysicsScene.Params.ccdSweptSphereRadius); 160 PhysicsScene.PE.SetCcdSweptSphereRadius(PhysBody, BSParam.CcdSweptSphereRadius);
152 } 161 }
153 162
154 UpdatePhysicalMassProperties(RawMass); 163 UpdatePhysicalMassProperties(RawMass, false);
155 164
156 // Make so capsule does not fall over 165 // Make so capsule does not fall over
157 BulletSimAPI.SetAngularFactorV2(PhysBody.ptr, OMV.Vector3.Zero); 166 PhysicsScene.PE.SetAngularFactorV(PhysBody, OMV.Vector3.Zero);
158 167
159 BulletSimAPI.AddToCollisionFlags2(PhysBody.ptr, CollisionFlags.CF_CHARACTER_OBJECT); 168 PhysicsScene.PE.AddToCollisionFlags(PhysBody, CollisionFlags.CF_CHARACTER_OBJECT);
160 169
161 BulletSimAPI.AddObjectToWorld2(PhysicsScene.World.ptr, PhysBody.ptr); 170 PhysicsScene.PE.AddObjectToWorld(PhysicsScene.World, PhysBody);
162 171
163 // BulletSimAPI.ForceActivationState2(BSBody.ptr, ActivationState.ACTIVE_TAG); 172 // PhysicsScene.PE.ForceActivationState(PhysBody, ActivationState.ACTIVE_TAG);
164 BulletSimAPI.ForceActivationState2(PhysBody.ptr, ActivationState.DISABLE_DEACTIVATION); 173 PhysicsScene.PE.ForceActivationState(PhysBody, ActivationState.DISABLE_DEACTIVATION);
165 BulletSimAPI.UpdateSingleAabb2(PhysicsScene.World.ptr, PhysBody.ptr); 174 PhysicsScene.PE.UpdateSingleAabb(PhysicsScene.World, PhysBody);
166 175
167 // Do this after the object has been added to the world 176 // Do this after the object has been added to the world
168 BulletSimAPI.SetCollisionFilterMask2(PhysBody.ptr, 177 PhysBody.collisionType = CollisionType.Avatar;
169 (uint)CollisionFilterGroups.AvatarFilter, 178 PhysBody.ApplyCollisionMask(PhysicsScene);
170 (uint)CollisionFilterGroups.AvatarMask); 179 }
180
181 // The avatar's movement is controlled by this motor that speeds up and slows down
182 // the avatar seeking to reach the motor's target speed.
183 // This motor runs as a prestep action for the avatar so it will keep the avatar
184 // standing as well as moving. Destruction of the avatar will destroy the pre-step action.
185 private void SetupMovementMotor()
186 {
187 // Infinite decay and timescale values so motor only changes current to target values.
188 _velocityMotor = new BSVMotor("BSCharacter.Velocity",
189 0.2f, // time scale
190 BSMotor.Infinite, // decay time scale
191 BSMotor.InfiniteVector, // friction timescale
192 1f // efficiency
193 );
194 // _velocityMotor.PhysicsScene = PhysicsScene; // DEBUG DEBUG so motor will output detail log messages.
195
196 RegisterPreStepAction("BSCharactor.Movement", LocalID, delegate(float timeStep)
197 {
198 // TODO: Decide if the step parameters should be changed depending on the avatar's
199 // state (flying, colliding, ...). There is code in ODE to do this.
200
201 OMV.Vector3 stepVelocity = _velocityMotor.Step(timeStep);
202
203 // If falling, we keep the world's downward vector no matter what the other axis specify.
204 if (!Flying && !IsColliding)
205 {
206 stepVelocity.Z = _velocity.Z;
207 // DetailLog("{0},BSCharacter.MoveMotor,taint,overrideStepZWithWorldZ,stepVel={1}", LocalID, stepVelocity);
208 }
209
210 // 'stepVelocity' is now the speed we'd like the avatar to move in. Turn that into an instantanous force.
211 OMV.Vector3 moveForce = (stepVelocity - _velocity) * Mass;
212
213 // Should we check for move force being small and forcing velocity to zero?
214
215 // Add special movement force to allow avatars to walk up stepped surfaces.
216 moveForce += WalkUpStairs();
217
218 DetailLog("{0},BSCharacter.MoveMotor,move,stepVel={1},vel={2},mass={3},moveForce={4}", LocalID, stepVelocity, _velocity, Mass, moveForce);
219 PhysicsScene.PE.ApplyCentralImpulse(PhysBody, moveForce);
220 });
221 }
222
223 // Decide of the character is colliding with a low object and compute a force to pop the
224 // avatar up so it has a chance of walking up and over the low object.
225 private OMV.Vector3 WalkUpStairs()
226 {
227 OMV.Vector3 ret = OMV.Vector3.Zero;
228
229 // This test is done if moving forward, not flying and is colliding with something.
230 // DetailLog("{0},BSCharacter.WalkUpStairs,IsColliding={1},flying={2},targSpeed={3},collisions={4}",
231 // LocalID, IsColliding, Flying, TargetSpeed, CollisionsLastTick.Count);
232 if (IsColliding && !Flying && TargetSpeed > 0.1f /* && ForwardSpeed < 0.1f */)
233 {
234 // The range near the character's feet where we will consider stairs
235 float nearFeetHeightMin = RawPosition.Z - (Size.Z / 2f) + 0.05f;
236 float nearFeetHeightMax = nearFeetHeightMin + BSParam.AvatarStepHeight;
237
238 // Look for a collision point that is near the character's feet and is oriented the same as the charactor is
239 foreach (KeyValuePair<uint, ContactPoint> kvp in CollisionsLastTick.m_objCollisionList)
240 {
241 // Don't care about collisions with the terrain
242 if (kvp.Key > PhysicsScene.TerrainManager.HighestTerrainID)
243 {
244 OMV.Vector3 touchPosition = kvp.Value.Position;
245 // DetailLog("{0},BSCharacter.WalkUpStairs,min={1},max={2},touch={3}",
246 // LocalID, nearFeetHeightMin, nearFeetHeightMax, touchPosition);
247 if (touchPosition.Z >= nearFeetHeightMin && touchPosition.Z <= nearFeetHeightMax)
248 {
249 // This contact is within the 'near the feet' range.
250 // The normal should be our contact point to the object so it is pointing away
251 // thus the difference between our facing orientation and the normal should be small.
252 OMV.Vector3 directionFacing = OMV.Vector3.UnitX * RawOrientation;
253 OMV.Vector3 touchNormal = OMV.Vector3.Normalize(kvp.Value.SurfaceNormal);
254 float diff = Math.Abs(OMV.Vector3.Distance(directionFacing, touchNormal));
255 if (diff < BSParam.AvatarStepApproachFactor)
256 {
257 // Found the stairs contact point. Push up a little to raise the character.
258 float upForce = (touchPosition.Z - nearFeetHeightMin) * Mass * BSParam.AvatarStepForceFactor;
259 ret = new OMV.Vector3(0f, 0f, upForce);
260
261 // Also move the avatar up for the new height
262 OMV.Vector3 displacement = new OMV.Vector3(0f, 0f, BSParam.AvatarStepHeight / 2f);
263 ForcePosition = RawPosition + displacement;
264 }
265 DetailLog("{0},BSCharacter.WalkUpStairs,touchPos={1},nearFeetMin={2},faceDir={3},norm={4},diff={5},ret={6}",
266 LocalID, touchPosition, nearFeetHeightMin, directionFacing, touchNormal, diff, ret);
267 }
268 }
269 }
270 }
271
272 return ret;
171 } 273 }
172 274
173 public override void RequestPhysicsterseUpdate() 275 public override void RequestPhysicsterseUpdate()
@@ -185,24 +287,31 @@ public sealed class BSCharacter : BSPhysObject
185 } 287 }
186 288
187 set { 289 set {
188 // When an avatar's size is set, only the height is changed.
189 _size = value; 290 _size = value;
190 ComputeAvatarScale(_size); 291 // Old versions of ScenePresence passed only the height. If width and/or depth are zero,
292 // replace with the default values.
293 if (_size.X == 0f) _size.X = BSParam.AvatarCapsuleDepth;
294 if (_size.Y == 0f) _size.Y = BSParam.AvatarCapsuleWidth;
295
296 Scale = ComputeAvatarScale(_size);
191 ComputeAvatarVolumeAndMass(); 297 ComputeAvatarVolumeAndMass();
192 DetailLog("{0},BSCharacter.setSize,call,size={1},scale={2},density={3},volume={4},mass={5}", 298 DetailLog("{0},BSCharacter.setSize,call,size={1},scale={2},density={3},volume={4},mass={5}",
193 LocalID, _size, Scale, _avatarDensity, _avatarVolume, RawMass); 299 LocalID, _size, Scale, _avatarDensity, _avatarVolume, RawMass);
194 300
195 PhysicsScene.TaintedObject("BSCharacter.setSize", delegate() 301 PhysicsScene.TaintedObject("BSCharacter.setSize", delegate()
196 { 302 {
197 BulletSimAPI.SetLocalScaling2(PhysShape.ptr, Scale); 303 if (PhysBody.HasPhysicalBody && PhysShape.HasPhysicalShape)
198 UpdatePhysicalMassProperties(RawMass); 304 {
305 PhysicsScene.PE.SetLocalScaling(PhysShape, Scale);
306 UpdatePhysicalMassProperties(RawMass, true);
307 // Make sure this change appears as a property update event
308 PhysicsScene.PE.PushUpdate(PhysBody);
309 }
199 }); 310 });
200 311
201 } 312 }
202 } 313 }
203 314
204 public override OMV.Vector3 Scale { get; set; }
205
206 public override PrimitiveBaseShape Shape 315 public override PrimitiveBaseShape Shape
207 { 316 {
208 set { BaseShape = value; } 317 set { BaseShape = value; }
@@ -219,6 +328,10 @@ public sealed class BSCharacter : BSPhysObject
219 public override bool Selected { 328 public override bool Selected {
220 set { _selected = value; } 329 set { _selected = value; }
221 } 330 }
331 public override bool IsSelected
332 {
333 get { return _selected; }
334 }
222 public override void CrossingFailure() { return; } 335 public override void CrossingFailure() { return; }
223 public override void link(PhysicsActor obj) { return; } 336 public override void link(PhysicsActor obj) { return; }
224 public override void delink() { return; } 337 public override void delink() { return; }
@@ -236,7 +349,8 @@ public sealed class BSCharacter : BSPhysObject
236 // Zero some other properties directly into the physics engine 349 // Zero some other properties directly into the physics engine
237 PhysicsScene.TaintedObject(inTaintTime, "BSCharacter.ZeroMotion", delegate() 350 PhysicsScene.TaintedObject(inTaintTime, "BSCharacter.ZeroMotion", delegate()
238 { 351 {
239 BulletSimAPI.ClearAllForces2(PhysBody.ptr); 352 if (PhysBody.HasPhysicalBody)
353 PhysicsScene.PE.ClearAllForces(PhysBody);
240 }); 354 });
241 } 355 }
242 public override void ZeroAngularMotion(bool inTaintTime) 356 public override void ZeroAngularMotion(bool inTaintTime)
@@ -245,10 +359,13 @@ public sealed class BSCharacter : BSPhysObject
245 359
246 PhysicsScene.TaintedObject(inTaintTime, "BSCharacter.ZeroMotion", delegate() 360 PhysicsScene.TaintedObject(inTaintTime, "BSCharacter.ZeroMotion", delegate()
247 { 361 {
248 BulletSimAPI.SetInterpolationAngularVelocity2(PhysBody.ptr, OMV.Vector3.Zero); 362 if (PhysBody.HasPhysicalBody)
249 BulletSimAPI.SetAngularVelocity2(PhysBody.ptr, OMV.Vector3.Zero); 363 {
250 // The next also get rid of applied linear force but the linear velocity is untouched. 364 PhysicsScene.PE.SetInterpolationAngularVelocity(PhysBody, OMV.Vector3.Zero);
251 BulletSimAPI.ClearForces2(PhysBody.ptr); 365 PhysicsScene.PE.SetAngularVelocity(PhysBody, OMV.Vector3.Zero);
366 // The next also get rid of applied linear force but the linear velocity is untouched.
367 PhysicsScene.PE.ClearForces(PhysBody);
368 }
252 }); 369 });
253 } 370 }
254 371
@@ -263,29 +380,31 @@ public sealed class BSCharacter : BSPhysObject
263 public override OMV.Vector3 Position { 380 public override OMV.Vector3 Position {
264 get { 381 get {
265 // Don't refetch the position because this function is called a zillion times 382 // Don't refetch the position because this function is called a zillion times
266 // _position = BulletSimAPI.GetObjectPosition2(Scene.World.ptr, LocalID); 383 // _position = PhysicsScene.PE.GetObjectPosition(Scene.World, LocalID);
267 return _position; 384 return _position;
268 } 385 }
269 set { 386 set {
270 _position = value; 387 _position = value;
271 PositionSanityCheck();
272 388
273 PhysicsScene.TaintedObject("BSCharacter.setPosition", delegate() 389 PhysicsScene.TaintedObject("BSCharacter.setPosition", delegate()
274 { 390 {
275 DetailLog("{0},BSCharacter.SetPosition,taint,pos={1},orient={2}", LocalID, _position, _orientation); 391 DetailLog("{0},BSCharacter.SetPosition,taint,pos={1},orient={2}", LocalID, _position, _orientation);
276 BulletSimAPI.SetTranslation2(PhysBody.ptr, _position, _orientation); 392 ForcePosition = _position;
277 }); 393 });
278 } 394 }
279 } 395 }
280 public override OMV.Vector3 ForcePosition { 396 public override OMV.Vector3 ForcePosition {
281 get { 397 get {
282 _position = BulletSimAPI.GetPosition2(PhysBody.ptr); 398 _position = PhysicsScene.PE.GetPosition(PhysBody);
283 return _position; 399 return _position;
284 } 400 }
285 set { 401 set {
286 _position = value; 402 _position = value;
287 PositionSanityCheck(); 403 if (PhysBody.HasPhysicalBody)
288 BulletSimAPI.SetTranslation2(PhysBody.ptr, _position, _orientation); 404 {
405 PositionSanityCheck();
406 PhysicsScene.PE.SetTranslation(PhysBody, _position, _orientation);
407 }
289 } 408 }
290 } 409 }
291 410
@@ -297,8 +416,17 @@ public sealed class BSCharacter : BSPhysObject
297 { 416 {
298 bool ret = false; 417 bool ret = false;
299 418
419 // TODO: check for out of bounds
420 if (!PhysicsScene.TerrainManager.IsWithinKnownTerrain(RawPosition))
421 {
422 // The character is out of the known/simulated area.
423 // Upper levels of code will handle the transition to other areas so, for
424 // the time, we just ignore the position.
425 return ret;
426 }
427
300 // If below the ground, move the avatar up 428 // If below the ground, move the avatar up
301 float terrainHeight = PhysicsScene.TerrainManager.GetTerrainHeightAtXYZ(_position); 429 float terrainHeight = PhysicsScene.TerrainManager.GetTerrainHeightAtXYZ(RawPosition);
302 if (Position.Z < terrainHeight) 430 if (Position.Z < terrainHeight)
303 { 431 {
304 DetailLog("{0},BSCharacter.PositionAdjustUnderGround,call,pos={1},terrain={2}", LocalID, _position, terrainHeight); 432 DetailLog("{0},BSCharacter.PositionAdjustUnderGround,call,pos={1},terrain={2}", LocalID, _position, terrainHeight);
@@ -307,7 +435,7 @@ public sealed class BSCharacter : BSPhysObject
307 } 435 }
308 if ((CurrentCollisionFlags & CollisionFlags.BS_FLOATS_ON_WATER) != 0) 436 if ((CurrentCollisionFlags & CollisionFlags.BS_FLOATS_ON_WATER) != 0)
309 { 437 {
310 float waterHeight = PhysicsScene.GetWaterLevelAtXYZ(_position); 438 float waterHeight = PhysicsScene.TerrainManager.GetWaterLevelAtXYZ(_position);
311 if (Position.Z < waterHeight) 439 if (Position.Z < waterHeight)
312 { 440 {
313 _position.Z = waterHeight; 441 _position.Z = waterHeight;
@@ -315,7 +443,6 @@ public sealed class BSCharacter : BSPhysObject
315 } 443 }
316 } 444 }
317 445
318 // TODO: check for out of bounds
319 return ret; 446 return ret;
320 } 447 }
321 448
@@ -332,7 +459,8 @@ public sealed class BSCharacter : BSPhysObject
332 PhysicsScene.TaintedObject(inTaintTime, "BSCharacter.PositionSanityCheck", delegate() 459 PhysicsScene.TaintedObject(inTaintTime, "BSCharacter.PositionSanityCheck", delegate()
333 { 460 {
334 DetailLog("{0},BSCharacter.PositionSanityCheck,taint,pos={1},orient={2}", LocalID, _position, _orientation); 461 DetailLog("{0},BSCharacter.PositionSanityCheck,taint,pos={1},orient={2}", LocalID, _position, _orientation);
335 BulletSimAPI.SetTranslation2(PhysBody.ptr, _position, _orientation); 462 if (PhysBody.HasPhysicalBody)
463 PhysicsScene.PE.SetTranslation(PhysBody, _position, _orientation);
336 }); 464 });
337 ret = true; 465 ret = true;
338 } 466 }
@@ -345,10 +473,10 @@ public sealed class BSCharacter : BSPhysObject
345 public override float RawMass { 473 public override float RawMass {
346 get {return _mass; } 474 get {return _mass; }
347 } 475 }
348 public override void UpdatePhysicalMassProperties(float physMass) 476 public override void UpdatePhysicalMassProperties(float physMass, bool inWorld)
349 { 477 {
350 OMV.Vector3 localInertia = BulletSimAPI.CalculateLocalInertia2(PhysShape.ptr, physMass); 478 OMV.Vector3 localInertia = PhysicsScene.PE.CalculateLocalInertia(PhysShape, physMass);
351 BulletSimAPI.SetMassProps2(PhysBody.ptr, physMass, localInertia); 479 PhysicsScene.PE.SetMassProps(PhysBody, physMass, localInertia);
352 } 480 }
353 481
354 public override OMV.Vector3 Force { 482 public override OMV.Vector3 Force {
@@ -359,7 +487,8 @@ public sealed class BSCharacter : BSPhysObject
359 PhysicsScene.TaintedObject("BSCharacter.SetForce", delegate() 487 PhysicsScene.TaintedObject("BSCharacter.SetForce", delegate()
360 { 488 {
361 DetailLog("{0},BSCharacter.setForce,taint,force={1}", LocalID, _force); 489 DetailLog("{0},BSCharacter.setForce,taint,force={1}", LocalID, _force);
362 BulletSimAPI.SetObjectForce2(PhysBody.ptr, _force); 490 if (PhysBody.HasPhysicalBody)
491 PhysicsScene.PE.SetObjectForce(PhysBody, _force);
363 }); 492 });
364 } 493 }
365 } 494 }
@@ -376,6 +505,36 @@ public sealed class BSCharacter : BSPhysObject
376 505
377 public override OMV.Vector3 GeometricCenter { get { return OMV.Vector3.Zero; } } 506 public override OMV.Vector3 GeometricCenter { get { return OMV.Vector3.Zero; } }
378 public override OMV.Vector3 CenterOfMass { get { return OMV.Vector3.Zero; } } 507 public override OMV.Vector3 CenterOfMass { get { return OMV.Vector3.Zero; } }
508
509 // Sets the target in the motor. This starts the changing of the avatar's velocity.
510 public override OMV.Vector3 TargetVelocity
511 {
512 get
513 {
514 return _velocityMotor.TargetValue;
515 }
516 set
517 {
518 DetailLog("{0},BSCharacter.setTargetVelocity,call,vel={1}", LocalID, value);
519 OMV.Vector3 targetVel = value;
520 if (_setAlwaysRun)
521 targetVel *= BSParam.AvatarAlwaysRunFactor;
522
523 PhysicsScene.TaintedObject("BSCharacter.setTargetVelocity", delegate()
524 {
525 _velocityMotor.Reset();
526 _velocityMotor.SetTarget(targetVel);
527 _velocityMotor.SetCurrent(_velocity);
528 _velocityMotor.Enabled = true;
529 });
530 }
531 }
532 public override OMV.Vector3 RawVelocity
533 {
534 get { return _velocity; }
535 set { _velocity = value; }
536 }
537 // Directly setting velocity means this is what the user really wants now.
379 public override OMV.Vector3 Velocity { 538 public override OMV.Vector3 Velocity {
380 get { return _velocity; } 539 get { return _velocity; }
381 set { 540 set {
@@ -383,6 +542,12 @@ public sealed class BSCharacter : BSPhysObject
383 // m_log.DebugFormat("{0}: set velocity = {1}", LogHeader, _velocity); 542 // m_log.DebugFormat("{0}: set velocity = {1}", LogHeader, _velocity);
384 PhysicsScene.TaintedObject("BSCharacter.setVelocity", delegate() 543 PhysicsScene.TaintedObject("BSCharacter.setVelocity", delegate()
385 { 544 {
545 _velocityMotor.Reset();
546 _velocityMotor.SetCurrent(_velocity);
547 _velocityMotor.SetTarget(_velocity);
548 // Even though the motor is initialized, it's not used and the velocity goes straight into the avatar.
549 _velocityMotor.Enabled = false;
550
386 DetailLog("{0},BSCharacter.setVelocity,taint,vel={1}", LocalID, _velocity); 551 DetailLog("{0},BSCharacter.setVelocity,taint,vel={1}", LocalID, _velocity);
387 ForceVelocity = _velocity; 552 ForceVelocity = _velocity;
388 }); 553 });
@@ -391,30 +556,32 @@ public sealed class BSCharacter : BSPhysObject
391 public override OMV.Vector3 ForceVelocity { 556 public override OMV.Vector3 ForceVelocity {
392 get { return _velocity; } 557 get { return _velocity; }
393 set { 558 set {
559 PhysicsScene.AssertInTaintTime("BSCharacter.ForceVelocity");
560
561 _velocity = value;
394 // Depending on whether the avatar is moving or not, change the friction 562 // Depending on whether the avatar is moving or not, change the friction
395 // to keep the avatar from slipping around 563 // to keep the avatar from slipping around
396 if (_velocity.Length() == 0) 564 if (_velocity.Length() == 0)
397 { 565 {
398 if (_currentFriction != PhysicsScene.Params.avatarStandingFriction) 566 if (_currentFriction != BSParam.AvatarStandingFriction)
399 { 567 {
400 _currentFriction = PhysicsScene.Params.avatarStandingFriction; 568 _currentFriction = BSParam.AvatarStandingFriction;
401 BulletSimAPI.SetFriction2(PhysBody.ptr, _currentFriction); 569 if (PhysBody.HasPhysicalBody)
570 PhysicsScene.PE.SetFriction(PhysBody, _currentFriction);
402 } 571 }
403 } 572 }
404 else 573 else
405 { 574 {
406 if (_currentFriction != PhysicsScene.Params.avatarFriction) 575 if (_currentFriction != BSParam.AvatarFriction)
407 { 576 {
408 _currentFriction = PhysicsScene.Params.avatarFriction; 577 _currentFriction = BSParam.AvatarFriction;
409 BulletSimAPI.SetFriction2(PhysBody.ptr, _currentFriction); 578 if (PhysBody.HasPhysicalBody)
579 PhysicsScene.PE.SetFriction(PhysBody, _currentFriction);
410 } 580 }
411 } 581 }
412 _velocity = value;
413 // Remember the set velocity so we can suppress the reduction by friction, ...
414 _appliedVelocity = value;
415 582
416 BulletSimAPI.SetLinearVelocity2(PhysBody.ptr, _velocity); 583 PhysicsScene.PE.SetLinearVelocity(PhysBody, _velocity);
417 BulletSimAPI.Activate2(PhysBody.ptr, true); 584 PhysicsScene.PE.Activate(PhysBody, true);
418 } 585 }
419 } 586 }
420 public override OMV.Vector3 Torque { 587 public override OMV.Vector3 Torque {
@@ -439,13 +606,16 @@ public sealed class BSCharacter : BSPhysObject
439 public override OMV.Quaternion Orientation { 606 public override OMV.Quaternion Orientation {
440 get { return _orientation; } 607 get { return _orientation; }
441 set { 608 set {
442 _orientation = value; 609 // Orientation is set zillions of times when an avatar is walking. It's like
443 // m_log.DebugFormat("{0}: set orientation to {1}", LogHeader, _orientation); 610 // the viewer doesn't trust us.
444 PhysicsScene.TaintedObject("BSCharacter.setOrientation", delegate() 611 if (_orientation != value)
445 { 612 {
446 // _position = BulletSimAPI.GetPosition2(BSBody.ptr); 613 _orientation = value;
447 BulletSimAPI.SetTranslation2(PhysBody.ptr, _position, _orientation); 614 PhysicsScene.TaintedObject("BSCharacter.setOrientation", delegate()
448 }); 615 {
616 ForceOrientation = _orientation;
617 });
618 }
449 } 619 }
450 } 620 }
451 // Go directly to Bullet to get/set the value. 621 // Go directly to Bullet to get/set the value.
@@ -453,13 +623,17 @@ public sealed class BSCharacter : BSPhysObject
453 { 623 {
454 get 624 get
455 { 625 {
456 _orientation = BulletSimAPI.GetOrientation2(PhysBody.ptr); 626 _orientation = PhysicsScene.PE.GetOrientation(PhysBody);
457 return _orientation; 627 return _orientation;
458 } 628 }
459 set 629 set
460 { 630 {
461 _orientation = value; 631 _orientation = value;
462 BulletSimAPI.SetTranslation2(PhysBody.ptr, _position, _orientation); 632 if (PhysBody.HasPhysicalBody)
633 {
634 // _position = PhysicsScene.PE.GetPosition(BSBody);
635 PhysicsScene.PE.SetTranslation(PhysBody, _position, _orientation);
636 }
463 } 637 }
464 } 638 }
465 public override int PhysicsActorType { 639 public override int PhysicsActorType {
@@ -482,6 +656,7 @@ public sealed class BSCharacter : BSPhysObject
482 get { return _flying; } 656 get { return _flying; }
483 set { 657 set {
484 _flying = value; 658 _flying = value;
659
485 // simulate flying by changing the effect of gravity 660 // simulate flying by changing the effect of gravity
486 Buoyancy = ComputeBuoyancyFromFlying(_flying); 661 Buoyancy = ComputeBuoyancyFromFlying(_flying);
487 } 662 }
@@ -500,27 +675,18 @@ public sealed class BSCharacter : BSPhysObject
500 get { return _throttleUpdates; } 675 get { return _throttleUpdates; }
501 set { _throttleUpdates = value; } 676 set { _throttleUpdates = value; }
502 } 677 }
503 public override bool IsColliding {
504 get { return (CollidingStep == PhysicsScene.SimulationStep); }
505 set { _isColliding = value; }
506 }
507 public override bool CollidingGround {
508 get { return (CollidingGroundStep == PhysicsScene.SimulationStep); }
509 set { CollidingGround = value; }
510 }
511 public override bool CollidingObj {
512 get { return _collidingObj; }
513 set { _collidingObj = value; }
514 }
515 public override bool FloatOnWater { 678 public override bool FloatOnWater {
516 set { 679 set {
517 _floatOnWater = value; 680 _floatOnWater = value;
518 PhysicsScene.TaintedObject("BSCharacter.setFloatOnWater", delegate() 681 PhysicsScene.TaintedObject("BSCharacter.setFloatOnWater", delegate()
519 { 682 {
520 if (_floatOnWater) 683 if (PhysBody.HasPhysicalBody)
521 CurrentCollisionFlags = BulletSimAPI.AddToCollisionFlags2(PhysBody.ptr, CollisionFlags.BS_FLOATS_ON_WATER); 684 {
522 else 685 if (_floatOnWater)
523 CurrentCollisionFlags = BulletSimAPI.RemoveFromCollisionFlags2(PhysBody.ptr, CollisionFlags.BS_FLOATS_ON_WATER); 686 CurrentCollisionFlags = PhysicsScene.PE.AddToCollisionFlags(PhysBody, CollisionFlags.BS_FLOATS_ON_WATER);
687 else
688 CurrentCollisionFlags = PhysicsScene.PE.RemoveFromCollisionFlags(PhysBody, CollisionFlags.BS_FLOATS_ON_WATER);
689 }
524 }); 690 });
525 } 691 }
526 } 692 }
@@ -549,11 +715,15 @@ public sealed class BSCharacter : BSPhysObject
549 } 715 }
550 public override float ForceBuoyancy { 716 public override float ForceBuoyancy {
551 get { return _buoyancy; } 717 get { return _buoyancy; }
552 set { _buoyancy = value; 718 set {
719 PhysicsScene.AssertInTaintTime("BSCharacter.ForceBuoyancy");
720
721 _buoyancy = value;
553 DetailLog("{0},BSCharacter.setForceBuoyancy,taint,buoy={1}", LocalID, _buoyancy); 722 DetailLog("{0},BSCharacter.setForceBuoyancy,taint,buoy={1}", LocalID, _buoyancy);
554 // Buoyancy is faked by changing the gravity applied to the object 723 // Buoyancy is faked by changing the gravity applied to the object
555 float grav = PhysicsScene.Params.gravity * (1f - _buoyancy); 724 float grav = PhysicsScene.Params.gravity * (1f - _buoyancy);
556 BulletSimAPI.SetGravity2(PhysBody.ptr, new OMV.Vector3(0f, 0f, grav)); 725 if (PhysBody.HasPhysicalBody)
726 PhysicsScene.PE.SetGravity(PhysBody, new OMV.Vector3(0f, 0f, grav));
557 } 727 }
558 } 728 }
559 729
@@ -589,24 +759,40 @@ public sealed class BSCharacter : BSPhysObject
589 public override float APIDStrength { set { return; } } 759 public override float APIDStrength { set { return; } }
590 public override float APIDDamping { set { return; } } 760 public override float APIDDamping { set { return; } }
591 761
592 public override void AddForce(OMV.Vector3 force, bool pushforce) { 762 public override void AddForce(OMV.Vector3 force, bool pushforce)
763 {
764 // Since this force is being applied in only one step, make this a force per second.
765 OMV.Vector3 addForce = force / PhysicsScene.LastTimeStep;
766 AddForce(addForce, pushforce, false);
767 }
768 private void AddForce(OMV.Vector3 force, bool pushforce, bool inTaintTime) {
593 if (force.IsFinite()) 769 if (force.IsFinite())
594 { 770 {
595 _force.X += force.X; 771 float magnitude = force.Length();
596 _force.Y += force.Y; 772 if (magnitude > BSParam.MaxAddForceMagnitude)
597 _force.Z += force.Z;
598 // m_log.DebugFormat("{0}: AddForce. adding={1}, newForce={2}", LogHeader, force, _force);
599 PhysicsScene.TaintedObject("BSCharacter.AddForce", delegate()
600 { 773 {
601 DetailLog("{0},BSCharacter.setAddForce,taint,addedForce={1}", LocalID, _force); 774 // Force has a limit
602 BulletSimAPI.SetObjectForce2(PhysBody.ptr, _force); 775 force = force / magnitude * BSParam.MaxAddForceMagnitude;
776 }
777
778 OMV.Vector3 addForce = force;
779 // DetailLog("{0},BSCharacter.addForce,call,force={1}", LocalID, addForce);
780
781 PhysicsScene.TaintedObject(inTaintTime, "BSCharacter.AddForce", delegate()
782 {
783 // Bullet adds this central force to the total force for this tick
784 // DetailLog("{0},BSCharacter.addForce,taint,force={1}", LocalID, addForce);
785 if (PhysBody.HasPhysicalBody)
786 {
787 PhysicsScene.PE.ApplyCentralForce(PhysBody, addForce);
788 }
603 }); 789 });
604 } 790 }
605 else 791 else
606 { 792 {
607 m_log.ErrorFormat("{0}: Got a NaN force applied to a Character", LogHeader); 793 m_log.WarnFormat("{0}: Got a NaN force applied to a character. LocalID={1}", LogHeader, LocalID);
794 return;
608 } 795 }
609 //m_lastUpdateSent = false;
610 } 796 }
611 797
612 public override void AddAngularForce(OMV.Vector3 force, bool pushforce) { 798 public override void AddAngularForce(OMV.Vector3 force, bool pushforce) {
@@ -614,24 +800,31 @@ public sealed class BSCharacter : BSPhysObject
614 public override void SetMomentum(OMV.Vector3 momentum) { 800 public override void SetMomentum(OMV.Vector3 momentum) {
615 } 801 }
616 802
617 private void ComputeAvatarScale(OMV.Vector3 size) 803 private OMV.Vector3 ComputeAvatarScale(OMV.Vector3 size)
618 { 804 {
619 // The 'size' given by the simulator is the mid-point of the avatar 805 OMV.Vector3 newScale;
620 // and X and Y are unspecified. 806
621 807 // Bullet's capsule total height is the "passed height + radius * 2";
622 OMV.Vector3 newScale = size; 808 // The base capsule is 1 diameter and 2 height (passed radius=0.5, passed height = 1)
623 // newScale.X = PhysicsScene.Params.avatarCapsuleWidth; 809 // The number we pass in for 'scaling' is the multiplier to get that base
624 // newScale.Y = PhysicsScene.Params.avatarCapsuleDepth; 810 // shape to be the size desired.
625 811 // So, when creating the scale for the avatar height, we take the passed height
626 // From the total height, remove the capsule half spheres that are at each end 812 // (size.Z) and remove the caps.
627 // The 1.15f came from ODE. Not sure what this factors in. 813 // Another oddity of the Bullet capsule implementation is that it presumes the Y
628 // newScale.Z = (size.Z * 1.15f) - (newScale.X + newScale.Y); 814 // dimension is the radius of the capsule. Even though some of the code allows
815 // for a asymmetrical capsule, other parts of the code presume it is cylindrical.
816
817 // Scale is multiplier of radius with one of "0.5"
818 newScale.X = size.X / 2f;
819 newScale.Y = size.Y / 2f;
629 820
630 // The total scale height is the central cylindar plus the caps on the two ends. 821 // The total scale height is the central cylindar plus the caps on the two ends.
631 newScale.Z = size.Z + (Math.Min(size.X, size.Y) * 2f); 822 newScale.Z = (size.Z + (Math.Min(size.X, size.Y) * 2)) / 2f;
823 // If smaller than the endcaps, just fake like we're almost that small
824 if (newScale.Z < 0)
825 newScale.Z = 0.1f;
632 826
633 // Convert diameters to radii and height to half height -- the way Bullet expects it. 827 return newScale;
634 Scale = newScale / 2f;
635 } 828 }
636 829
637 // set _avatarVolume and _mass based on capsule size, _density and Scale 830 // set _avatarVolume and _mass based on capsule size, _density and Scale
@@ -639,14 +832,14 @@ public sealed class BSCharacter : BSPhysObject
639 { 832 {
640 _avatarVolume = (float)( 833 _avatarVolume = (float)(
641 Math.PI 834 Math.PI
642 * Scale.X 835 * Size.X / 2f
643 * Scale.Y // the area of capsule cylinder 836 * Size.Y / 2f // the area of capsule cylinder
644 * Scale.Z // times height of capsule cylinder 837 * Size.Z // times height of capsule cylinder
645 + 1.33333333f 838 + 1.33333333f
646 * Math.PI 839 * Math.PI
647 * Scale.X 840 * Size.X / 2f
648 * Math.Min(Scale.X, Scale.Y) 841 * Math.Min(Size.X, Size.Y) / 2
649 * Scale.Y // plus the volume of the capsule end caps 842 * Size.Y / 2f // plus the volume of the capsule end caps
650 ); 843 );
651 _mass = _avatarDensity * _avatarVolume; 844 _mass = _avatarDensity * _avatarVolume;
652 } 845 }
@@ -660,6 +853,7 @@ public sealed class BSCharacter : BSPhysObject
660 _velocity = entprop.Velocity; 853 _velocity = entprop.Velocity;
661 _acceleration = entprop.Acceleration; 854 _acceleration = entprop.Acceleration;
662 _rotationalVelocity = entprop.RotationalVelocity; 855 _rotationalVelocity = entprop.RotationalVelocity;
856
663 // Do some sanity checking for the avatar. Make sure it's above ground and inbounds. 857 // Do some sanity checking for the avatar. Make sure it's above ground and inbounds.
664 PositionSanityCheck(true); 858 PositionSanityCheck(true);
665 859
@@ -667,17 +861,8 @@ public sealed class BSCharacter : BSPhysObject
667 LastEntityProperties = CurrentEntityProperties; 861 LastEntityProperties = CurrentEntityProperties;
668 CurrentEntityProperties = entprop; 862 CurrentEntityProperties = entprop;
669 863
670 if (entprop.Velocity != LastEntityProperties.Velocity)
671 {
672 // Changes in the velocity are suppressed in avatars.
673 // That's just the way they are defined.
674 OMV.Vector3 avVel = new OMV.Vector3(_appliedVelocity.X, _appliedVelocity.Y, entprop.Velocity.Z);
675 _velocity = avVel;
676 BulletSimAPI.SetLinearVelocity2(PhysBody.ptr, avVel);
677 }
678
679 // Tell the linkset about value changes 864 // Tell the linkset about value changes
680 Linkset.UpdateProperties(this); 865 Linkset.UpdateProperties(this, true);
681 866
682 // Avatars don't report their changes the usual way. Changes are checked for in the heartbeat loop. 867 // Avatars don't report their changes the usual way. Changes are checked for in the heartbeat loop.
683 // base.RequestPhysicsterseUpdate(); 868 // base.RequestPhysicsterseUpdate();
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSConstraint.cs b/OpenSim/Region/Physics/BulletSPlugin/BSConstraint.cs
index 65fac00..b813974 100755
--- a/OpenSim/Region/Physics/BulletSPlugin/BSConstraint.cs
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSConstraint.cs
@@ -36,7 +36,8 @@ public abstract class BSConstraint : IDisposable
36{ 36{
37 private static string LogHeader = "[BULLETSIM CONSTRAINT]"; 37 private static string LogHeader = "[BULLETSIM CONSTRAINT]";
38 38
39 protected BulletSim m_world; 39 protected BulletWorld m_world;
40 protected BSScene PhysicsScene;
40 protected BulletBody m_body1; 41 protected BulletBody m_body1;
41 protected BulletBody m_body2; 42 protected BulletBody m_body2;
42 protected BulletConstraint m_constraint; 43 protected BulletConstraint m_constraint;
@@ -48,8 +49,10 @@ public abstract class BSConstraint : IDisposable
48 public abstract ConstraintType Type { get; } 49 public abstract ConstraintType Type { get; }
49 public bool IsEnabled { get { return m_enabled; } } 50 public bool IsEnabled { get { return m_enabled; } }
50 51
51 public BSConstraint() 52 public BSConstraint(BulletWorld world)
52 { 53 {
54 m_world = world;
55 PhysicsScene = m_world.physicsScene;
53 } 56 }
54 57
55 public virtual void Dispose() 58 public virtual void Dispose()
@@ -57,15 +60,15 @@ public abstract class BSConstraint : IDisposable
57 if (m_enabled) 60 if (m_enabled)
58 { 61 {
59 m_enabled = false; 62 m_enabled = false;
60 if (m_constraint.ptr != IntPtr.Zero) 63 if (m_constraint.HasPhysicalConstraint)
61 { 64 {
62 bool success = BulletSimAPI.DestroyConstraint2(m_world.ptr, m_constraint.ptr); 65 bool success = PhysicsScene.PE.DestroyConstraint(m_world, m_constraint);
63 m_world.physicsScene.DetailLog("{0},BSConstraint.Dispose,taint,id1={1},body1={2},id2={3},body2={4},success={5}", 66 m_world.physicsScene.DetailLog("{0},BSConstraint.Dispose,taint,id1={1},body1={2},id2={3},body2={4},success={5}",
64 BSScene.DetailLogZero, 67 BSScene.DetailLogZero,
65 m_body1.ID, m_body1.ptr.ToString("X"), 68 m_body1.ID, m_body1.AddrString,
66 m_body2.ID, m_body2.ptr.ToString("X"), 69 m_body2.ID, m_body2.AddrString,
67 success); 70 success);
68 m_constraint.ptr = System.IntPtr.Zero; 71 m_constraint.Clear();
69 } 72 }
70 } 73 }
71 } 74 }
@@ -74,7 +77,7 @@ public abstract class BSConstraint : IDisposable
74 { 77 {
75 bool ret = false; 78 bool ret = false;
76 if (m_enabled) 79 if (m_enabled)
77 ret = BulletSimAPI.SetLinearLimits2(m_constraint.ptr, low, high); 80 ret = PhysicsScene.PE.SetLinearLimits(m_constraint, low, high);
78 return ret; 81 return ret;
79 } 82 }
80 83
@@ -82,7 +85,7 @@ public abstract class BSConstraint : IDisposable
82 { 85 {
83 bool ret = false; 86 bool ret = false;
84 if (m_enabled) 87 if (m_enabled)
85 ret = BulletSimAPI.SetAngularLimits2(m_constraint.ptr, low, high); 88 ret = PhysicsScene.PE.SetAngularLimits(m_constraint, low, high);
86 return ret; 89 return ret;
87 } 90 }
88 91
@@ -91,7 +94,7 @@ public abstract class BSConstraint : IDisposable
91 bool ret = false; 94 bool ret = false;
92 if (m_enabled) 95 if (m_enabled)
93 { 96 {
94 BulletSimAPI.SetConstraintNumSolverIterations2(m_constraint.ptr, cnt); 97 PhysicsScene.PE.SetConstraintNumSolverIterations(m_constraint, cnt);
95 ret = true; 98 ret = true;
96 } 99 }
97 return ret; 100 return ret;
@@ -103,7 +106,7 @@ public abstract class BSConstraint : IDisposable
103 if (m_enabled) 106 if (m_enabled)
104 { 107 {
105 // Recompute the internal transforms 108 // Recompute the internal transforms
106 BulletSimAPI.CalculateTransforms2(m_constraint.ptr); 109 PhysicsScene.PE.CalculateTransforms(m_constraint);
107 ret = true; 110 ret = true;
108 } 111 }
109 return ret; 112 return ret;
@@ -122,7 +125,7 @@ public abstract class BSConstraint : IDisposable
122 // Setting an object's mass to zero (making it static like when it's selected) 125 // Setting an object's mass to zero (making it static like when it's selected)
123 // automatically disables the constraints. 126 // automatically disables the constraints.
124 // If the link is enabled, be sure to set the constraint itself to enabled. 127 // If the link is enabled, be sure to set the constraint itself to enabled.
125 BulletSimAPI.SetConstraintEnable2(m_constraint.ptr, m_world.physicsScene.NumericBool(true)); 128 PhysicsScene.PE.SetConstraintEnable(m_constraint, BSParam.NumericBool(true));
126 } 129 }
127 else 130 else
128 { 131 {
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSConstraint6Dof.cs b/OpenSim/Region/Physics/BulletSPlugin/BSConstraint6Dof.cs
index 23ef052..ecb1b32 100755
--- a/OpenSim/Region/Physics/BulletSPlugin/BSConstraint6Dof.cs
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSConstraint6Dof.cs
@@ -39,51 +39,49 @@ public sealed class BSConstraint6Dof : BSConstraint
39 public override ConstraintType Type { get { return ConstraintType.D6_CONSTRAINT_TYPE; } } 39 public override ConstraintType Type { get { return ConstraintType.D6_CONSTRAINT_TYPE; } }
40 40
41 // Create a btGeneric6DofConstraint 41 // Create a btGeneric6DofConstraint
42 public BSConstraint6Dof(BulletSim world, BulletBody obj1, BulletBody obj2, 42 public BSConstraint6Dof(BulletWorld world, BulletBody obj1, BulletBody obj2,
43 Vector3 frame1, Quaternion frame1rot, 43 Vector3 frame1, Quaternion frame1rot,
44 Vector3 frame2, Quaternion frame2rot, 44 Vector3 frame2, Quaternion frame2rot,
45 bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies) 45 bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies)
46 : base(world)
46 { 47 {
47 m_world = world;
48 m_body1 = obj1; 48 m_body1 = obj1;
49 m_body2 = obj2; 49 m_body2 = obj2;
50 m_constraint = new BulletConstraint( 50 m_constraint = PhysicsScene.PE.Create6DofConstraint(m_world, m_body1, m_body2,
51 BulletSimAPI.Create6DofConstraint2(m_world.ptr, m_body1.ptr, m_body2.ptr,
52 frame1, frame1rot, 51 frame1, frame1rot,
53 frame2, frame2rot, 52 frame2, frame2rot,
54 useLinearReferenceFrameA, disableCollisionsBetweenLinkedBodies)); 53 useLinearReferenceFrameA, disableCollisionsBetweenLinkedBodies);
55 m_enabled = true; 54 m_enabled = true;
56 world.physicsScene.DetailLog("{0},BS6DofConstraint,createFrame,wID={1}, rID={2}, rBody={3}, cID={4}, cBody={5}", 55 world.physicsScene.DetailLog("{0},BS6DofConstraint,createFrame,wID={1}, rID={2}, rBody={3}, cID={4}, cBody={5}",
57 BSScene.DetailLogZero, world.worldID, 56 BSScene.DetailLogZero, world.worldID,
58 obj1.ID, obj1.ptr.ToString("X"), obj2.ID, obj2.ptr.ToString("X")); 57 obj1.ID, obj1.AddrString, obj2.ID, obj2.AddrString);
59 } 58 }
60 59
61 public BSConstraint6Dof(BulletSim world, BulletBody obj1, BulletBody obj2, 60 public BSConstraint6Dof(BulletWorld world, BulletBody obj1, BulletBody obj2,
62 Vector3 joinPoint, 61 Vector3 joinPoint,
63 bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies) 62 bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies)
63 : base(world)
64 { 64 {
65 m_world = world;
66 m_body1 = obj1; 65 m_body1 = obj1;
67 m_body2 = obj2; 66 m_body2 = obj2;
68 if (obj1.ptr == IntPtr.Zero || obj2.ptr == IntPtr.Zero) 67 if (!obj1.HasPhysicalBody || !obj2.HasPhysicalBody)
69 { 68 {
70 world.physicsScene.DetailLog("{0},BS6DOFConstraint,badBodyPtr,wID={1}, rID={2}, rBody={3}, cID={4}, cBody={5}", 69 world.physicsScene.DetailLog("{0},BS6DOFConstraint,badBodyPtr,wID={1}, rID={2}, rBody={3}, cID={4}, cBody={5}",
71 BSScene.DetailLogZero, world.worldID, 70 BSScene.DetailLogZero, world.worldID,
72 obj1.ID, obj1.ptr.ToString("X"), obj2.ID, obj2.ptr.ToString("X")); 71 obj1.ID, obj1.AddrString, obj2.ID, obj2.AddrString);
73 world.physicsScene.Logger.ErrorFormat("{0} Attempt to build 6DOF constraint with missing bodies: wID={1}, rID={2}, rBody={3}, cID={4}, cBody={5}", 72 world.physicsScene.Logger.ErrorFormat("{0} Attempt to build 6DOF constraint with missing bodies: wID={1}, rID={2}, rBody={3}, cID={4}, cBody={5}",
74 LogHeader, world.worldID, obj1.ID, obj1.ptr.ToString("X"), obj2.ID, obj2.ptr.ToString("X")); 73 LogHeader, world.worldID, obj1.ID, obj1.AddrString, obj2.ID, obj2.AddrString);
75 m_enabled = false; 74 m_enabled = false;
76 } 75 }
77 else 76 else
78 { 77 {
79 m_constraint = new BulletConstraint( 78 m_constraint = PhysicsScene.PE.Create6DofConstraintToPoint(m_world, m_body1, m_body2,
80 BulletSimAPI.Create6DofConstraintToPoint2(m_world.ptr, m_body1.ptr, m_body2.ptr,
81 joinPoint, 79 joinPoint,
82 useLinearReferenceFrameA, disableCollisionsBetweenLinkedBodies)); 80 useLinearReferenceFrameA, disableCollisionsBetweenLinkedBodies);
83 world.physicsScene.DetailLog("{0},BS6DofConstraint,createMidPoint,wID={1}, csrt={2}, rID={3}, rBody={4}, cID={5}, cBody={6}", 81 PhysicsScene.DetailLog("{0},BS6DofConstraint,createMidPoint,wID={1}, csrt={2}, rID={3}, rBody={4}, cID={5}, cBody={6}",
84 BSScene.DetailLogZero, world.worldID, m_constraint.ptr.ToString("X"), 82 BSScene.DetailLogZero, world.worldID, m_constraint.AddrString,
85 obj1.ID, obj1.ptr.ToString("X"), obj2.ID, obj2.ptr.ToString("X")); 83 obj1.ID, obj1.AddrString, obj2.ID, obj2.AddrString);
86 if (m_constraint.ptr == IntPtr.Zero) 84 if (!m_constraint.HasPhysicalConstraint)
87 { 85 {
88 world.physicsScene.Logger.ErrorFormat("{0} Failed creation of 6Dof constraint. rootID={1}, childID={2}", 86 world.physicsScene.Logger.ErrorFormat("{0} Failed creation of 6Dof constraint. rootID={1}, childID={2}",
89 LogHeader, obj1.ID, obj2.ID); 87 LogHeader, obj1.ID, obj2.ID);
@@ -101,7 +99,7 @@ public sealed class BSConstraint6Dof : BSConstraint
101 bool ret = false; 99 bool ret = false;
102 if (m_enabled) 100 if (m_enabled)
103 { 101 {
104 BulletSimAPI.SetFrames2(m_constraint.ptr, frameA, frameArot, frameB, frameBrot); 102 PhysicsScene.PE.SetFrames(m_constraint, frameA, frameArot, frameB, frameBrot);
105 ret = true; 103 ret = true;
106 } 104 }
107 return ret; 105 return ret;
@@ -112,9 +110,9 @@ public sealed class BSConstraint6Dof : BSConstraint
112 bool ret = false; 110 bool ret = false;
113 if (m_enabled) 111 if (m_enabled)
114 { 112 {
115 BulletSimAPI.SetConstraintParam2(m_constraint.ptr, ConstraintParams.BT_CONSTRAINT_STOP_CFM, cfm, ConstraintParamAxis.AXIS_ALL); 113 PhysicsScene.PE.SetConstraintParam(m_constraint, ConstraintParams.BT_CONSTRAINT_STOP_CFM, cfm, ConstraintParamAxis.AXIS_ALL);
116 BulletSimAPI.SetConstraintParam2(m_constraint.ptr, ConstraintParams.BT_CONSTRAINT_STOP_ERP, erp, ConstraintParamAxis.AXIS_ALL); 114 PhysicsScene.PE.SetConstraintParam(m_constraint, ConstraintParams.BT_CONSTRAINT_STOP_ERP, erp, ConstraintParamAxis.AXIS_ALL);
117 BulletSimAPI.SetConstraintParam2(m_constraint.ptr, ConstraintParams.BT_CONSTRAINT_CFM, cfm, ConstraintParamAxis.AXIS_ALL); 115 PhysicsScene.PE.SetConstraintParam(m_constraint, ConstraintParams.BT_CONSTRAINT_CFM, cfm, ConstraintParamAxis.AXIS_ALL);
118 ret = true; 116 ret = true;
119 } 117 }
120 return ret; 118 return ret;
@@ -125,7 +123,7 @@ public sealed class BSConstraint6Dof : BSConstraint
125 bool ret = false; 123 bool ret = false;
126 float onOff = useOffset ? ConfigurationParameters.numericTrue : ConfigurationParameters.numericFalse; 124 float onOff = useOffset ? ConfigurationParameters.numericTrue : ConfigurationParameters.numericFalse;
127 if (m_enabled) 125 if (m_enabled)
128 ret = BulletSimAPI.UseFrameOffset2(m_constraint.ptr, onOff); 126 ret = PhysicsScene.PE.UseFrameOffset(m_constraint, onOff);
129 return ret; 127 return ret;
130 } 128 }
131 129
@@ -135,7 +133,7 @@ public sealed class BSConstraint6Dof : BSConstraint
135 float onOff = enable ? ConfigurationParameters.numericTrue : ConfigurationParameters.numericFalse; 133 float onOff = enable ? ConfigurationParameters.numericTrue : ConfigurationParameters.numericFalse;
136 if (m_enabled) 134 if (m_enabled)
137 { 135 {
138 ret = BulletSimAPI.TranslationalLimitMotor2(m_constraint.ptr, onOff, targetVelocity, maxMotorForce); 136 ret = PhysicsScene.PE.TranslationalLimitMotor(m_constraint, onOff, targetVelocity, maxMotorForce);
139 m_world.physicsScene.DetailLog("{0},BS6DOFConstraint,TransLimitMotor,enable={1},vel={2},maxForce={3}", 137 m_world.physicsScene.DetailLog("{0},BS6DOFConstraint,TransLimitMotor,enable={1},vel={2},maxForce={3}",
140 BSScene.DetailLogZero, enable, targetVelocity, maxMotorForce); 138 BSScene.DetailLogZero, enable, targetVelocity, maxMotorForce);
141 } 139 }
@@ -146,7 +144,7 @@ public sealed class BSConstraint6Dof : BSConstraint
146 { 144 {
147 bool ret = false; 145 bool ret = false;
148 if (m_enabled) 146 if (m_enabled)
149 ret = BulletSimAPI.SetBreakingImpulseThreshold2(m_constraint.ptr, threshold); 147 ret = PhysicsScene.PE.SetBreakingImpulseThreshold(m_constraint, threshold);
150 return ret; 148 return ret;
151 } 149 }
152} 150}
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSConstraintCollection.cs b/OpenSim/Region/Physics/BulletSPlugin/BSConstraintCollection.cs
index a9fd826..2aeff25 100755
--- a/OpenSim/Region/Physics/BulletSPlugin/BSConstraintCollection.cs
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSConstraintCollection.cs
@@ -41,9 +41,9 @@ public sealed class BSConstraintCollection : IDisposable
41 delegate bool ConstraintAction(BSConstraint constrain); 41 delegate bool ConstraintAction(BSConstraint constrain);
42 42
43 private List<BSConstraint> m_constraints; 43 private List<BSConstraint> m_constraints;
44 private BulletSim m_world; 44 private BulletWorld m_world;
45 45
46 public BSConstraintCollection(BulletSim world) 46 public BSConstraintCollection(BulletWorld world)
47 { 47 {
48 m_world = world; 48 m_world = world;
49 m_constraints = new List<BSConstraint>(); 49 m_constraints = new List<BSConstraint>();
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSConstraintHinge.cs b/OpenSim/Region/Physics/BulletSPlugin/BSConstraintHinge.cs
index ed3ffa7..7714a03 100755
--- a/OpenSim/Region/Physics/BulletSPlugin/BSConstraintHinge.cs
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSConstraintHinge.cs
@@ -36,19 +36,17 @@ public sealed class BSConstraintHinge : BSConstraint
36{ 36{
37 public override ConstraintType Type { get { return ConstraintType.HINGE_CONSTRAINT_TYPE; } } 37 public override ConstraintType Type { get { return ConstraintType.HINGE_CONSTRAINT_TYPE; } }
38 38
39 public BSConstraintHinge(BulletSim world, BulletBody obj1, BulletBody obj2, 39 public BSConstraintHinge(BulletWorld world, BulletBody obj1, BulletBody obj2,
40 Vector3 pivotInA, Vector3 pivotInB, 40 Vector3 pivotInA, Vector3 pivotInB,
41 Vector3 axisInA, Vector3 axisInB, 41 Vector3 axisInA, Vector3 axisInB,
42 bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies) 42 bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies)
43 : base(world)
43 { 44 {
44 m_world = world;
45 m_body1 = obj1; 45 m_body1 = obj1;
46 m_body2 = obj2; 46 m_body2 = obj2;
47 m_constraint = new BulletConstraint( 47 m_constraint = PhysicsScene.PE.CreateHingeConstraint(world, obj1, obj2,
48 BulletSimAPI.CreateHingeConstraint2(m_world.ptr, m_body1.ptr, m_body2.ptr, 48 pivotInA, pivotInB, axisInA, axisInB,
49 pivotInA, pivotInB, 49 useLinearReferenceFrameA, disableCollisionsBetweenLinkedBodies);
50 axisInA, axisInB,
51 useLinearReferenceFrameA, disableCollisionsBetweenLinkedBodies));
52 m_enabled = true; 50 m_enabled = true;
53 } 51 }
54 52
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSDynamics.cs b/OpenSim/Region/Physics/BulletSPlugin/BSDynamics.cs
index dbc9039..e434412 100644
--- a/OpenSim/Region/Physics/BulletSPlugin/BSDynamics.cs
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSDynamics.cs
@@ -24,30 +24,17 @@
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * 26 *
27 27 * The quotations from http://wiki.secondlife.com/wiki/Linden_Vehicle_Tutorial
28/* RA: June 14, 2011. Copied from ODEDynamics.cs and converted to 28 * are Copyright (c) 2009 Linden Research, Inc and are used under their license
29 * call the BulletSim system. 29 * of Creative Commons Attribution-Share Alike 3.0
30 */ 30 * (http://creativecommons.org/licenses/by-sa/3.0/).
31/* Revised Aug, Sept 2009 by Kitto Flora. ODEDynamics.cs replaces
32 * ODEVehicleSettings.cs. It and ODEPrim.cs are re-organised:
33 * ODEPrim.cs contains methods dealing with Prim editing, Prim
34 * characteristics and Kinetic motion.
35 * ODEDynamics.cs contains methods dealing with Prim Physical motion
36 * (dynamics) and the associated settings. Old Linear and angular
37 * motors for dynamic motion have been replace with MoveLinear()
38 * and MoveAngular(); 'Physical' is used only to switch ODE dynamic
39 * simualtion on/off; VEHICAL_TYPE_NONE/VEHICAL_TYPE_<other> is to
40 * switch between 'VEHICLE' parameter use and general dynamics
41 * settings use.
42 */ 31 */
43 32
44using System; 33using System;
45using System.Collections.Generic; 34using System.Collections.Generic;
46using System.Reflection; 35using System.Reflection;
47using System.Runtime.InteropServices; 36using System.Runtime.InteropServices;
48using log4net;
49using OpenMetaverse; 37using OpenMetaverse;
50using OpenSim.Framework;
51using OpenSim.Region.Physics.Manager; 38using OpenSim.Region.Physics.Manager;
52 39
53namespace OpenSim.Region.Physics.BulletSPlugin 40namespace OpenSim.Region.Physics.BulletSPlugin
@@ -80,10 +67,10 @@ namespace OpenSim.Region.Physics.BulletSPlugin
80 private Quaternion m_referenceFrame = Quaternion.Identity; 67 private Quaternion m_referenceFrame = Quaternion.Identity;
81 68
82 // Linear properties 69 // Linear properties
70 private BSVMotor m_linearMotor = new BSVMotor("LinearMotor");
83 private Vector3 m_linearMotorDirection = Vector3.Zero; // velocity requested by LSL, decayed by time 71 private Vector3 m_linearMotorDirection = Vector3.Zero; // velocity requested by LSL, decayed by time
84 private Vector3 m_linearMotorOffset = Vector3.Zero; // the point of force can be offset from the center 72 private Vector3 m_linearMotorOffset = Vector3.Zero; // the point of force can be offset from the center
85 private Vector3 m_linearMotorDirectionLASTSET = Vector3.Zero; // velocity requested by LSL 73 private Vector3 m_linearMotorDirectionLASTSET = Vector3.Zero; // velocity requested by LSL
86 private Vector3 m_newVelocity = Vector3.Zero; // velocity computed to be applied to body
87 private Vector3 m_linearFrictionTimescale = Vector3.Zero; 74 private Vector3 m_linearFrictionTimescale = Vector3.Zero;
88 private float m_linearMotorDecayTimescale = 0; 75 private float m_linearMotorDecayTimescale = 0;
89 private float m_linearMotorTimescale = 0; 76 private float m_linearMotorTimescale = 0;
@@ -93,16 +80,18 @@ namespace OpenSim.Region.Physics.BulletSPlugin
93 // private Vector3 m_linearMotorOffset = Vector3.Zero; 80 // private Vector3 m_linearMotorOffset = Vector3.Zero;
94 81
95 //Angular properties 82 //Angular properties
83 private BSVMotor m_angularMotor = new BSVMotor("AngularMotor");
96 private Vector3 m_angularMotorDirection = Vector3.Zero; // angular velocity requested by LSL motor 84 private Vector3 m_angularMotorDirection = Vector3.Zero; // angular velocity requested by LSL motor
97 // private int m_angularMotorApply = 0; // application frame counter 85 // private int m_angularMotorApply = 0; // application frame counter
98 private Vector3 m_angularMotorVelocity = Vector3.Zero; // current angular motor velocity 86 private Vector3 m_angularMotorVelocity = Vector3.Zero; // current angular motor velocity
99 private float m_angularMotorTimescale = 0; // motor angular velocity ramp up rate 87 private float m_angularMotorTimescale = 0; // motor angular velocity ramp up rate
100 private float m_angularMotorDecayTimescale = 0; // motor angular velocity decay rate 88 private float m_angularMotorDecayTimescale = 0; // motor angular velocity decay rate
101 private Vector3 m_angularFrictionTimescale = Vector3.Zero; // body angular velocity decay rate 89 private Vector3 m_angularFrictionTimescale = Vector3.Zero; // body angular velocity decay rate
102 private Vector3 m_lastAngularVelocity = Vector3.Zero; // what was last applied to body 90 private Vector3 m_lastAngularVelocity = Vector3.Zero;
103 private Vector3 m_lastVertAttractor = Vector3.Zero; // what VA was last applied to body 91 private Vector3 m_lastVertAttractor = Vector3.Zero; // what VA was last applied to body
104 92
105 //Deflection properties 93 //Deflection properties
94 private BSVMotor m_angularDeflectionMotor = new BSVMotor("AngularDeflection");
106 private float m_angularDeflectionEfficiency = 0; 95 private float m_angularDeflectionEfficiency = 0;
107 private float m_angularDeflectionTimescale = 0; 96 private float m_angularDeflectionTimescale = 0;
108 private float m_linearDeflectionEfficiency = 0; 97 private float m_linearDeflectionEfficiency = 0;
@@ -114,32 +103,61 @@ namespace OpenSim.Region.Physics.BulletSPlugin
114 private float m_bankingTimescale = 0; 103 private float m_bankingTimescale = 0;
115 104
116 //Hover and Buoyancy properties 105 //Hover and Buoyancy properties
106 private BSVMotor m_hoverMotor = new BSVMotor("Hover");
117 private float m_VhoverHeight = 0f; 107 private float m_VhoverHeight = 0f;
118 private float m_VhoverEfficiency = 0f; 108 private float m_VhoverEfficiency = 0f;
119 private float m_VhoverTimescale = 0f; 109 private float m_VhoverTimescale = 0f;
120 private float m_VhoverTargetHeight = -1.0f; // if <0 then no hover, else its the current target height 110 private float m_VhoverTargetHeight = -1.0f; // if <0 then no hover, else its the current target height
121 private float m_VehicleBuoyancy = 0f; //KF: m_VehicleBuoyancy is set by VEHICLE_BUOYANCY for a vehicle. 111 // Modifies gravity. Slider between -1 (double-gravity) and 1 (full anti-gravity)
122 // Modifies gravity. Slider between -1 (double-gravity) and 1 (full anti-gravity) 112 private float m_VehicleBuoyancy = 0f;
123 // KF: So far I have found no good method to combine a script-requested .Z velocity and gravity. 113 private Vector3 m_VehicleGravity = Vector3.Zero; // Gravity computed when buoyancy set
124 // Therefore only m_VehicleBuoyancy=1 (0g) will use the script-requested .Z velocity.
125 114
126 //Attractor properties 115 //Attractor properties
127 private float m_verticalAttractionEfficiency = 1.0f; // damped 116 private BSVMotor m_verticalAttractionMotor = new BSVMotor("VerticalAttraction");
128 private float m_verticalAttractionTimescale = 500f; // Timescale > 300 means no vert attractor. 117 private float m_verticalAttractionEfficiency = 1.0f; // damped
118 private float m_verticalAttractionCutoff = 500f; // per the documentation
119 // Timescale > cutoff means no vert attractor.
120 private float m_verticalAttractionTimescale = 510f;
121
122 // Just some recomputed constants:
123 static readonly float PIOverFour = ((float)Math.PI) / 4f;
124 static readonly float PIOverTwo = ((float)Math.PI) / 2f;
125
126 // For debugging, flags to turn on and off individual corrections.
127 private bool enableAngularVerticalAttraction;
128 private bool enableAngularDeflection;
129 private bool enableAngularBanking;
129 130
130 public BSDynamics(BSScene myScene, BSPrim myPrim) 131 public BSDynamics(BSScene myScene, BSPrim myPrim)
131 { 132 {
132 PhysicsScene = myScene; 133 PhysicsScene = myScene;
133 Prim = myPrim; 134 Prim = myPrim;
134 Type = Vehicle.TYPE_NONE; 135 Type = Vehicle.TYPE_NONE;
136 SetupVehicleDebugging();
137 }
138
139 // Stopgap debugging enablement. Allows source level debugging but still checking
140 // in changes by making enablement of debugging flags from INI file.
141 public void SetupVehicleDebugging()
142 {
143 enableAngularVerticalAttraction = true;
144 enableAngularDeflection = false;
145 enableAngularBanking = false;
146 if (BSParam.VehicleDebuggingEnabled != ConfigurationParameters.numericFalse)
147 {
148 enableAngularVerticalAttraction = false;
149 enableAngularDeflection = false;
150 enableAngularBanking = false;
151 }
135 } 152 }
136 153
137 // Return 'true' if this vehicle is doing vehicle things 154 // Return 'true' if this vehicle is doing vehicle things
138 public bool IsActive 155 public bool IsActive
139 { 156 {
140 get { return Type != Vehicle.TYPE_NONE; } 157 get { return (Type != Vehicle.TYPE_NONE && !Prim.IsStatic); }
141 } 158 }
142 159
160 #region Vehicle parameter setting
143 internal void ProcessFloatVehicleParam(Vehicle pParam, float pValue) 161 internal void ProcessFloatVehicleParam(Vehicle pParam, float pValue)
144 { 162 {
145 VDetailLog("{0},ProcessFloatVehicleParam,param={1},val={2}", Prim.LocalID, pParam, pValue); 163 VDetailLog("{0},ProcessFloatVehicleParam,param={1},val={2}", Prim.LocalID, pParam, pValue);
@@ -152,13 +170,15 @@ namespace OpenSim.Region.Physics.BulletSPlugin
152 m_angularDeflectionTimescale = Math.Max(pValue, 0.01f); 170 m_angularDeflectionTimescale = Math.Max(pValue, 0.01f);
153 break; 171 break;
154 case Vehicle.ANGULAR_MOTOR_DECAY_TIMESCALE: 172 case Vehicle.ANGULAR_MOTOR_DECAY_TIMESCALE:
155 m_angularMotorDecayTimescale = Math.Max(pValue, 0.01f); 173 m_angularMotorDecayTimescale = ClampInRange(0.01f, pValue, 120);
174 m_angularMotor.TargetValueDecayTimeScale = m_angularMotorDecayTimescale;
156 break; 175 break;
157 case Vehicle.ANGULAR_MOTOR_TIMESCALE: 176 case Vehicle.ANGULAR_MOTOR_TIMESCALE:
158 m_angularMotorTimescale = Math.Max(pValue, 0.01f); 177 m_angularMotorTimescale = Math.Max(pValue, 0.01f);
178 m_angularMotor.TimeScale = m_angularMotorTimescale;
159 break; 179 break;
160 case Vehicle.BANKING_EFFICIENCY: 180 case Vehicle.BANKING_EFFICIENCY:
161 m_bankingEfficiency = Math.Max(-1f, Math.Min(pValue, 1f)); 181 m_bankingEfficiency = ClampInRange(-1f, pValue, 1f);
162 break; 182 break;
163 case Vehicle.BANKING_MIX: 183 case Vehicle.BANKING_MIX:
164 m_bankingMix = Math.Max(pValue, 0.01f); 184 m_bankingMix = Math.Max(pValue, 0.01f);
@@ -167,10 +187,11 @@ namespace OpenSim.Region.Physics.BulletSPlugin
167 m_bankingTimescale = Math.Max(pValue, 0.01f); 187 m_bankingTimescale = Math.Max(pValue, 0.01f);
168 break; 188 break;
169 case Vehicle.BUOYANCY: 189 case Vehicle.BUOYANCY:
170 m_VehicleBuoyancy = Math.Max(-1f, Math.Min(pValue, 1f)); 190 m_VehicleBuoyancy = ClampInRange(-1f, pValue, 1f);
191 m_VehicleGravity = Prim.ComputeGravity(m_VehicleBuoyancy);
171 break; 192 break;
172 case Vehicle.HOVER_EFFICIENCY: 193 case Vehicle.HOVER_EFFICIENCY:
173 m_VhoverEfficiency = Math.Max(0f, Math.Min(pValue, 1f)); 194 m_VhoverEfficiency = ClampInRange(0f, pValue, 1f);
174 break; 195 break;
175 case Vehicle.HOVER_HEIGHT: 196 case Vehicle.HOVER_HEIGHT:
176 m_VhoverHeight = pValue; 197 m_VhoverHeight = pValue;
@@ -185,33 +206,40 @@ namespace OpenSim.Region.Physics.BulletSPlugin
185 m_linearDeflectionTimescale = Math.Max(pValue, 0.01f); 206 m_linearDeflectionTimescale = Math.Max(pValue, 0.01f);
186 break; 207 break;
187 case Vehicle.LINEAR_MOTOR_DECAY_TIMESCALE: 208 case Vehicle.LINEAR_MOTOR_DECAY_TIMESCALE:
188 m_linearMotorDecayTimescale = Math.Max(pValue, 0.01f); 209 m_linearMotorDecayTimescale = ClampInRange(0.01f, pValue, 120);
210 m_linearMotor.TargetValueDecayTimeScale = m_linearMotorDecayTimescale;
189 break; 211 break;
190 case Vehicle.LINEAR_MOTOR_TIMESCALE: 212 case Vehicle.LINEAR_MOTOR_TIMESCALE:
191 m_linearMotorTimescale = Math.Max(pValue, 0.01f); 213 m_linearMotorTimescale = Math.Max(pValue, 0.01f);
214 m_linearMotor.TimeScale = m_linearMotorTimescale;
192 break; 215 break;
193 case Vehicle.VERTICAL_ATTRACTION_EFFICIENCY: 216 case Vehicle.VERTICAL_ATTRACTION_EFFICIENCY:
194 m_verticalAttractionEfficiency = Math.Max(0.1f, Math.Min(pValue, 1f)); 217 m_verticalAttractionEfficiency = ClampInRange(0.1f, pValue, 1f);
218 m_verticalAttractionMotor.Efficiency = m_verticalAttractionEfficiency;
195 break; 219 break;
196 case Vehicle.VERTICAL_ATTRACTION_TIMESCALE: 220 case Vehicle.VERTICAL_ATTRACTION_TIMESCALE:
197 m_verticalAttractionTimescale = Math.Max(pValue, 0.01f); 221 m_verticalAttractionTimescale = Math.Max(pValue, 0.01f);
222 m_verticalAttractionMotor.TimeScale = m_verticalAttractionTimescale;
198 break; 223 break;
199 224
200 // These are vector properties but the engine lets you use a single float value to 225 // These are vector properties but the engine lets you use a single float value to
201 // set all of the components to the same value 226 // set all of the components to the same value
202 case Vehicle.ANGULAR_FRICTION_TIMESCALE: 227 case Vehicle.ANGULAR_FRICTION_TIMESCALE:
203 m_angularFrictionTimescale = new Vector3(pValue, pValue, pValue); 228 m_angularFrictionTimescale = new Vector3(pValue, pValue, pValue);
229 m_angularMotor.FrictionTimescale = m_angularFrictionTimescale;
204 break; 230 break;
205 case Vehicle.ANGULAR_MOTOR_DIRECTION: 231 case Vehicle.ANGULAR_MOTOR_DIRECTION:
206 m_angularMotorDirection = new Vector3(pValue, pValue, pValue); 232 m_angularMotorDirection = new Vector3(pValue, pValue, pValue);
207 // m_angularMotorApply = 100; 233 m_angularMotor.SetTarget(m_angularMotorDirection);
208 break; 234 break;
209 case Vehicle.LINEAR_FRICTION_TIMESCALE: 235 case Vehicle.LINEAR_FRICTION_TIMESCALE:
210 m_linearFrictionTimescale = new Vector3(pValue, pValue, pValue); 236 m_linearFrictionTimescale = new Vector3(pValue, pValue, pValue);
237 m_linearMotor.FrictionTimescale = m_linearFrictionTimescale;
211 break; 238 break;
212 case Vehicle.LINEAR_MOTOR_DIRECTION: 239 case Vehicle.LINEAR_MOTOR_DIRECTION:
213 m_linearMotorDirection = new Vector3(pValue, pValue, pValue); 240 m_linearMotorDirection = new Vector3(pValue, pValue, pValue);
214 m_linearMotorDirectionLASTSET = new Vector3(pValue, pValue, pValue); 241 m_linearMotorDirectionLASTSET = new Vector3(pValue, pValue, pValue);
242 m_linearMotor.SetTarget(m_linearMotorDirection);
215 break; 243 break;
216 case Vehicle.LINEAR_MOTOR_OFFSET: 244 case Vehicle.LINEAR_MOTOR_OFFSET:
217 m_linearMotorOffset = new Vector3(pValue, pValue, pValue); 245 m_linearMotorOffset = new Vector3(pValue, pValue, pValue);
@@ -227,21 +255,24 @@ namespace OpenSim.Region.Physics.BulletSPlugin
227 { 255 {
228 case Vehicle.ANGULAR_FRICTION_TIMESCALE: 256 case Vehicle.ANGULAR_FRICTION_TIMESCALE:
229 m_angularFrictionTimescale = new Vector3(pValue.X, pValue.Y, pValue.Z); 257 m_angularFrictionTimescale = new Vector3(pValue.X, pValue.Y, pValue.Z);
258 m_angularMotor.FrictionTimescale = m_angularFrictionTimescale;
230 break; 259 break;
231 case Vehicle.ANGULAR_MOTOR_DIRECTION: 260 case Vehicle.ANGULAR_MOTOR_DIRECTION:
232 // Limit requested angular speed to 2 rps= 4 pi rads/sec 261 // Limit requested angular speed to 2 rps= 4 pi rads/sec
233 pValue.X = Math.Max(-12.56f, Math.Min(pValue.X, 12.56f)); 262 pValue.X = ClampInRange(-12.56f, pValue.X, 12.56f);
234 pValue.Y = Math.Max(-12.56f, Math.Min(pValue.Y, 12.56f)); 263 pValue.Y = ClampInRange(-12.56f, pValue.Y, 12.56f);
235 pValue.Z = Math.Max(-12.56f, Math.Min(pValue.Z, 12.56f)); 264 pValue.Z = ClampInRange(-12.56f, pValue.Z, 12.56f);
236 m_angularMotorDirection = new Vector3(pValue.X, pValue.Y, pValue.Z); 265 m_angularMotorDirection = new Vector3(pValue.X, pValue.Y, pValue.Z);
237 // m_angularMotorApply = 100; 266 m_angularMotor.SetTarget(m_angularMotorDirection);
238 break; 267 break;
239 case Vehicle.LINEAR_FRICTION_TIMESCALE: 268 case Vehicle.LINEAR_FRICTION_TIMESCALE:
240 m_linearFrictionTimescale = new Vector3(pValue.X, pValue.Y, pValue.Z); 269 m_linearFrictionTimescale = new Vector3(pValue.X, pValue.Y, pValue.Z);
270 m_linearMotor.FrictionTimescale = m_linearFrictionTimescale;
241 break; 271 break;
242 case Vehicle.LINEAR_MOTOR_DIRECTION: 272 case Vehicle.LINEAR_MOTOR_DIRECTION:
243 m_linearMotorDirection = new Vector3(pValue.X, pValue.Y, pValue.Z); 273 m_linearMotorDirection = new Vector3(pValue.X, pValue.Y, pValue.Z);
244 m_linearMotorDirectionLASTSET = new Vector3(pValue.X, pValue.Y, pValue.Z); 274 m_linearMotorDirectionLASTSET = new Vector3(pValue.X, pValue.Y, pValue.Z);
275 m_linearMotor.SetTarget(m_linearMotorDirection);
245 break; 276 break;
246 case Vehicle.LINEAR_MOTOR_OFFSET: 277 case Vehicle.LINEAR_MOTOR_OFFSET:
247 m_linearMotorOffset = new Vector3(pValue.X, pValue.Y, pValue.Z); 278 m_linearMotorOffset = new Vector3(pValue.X, pValue.Y, pValue.Z);
@@ -303,7 +334,7 @@ namespace OpenSim.Region.Physics.BulletSPlugin
303 m_VhoverEfficiency = 0; 334 m_VhoverEfficiency = 0;
304 m_VhoverTimescale = 0; 335 m_VhoverTimescale = 0;
305 m_VehicleBuoyancy = 0; 336 m_VehicleBuoyancy = 0;
306 337
307 m_linearDeflectionEfficiency = 1; 338 m_linearDeflectionEfficiency = 1;
308 m_linearDeflectionTimescale = 1; 339 m_linearDeflectionTimescale = 1;
309 340
@@ -319,6 +350,7 @@ namespace OpenSim.Region.Physics.BulletSPlugin
319 350
320 m_referenceFrame = Quaternion.Identity; 351 m_referenceFrame = Quaternion.Identity;
321 m_flags = (VehicleFlag)0; 352 m_flags = (VehicleFlag)0;
353
322 break; 354 break;
323 355
324 case Vehicle.TYPE_SLED: 356 case Vehicle.TYPE_SLED:
@@ -351,10 +383,14 @@ namespace OpenSim.Region.Physics.BulletSPlugin
351 m_bankingMix = 1; 383 m_bankingMix = 1;
352 384
353 m_referenceFrame = Quaternion.Identity; 385 m_referenceFrame = Quaternion.Identity;
354 m_flags |= (VehicleFlag.NO_DEFLECTION_UP | VehicleFlag.LIMIT_ROLL_ONLY | VehicleFlag.LIMIT_MOTOR_UP); 386 m_flags &= ~(VehicleFlag.HOVER_WATER_ONLY
355 m_flags &= 387 | VehicleFlag.HOVER_TERRAIN_ONLY
356 ~(VehicleFlag.HOVER_WATER_ONLY | VehicleFlag.HOVER_TERRAIN_ONLY | 388 | VehicleFlag.HOVER_GLOBAL_HEIGHT
357 VehicleFlag.HOVER_GLOBAL_HEIGHT | VehicleFlag.HOVER_UP_ONLY); 389 | VehicleFlag.HOVER_UP_ONLY);
390 m_flags |= (VehicleFlag.NO_DEFLECTION_UP
391 | VehicleFlag.LIMIT_ROLL_ONLY
392 | VehicleFlag.LIMIT_MOTOR_UP);
393
358 break; 394 break;
359 case Vehicle.TYPE_CAR: 395 case Vehicle.TYPE_CAR:
360 m_linearMotorDirection = Vector3.Zero; 396 m_linearMotorDirection = Vector3.Zero;
@@ -498,6 +534,7 @@ namespace OpenSim.Region.Physics.BulletSPlugin
498 m_bankingEfficiency = 0; 534 m_bankingEfficiency = 0;
499 m_bankingMix = 0.7f; 535 m_bankingMix = 0.7f;
500 m_bankingTimescale = 5; 536 m_bankingTimescale = 5;
537
501 m_referenceFrame = Quaternion.Identity; 538 m_referenceFrame = Quaternion.Identity;
502 539
503 m_referenceFrame = Quaternion.Identity; 540 m_referenceFrame = Quaternion.Identity;
@@ -510,7 +547,30 @@ namespace OpenSim.Region.Physics.BulletSPlugin
510 | VehicleFlag.HOVER_GLOBAL_HEIGHT); 547 | VehicleFlag.HOVER_GLOBAL_HEIGHT);
511 break; 548 break;
512 } 549 }
550
551 // Update any physical parameters based on this type.
552 Refresh();
553
554 m_linearMotor = new BSVMotor("LinearMotor", m_linearMotorTimescale,
555 m_linearMotorDecayTimescale, m_linearFrictionTimescale,
556 1f);
557 m_linearMotor.PhysicsScene = PhysicsScene; // DEBUG DEBUG DEBUG (enables detail logging)
558
559 m_angularMotor = new BSVMotor("AngularMotor", m_angularMotorTimescale,
560 m_angularMotorDecayTimescale, m_angularFrictionTimescale,
561 1f);
562 m_angularMotor.PhysicsScene = PhysicsScene; // DEBUG DEBUG DEBUG (enables detail logging)
563
564 /* Not implemented
565 m_verticalAttractionMotor = new BSVMotor("VerticalAttraction", m_verticalAttractionTimescale,
566 BSMotor.Infinite, BSMotor.InfiniteVector,
567 m_verticalAttractionEfficiency);
568 // Z goes away and we keep X and Y
569 m_verticalAttractionMotor.FrictionTimescale = new Vector3(BSMotor.Infinite, BSMotor.Infinite, 0.1f);
570 m_verticalAttractionMotor.PhysicsScene = PhysicsScene; // DEBUG DEBUG DEBUG (enables detail logging)
571 */
513 } 572 }
573 #endregion // Vehicle parameter setting
514 574
515 // Some of the properties of this prim may have changed. 575 // Some of the properties of this prim may have changed.
516 // Do any updating needed for a vehicle 576 // Do any updating needed for a vehicle
@@ -518,13 +578,38 @@ namespace OpenSim.Region.Physics.BulletSPlugin
518 { 578 {
519 if (IsActive) 579 if (IsActive)
520 { 580 {
521 // Friction effects are handled by this vehicle code 581 // Remember the mass so we don't have to fetch it every step
522 BulletSimAPI.SetFriction2(Prim.PhysBody.ptr, 0f); 582 m_vehicleMass = Prim.Linkset.LinksetMass;
523 BulletSimAPI.SetHitFraction2(Prim.PhysBody.ptr, 0f); 583
524 584 // Friction affects are handled by this vehicle code
525 // BulletSimAPI.SetAngularDamping2(Prim.PhysBody.ptr, 0.8f); 585 float friction = 0f;
526 586 PhysicsScene.PE.SetFriction(Prim.PhysBody, friction);
527 VDetailLog("{0},BSDynamics.Refresh,zeroingFriction and adding damping", Prim.LocalID); 587
588 // Moderate angular movement introduced by Bullet.
589 // TODO: possibly set AngularFactor and LinearFactor for the type of vehicle.
590 // Maybe compute linear and angular factor and damping from params.
591 float angularDamping = BSParam.VehicleAngularDamping;
592 PhysicsScene.PE.SetAngularDamping(Prim.PhysBody, angularDamping);
593
594 // Vehicles report collision events so we know when it's on the ground
595 PhysicsScene.PE.AddToCollisionFlags(Prim.PhysBody, CollisionFlags.BS_VEHICLE_COLLISIONS);
596
597 Prim.Inertia = PhysicsScene.PE.CalculateLocalInertia(Prim.PhysShape, m_vehicleMass);
598 PhysicsScene.PE.SetMassProps(Prim.PhysBody, m_vehicleMass, Prim.Inertia);
599 PhysicsScene.PE.UpdateInertiaTensor(Prim.PhysBody);
600
601 // Set the gravity for the vehicle depending on the buoyancy
602 // TODO: what should be done if prim and vehicle buoyancy differ?
603 m_VehicleGravity = Prim.ComputeGravity(m_VehicleBuoyancy);
604 // The actual vehicle gravity is set to zero in Bullet so we can do all the application of same.
605 PhysicsScene.PE.SetGravity(Prim.PhysBody, Vector3.Zero);
606
607 VDetailLog("{0},BSDynamics.Refresh,mass={1},frict={2},inert={3},aDamp={4},grav={5}",
608 Prim.LocalID, m_vehicleMass, friction, Prim.Inertia, angularDamping, m_VehicleGravity);
609 }
610 else
611 {
612 PhysicsScene.PE.RemoveFromCollisionFlags(Prim.PhysBody, CollisionFlags.BS_VEHICLE_COLLISIONS);
528 } 613 }
529 } 614 }
530 615
@@ -546,116 +631,383 @@ namespace OpenSim.Region.Physics.BulletSPlugin
546 Refresh(); 631 Refresh();
547 } 632 }
548 633
634 #region Known vehicle value functions
635 // Vehicle physical parameters that we buffer from constant getting and setting.
636 // The "m_known*" values are unknown until they are fetched and the m_knownHas flag is set.
637 // Changing is remembered and the parameter is stored back into the physics engine only if updated.
638 // This does two things: 1) saves continuious calls into unmanaged code, and
639 // 2) signals when a physics property update must happen back to the simulator
640 // to update values modified for the vehicle.
641 private int m_knownChanged;
642 private int m_knownHas;
643 private float m_knownTerrainHeight;
644 private float m_knownWaterLevel;
645 private Vector3 m_knownPosition;
646 private Vector3 m_knownVelocity;
647 private Vector3 m_knownForce;
648 private Vector3 m_knownForceImpulse;
649 private Quaternion m_knownOrientation;
650 private Vector3 m_knownRotationalVelocity;
651 private Vector3 m_knownRotationalForce;
652 private Vector3 m_knownRotationalImpulse;
653 private Vector3 m_knownForwardVelocity; // vehicle relative forward speed
654
655 private const int m_knownChangedPosition = 1 << 0;
656 private const int m_knownChangedVelocity = 1 << 1;
657 private const int m_knownChangedForce = 1 << 2;
658 private const int m_knownChangedForceImpulse = 1 << 3;
659 private const int m_knownChangedOrientation = 1 << 4;
660 private const int m_knownChangedRotationalVelocity = 1 << 5;
661 private const int m_knownChangedRotationalForce = 1 << 6;
662 private const int m_knownChangedRotationalImpulse = 1 << 7;
663 private const int m_knownChangedTerrainHeight = 1 << 8;
664 private const int m_knownChangedWaterLevel = 1 << 9;
665 private const int m_knownChangedForwardVelocity = 1 <<10;
666
667 private void ForgetKnownVehicleProperties()
668 {
669 m_knownHas = 0;
670 m_knownChanged = 0;
671 }
672 // Push all the changed values back into the physics engine
673 private void PushKnownChanged()
674 {
675 if (m_knownChanged != 0)
676 {
677 if ((m_knownChanged & m_knownChangedPosition) != 0)
678 Prim.ForcePosition = m_knownPosition;
679
680 if ((m_knownChanged & m_knownChangedOrientation) != 0)
681 Prim.ForceOrientation = m_knownOrientation;
682
683 if ((m_knownChanged & m_knownChangedVelocity) != 0)
684 {
685 Prim.ForceVelocity = m_knownVelocity;
686 // Fake out Bullet by making it think the velocity is the same as last time.
687 // Bullet does a bunch of smoothing for changing parameters.
688 // Since the vehicle is demanding this setting, we override Bullet's smoothing
689 // by telling Bullet the value was the same last time.
690 PhysicsScene.PE.SetInterpolationLinearVelocity(Prim.PhysBody, m_knownVelocity);
691 }
692
693 if ((m_knownChanged & m_knownChangedForce) != 0)
694 Prim.AddForce((Vector3)m_knownForce, false /*pushForce*/, true /*inTaintTime*/);
695
696 if ((m_knownChanged & m_knownChangedForceImpulse) != 0)
697 Prim.AddForceImpulse((Vector3)m_knownForceImpulse, false /*pushforce*/, true /*inTaintTime*/);
698
699 if ((m_knownChanged & m_knownChangedRotationalVelocity) != 0)
700 {
701 Prim.ForceRotationalVelocity = m_knownRotationalVelocity;
702 PhysicsScene.PE.SetInterpolationAngularVelocity(Prim.PhysBody, m_knownRotationalVelocity);
703 }
704
705 if ((m_knownChanged & m_knownChangedRotationalImpulse) != 0)
706 Prim.ApplyTorqueImpulse((Vector3)m_knownRotationalImpulse, true /*inTaintTime*/);
707
708 if ((m_knownChanged & m_knownChangedRotationalForce) != 0)
709 {
710 Prim.AddAngularForce((Vector3)m_knownRotationalForce, false /*pushForce*/, true /*inTaintTime*/);
711 }
712
713 // If we set one of the values (ie, the physics engine didn't do it) we must force
714 // an UpdateProperties event to send the changes up to the simulator.
715 PhysicsScene.PE.PushUpdate(Prim.PhysBody);
716 }
717 m_knownChanged = 0;
718 }
719
720 // Since the computation of terrain height can be a little involved, this routine
721 // is used to fetch the height only once for each vehicle simulation step.
722 private float GetTerrainHeight(Vector3 pos)
723 {
724 if ((m_knownHas & m_knownChangedTerrainHeight) == 0)
725 {
726 m_knownTerrainHeight = Prim.PhysicsScene.TerrainManager.GetTerrainHeightAtXYZ(pos);
727 m_knownHas |= m_knownChangedTerrainHeight;
728 }
729 return m_knownTerrainHeight;
730 }
731
732 // Since the computation of water level can be a little involved, this routine
733 // is used ot fetch the level only once for each vehicle simulation step.
734 private float GetWaterLevel(Vector3 pos)
735 {
736 if ((m_knownHas & m_knownChangedWaterLevel) == 0)
737 {
738 m_knownWaterLevel = Prim.PhysicsScene.TerrainManager.GetWaterLevelAtXYZ(pos);
739 m_knownHas |= m_knownChangedWaterLevel;
740 }
741 return (float)m_knownWaterLevel;
742 }
743
744 private Vector3 VehiclePosition
745 {
746 get
747 {
748 if ((m_knownHas & m_knownChangedPosition) == 0)
749 {
750 m_knownPosition = Prim.ForcePosition;
751 m_knownHas |= m_knownChangedPosition;
752 }
753 return m_knownPosition;
754 }
755 set
756 {
757 m_knownPosition = value;
758 m_knownChanged |= m_knownChangedPosition;
759 m_knownHas |= m_knownChangedPosition;
760 }
761 }
762
763 private Quaternion VehicleOrientation
764 {
765 get
766 {
767 if ((m_knownHas & m_knownChangedOrientation) == 0)
768 {
769 m_knownOrientation = Prim.ForceOrientation;
770 m_knownHas |= m_knownChangedOrientation;
771 }
772 return m_knownOrientation;
773 }
774 set
775 {
776 m_knownOrientation = value;
777 m_knownChanged |= m_knownChangedOrientation;
778 m_knownHas |= m_knownChangedOrientation;
779 }
780 }
781
782 private Vector3 VehicleVelocity
783 {
784 get
785 {
786 if ((m_knownHas & m_knownChangedVelocity) == 0)
787 {
788 m_knownVelocity = Prim.ForceVelocity;
789 m_knownHas |= m_knownChangedVelocity;
790 }
791 return (Vector3)m_knownVelocity;
792 }
793 set
794 {
795 m_knownVelocity = value;
796 m_knownChanged |= m_knownChangedVelocity;
797 m_knownHas |= m_knownChangedVelocity;
798 }
799 }
800
801 private void VehicleAddForce(Vector3 pForce)
802 {
803 if ((m_knownHas & m_knownChangedForce) == 0)
804 {
805 m_knownForce = Vector3.Zero;
806 m_knownHas |= m_knownChangedForce;
807 }
808 m_knownForce += pForce;
809 m_knownChanged |= m_knownChangedForce;
810 }
811
812 private void VehicleAddForceImpulse(Vector3 pImpulse)
813 {
814 if ((m_knownHas & m_knownChangedForceImpulse) == 0)
815 {
816 m_knownForceImpulse = Vector3.Zero;
817 m_knownHas |= m_knownChangedForceImpulse;
818 }
819 m_knownForceImpulse += pImpulse;
820 m_knownChanged |= m_knownChangedForceImpulse;
821 }
822
823 private Vector3 VehicleRotationalVelocity
824 {
825 get
826 {
827 if ((m_knownHas & m_knownChangedRotationalVelocity) == 0)
828 {
829 m_knownRotationalVelocity = Prim.ForceRotationalVelocity;
830 m_knownHas |= m_knownChangedRotationalVelocity;
831 }
832 return (Vector3)m_knownRotationalVelocity;
833 }
834 set
835 {
836 m_knownRotationalVelocity = value;
837 m_knownChanged |= m_knownChangedRotationalVelocity;
838 m_knownHas |= m_knownChangedRotationalVelocity;
839 }
840 }
841 private void VehicleAddAngularForce(Vector3 aForce)
842 {
843 if ((m_knownHas & m_knownChangedRotationalForce) == 0)
844 {
845 m_knownRotationalForce = Vector3.Zero;
846 }
847 m_knownRotationalForce += aForce;
848 m_knownChanged |= m_knownChangedRotationalForce;
849 m_knownHas |= m_knownChangedRotationalForce;
850 }
851 private void VehicleAddRotationalImpulse(Vector3 pImpulse)
852 {
853 if ((m_knownHas & m_knownChangedRotationalImpulse) == 0)
854 {
855 m_knownRotationalImpulse = Vector3.Zero;
856 m_knownHas |= m_knownChangedRotationalImpulse;
857 }
858 m_knownRotationalImpulse += pImpulse;
859 m_knownChanged |= m_knownChangedRotationalImpulse;
860 }
861
862 // Vehicle relative forward velocity
863 private Vector3 VehicleForwardVelocity
864 {
865 get
866 {
867 if ((m_knownHas & m_knownChangedForwardVelocity) == 0)
868 {
869 m_knownForwardVelocity = VehicleVelocity * Quaternion.Inverse(Quaternion.Normalize(VehicleOrientation));
870 m_knownHas |= m_knownChangedForwardVelocity;
871 }
872 return m_knownForwardVelocity;
873 }
874 }
875 private float VehicleForwardSpeed
876 {
877 get
878 {
879 return VehicleForwardVelocity.X;
880 }
881 }
882
883 #endregion // Known vehicle value functions
884
549 // One step of the vehicle properties for the next 'pTimestep' seconds. 885 // One step of the vehicle properties for the next 'pTimestep' seconds.
550 internal void Step(float pTimestep) 886 internal void Step(float pTimestep)
551 { 887 {
552 if (!IsActive) return; 888 if (!IsActive) return;
553 889
554 // DEBUG 890 if (PhysicsScene.VehiclePhysicalLoggingEnabled)
555 // Because Bullet does apply forces to the vehicle, our last computed 891 PhysicsScene.PE.DumpRigidBody(PhysicsScene.World, Prim.PhysBody);
556 // linear and angular velocities are not what is happening now.
557 // Vector3 externalAngularVelocity = Prim.ForceRotationalVelocity - m_lastAngularVelocity;
558 // m_lastAngularVelocity += (externalAngularVelocity * 0.5f) * pTimestep;
559 // m_lastAngularVelocity = Prim.ForceRotationalVelocity; // DEBUG: account for what Bullet did last time
560 // m_lastLinearVelocityVector = Prim.ForceVelocity * Quaternion.Inverse(Prim.ForceOrientation); // DEBUG:
561 // END DEBUG
562 892
563 m_vehicleMass = Prim.Linkset.LinksetMass; 893 ForgetKnownVehicleProperties();
564 894
565 MoveLinear(pTimestep); 895 MoveLinear(pTimestep);
566 // Commented out for debug
567 MoveAngular(pTimestep); 896 MoveAngular(pTimestep);
568 // Prim.ApplyTorqueImpulse(-Prim.RotationalVelocity * m_vehicleMass, false); // DEBUG DEBUG
569 // Prim.ForceRotationalVelocity = -Prim.RotationalVelocity; // DEBUG DEBUG
570 897
571 LimitRotation(pTimestep); 898 LimitRotation(pTimestep);
572 899
573 // remember the position so next step we can limit absolute movement effects 900 // remember the position so next step we can limit absolute movement effects
574 m_lastPositionVector = Prim.ForcePosition; 901 m_lastPositionVector = VehiclePosition;
575 902
576 VDetailLog("{0},BSDynamics.Step,frict={1},grav={2},inertia={3},mass={4}", // DEBUG DEBUG 903 // If we forced the changing of some vehicle parameters, update the values and
577 Prim.LocalID, 904 // for the physics engine to note the changes so an UpdateProperties event will happen.
578 BulletSimAPI.GetFriction2(Prim.PhysBody.ptr), 905 PushKnownChanged();
579 BulletSimAPI.GetGravity2(Prim.PhysBody.ptr),
580 Prim.Inertia,
581 m_vehicleMass
582 );
583 VDetailLog("{0},BSDynamics.Step,done,pos={1},force={2},velocity={3},angvel={4}",
584 Prim.LocalID, Prim.ForcePosition, Prim.Force, Prim.ForceVelocity, Prim.RotationalVelocity);
585 }// end Step
586 906
587 // Apply the effect of the linear motor. 907 if (PhysicsScene.VehiclePhysicalLoggingEnabled)
588 // Also does hover and float. 908 PhysicsScene.PE.DumpRigidBody(PhysicsScene.World, Prim.PhysBody);
909
910 VDetailLog("{0},BSDynamics.Step,done,pos={1}, force={2},velocity={3},angvel={4}",
911 Prim.LocalID, VehiclePosition, m_knownForce, VehicleVelocity, VehicleRotationalVelocity);
912 }
913
914 // Apply the effect of the linear motor and other linear motions (like hover and float).
589 private void MoveLinear(float pTimestep) 915 private void MoveLinear(float pTimestep)
590 { 916 {
591 // m_linearMotorDirection is the target direction we are moving relative to the vehicle coordinates 917 ComputeLinearVelocity(pTimestep);
592 // m_lastLinearVelocityVector is the current speed we are moving in that direction 918
593 if (m_linearMotorDirection.LengthSquared() > 0.001f) 919 ComputeLinearTerrainHeightCorrection(pTimestep);
920
921 ComputeLinearHover(pTimestep);
922
923 ComputeLinearBlockingEndPoint(pTimestep);
924
925 ComputeLinearMotorUp(pTimestep);
926
927 ApplyGravity(pTimestep);
928
929 // If not changing some axis, reduce out velocity
930 if ((m_flags & (VehicleFlag.NO_X | VehicleFlag.NO_Y | VehicleFlag.NO_Z)) != 0)
931 {
932 Vector3 vel = VehicleVelocity;
933 if ((m_flags & (VehicleFlag.NO_X)) != 0)
934 vel.X = 0;
935 if ((m_flags & (VehicleFlag.NO_Y)) != 0)
936 vel.Y = 0;
937 if ((m_flags & (VehicleFlag.NO_Z)) != 0)
938 vel.Z = 0;
939 VehicleVelocity = vel;
940 }
941
942 // ==================================================================
943 // Clamp high or low velocities
944 float newVelocityLengthSq = VehicleVelocity.LengthSquared();
945 if (newVelocityLengthSq > 1000f)
594 { 946 {
595 Vector3 origDir = m_linearMotorDirection; // DEBUG 947 VehicleVelocity /= VehicleVelocity.Length();
596 Vector3 origVel = m_lastLinearVelocityVector; // DEBUG 948 VehicleVelocity *= 1000f;
597 // DEBUG: the vehicle velocity rotated to be relative to vehicle coordinates for comparison 949 }
598 Vector3 vehicleVelocity = Prim.ForceVelocity * Quaternion.Inverse(Prim.ForceOrientation); // DEBUG 950 else if (newVelocityLengthSq < 0.001f)
951 VehicleVelocity = Vector3.Zero;
599 952
600 // Add (desiredVelocity - lastAppliedVelocity) / howLongItShouldTakeToComplete 953 VDetailLog("{0}, MoveLinear,done,isColl={1},newVel={2}", Prim.LocalID, Prim.IsColliding, VehicleVelocity );
601 Vector3 addAmount = (m_linearMotorDirection - m_lastLinearVelocityVector)/(m_linearMotorTimescale) * pTimestep;
602 m_lastLinearVelocityVector += addAmount;
603 954
604 float decayFactor = (1.0f / m_linearMotorDecayTimescale) * pTimestep; 955 } // end MoveLinear()
605 m_linearMotorDirection *= (1f - decayFactor);
606 956
607 // Rotate new object velocity from vehicle relative to world coordinates 957 public void ComputeLinearVelocity(float pTimestep)
608 m_newVelocity = m_lastLinearVelocityVector * Prim.ForceOrientation; 958 {
959 Vector3 linearMotorStep = m_linearMotor.Step(pTimestep);
609 960
610 // Apply friction for next time 961 // The movement computed in the linear motor is relative to the vehicle
611 Vector3 frictionFactor = (Vector3.One / m_linearFrictionTimescale) * pTimestep; 962 // coordinates. Rotate the movement to world coordinates.
612 m_lastLinearVelocityVector *= (Vector3.One - frictionFactor); 963 Vector3 linearMotorVelocity = linearMotorStep * VehicleOrientation;
613 964
614 VDetailLog("{0},MoveLinear,nonZero,origlmDir={1},origlvVel={2},vehVel={3},add={4},decay={5},frict={6},lmDir={7},lvVec={8},newVel={9}", 965 // If we're a ground vehicle, don't loose any Z action (like gravity acceleration).
615 Prim.LocalID, origDir, origVel, vehicleVelocity, addAmount, decayFactor, frictionFactor, 966 float mixFactor = 1f; // 1 means use all linear motor Z value, 0 means use all existing Z
616 m_linearMotorDirection, m_lastLinearVelocityVector, m_newVelocity); 967 if ((m_flags & VehicleFlag.LIMIT_MOTOR_UP) != 0)
968 {
969 if (!Prim.IsColliding)
970 {
971 // If a ground vehicle and not on the ground, I want gravity effect
972 mixFactor = 0.2f;
973 }
617 } 974 }
618 else 975 else
619 { 976 {
620 // if what remains of direction is very small, zero it. 977 // I'm not a ground vehicle but don't totally loose the effect of the environment
621 m_linearMotorDirection = Vector3.Zero; 978 mixFactor = 0.8f;
622 m_lastLinearVelocityVector = Vector3.Zero;
623 m_newVelocity = Vector3.Zero;
624
625 VDetailLog("{0},MoveLinear,zeroed", Prim.LocalID);
626 } 979 }
980 linearMotorVelocity.Z = mixFactor * linearMotorVelocity.Z + (1f - mixFactor) * VehicleVelocity.Z;
627 981
628 // m_newVelocity is velocity computed from linear motor in world coordinates 982 // What we want to contribute to the vehicle's existing velocity
983 Vector3 linearMotorForce = linearMotorVelocity - VehicleVelocity;
629 984
630 // Gravity and Buoyancy 985 // Act against the inertia of the vehicle
631 // There is some gravity, make a gravity force vector that is applied after object velocity. 986 linearMotorForce *= m_vehicleMass;
632 // m_VehicleBuoyancy: -1=2g; 0=1g; 1=0g;
633 Vector3 grav = Prim.PhysicsScene.DefaultGravity * (1f - m_VehicleBuoyancy);
634 987
635 /* 988 VehicleAddForceImpulse(linearMotorForce * pTimestep);
636 * RA: Not sure why one would do this unless we are hoping external forces are doing gravity, ...
637 // Preserve the current Z velocity
638 Vector3 vel_now = m_prim.Velocity;
639 m_dir.Z = vel_now.Z; // Preserve the accumulated falling velocity
640 */
641 989
642 Vector3 pos = Prim.ForcePosition; 990 VDetailLog("{0}, MoveLinear,velocity,vehVel={1},step={2},stepVel={3},mix={4},force={5}",
643// Vector3 accel = new Vector3(-(m_dir.X - m_lastLinearVelocityVector.X / 0.1f), -(m_dir.Y - m_lastLinearVelocityVector.Y / 0.1f), m_dir.Z - m_lastLinearVelocityVector.Z / 0.1f); 991 Prim.LocalID, VehicleVelocity, linearMotorStep, linearMotorVelocity, mixFactor, linearMotorForce);
992 }
644 993
994 public void ComputeLinearTerrainHeightCorrection(float pTimestep)
995 {
645 // If below the terrain, move us above the ground a little. 996 // If below the terrain, move us above the ground a little.
646 float terrainHeight = Prim.PhysicsScene.TerrainManager.GetTerrainHeightAtXYZ(pos); 997 // TODO: Consider taking the rotated size of the object or possibly casting a ray.
647 // Taking the rotated size doesn't work here because m_prim.Size is the size of the root prim and not the linkset. 998 if (VehiclePosition.Z < GetTerrainHeight(VehiclePosition))
648 // TODO: Add a m_prim.LinkSet.Size similar to m_prim.LinkSet.Mass.
649 // Vector3 rotatedSize = m_prim.Size * m_prim.ForceOrientation;
650 // if (rotatedSize.Z < terrainHeight)
651 if (pos.Z < terrainHeight)
652 { 999 {
653 pos.Z = terrainHeight + 2; 1000 // Force position because applying force won't get the vehicle through the terrain
654 Prim.ForcePosition = pos; 1001 Vector3 newPosition = VehiclePosition;
655 VDetailLog("{0},MoveLinear,terrainHeight,terrainHeight={1},pos={2}", Prim.LocalID, terrainHeight, pos); 1002 newPosition.Z = GetTerrainHeight(VehiclePosition) + 1f;
1003 VehiclePosition = newPosition;
1004 VDetailLog("{0}, MoveLinear,terrainHeight,terrainHeight={1},pos={2}",
1005 Prim.LocalID, GetTerrainHeight(VehiclePosition), VehiclePosition);
656 } 1006 }
1007 }
657 1008
658 // Check if hovering 1009 public void ComputeLinearHover(float pTimestep)
1010 {
659 // m_VhoverEfficiency: 0=bouncy, 1=totally damped 1011 // m_VhoverEfficiency: 0=bouncy, 1=totally damped
660 // m_VhoverTimescale: time to achieve height 1012 // m_VhoverTimescale: time to achieve height
661 if ((m_flags & (VehicleFlag.HOVER_WATER_ONLY | VehicleFlag.HOVER_TERRAIN_ONLY | VehicleFlag.HOVER_GLOBAL_HEIGHT)) != 0) 1013 if ((m_flags & (VehicleFlag.HOVER_WATER_ONLY | VehicleFlag.HOVER_TERRAIN_ONLY | VehicleFlag.HOVER_GLOBAL_HEIGHT)) != 0)
@@ -663,11 +1015,11 @@ namespace OpenSim.Region.Physics.BulletSPlugin
663 // We should hover, get the target height 1015 // We should hover, get the target height
664 if ((m_flags & VehicleFlag.HOVER_WATER_ONLY) != 0) 1016 if ((m_flags & VehicleFlag.HOVER_WATER_ONLY) != 0)
665 { 1017 {
666 m_VhoverTargetHeight = Prim.PhysicsScene.GetWaterLevelAtXYZ(pos) + m_VhoverHeight; 1018 m_VhoverTargetHeight = GetWaterLevel(VehiclePosition) + m_VhoverHeight;
667 } 1019 }
668 if ((m_flags & VehicleFlag.HOVER_TERRAIN_ONLY) != 0) 1020 if ((m_flags & VehicleFlag.HOVER_TERRAIN_ONLY) != 0)
669 { 1021 {
670 m_VhoverTargetHeight = terrainHeight + m_VhoverHeight; 1022 m_VhoverTargetHeight = GetTerrainHeight(VehiclePosition) + m_VhoverHeight;
671 } 1023 }
672 if ((m_flags & VehicleFlag.HOVER_GLOBAL_HEIGHT) != 0) 1024 if ((m_flags & VehicleFlag.HOVER_GLOBAL_HEIGHT) != 0)
673 { 1025 {
@@ -677,45 +1029,63 @@ namespace OpenSim.Region.Physics.BulletSPlugin
677 if ((m_flags & VehicleFlag.HOVER_UP_ONLY) != 0) 1029 if ((m_flags & VehicleFlag.HOVER_UP_ONLY) != 0)
678 { 1030 {
679 // If body is already heigher, use its height as target height 1031 // If body is already heigher, use its height as target height
680 if (pos.Z > m_VhoverTargetHeight) 1032 if (VehiclePosition.Z > m_VhoverTargetHeight)
681 m_VhoverTargetHeight = pos.Z; 1033 m_VhoverTargetHeight = VehiclePosition.Z;
682 } 1034 }
1035
683 if ((m_flags & VehicleFlag.LOCK_HOVER_HEIGHT) != 0) 1036 if ((m_flags & VehicleFlag.LOCK_HOVER_HEIGHT) != 0)
684 { 1037 {
685 if (Math.Abs(pos.Z - m_VhoverTargetHeight) > 0.2f) 1038 if (Math.Abs(VehiclePosition.Z - m_VhoverTargetHeight) > 0.2f)
686 { 1039 {
1040 Vector3 pos = VehiclePosition;
687 pos.Z = m_VhoverTargetHeight; 1041 pos.Z = m_VhoverTargetHeight;
688 Prim.ForcePosition = pos; 1042 VehiclePosition = pos;
1043
1044 VDetailLog("{0}, MoveLinear,hover,pos={1},lockHoverHeight", Prim.LocalID, pos);
689 } 1045 }
690 } 1046 }
691 else 1047 else
692 { 1048 {
693 float verticalError = pos.Z - m_VhoverTargetHeight; 1049 // Error is positive if below the target and negative if above.
694 // RA: where does the 50 come from? 1050 Vector3 hpos = VehiclePosition;
695 float verticalCorrectionVelocity = pTimestep * ((verticalError * 50.0f) / m_VhoverTimescale); 1051 float verticalError = m_VhoverTargetHeight - hpos.Z;
696 // Replace Vertical speed with correction figure if significant 1052 float verticalCorrection = verticalError / m_VhoverTimescale;
697 if (Math.Abs(verticalError) > 0.01f) 1053 verticalCorrection *= m_VhoverEfficiency;
698 { 1054
699 m_newVelocity.Z += verticalCorrectionVelocity; 1055 hpos.Z += verticalCorrection;
700 //KF: m_VhoverEfficiency is not yet implemented 1056 VehiclePosition = hpos;
701 } 1057
702 else if (verticalError < -0.01) 1058 // Since we are hovering, we need to do the opposite of falling -- get rid of world Z
703 { 1059 Vector3 vel = VehicleVelocity;
704 m_newVelocity.Z -= verticalCorrectionVelocity; 1060 vel.Z = 0f;
705 } 1061 VehicleVelocity = vel;
706 else 1062
707 { 1063 /*
708 m_newVelocity.Z = 0f; 1064 float verticalCorrectionVelocity = verticalError / m_VhoverTimescale;
709 } 1065 Vector3 verticalCorrection = new Vector3(0f, 0f, verticalCorrectionVelocity);
1066 verticalCorrection *= m_vehicleMass;
1067
1068 // TODO: implement m_VhoverEfficiency correctly
1069 VehicleAddForceImpulse(verticalCorrection);
1070 */
1071
1072 VDetailLog("{0}, MoveLinear,hover,pos={1},eff={2},hoverTS={3},height={4},target={5},err={6},corr={7}",
1073 Prim.LocalID, VehiclePosition, m_VhoverEfficiency,
1074 m_VhoverTimescale, m_VhoverHeight, m_VhoverTargetHeight,
1075 verticalError, verticalCorrection);
710 } 1076 }
711 1077
712 VDetailLog("{0},MoveLinear,hover,pos={1},dir={2},height={3},target={4}", Prim.LocalID, pos, m_newVelocity, m_VhoverHeight, m_VhoverTargetHeight);
713 } 1078 }
1079 }
1080
1081 public bool ComputeLinearBlockingEndPoint(float pTimestep)
1082 {
1083 bool changed = false;
714 1084
1085 Vector3 pos = VehiclePosition;
715 Vector3 posChange = pos - m_lastPositionVector; 1086 Vector3 posChange = pos - m_lastPositionVector;
716 if (m_BlockingEndPoint != Vector3.Zero) 1087 if (m_BlockingEndPoint != Vector3.Zero)
717 { 1088 {
718 bool changed = false;
719 if (pos.X >= (m_BlockingEndPoint.X - (float)1)) 1089 if (pos.X >= (m_BlockingEndPoint.X - (float)1))
720 { 1090 {
721 pos.X -= posChange.X + 1; 1091 pos.X -= posChange.X + 1;
@@ -743,233 +1113,152 @@ namespace OpenSim.Region.Physics.BulletSPlugin
743 } 1113 }
744 if (changed) 1114 if (changed)
745 { 1115 {
746 Prim.ForcePosition = pos; 1116 VehiclePosition = pos;
747 VDetailLog("{0},MoveLinear,blockingEndPoint,block={1},origPos={2},pos={3}", 1117 VDetailLog("{0}, MoveLinear,blockingEndPoint,block={1},origPos={2},pos={3}",
748 Prim.LocalID, m_BlockingEndPoint, posChange, pos); 1118 Prim.LocalID, m_BlockingEndPoint, posChange, pos);
749 } 1119 }
750 } 1120 }
1121 return changed;
1122 }
751 1123
752 #region downForce 1124 // From http://wiki.secondlife.com/wiki/LlSetVehicleFlags :
753 Vector3 downForce = Vector3.Zero; 1125 // Prevent ground vehicles from motoring into the sky. This flag has a subtle effect when
1126 // used with conjunction with banking: the strength of the banking will decay when the
1127 // vehicle no longer experiences collisions. The decay timescale is the same as
1128 // VEHICLE_BANKING_TIMESCALE. This is to help prevent ground vehicles from steering
1129 // when they are in mid jump.
1130 // TODO: this code is wrong. Also, what should it do for boats (height from water)?
1131 // This is just using the ground and a general collision check. Should really be using
1132 // a downward raycast to find what is below.
1133 public void ComputeLinearMotorUp(float pTimestep)
1134 {
1135 Vector3 ret = Vector3.Zero;
754 1136
755 if ((m_flags & (VehicleFlag.LIMIT_MOTOR_UP)) != 0) 1137 if ((m_flags & (VehicleFlag.LIMIT_MOTOR_UP)) != 0)
756 { 1138 {
757 // If the vehicle is motoring into the sky, get it going back down. 1139 // This code tries to decide if the object is not on the ground and then pushing down
758 // Is this an angular force or both linear and angular?? 1140 /*
759 float distanceAboveGround = pos.Z - terrainHeight; 1141 float targetHeight = Type == Vehicle.TYPE_BOAT ? GetWaterLevel(VehiclePosition) : GetTerrainHeight(VehiclePosition);
760 if (distanceAboveGround > 2f) 1142 distanceAboveGround = VehiclePosition.Z - targetHeight;
1143 // Not colliding if the vehicle is off the ground
1144 if (!Prim.IsColliding)
761 { 1145 {
762 // downForce = new Vector3(0, 0, (-distanceAboveGround / m_bankingTimescale) * pTimestep);
763 // downForce = new Vector3(0, 0, -distanceAboveGround / m_bankingTimescale); 1146 // downForce = new Vector3(0, 0, -distanceAboveGround / m_bankingTimescale);
764 downForce = new Vector3(0, 0, -distanceAboveGround); 1147 VehicleVelocity += new Vector3(0, 0, -distanceAboveGround);
765 } 1148 }
766 // TODO: this calculation is all wrong. From the description at 1149 // TODO: this calculation is wrong. From the description at
767 // (http://wiki.secondlife.com/wiki/Category:LSL_Vehicle), the downForce 1150 // (http://wiki.secondlife.com/wiki/Category:LSL_Vehicle), the downForce
768 // has a decay factor. This says this force should 1151 // has a decay factor. This says this force should
769 // be computed with a motor. 1152 // be computed with a motor.
770 VDetailLog("{0},MoveLinear,limitMotorUp,distAbove={1},downForce={2}", 1153 // TODO: add interaction with banking.
771 Prim.LocalID, distanceAboveGround, downForce); 1154 VDetailLog("{0}, MoveLinear,limitMotorUp,distAbove={1},colliding={2},ret={3}",
772 } 1155 Prim.LocalID, distanceAboveGround, Prim.IsColliding, ret);
773 #endregion // downForce 1156 */
774 1157
775 // If not changing some axis, reduce out velocity 1158 // Another approach is to measure if we're going up. If going up and not colliding,
776 if ((m_flags & (VehicleFlag.NO_X)) != 0) 1159 // the vehicle is in the air. Fix that by pushing down.
777 m_newVelocity.X = 0; 1160 if (!Prim.IsColliding && VehicleVelocity.Z > 0.1)
778 if ((m_flags & (VehicleFlag.NO_Y)) != 0) 1161 {
779 m_newVelocity.Y = 0; 1162 // Get rid of any of the velocity vector that is pushing us up.
780 if ((m_flags & (VehicleFlag.NO_Z)) != 0) 1163 VehicleVelocity += new Vector3(0, 0, -VehicleVelocity.Z);
781 m_newVelocity.Z = 0;
782
783 // Clamp REALLY high or low velocities
784 if (m_newVelocity.LengthSquared() > 1e6f)
785 {
786 m_newVelocity /= m_newVelocity.Length();
787 m_newVelocity *= 1000f;
788 }
789 else if (m_newVelocity.LengthSquared() < 1e-6f)
790 m_newVelocity = Vector3.Zero;
791
792 // Stuff new linear velocity into the vehicle
793 Prim.ForceVelocity = m_newVelocity;
794 // Prim.ApplyForceImpulse((m_newVelocity - Prim.Velocity) * m_vehicleMass, false); // DEBUG DEBUG
795 1164
796 Vector3 totalDownForce = downForce + grav; 1165 // If we're pointed up into the air, we should nose down
797 if (totalDownForce != Vector3.Zero) 1166 Vector3 pointingDirection = Vector3.UnitX * VehicleOrientation;
798 { 1167 // The rotation around the Y axis is pitch up or down
799 Prim.AddForce(totalDownForce * m_vehicleMass, false); 1168 if (pointingDirection.Y > 0.01f)
800 // Prim.ApplyForceImpulse(totalDownForce * m_vehicleMass, false); 1169 {
1170 float angularCorrectionForce = -(float)Math.Asin(pointingDirection.Y);
1171 Vector3 angularCorrectionVector = new Vector3(0f, angularCorrectionForce, 0f);
1172 // Rotate into world coordinates and apply to vehicle
1173 angularCorrectionVector *= VehicleOrientation;
1174 VehicleAddAngularForce(angularCorrectionVector);
1175 VDetailLog("{0}, MoveLinear,limitMotorUp,newVel={1},pntDir={2},corrFrc={3},aCorr={4}",
1176 Prim.LocalID, VehicleVelocity, pointingDirection, angularCorrectionForce, angularCorrectionVector);
1177 }
1178 else
1179 {
1180 VDetailLog("{0}, MoveLinear,limitMotorUp,newVel={1},pntDir={2}",
1181 Prim.LocalID, VehicleVelocity, pointingDirection);
1182 }
1183 }
801 } 1184 }
1185 }
802 1186
803 VDetailLog("{0},MoveLinear,done,lmDir={1},lmVel={2},newVel={3},primVel={4},totalDown={5}", 1187 private void ApplyGravity(float pTimeStep)
804 Prim.LocalID, m_linearMotorDirection, m_lastLinearVelocityVector, m_newVelocity, Prim.Velocity, totalDownForce); 1188 {
1189 Vector3 appliedGravity = m_VehicleGravity * m_vehicleMass;
1190 VehicleAddForce(appliedGravity);
805 1191
806 } // end MoveLinear() 1192 VDetailLog("{0}, MoveLinear,applyGravity,vehGrav={1},appliedForce-{2}",
1193 Prim.LocalID, m_VehicleGravity, appliedGravity);
1194 }
807 1195
808 // ======================================================================= 1196 // =======================================================================
1197 // =======================================================================
809 // Apply the effect of the angular motor. 1198 // Apply the effect of the angular motor.
1199 // The 'contribution' is how much angular correction velocity each function wants.
1200 // All the contributions are added together and the resulting velocity is
1201 // set directly on the vehicle.
810 private void MoveAngular(float pTimestep) 1202 private void MoveAngular(float pTimestep)
811 { 1203 {
812 // m_angularMotorDirection // angular velocity requested by LSL motor 1204 // The user wants this many radians per second angular change?
813 // m_angularMotorApply // application frame counter 1205 Vector3 angularMotorContribution = m_angularMotor.Step(pTimestep);
814 // m_angularMotorVelocity // current angular motor velocity (ramps up and down) 1206
815 // m_angularMotorTimescale // motor angular velocity ramp up rate 1207 // ==================================================================
816 // m_angularMotorDecayTimescale // motor angular velocity decay rate 1208 // From http://wiki.secondlife.com/wiki/LlSetVehicleFlags :
817 // m_angularFrictionTimescale // body angular velocity decay rate 1209 // This flag prevents linear deflection parallel to world z-axis. This is useful
818 // m_lastAngularVelocity // what was last applied to body 1210 // for preventing ground vehicles with large linear deflection, like bumper cars,
819 1211 // from climbing their linear deflection into the sky.
820 if (m_angularMotorDirection.LengthSquared() > 0.0001) 1212 // That is, NO_DEFLECTION_UP says angular motion should not add any pitch or roll movement
821 { 1213 // TODO: This is here because this is where ODE put it but documentation says it
822 Vector3 origVel = m_angularMotorVelocity; 1214 // is a linear effect. Where should this check go?
823 Vector3 origDir = m_angularMotorDirection; 1215 if ((m_flags & (VehicleFlag.NO_DEFLECTION_UP)) != 0)
824
825 // new velocity += error / ( time to get there / step interval)
826 // requested direction - current vehicle direction
827 m_angularMotorVelocity += (m_angularMotorDirection - m_angularMotorVelocity) / (m_angularMotorTimescale / pTimestep);
828 // decay requested direction
829 m_angularMotorDirection *= (1.0f - (pTimestep * 1.0f/m_angularMotorDecayTimescale));
830
831 VDetailLog("{0},MoveAngular,angularMotorApply,angTScale={1},timeStep={2},origvel={3},origDir={4},vel={5}",
832 Prim.LocalID, m_angularMotorTimescale, pTimestep, origVel, origDir, m_angularMotorVelocity);
833 }
834 else
835 { 1216 {
836 m_angularMotorVelocity = Vector3.Zero; 1217 angularMotorContribution.X = 0f;
1218 angularMotorContribution.Y = 0f;
1219 VDetailLog("{0}, MoveAngular,noDeflectionUp,angularMotorContrib={1}", Prim.LocalID, angularMotorContribution);
837 } 1220 }
838 1221
839 #region Vertical attactor 1222 Vector3 verticalAttractionContribution = ComputeAngularVerticalAttraction();
840 1223
841 Vector3 vertattr = Vector3.Zero; 1224 Vector3 deflectionContribution = ComputeAngularDeflection();
842 Vector3 deflection = Vector3.Zero;
843 Vector3 banking = Vector3.Zero;
844 1225
845 // If vertical attaction timescale is reasonable and we applied an angular force last time... 1226 Vector3 bankingContribution = ComputeAngularBanking();
846 if (m_verticalAttractionTimescale < 300 && m_lastAngularVelocity != Vector3.Zero)
847 {
848 float VAservo = pTimestep * 0.2f / m_verticalAttractionTimescale;
849 if (Prim.IsColliding)
850 VAservo = pTimestep * 0.05f / (m_verticalAttractionTimescale);
851
852 VAservo *= (m_verticalAttractionEfficiency * m_verticalAttractionEfficiency);
853
854 // Create a vector of the vehicle "up" in world coordinates
855 Vector3 verticalError = Vector3.UnitZ * Prim.ForceOrientation;
856 // verticalError.X and .Y are the World error amounts. They are 0 when there is no
857 // error (Vehicle Body is 'vertical'), and .Z will be 1. As the body leans to its
858 // side |.X| will increase to 1 and .Z fall to 0. As body inverts |.X| will fall
859 // and .Z will go // negative. Similar for tilt and |.Y|. .X and .Y must be
860 // modulated to prevent a stable inverted body.
861
862 // Error is 0 (no error) to +/- 2 (max error)
863 if (verticalError.Z < 0.0f)
864 {
865 verticalError.X = 2.0f - verticalError.X;
866 verticalError.Y = 2.0f - verticalError.Y;
867 }
868 // scale it by VAservo (timestep and timescale)
869 verticalError = verticalError * VAservo;
870 1227
871 // As the body rotates around the X axis, then verticalError.Y increases; Rotated around Y 1228 // ==================================================================
872 // then .X increases, so change Body angular velocity X based on Y, and Y based on X. 1229 m_lastVertAttractor = verticalAttractionContribution;
873 // Z is not changed.
874 vertattr.X = verticalError.Y;
875 vertattr.Y = - verticalError.X;
876 vertattr.Z = 0f;
877 1230
878 // scaling appears better usingsquare-law 1231 m_lastAngularVelocity = angularMotorContribution
879 Vector3 angularVelocity = Prim.ForceRotationalVelocity; 1232 + verticalAttractionContribution
880 float bounce = 1.0f - (m_verticalAttractionEfficiency * m_verticalAttractionEfficiency); 1233 + deflectionContribution
881 vertattr.X += bounce * angularVelocity.X; 1234 + bankingContribution;
882 vertattr.Y += bounce * angularVelocity.Y;
883
884 VDetailLog("{0},MoveAngular,verticalAttraction,VAservo={1},effic={2},verticalError={3},bounce={4},vertattr={5}",
885 Prim.LocalID, VAservo, m_verticalAttractionEfficiency, verticalError, bounce, vertattr);
886
887 }
888 #endregion // Vertical attactor
889 1235
890 #region Deflection 1236 // Add of the above computation are made relative to vehicle coordinates.
1237 // Convert to world coordinates.
1238 m_lastAngularVelocity *= VehicleOrientation;
891 1239
892 if (m_angularDeflectionEfficiency != 0) 1240 // ==================================================================
1241 // Apply the correction velocity.
1242 // TODO: Should this be applied as an angular force (torque)?
1243 if (!m_lastAngularVelocity.ApproxEquals(Vector3.Zero, 0.01f))
893 { 1244 {
894 // Compute a scaled vector that points in the preferred axis (X direction) 1245 VehicleRotationalVelocity = m_lastAngularVelocity;
895 Vector3 scaledDefaultDirection =
896 new Vector3((pTimestep * 10 * (m_angularDeflectionEfficiency / m_angularDeflectionTimescale)), 0, 0);
897 // Adding the current vehicle orientation and reference frame displaces the orientation to the frame.
898 // Rotate the scaled default axix relative to the actual vehicle direction giving where it should point.
899 Vector3 preferredAxisOfMotion = scaledDefaultDirection * Quaternion.Add(Prim.ForceOrientation, m_referenceFrame);
900
901 // Scale by efficiency and timescale
902 deflection = (preferredAxisOfMotion * (m_angularDeflectionEfficiency) / m_angularDeflectionTimescale) * pTimestep;
903
904 VDetailLog("{0},MoveAngular,Deflection,perfAxis={1},deflection={2}",
905 Prim.LocalID, preferredAxisOfMotion, deflection);
906 // This deflection computation is not correct.
907 deflection = Vector3.Zero;
908 }
909
910 #endregion
911 1246
912 #region Banking 1247 VDetailLog("{0}, MoveAngular,done,nonZero,angMotorContrib={1},vertAttrContrib={2},bankContrib={3},deflectContrib={4},totalContrib={5}",
913 1248 Prim.LocalID,
914 if (m_bankingEfficiency != 0) 1249 angularMotorContribution, verticalAttractionContribution,
1250 bankingContribution, deflectionContribution,
1251 m_lastAngularVelocity
1252 );
1253 }
1254 else
915 { 1255 {
916 Vector3 dir = Vector3.One * Prim.ForceOrientation; 1256 // The vehicle is not adding anything angular wise.
917 float mult = (m_bankingMix*m_bankingMix)*-1*(m_bankingMix < 0 ? -1 : 1); 1257 VehicleRotationalVelocity = Vector3.Zero;
918 //Changes which way it banks in and out of turns 1258 VDetailLog("{0}, MoveAngular,done,zero", Prim.LocalID);
919
920 //Use the square of the efficiency, as it looks much more how SL banking works
921 float effSquared = (m_bankingEfficiency*m_bankingEfficiency);
922 if (m_bankingEfficiency < 0)
923 effSquared *= -1; //Keep the negative!
924
925 float mix = Math.Abs(m_bankingMix);
926 if (m_angularMotorVelocity.X == 0)
927 {
928 /*if (!parent.Orientation.ApproxEquals(this.m_referenceFrame, 0.25f))
929 {
930 Vector3 axisAngle;
931 float angle;
932 parent.Orientation.GetAxisAngle(out axisAngle, out angle);
933 Vector3 rotatedVel = parent.Velocity * parent.Orientation;
934 if ((rotatedVel.X < 0 && axisAngle.Y > 0) || (rotatedVel.X > 0 && axisAngle.Y < 0))
935 m_angularMotorVelocity.X += (effSquared * (mult * mix)) * (1f) * 10;
936 else
937 m_angularMotorVelocity.X += (effSquared * (mult * mix)) * (-1f) * 10;
938 }*/
939 }
940 else
941 banking.Z += (effSquared*(mult*mix))*(m_angularMotorVelocity.X) * 4;
942 if (!Prim.IsColliding && Math.Abs(m_angularMotorVelocity.X) > mix)
943 //If they are colliding, we probably shouldn't shove the prim around... probably
944 {
945 float angVelZ = m_angularMotorVelocity.X*-1;
946 /*if(angVelZ > mix)
947 angVelZ = mix;
948 else if(angVelZ < -mix)
949 angVelZ = -mix;*/
950 //This controls how fast and how far the banking occurs
951 Vector3 bankingRot = new Vector3(angVelZ*(effSquared*mult), 0, 0);
952 if (bankingRot.X > 3)
953 bankingRot.X = 3;
954 else if (bankingRot.X < -3)
955 bankingRot.X = -3;
956 bankingRot *= Prim.ForceOrientation;
957 banking += bankingRot;
958 }
959 m_angularMotorVelocity.X *= m_bankingEfficiency == 1 ? 0.0f : 1 - m_bankingEfficiency;
960 VDetailLog("{0},MoveAngular,Banking,bEff={1},angMotVel={2},banking={3}",
961 Prim.LocalID, m_bankingEfficiency, m_angularMotorVelocity, banking);
962 } 1259 }
963 1260
964 #endregion 1261 // ==================================================================
965
966 m_lastVertAttractor = vertattr;
967
968 // Sum velocities
969 m_lastAngularVelocity = m_angularMotorVelocity + vertattr + banking + deflection;
970
971 #region Linear Motor Offset
972
973 //Offset section 1262 //Offset section
974 if (m_linearMotorOffset != Vector3.Zero) 1263 if (m_linearMotorOffset != Vector3.Zero)
975 { 1264 {
@@ -985,8 +1274,8 @@ namespace OpenSim.Region.Physics.BulletSPlugin
985 // 1274 //
986 // The torque created is the linear velocity crossed with the offset 1275 // The torque created is the linear velocity crossed with the offset
987 1276
988 // NOTE: this computation does should be in the linear section 1277 // TODO: this computation should be in the linear section
989 // because there we know the impulse being applied. 1278 // because that is where we know the impulse being applied.
990 Vector3 torqueFromOffset = Vector3.Zero; 1279 Vector3 torqueFromOffset = Vector3.Zero;
991 // torqueFromOffset = Vector3.Cross(m_linearMotorOffset, appliedImpulse); 1280 // torqueFromOffset = Vector3.Cross(m_linearMotorOffset, appliedImpulse);
992 if (float.IsNaN(torqueFromOffset.X)) 1281 if (float.IsNaN(torqueFromOffset.X))
@@ -995,47 +1284,185 @@ namespace OpenSim.Region.Physics.BulletSPlugin
995 torqueFromOffset.Y = 0; 1284 torqueFromOffset.Y = 0;
996 if (float.IsNaN(torqueFromOffset.Z)) 1285 if (float.IsNaN(torqueFromOffset.Z))
997 torqueFromOffset.Z = 0; 1286 torqueFromOffset.Z = 0;
998 torqueFromOffset *= m_vehicleMass; 1287
999 Prim.ApplyTorqueImpulse(torqueFromOffset, true); 1288 VehicleAddAngularForce(torqueFromOffset * m_vehicleMass);
1000 VDetailLog("{0},BSDynamic.MoveAngular,motorOffset,applyTorqueImpulse={1}", Prim.LocalID, torqueFromOffset); 1289 VDetailLog("{0}, BSDynamic.MoveAngular,motorOffset,applyTorqueImpulse={1}", Prim.LocalID, torqueFromOffset);
1001 } 1290 }
1002 1291
1003 #endregion 1292 }
1293 // From http://wiki.secondlife.com/wiki/Linden_Vehicle_Tutorial:
1294 // Some vehicles, like boats, should always keep their up-side up. This can be done by
1295 // enabling the "vertical attractor" behavior that springs the vehicle's local z-axis to
1296 // the world z-axis (a.k.a. "up"). To take advantage of this feature you would set the
1297 // VEHICLE_VERTICAL_ATTRACTION_TIMESCALE to control the period of the spring frequency,
1298 // and then set the VEHICLE_VERTICAL_ATTRACTION_EFFICIENCY to control the damping. An
1299 // efficiency of 0.0 will cause the spring to wobble around its equilibrium, while an
1300 // efficiency of 1.0 will cause the spring to reach its equilibrium with exponential decay.
1301 public Vector3 ComputeAngularVerticalAttraction()
1302 {
1303 Vector3 ret = Vector3.Zero;
1004 1304
1005 if ((m_flags & (VehicleFlag.NO_DEFLECTION_UP)) != 0) 1305 // If vertical attaction timescale is reasonable
1306 if (enableAngularVerticalAttraction && m_verticalAttractionTimescale < m_verticalAttractionCutoff)
1006 { 1307 {
1007 m_lastAngularVelocity.X = 0; 1308 // Take a vector pointing up and convert it from world to vehicle relative coords.
1008 m_lastAngularVelocity.Y = 0; 1309 Vector3 verticalError = Vector3.UnitZ * VehicleOrientation;
1009 VDetailLog("{0},MoveAngular,noDeflectionUp,lastAngular={1}", Prim.LocalID, m_lastAngularVelocity); 1310
1311 // If vertical attraction correction is needed, the vector that was pointing up (UnitZ)
1312 // is now:
1313 // leaning to one side: rotated around the X axis with the Y value going
1314 // from zero (nearly straight up) to one (completely to the side)) or
1315 // leaning front-to-back: rotated around the Y axis with the value of X being between
1316 // zero and one.
1317 // The value of Z is how far the rotation is off with 1 meaning none and 0 being 90 degrees.
1318
1319 // Y error means needed rotation around X axis and visa versa.
1320 // Since the error goes from zero to one, the asin is the corresponding angle.
1321 ret.X = (float)Math.Asin(verticalError.Y);
1322 // (Tilt forward (positive X) needs to tilt back (rotate negative) around Y axis.)
1323 ret.Y = -(float)Math.Asin(verticalError.X);
1324
1325 // If verticalError.Z is negative, the vehicle is upside down. Add additional push.
1326 if (verticalError.Z < 0f)
1327 {
1328 ret.X += PIOverFour;
1329 ret.Y += PIOverFour;
1330 }
1331
1332 // 'ret' is now the necessary velocity to correct tilt in one second.
1333 // Correction happens over a number of seconds.
1334 Vector3 unscaledContrib = ret;
1335 ret /= m_verticalAttractionTimescale;
1336
1337 VDetailLog("{0}, MoveAngular,verticalAttraction,,verticalError={1},unscaled={2},eff={3},ts={4},vertAttr={5}",
1338 Prim.LocalID, verticalError, unscaledContrib, m_verticalAttractionEfficiency, m_verticalAttractionTimescale, ret);
1010 } 1339 }
1340 return ret;
1341 }
1342
1343 // Return the angular correction to correct the direction the vehicle is pointing to be
1344 // the direction is should want to be pointing.
1345 // The vehicle is moving in some direction and correct its orientation to it is pointing
1346 // in that direction.
1347 // TODO: implement reference frame.
1348 public Vector3 ComputeAngularDeflection()
1349 {
1350 Vector3 ret = Vector3.Zero;
1351
1352 // Since angularMotorUp and angularDeflection are computed independently, they will calculate
1353 // approximately the same X or Y correction. When added together (when contributions are combined)
1354 // this creates an over-correction and then wabbling as the target is overshot.
1355 // TODO: rethink how the different correction computations inter-relate.
1011 1356
1012 if (m_lastAngularVelocity.ApproxEquals(Vector3.Zero, 0.01f)) 1357 if (enableAngularDeflection && m_angularDeflectionEfficiency != 0 && VehicleForwardSpeed > 0.2)
1013 { 1358 {
1014 m_lastAngularVelocity = Vector3.Zero; // Reduce small value to zero. 1359 // The direction the vehicle is moving
1015 Prim.ZeroAngularMotion(true); 1360 Vector3 movingDirection = VehicleVelocity;
1016 VDetailLog("{0},MoveAngular,zeroAngularMotion,lastAngular={1}", Prim.LocalID, m_lastAngularVelocity); 1361 movingDirection.Normalize();
1362
1363 // If the vehicle is going backward, it is still pointing forward
1364 movingDirection *= Math.Sign(VehicleForwardSpeed);
1365
1366 // The direction the vehicle is pointing
1367 Vector3 pointingDirection = Vector3.UnitX * VehicleOrientation;
1368 pointingDirection.Normalize();
1369
1370 // The difference between what is and what should be.
1371 Vector3 deflectionError = movingDirection - pointingDirection;
1372
1373 // Don't try to correct very large errors (not our job)
1374 // if (Math.Abs(deflectionError.X) > PIOverFour) deflectionError.X = PIOverTwo * Math.Sign(deflectionError.X);
1375 // if (Math.Abs(deflectionError.Y) > PIOverFour) deflectionError.Y = PIOverTwo * Math.Sign(deflectionError.Y);
1376 // if (Math.Abs(deflectionError.Z) > PIOverFour) deflectionError.Z = PIOverTwo * Math.Sign(deflectionError.Z);
1377 if (Math.Abs(deflectionError.X) > PIOverFour) deflectionError.X = 0f;
1378 if (Math.Abs(deflectionError.Y) > PIOverFour) deflectionError.Y = 0f;
1379 if (Math.Abs(deflectionError.Z) > PIOverFour) deflectionError.Z = 0f;
1380
1381 // ret = m_angularDeflectionCorrectionMotor(1f, deflectionError);
1382
1383 // Scale the correction by recovery timescale and efficiency
1384 ret = (-deflectionError) * m_angularDeflectionEfficiency;
1385 ret /= m_angularDeflectionTimescale;
1386
1387 VDetailLog("{0}, MoveAngular,Deflection,movingDir={1},pointingDir={2},deflectError={3},ret={4}",
1388 Prim.LocalID, movingDirection, pointingDirection, deflectionError, ret);
1389 VDetailLog("{0}, MoveAngular,Deflection,fwdSpd={1},defEff={2},defTS={3}",
1390 Prim.LocalID, VehicleForwardSpeed, m_angularDeflectionEfficiency, m_angularDeflectionTimescale);
1017 } 1391 }
1018 else 1392 return ret;
1393 }
1394
1395 // Return an angular change to rotate the vehicle around the Z axis when the vehicle
1396 // is tipped around the X axis.
1397 // From http://wiki.secondlife.com/wiki/Linden_Vehicle_Tutorial:
1398 // The vertical attractor feature must be enabled in order for the banking behavior to
1399 // function. The way banking works is this: a rotation around the vehicle's roll-axis will
1400 // produce a angular velocity around the yaw-axis, causing the vehicle to turn. The magnitude
1401 // of the yaw effect will be proportional to the
1402 // VEHICLE_BANKING_EFFICIENCY, the angle of the roll rotation, and sometimes the vehicle's
1403 // velocity along its preferred axis of motion.
1404 // The VEHICLE_BANKING_EFFICIENCY can vary between -1 and +1. When it is positive then any
1405 // positive rotation (by the right-hand rule) about the roll-axis will effect a
1406 // (negative) torque around the yaw-axis, making it turn to the right--that is the
1407 // vehicle will lean into the turn, which is how real airplanes and motorcycle's work.
1408 // Negating the banking coefficient will make it so that the vehicle leans to the
1409 // outside of the turn (not very "physical" but might allow interesting vehicles so why not?).
1410 // The VEHICLE_BANKING_MIX is a fake (i.e. non-physical) parameter that is useful for making
1411 // banking vehicles do what you want rather than what the laws of physics allow.
1412 // For example, consider a real motorcycle...it must be moving forward in order for
1413 // it to turn while banking, however video-game motorcycles are often configured
1414 // to turn in place when at a dead stop--because they are often easier to control
1415 // that way using the limited interface of the keyboard or game controller. The
1416 // VEHICLE_BANKING_MIX enables combinations of both realistic and non-realistic
1417 // banking by functioning as a slider between a banking that is correspondingly
1418 // totally static (0.0) and totally dynamic (1.0). By "static" we mean that the
1419 // banking effect depends only on the vehicle's rotation about its roll-axis compared
1420 // to "dynamic" where the banking is also proportional to its velocity along its
1421 // roll-axis. Finding the best value of the "mixture" will probably require trial and error.
1422 // The time it takes for the banking behavior to defeat a preexisting angular velocity about the
1423 // world z-axis is determined by the VEHICLE_BANKING_TIMESCALE. So if you want the vehicle to
1424 // bank quickly then give it a banking timescale of about a second or less, otherwise you can
1425 // make a sluggish vehicle by giving it a timescale of several seconds.
1426 public Vector3 ComputeAngularBanking()
1427 {
1428 Vector3 ret = Vector3.Zero;
1429
1430 if (enableAngularBanking && m_bankingEfficiency != 0 && m_verticalAttractionTimescale < m_verticalAttractionCutoff)
1019 { 1431 {
1020 // Apply to the body. 1432 // Rotate a UnitZ vector (pointing up) to how the vehicle is oriented.
1021 // The above calculates the absolute angular velocity needed. Angular velocity is massless. 1433 // As the vehicle rolls to the right or left, the Y value will increase from
1022 // Since we are stuffing the angular velocity directly into the object, the computed 1434 // zero (straight up) to 1 or -1 (full tilt right or left)
1023 // velocity needs to be scaled by the timestep. 1435 Vector3 rollComponents = Vector3.UnitZ * VehicleOrientation;
1024 Vector3 applyAngularForce = ((m_lastAngularVelocity * pTimestep) - Prim.ForceRotationalVelocity); 1436
1025 Prim.ForceRotationalVelocity = applyAngularForce; 1437 // Figure out the yaw value for this much roll.
1026 1438 // Squared because that seems to give a good value
1027 // Decay the angular movement for next time 1439 float yawAngle = (float)Math.Asin(rollComponents.Y * rollComponents.Y) * m_bankingEfficiency;
1028 Vector3 decayamount = (Vector3.One / m_angularFrictionTimescale) * pTimestep; 1440
1029 m_lastAngularVelocity *= Vector3.One - decayamount; 1441 // actual error = static turn error + dynamic turn error
1030 1442 float mixedYawAngle = yawAngle * (1f - m_bankingMix) + yawAngle * m_bankingMix * VehicleForwardSpeed;
1031 VDetailLog("{0},MoveAngular,done,newRotVel={1},decay={2},lastAngular={3}", 1443
1032 Prim.LocalID, applyAngularForce, decayamount, m_lastAngularVelocity); 1444 // TODO: the banking effect should not go to infinity but what to limit it to?
1445 mixedYawAngle = ClampInRange(-20f, mixedYawAngle, 20f);
1446
1447 // Build the force vector to change rotation from what it is to what it should be
1448 ret.Z = -mixedYawAngle;
1449
1450 // Don't do it all at once.
1451 ret /= m_bankingTimescale;
1452
1453 VDetailLog("{0}, MoveAngular,Banking,rollComp={1},speed={2},rollComp={3},yAng={4},mYAng={5},ret={6}",
1454 Prim.LocalID, rollComponents, VehicleForwardSpeed, rollComponents, yawAngle, mixedYawAngle, ret);
1033 } 1455 }
1034 } //end MoveAngular 1456 return ret;
1457 }
1035 1458
1459 // This is from previous instantiations of XXXDynamics.cs.
1460 // Applies roll reference frame.
1461 // TODO: is this the right way to separate the code to do this operation?
1462 // Should this be in MoveAngular()?
1036 internal void LimitRotation(float timestep) 1463 internal void LimitRotation(float timestep)
1037 { 1464 {
1038 Quaternion rotq = Prim.ForceOrientation; 1465 Quaternion rotq = VehicleOrientation;
1039 Quaternion m_rot = rotq; 1466 Quaternion m_rot = rotq;
1040 if (m_RollreferenceFrame != Quaternion.Identity) 1467 if (m_RollreferenceFrame != Quaternion.Identity)
1041 { 1468 {
@@ -1063,12 +1490,18 @@ namespace OpenSim.Region.Physics.BulletSPlugin
1063 } 1490 }
1064 if (rotq != m_rot) 1491 if (rotq != m_rot)
1065 { 1492 {
1066 Prim.ForceOrientation = m_rot; 1493 VehicleOrientation = m_rot;
1067 VDetailLog("{0},LimitRotation,done,orig={1},new={2}", Prim.LocalID, rotq, m_rot); 1494 VDetailLog("{0}, LimitRotation,done,orig={1},new={2}", Prim.LocalID, rotq, m_rot);
1068 } 1495 }
1069 1496
1070 } 1497 }
1071 1498
1499 private float ClampInRange(float low, float val, float high)
1500 {
1501 return Math.Max(low, Math.Min(val, high));
1502 // return Utils.Clamp(val, low, high);
1503 }
1504
1072 // Invoke the detailed logger and output something if it's enabled. 1505 // Invoke the detailed logger and output something if it's enabled.
1073 private void VDetailLog(string msg, params Object[] args) 1506 private void VDetailLog(string msg, params Object[] args)
1074 { 1507 {
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSLinkset.cs b/OpenSim/Region/Physics/BulletSPlugin/BSLinkset.cs
index 0df4310..cbd160f 100755
--- a/OpenSim/Region/Physics/BulletSPlugin/BSLinkset.cs
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSLinkset.cs
@@ -32,6 +32,15 @@ using OMV = OpenMetaverse;
32 32
33namespace OpenSim.Region.Physics.BulletSPlugin 33namespace OpenSim.Region.Physics.BulletSPlugin
34{ 34{
35
36// A BSPrim can get individual information about its linkedness attached
37// to it through an instance of a subclass of LinksetInfo.
38// Each type of linkset will define the information needed for its type.
39public abstract class BSLinksetInfo
40{
41 public virtual void Clear() { }
42}
43
35public abstract class BSLinkset 44public abstract class BSLinkset
36{ 45{
37 // private static string LogHeader = "[BULLETSIM LINKSET]"; 46 // private static string LogHeader = "[BULLETSIM LINKSET]";
@@ -47,7 +56,7 @@ public abstract class BSLinkset
47 { 56 {
48 BSLinkset ret = null; 57 BSLinkset ret = null;
49 58
50 switch ((int)physScene.Params.linksetImplementation) 59 switch ((int)BSParam.LinksetImplementation)
51 { 60 {
52 case (int)LinksetImplementation.Constraint: 61 case (int)LinksetImplementation.Constraint:
53 ret = new BSLinksetConstraints(physScene, parent); 62 ret = new BSLinksetConstraints(physScene, parent);
@@ -87,22 +96,8 @@ public abstract class BSLinkset
87 return BSPhysicsShapeType.SHAPE_UNKNOWN; 96 return BSPhysicsShapeType.SHAPE_UNKNOWN;
88 } 97 }
89 98
90 // Linksets move around the children so the linkset might need to compute the child position
91 public virtual OMV.Vector3 Position(BSPhysObject member)
92 { return member.RawPosition; }
93 public virtual OMV.Quaternion Orientation(BSPhysObject member)
94 { return member.RawOrientation; }
95 // TODO: does this need to be done for Velocity and RotationalVelocityy?
96
97 // We keep the prim's mass in the linkset structure since it could be dependent on other prims 99 // We keep the prim's mass in the linkset structure since it could be dependent on other prims
98 protected float m_mass; 100 public float LinksetMass { get; protected set; }
99 public float LinksetMass
100 {
101 get
102 {
103 return m_mass;
104 }
105 }
106 101
107 public virtual bool LinksetIsColliding { get { return false; } } 102 public virtual bool LinksetIsColliding { get { return false; } }
108 103
@@ -116,7 +111,7 @@ public abstract class BSLinkset
116 get { return ComputeLinksetGeometricCenter(); } 111 get { return ComputeLinksetGeometricCenter(); }
117 } 112 }
118 113
119 protected void Initialize(BSScene scene, BSPhysObject parent) 114 protected BSLinkset(BSScene scene, BSPhysObject parent)
120 { 115 {
121 // A simple linkset of one (no children) 116 // A simple linkset of one (no children)
122 LinksetID = m_nextLinksetID++; 117 LinksetID = m_nextLinksetID++;
@@ -126,7 +121,8 @@ public abstract class BSLinkset
126 PhysicsScene = scene; 121 PhysicsScene = scene;
127 LinksetRoot = parent; 122 LinksetRoot = parent;
128 m_children = new HashSet<BSPhysObject>(); 123 m_children = new HashSet<BSPhysObject>();
129 m_mass = parent.RawMass; 124 LinksetMass = parent.RawMass;
125 Rebuilding = false;
130 } 126 }
131 127
132 // Link to a linkset where the child knows the parent. 128 // Link to a linkset where the child knows the parent.
@@ -140,7 +136,7 @@ public abstract class BSLinkset
140 // Don't add the root to its own linkset 136 // Don't add the root to its own linkset
141 if (!IsRoot(child)) 137 if (!IsRoot(child))
142 AddChildToLinkset(child); 138 AddChildToLinkset(child);
143 m_mass = ComputeLinksetMass(); 139 LinksetMass = ComputeLinksetMass();
144 } 140 }
145 return this; 141 return this;
146 } 142 }
@@ -156,13 +152,15 @@ public abstract class BSLinkset
156 if (IsRoot(child)) 152 if (IsRoot(child))
157 { 153 {
158 // Cannot remove the root from a linkset. 154 // Cannot remove the root from a linkset.
155 child.PositionDisplacement = OMV.Vector3.Zero;
159 return this; 156 return this;
160 } 157 }
161 RemoveChildFromLinkset(child); 158 RemoveChildFromLinkset(child);
162 m_mass = ComputeLinksetMass(); 159 LinksetMass = ComputeLinksetMass();
163 } 160 }
164 161
165 // The child is down to a linkset of just itself 162 // The child is down to a linkset of just itself
163 child.PositionDisplacement = OMV.Vector3.Zero;
166 return BSLinkset.Factory(PhysicsScene, child); 164 return BSLinkset.Factory(PhysicsScene, child);
167 } 165 }
168 166
@@ -219,7 +217,7 @@ public abstract class BSLinkset
219 // I am the root of a linkset and a new child is being added 217 // I am the root of a linkset and a new child is being added
220 // Called while LinkActivity is locked. 218 // Called while LinkActivity is locked.
221 protected abstract void AddChildToLinkset(BSPhysObject child); 219 protected abstract void AddChildToLinkset(BSPhysObject child);
222 220
223 // I am the root of a linkset and one of my children is being removed. 221 // I am the root of a linkset and one of my children is being removed.
224 // Safe to call even if the child is not really in my linkset. 222 // Safe to call even if the child is not really in my linkset.
225 protected abstract void RemoveChildFromLinkset(BSPhysObject child); 223 protected abstract void RemoveChildFromLinkset(BSPhysObject child);
@@ -227,7 +225,14 @@ public abstract class BSLinkset
227 // When physical properties are changed the linkset needs to recalculate 225 // When physical properties are changed the linkset needs to recalculate
228 // its internal properties. 226 // its internal properties.
229 // May be called at runtime or taint-time. 227 // May be called at runtime or taint-time.
230 public abstract void Refresh(BSPhysObject requestor); 228 public virtual void Refresh(BSPhysObject requestor)
229 {
230 LinksetMass = ComputeLinksetMass();
231 }
232
233 // Flag denoting the linkset is in the process of being rebuilt.
234 // Used to know not the schedule a rebuild in the middle of a rebuild.
235 protected bool Rebuilding { get; set; }
231 236
232 // The object is going dynamic (physical). Do any setup necessary 237 // The object is going dynamic (physical). Do any setup necessary
233 // for a dynamic linkset. 238 // for a dynamic linkset.
@@ -245,8 +250,9 @@ public abstract class BSLinkset
245 250
246 // Called when a parameter update comes from the physics engine for any object 251 // Called when a parameter update comes from the physics engine for any object
247 // of the linkset is received. 252 // of the linkset is received.
253 // Passed flag is update came from physics engine (true) or the user (false).
248 // Called at taint-time!! 254 // Called at taint-time!!
249 public abstract void UpdateProperties(BSPhysObject physObject); 255 public abstract void UpdateProperties(BSPhysObject physObject, bool physicalUpdate);
250 256
251 // Routine used when rebuilding the body of the root of the linkset 257 // Routine used when rebuilding the body of the root of the linkset
252 // Destroy all the constraints have have been made to root. 258 // Destroy all the constraints have have been made to root.
@@ -306,7 +312,7 @@ public abstract class BSLinkset
306 312
307 foreach (BSPhysObject bp in m_children) 313 foreach (BSPhysObject bp in m_children)
308 { 314 {
309 com += bp.Position * bp.RawMass; 315 com += bp.Position;
310 } 316 }
311 com /= (m_children.Count + 1); 317 com /= (m_children.Count + 1);
312 } 318 }
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSLinksetCompound.cs b/OpenSim/Region/Physics/BulletSPlugin/BSLinksetCompound.cs
index b9c2cf9..2dc89b5 100755
--- a/OpenSim/Region/Physics/BulletSPlugin/BSLinksetCompound.cs
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSLinksetCompound.cs
@@ -28,22 +28,64 @@ using System;
28using System.Collections.Generic; 28using System.Collections.Generic;
29using System.Text; 29using System.Text;
30 30
31using OpenSim.Framework;
32
31using OMV = OpenMetaverse; 33using OMV = OpenMetaverse;
32 34
33namespace OpenSim.Region.Physics.BulletSPlugin 35namespace OpenSim.Region.Physics.BulletSPlugin
34{ 36{
37
38// When a child is linked, the relationship position of the child to the parent
39// is remembered so the child's world position can be recomputed when it is
40// removed from the linkset.
41sealed class BSLinksetCompoundInfo : BSLinksetInfo
42{
43 public int Index;
44 public OMV.Vector3 OffsetFromRoot;
45 public OMV.Vector3 OffsetFromCenterOfMass;
46 public OMV.Quaternion OffsetRot;
47 public BSLinksetCompoundInfo(int indx, OMV.Vector3 p, OMV.Quaternion r)
48 {
49 Index = indx;
50 OffsetFromRoot = p;
51 OffsetFromCenterOfMass = p;
52 OffsetRot = r;
53 }
54 public override void Clear()
55 {
56 Index = 0;
57 OffsetFromRoot = OMV.Vector3.Zero;
58 OffsetFromCenterOfMass = OMV.Vector3.Zero;
59 OffsetRot = OMV.Quaternion.Identity;
60 }
61 public override string ToString()
62 {
63 StringBuilder buff = new StringBuilder();
64 buff.Append("<i=");
65 buff.Append(Index.ToString());
66 buff.Append(",p=");
67 buff.Append(OffsetFromRoot.ToString());
68 buff.Append(",m=");
69 buff.Append(OffsetFromCenterOfMass.ToString());
70 buff.Append(",r=");
71 buff.Append(OffsetRot.ToString());
72 buff.Append(">");
73 return buff.ToString();
74 }
75};
76
35public sealed class BSLinksetCompound : BSLinkset 77public sealed class BSLinksetCompound : BSLinkset
36{ 78{
37 private static string LogHeader = "[BULLETSIM LINKSET COMPOUND]"; 79 private static string LogHeader = "[BULLETSIM LINKSET COMPOUND]";
38 80
39 public BSLinksetCompound(BSScene scene, BSPhysObject parent) 81 public BSLinksetCompound(BSScene scene, BSPhysObject parent) : base(scene, parent)
40 { 82 {
41 base.Initialize(scene, parent);
42 } 83 }
43 84
44 // For compound implimented linksets, if there are children, use compound shape for the root. 85 // For compound implimented linksets, if there are children, use compound shape for the root.
45 public override BSPhysicsShapeType PreferredPhysicalShape(BSPhysObject requestor) 86 public override BSPhysicsShapeType PreferredPhysicalShape(BSPhysObject requestor)
46 { 87 {
88 // Returning 'unknown' means we don't have a preference.
47 BSPhysicsShapeType ret = BSPhysicsShapeType.SHAPE_UNKNOWN; 89 BSPhysicsShapeType ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
48 if (IsRoot(requestor) && HasAnyChildren) 90 if (IsRoot(requestor) && HasAnyChildren)
49 { 91 {
@@ -55,27 +97,33 @@ public sealed class BSLinksetCompound : BSLinkset
55 97
56 // When physical properties are changed the linkset needs to recalculate 98 // When physical properties are changed the linkset needs to recalculate
57 // its internal properties. 99 // its internal properties.
58 // This is queued in the 'post taint' queue so the
59 // refresh will happen once after all the other taints are applied.
60 public override void Refresh(BSPhysObject requestor) 100 public override void Refresh(BSPhysObject requestor)
61 { 101 {
62 // External request for Refresh (from BSPrim) is not necessary 102 base.Refresh(requestor);
63 // InternalRefresh(requestor); 103
104 // Something changed so do the rebuilding thing
105 // ScheduleRebuild();
64 } 106 }
65 107
66 private void InternalRefresh(BSPhysObject requestor) 108 // Schedule a refresh to happen after all the other taint processing.
109 private void ScheduleRebuild(BSPhysObject requestor)
67 { 110 {
68 DetailLog("{0},BSLinksetCompound.Refresh,schedulingRefresh,requestor={1}", LinksetRoot.LocalID, requestor.LocalID); 111 DetailLog("{0},BSLinksetCompound.ScheduleRebuild,,rebuilding={1},hasChildren={2}",
69 // Queue to happen after all the other taint processing 112 requestor.LocalID, Rebuilding, HasAnyChildren);
70 PhysicsScene.PostTaintObject("BSLinksetCompound.Refresh", requestor.LocalID, delegate() 113 // When rebuilding, it is possible to set properties that would normally require a rebuild.
114 // If already rebuilding, don't request another rebuild.
115 // If a linkset with just a root prim (simple non-linked prim) don't bother rebuilding.
116 if (!Rebuilding && HasAnyChildren)
71 { 117 {
72 if (IsRoot(requestor) && HasAnyChildren) 118 PhysicsScene.PostTaintObject("BSLinksetCompound.ScheduleRebuild", LinksetRoot.LocalID, delegate()
73 RecomputeLinksetCompound(); 119 {
74 }); 120 if (HasAnyChildren)
121 RecomputeLinksetCompound();
122 });
123 }
75 } 124 }
76 125
77 // The object is going dynamic (physical). Do any setup necessary 126 // The object is going dynamic (physical). Do any setup necessary for a dynamic linkset.
78 // for a dynamic linkset.
79 // Only the state of the passed object can be modified. The rest of the linkset 127 // Only the state of the passed object can be modified. The rest of the linkset
80 // has not yet been fully constructed. 128 // has not yet been fully constructed.
81 // Return 'true' if any properties updated on the passed object. 129 // Return 'true' if any properties updated on the passed object.
@@ -84,12 +132,22 @@ public sealed class BSLinksetCompound : BSLinkset
84 { 132 {
85 bool ret = false; 133 bool ret = false;
86 DetailLog("{0},BSLinksetCompound.MakeDynamic,call,IsRoot={1}", child.LocalID, IsRoot(child)); 134 DetailLog("{0},BSLinksetCompound.MakeDynamic,call,IsRoot={1}", child.LocalID, IsRoot(child));
87 if (!IsRoot(child)) 135 if (IsRoot(child))
136 {
137 // The root is going dynamic. Rebuild the linkset so parts and mass get computed properly.
138 ScheduleRebuild(LinksetRoot);
139 }
140 else
88 { 141 {
89 // Physical children are removed from the world as the shape ofthe root compound 142 // The origional prims are removed from the world as the shape of the root compound
90 // shape takes over. 143 // shape takes over.
91 BulletSimAPI.AddToCollisionFlags2(child.PhysBody.ptr, CollisionFlags.CF_NO_CONTACT_RESPONSE); 144 PhysicsScene.PE.AddToCollisionFlags(child.PhysBody, CollisionFlags.CF_NO_CONTACT_RESPONSE);
92 BulletSimAPI.ForceActivationState2(child.PhysBody.ptr, ActivationState.DISABLE_SIMULATION); 145 PhysicsScene.PE.ForceActivationState(child.PhysBody, ActivationState.DISABLE_SIMULATION);
146 // We don't want collisions from the old linkset children.
147 PhysicsScene.PE.RemoveFromCollisionFlags(child.PhysBody, CollisionFlags.BS_SUBSCRIBE_COLLISION_EVENTS);
148
149 child.PhysBody.collisionType = CollisionType.LinksetChild;
150
93 ret = true; 151 ret = true;
94 } 152 }
95 return ret; 153 return ret;
@@ -104,33 +162,45 @@ public sealed class BSLinksetCompound : BSLinkset
104 { 162 {
105 bool ret = false; 163 bool ret = false;
106 DetailLog("{0},BSLinksetCompound.MakeStatic,call,IsRoot={1}", child.LocalID, IsRoot(child)); 164 DetailLog("{0},BSLinksetCompound.MakeStatic,call,IsRoot={1}", child.LocalID, IsRoot(child));
107 if (!IsRoot(child)) 165 if (IsRoot(child))
166 {
167 ScheduleRebuild(LinksetRoot);
168 }
169 else
108 { 170 {
109 // The non-physical children can come back to life. 171 // The non-physical children can come back to life.
110 BulletSimAPI.RemoveFromCollisionFlags2(child.PhysBody.ptr, CollisionFlags.CF_NO_CONTACT_RESPONSE); 172 PhysicsScene.PE.RemoveFromCollisionFlags(child.PhysBody, CollisionFlags.CF_NO_CONTACT_RESPONSE);
111 // Don't force activation so setting of DISABLE_SIMULATION can stay. 173
112 BulletSimAPI.Activate2(child.PhysBody.ptr, false); 174 child.PhysBody.collisionType = CollisionType.LinksetChild;
175
176 // Don't force activation so setting of DISABLE_SIMULATION can stay if used.
177 PhysicsScene.PE.Activate(child.PhysBody, false);
113 ret = true; 178 ret = true;
114 } 179 }
115 return ret; 180 return ret;
116 } 181 }
117 182
118 // Called at taint-time!! 183 // 'physicalUpdate' is true if these changes came directly from the physics engine. Don't need to rebuild then.
119 public override void UpdateProperties(BSPhysObject updated) 184 // Called at taint-time.
120 { 185 public override void UpdateProperties(BSPhysObject updated, bool physicalUpdate)
121 // Nothing to do for constraints on property updates
122 }
123
124 // The children move around in relationship to the root.
125 // Just grab the current values of wherever it is right now.
126 public override OMV.Vector3 Position(BSPhysObject member)
127 {
128 return BulletSimAPI.GetPosition2(member.PhysBody.ptr);
129 }
130
131 public override OMV.Quaternion Orientation(BSPhysObject member)
132 { 186 {
133 return BulletSimAPI.GetOrientation2(member.PhysBody.ptr); 187 // The user moving a child around requires the rebuilding of the linkset compound shape
188 // One problem is this happens when a border is crossed -- the simulator implementation
189 // is to store the position into the group which causes the move of the object
190 // but it also means all the child positions get updated.
191 // What would cause an unnecessary rebuild so we make sure the linkset is in a
192 // region before bothering to do a rebuild.
193 if (!IsRoot(updated)
194 && !physicalUpdate
195 && PhysicsScene.TerrainManager.IsWithinKnownTerrain(LinksetRoot.RawPosition))
196 {
197 // TODO: replace this with are calculation of the child prim's orientation and pos.
198 // TODO: for the moment, don't rebuild the compound shape.
199 // This is often just the car turning its wheels. When we can just reorient the one
200 // member shape of the compound shape, the overhead of rebuilding won't be a problem.
201 // updated.LinksetInfo = null;
202 // ScheduleRebuild(updated);
203 }
134 } 204 }
135 205
136 // Routine called when rebuilding the body of some member of the linkset. 206 // Routine called when rebuilding the body of some member of the linkset.
@@ -142,24 +212,62 @@ public sealed class BSLinksetCompound : BSLinkset
142 bool ret = false; 212 bool ret = false;
143 213
144 DetailLog("{0},BSLinksetCompound.RemoveBodyDependencies,refreshIfChild,rID={1},rBody={2},isRoot={3}", 214 DetailLog("{0},BSLinksetCompound.RemoveBodyDependencies,refreshIfChild,rID={1},rBody={2},isRoot={3}",
145 child.LocalID, LinksetRoot.LocalID, LinksetRoot.PhysBody.ptr.ToString("X"), IsRoot(child)); 215 child.LocalID, LinksetRoot.LocalID, LinksetRoot.PhysBody.AddrString, IsRoot(child));
146 216
147 if (!IsRoot(child)) 217 if (!IsRoot(child))
148 { 218 {
149 // Cause the current shape to be freed and the new one to be built. 219 // Because it is a convenient time, recompute child world position and rotation based on
150 InternalRefresh(LinksetRoot); 220 // its position in the linkset.
151 ret = true; 221 RecomputeChildWorldPosition(child, true);
152 } 222 }
153 223
224 // Cannot schedule a refresh/rebuild here because this routine is called when
225 // the linkset is being rebuilt.
226 // InternalRefresh(LinksetRoot);
227
154 return ret; 228 return ret;
155 } 229 }
156 230
157 // Companion to RemoveBodyDependencies(). If RemoveBodyDependencies() returns 'true', 231 // Companion to RemoveBodyDependencies(). If RemoveBodyDependencies() returns 'true',
158 // this routine will restore the removed constraints. 232 // this routine will restore the removed constraints.
159 // Called at taint-time!! 233 // Called at taint-time!!
160 public override void RestoreBodyDependencies(BSPrim child) 234 public override void RestoreBodyDependencies(BSPrim child)
161 { 235 {
162 // The Refresh operation queued by RemoveBodyDependencies() will build any missing constraints. 236 }
237
238 // When the linkset is built, the child shape is added to the compound shape relative to the
239 // root shape. The linkset then moves around but this does not move the actual child
240 // prim. The child prim's location must be recomputed based on the location of the root shape.
241 private void RecomputeChildWorldPosition(BSPhysObject child, bool inTaintTime)
242 {
243 BSLinksetCompoundInfo lci = child.LinksetInfo as BSLinksetCompoundInfo;
244 if (lci != null)
245 {
246 if (inTaintTime)
247 {
248 OMV.Vector3 oldPos = child.RawPosition;
249 child.ForcePosition = LinksetRoot.RawPosition + lci.OffsetFromRoot;
250 child.ForceOrientation = LinksetRoot.RawOrientation * lci.OffsetRot;
251 DetailLog("{0},BSLinksetCompound.RecomputeChildWorldPosition,oldPos={1},lci={2},newPos={3}",
252 child.LocalID, oldPos, lci, child.RawPosition);
253 }
254 else
255 {
256 // TaintedObject is not used here so the raw position is set now and not at taint-time.
257 child.Position = LinksetRoot.RawPosition + lci.OffsetFromRoot;
258 child.Orientation = LinksetRoot.RawOrientation * lci.OffsetRot;
259 }
260 }
261 else
262 {
263 // This happens when children have been added to the linkset but the linkset
264 // has not been constructed yet. So like, at taint time, adding children to a linkset
265 // and then changing properties of the children (makePhysical, for instance)
266 // but the post-print action of actually rebuilding the linkset has not yet happened.
267 // PhysicsScene.Logger.WarnFormat("{0} Restoring linkset child position failed because of no relative position computed. ID={1}",
268 // LogHeader, child.LocalID);
269 DetailLog("{0},BSLinksetCompound.recomputeChildWorldPosition,noRelativePositonInfo", child.LocalID);
270 }
163 } 271 }
164 272
165 // ================================================================ 273 // ================================================================
@@ -174,24 +282,25 @@ public sealed class BSLinksetCompound : BSLinkset
174 282
175 DetailLog("{0},BSLinksetCompound.AddChildToLinkset,call,child={1}", LinksetRoot.LocalID, child.LocalID); 283 DetailLog("{0},BSLinksetCompound.AddChildToLinkset,call,child={1}", LinksetRoot.LocalID, child.LocalID);
176 284
177 // Cause constraints and assorted properties to be recomputed before the next simulation step. 285 // Rebuild the compound shape with the new child shape included
178 InternalRefresh(LinksetRoot); 286 ScheduleRebuild(child);
179 } 287 }
180 return; 288 return;
181 } 289 }
182 290
183 // Remove the specified child from the linkset. 291 // Remove the specified child from the linkset.
184 // Safe to call even if the child is not really in my linkset. 292 // Safe to call even if the child is not really in the linkset.
185 protected override void RemoveChildFromLinkset(BSPhysObject child) 293 protected override void RemoveChildFromLinkset(BSPhysObject child)
186 { 294 {
187 if (m_children.Remove(child)) 295 if (m_children.Remove(child))
188 { 296 {
189 DetailLog("{0},BSLinksetCompound.RemoveChildFromLinkset,call,rID={1},rBody={2},cID={3},cBody={4}", 297 DetailLog("{0},BSLinksetCompound.RemoveChildFromLinkset,call,rID={1},rBody={2},cID={3},cBody={4}",
190 child.LocalID, 298 child.LocalID,
191 LinksetRoot.LocalID, LinksetRoot.PhysBody.ptr.ToString("X"), 299 LinksetRoot.LocalID, LinksetRoot.PhysBody.AddrString,
192 child.LocalID, child.PhysBody.ptr.ToString("X")); 300 child.LocalID, child.PhysBody.AddrString);
193 301
194 // Cause the child's body to be rebuilt and thus restored to normal operation 302 // Cause the child's body to be rebuilt and thus restored to normal operation
303 RecomputeChildWorldPosition(child, false);
195 child.ForceBodyShapeRebuild(false); 304 child.ForceBodyShapeRebuild(false);
196 305
197 if (!HasAnyChildren) 306 if (!HasAnyChildren)
@@ -201,8 +310,8 @@ public sealed class BSLinksetCompound : BSLinkset
201 } 310 }
202 else 311 else
203 { 312 {
204 // Schedule a rebuild of the linkset before the next simulation tick. 313 // Rebuild the compound shape with the child removed
205 InternalRefresh(LinksetRoot); 314 ScheduleRebuild(child);
206 } 315 }
207 } 316 }
208 return; 317 return;
@@ -213,63 +322,116 @@ public sealed class BSLinksetCompound : BSLinkset
213 // Constraint linksets are rebuilt every time. 322 // Constraint linksets are rebuilt every time.
214 // Note that this works for rebuilding just the root after a linkset is taken apart. 323 // Note that this works for rebuilding just the root after a linkset is taken apart.
215 // Called at taint time!! 324 // Called at taint time!!
325 private bool disableCOM = true; // disable until we get this debugged
216 private void RecomputeLinksetCompound() 326 private void RecomputeLinksetCompound()
217 { 327 {
218 // Cause the root shape to be rebuilt as a compound object with just the root in it 328 try
219 LinksetRoot.ForceBodyShapeRebuild(true);
220
221 DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,start,rBody={1},rShape={2},numChildren={3}",
222 LinksetRoot.LocalID, LinksetRoot.PhysBody, LinksetRoot.PhysShape, NumberOfChildren);
223
224 // Add a shape for each of the other children in the linkset
225 ForEachMember(delegate(BSPhysObject cPrim)
226 { 329 {
227 if (!IsRoot(cPrim)) 330 // Suppress rebuilding while rebuilding
331 Rebuilding = true;
332
333 // Cause the root shape to be rebuilt as a compound object with just the root in it
334 LinksetRoot.ForceBodyShapeRebuild(true);
335
336 // The center of mass for the linkset is the geometric center of the group.
337 // Compute a displacement for each component so it is relative to the center-of-mass.
338 // Bullet presumes an object's origin (relative <0,0,0>) is its center-of-mass
339 OMV.Vector3 centerOfMass;
340 OMV.Vector3 centerDisplacement = OMV.Vector3.Zero;
341 if (disableCOM) // DEBUG DEBUG
342 { // DEBUG DEBUG
343 centerOfMass = LinksetRoot.RawPosition; // DEBUG DEBUG
344 LinksetRoot.PositionDisplacement = OMV.Vector3.Zero;
345 } // DEBUG DEBUG
346 else
228 { 347 {
229 // Each child position and rotation is given relative to the root. 348 centerOfMass = ComputeLinksetGeometricCenter();
230 OMV.Quaternion invRootOrientation = OMV.Quaternion.Inverse(LinksetRoot.RawOrientation); 349 centerDisplacement = centerOfMass - LinksetRoot.RawPosition;
231 OMV.Vector3 displacementPos = (cPrim.RawPosition - LinksetRoot.RawPosition) * invRootOrientation;
232 OMV.Quaternion displacementRot = cPrim.RawOrientation * invRootOrientation;
233 350
234 DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,addMemberToShape,mID={1},mShape={2},dispPos={3},dispRot={4}", 351 // Since we're displacing the center of the shape, we need to move the body in the world
235 LinksetRoot.LocalID, cPrim.LocalID, cPrim.PhysShape, displacementPos, displacementRot); 352 LinksetRoot.PositionDisplacement = centerDisplacement;
236 353
237 if (cPrim.PhysShape.isNativeShape) 354 PhysicsScene.PE.UpdateChildTransform(LinksetRoot.PhysShape, 0, -centerDisplacement, OMV.Quaternion.Identity, false);
238 { 355 DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,COM,com={1},rootPos={2},centerDisp={3}",
239 // Native shapes are not shared so we need to create a new one. 356 LinksetRoot.LocalID, centerOfMass, LinksetRoot.RawPosition, centerDisplacement);
240 // A mesh or hull is created because scale is not available on a native shape. 357 }
241 // (TODO: Bullet does have a btScaledCollisionShape. Can that be used?) 358
242 BulletShape saveShape = cPrim.PhysShape; 359 DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,start,rBody={1},rShape={2},numChildren={3}",
243 cPrim.PhysShape.ptr = IntPtr.Zero; // Don't let the create free the child's shape 360 LinksetRoot.LocalID, LinksetRoot.PhysBody, LinksetRoot.PhysShape, NumberOfChildren);
244 PhysicsScene.Shapes.CreateGeomMeshOrHull(cPrim, null); 361
245 BulletShape newShape = cPrim.PhysShape; 362 // Add a shape for each of the other children in the linkset
246 cPrim.PhysShape = saveShape; 363 int memberIndex = 1;
247 BulletSimAPI.AddChildShapeToCompoundShape2(LinksetRoot.PhysShape.ptr, newShape.ptr, displacementPos, displacementRot); 364 ForEachMember(delegate(BSPhysObject cPrim)
248 } 365 {
249 else 366 if (!IsRoot(cPrim))
250 { 367 {
251 // For the shared shapes (meshes and hulls), just use the shape in the child. 368 // Compute the displacement of the child from the root of the linkset.
252 if (PhysicsScene.Shapes.ReferenceShape(cPrim.PhysShape)) 369 // This info is saved in the child prim so the relationship does not
370 // change over time and the new child position can be computed
371 // when the linkset is being disassembled (the linkset may have moved).
372 BSLinksetCompoundInfo lci = cPrim.LinksetInfo as BSLinksetCompoundInfo;
373 if (lci == null)
253 { 374 {
254 PhysicsScene.Logger.ErrorFormat("{0} Rebuilt sharable shape when building linkset! Region={1}, primID={2}, shape={3}", 375 // Each child position and rotation is given relative to the center-of-mass.
255 LogHeader, PhysicsScene.RegionName, cPrim.LocalID, cPrim.PhysShape); 376 OMV.Quaternion invRootOrientation = OMV.Quaternion.Inverse(LinksetRoot.RawOrientation);
377 OMV.Vector3 displacementFromRoot = (cPrim.RawPosition - LinksetRoot.RawPosition) * invRootOrientation;
378 OMV.Vector3 displacementFromCOM = displacementFromRoot - centerDisplacement;
379 OMV.Quaternion displacementRot = cPrim.RawOrientation * invRootOrientation;
380
381 // Save relative position for recomputing child's world position after moving linkset.
382 lci = new BSLinksetCompoundInfo(memberIndex, displacementFromCOM, displacementRot);
383 lci.OffsetFromRoot = displacementFromRoot;
384 cPrim.LinksetInfo = lci;
385 DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,creatingRelPos,lci={1}", cPrim.LocalID, lci);
256 } 386 }
257 BulletSimAPI.AddChildShapeToCompoundShape2(LinksetRoot.PhysShape.ptr, cPrim.PhysShape.ptr, displacementPos, displacementRot);
258 }
259 }
260 return false; // 'false' says to move onto the next child in the list
261 });
262 387
263 // With all of the linkset packed into the root prim, it has the mass of everyone. 388 DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,addMemberToShape,mID={1},mShape={2},lci={3}",
264 float linksetMass = LinksetMass; 389 LinksetRoot.LocalID, cPrim.LocalID, cPrim.PhysShape, lci);
265 LinksetRoot.UpdatePhysicalMassProperties(linksetMass);
266 390
267 BulletSimAPI.RecalculateCompoundShapeLocalAabb2(LinksetRoot.PhysShape.ptr); 391 if (cPrim.PhysShape.isNativeShape)
392 {
393 // A native shape is turning into a hull collision shape because native
394 // shapes are not shared so we have to hullify it so it will be tracked
395 // and freed at the correct time. This also solves the scaling problem
396 // (native shapes scaled but hull/meshes are assumed to not be).
397 // TODO: decide of the native shape can just be used in the compound shape.
398 // Use call to CreateGeomNonSpecial().
399 BulletShape saveShape = cPrim.PhysShape;
400 cPrim.PhysShape.Clear(); // Don't let the create free the child's shape
401 // PhysicsScene.Shapes.CreateGeomNonSpecial(true, cPrim, null);
402 PhysicsScene.Shapes.CreateGeomMeshOrHull(cPrim, null);
403 BulletShape newShape = cPrim.PhysShape;
404 cPrim.PhysShape = saveShape;
405 PhysicsScene.PE.AddChildShapeToCompoundShape(LinksetRoot.PhysShape, newShape, lci.OffsetFromCenterOfMass, lci.OffsetRot);
406 }
407 else
408 {
409 // For the shared shapes (meshes and hulls), just use the shape in the child.
410 // The reference count added here will be decremented when the compound shape
411 // is destroyed in BSShapeCollection (the child shapes are looped over and dereferenced).
412 if (PhysicsScene.Shapes.ReferenceShape(cPrim.PhysShape))
413 {
414 PhysicsScene.Logger.ErrorFormat("{0} Rebuilt sharable shape when building linkset! Region={1}, primID={2}, shape={3}",
415 LogHeader, PhysicsScene.RegionName, cPrim.LocalID, cPrim.PhysShape);
416 }
417 PhysicsScene.PE.AddChildShapeToCompoundShape(LinksetRoot.PhysShape, cPrim.PhysShape, lci.OffsetFromCenterOfMass, lci.OffsetRot);
418 }
419 lci.Index = memberIndex;
420 memberIndex++;
421 }
422 return false; // 'false' says to move onto the next child in the list
423 });
268 424
269 // DEBUG: see of inter-linkset collisions are causing problems for constraint linksets. 425 // With all of the linkset packed into the root prim, it has the mass of everyone.
270 // BulletSimAPI.SetCollisionFilterMask2(LinksetRoot.BSBody.ptr, 426 LinksetMass = ComputeLinksetMass();
271 // (uint)CollisionFilterGroups.LinksetFilter, (uint)CollisionFilterGroups.LinksetMask); 427 LinksetRoot.UpdatePhysicalMassProperties(LinksetMass, true);
428 }
429 finally
430 {
431 Rebuilding = false;
432 }
272 433
434 PhysicsScene.PE.RecalculateCompoundShapeLocalAabb(LinksetRoot.PhysShape);
273 } 435 }
274} 436}
275} \ No newline at end of file 437} \ No newline at end of file
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSLinksetConstraints.cs b/OpenSim/Region/Physics/BulletSPlugin/BSLinksetConstraints.cs
index c855fda..d0b2a56 100755
--- a/OpenSim/Region/Physics/BulletSPlugin/BSLinksetConstraints.cs
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSLinksetConstraints.cs
@@ -36,9 +36,8 @@ public sealed class BSLinksetConstraints : BSLinkset
36{ 36{
37 // private static string LogHeader = "[BULLETSIM LINKSET CONSTRAINTS]"; 37 // private static string LogHeader = "[BULLETSIM LINKSET CONSTRAINTS]";
38 38
39 public BSLinksetConstraints(BSScene scene, BSPhysObject parent) 39 public BSLinksetConstraints(BSScene scene, BSPhysObject parent) : base(scene, parent)
40 { 40 {
41 base.Initialize(scene, parent);
42 } 41 }
43 42
44 // When physical properties are changed the linkset needs to recalculate 43 // When physical properties are changed the linkset needs to recalculate
@@ -47,12 +46,17 @@ public sealed class BSLinksetConstraints : BSLinkset
47 // refresh will happen once after all the other taints are applied. 46 // refresh will happen once after all the other taints are applied.
48 public override void Refresh(BSPhysObject requestor) 47 public override void Refresh(BSPhysObject requestor)
49 { 48 {
50 // Queue to happen after all the other taint processing 49 base.Refresh(requestor);
51 PhysicsScene.PostTaintObject("BSLinksetContraints.Refresh", requestor.LocalID, delegate() 50
52 { 51 if (HasAnyChildren && IsRoot(requestor))
53 if (HasAnyChildren && IsRoot(requestor)) 52 {
54 RecomputeLinksetConstraints(); 53 // Queue to happen after all the other taint processing
55 }); 54 PhysicsScene.PostTaintObject("BSLinksetContraints.Refresh", requestor.LocalID, delegate()
55 {
56 if (HasAnyChildren && IsRoot(requestor))
57 RecomputeLinksetConstraints();
58 });
59 }
56 } 60 }
57 61
58 // The object is going dynamic (physical). Do any setup necessary 62 // The object is going dynamic (physical). Do any setup necessary
@@ -79,23 +83,11 @@ public sealed class BSLinksetConstraints : BSLinkset
79 } 83 }
80 84
81 // Called at taint-time!! 85 // Called at taint-time!!
82 public override void UpdateProperties(BSPhysObject updated) 86 public override void UpdateProperties(BSPhysObject updated, bool inTaintTime)
83 { 87 {
84 // Nothing to do for constraints on property updates 88 // Nothing to do for constraints on property updates
85 } 89 }
86 90
87 // The children of the linkset are moved around by the constraints.
88 // Just grab the current values of wherever it is right now.
89 public override OMV.Vector3 Position(BSPhysObject member)
90 {
91 return BulletSimAPI.GetPosition2(member.PhysBody.ptr);
92 }
93
94 public override OMV.Quaternion Orientation(BSPhysObject member)
95 {
96 return BulletSimAPI.GetOrientation2(member.PhysBody.ptr);
97 }
98
99 // Routine called when rebuilding the body of some member of the linkset. 91 // Routine called when rebuilding the body of some member of the linkset.
100 // Destroy all the constraints have have been made to root and set 92 // Destroy all the constraints have have been made to root and set
101 // up to rebuild the constraints before the next simulation step. 93 // up to rebuild the constraints before the next simulation step.
@@ -106,7 +98,7 @@ public sealed class BSLinksetConstraints : BSLinkset
106 bool ret = false; 98 bool ret = false;
107 99
108 DetailLog("{0},BSLinksetConstraint.RemoveBodyDependencies,removeChildrenForRoot,rID={1},rBody={2}", 100 DetailLog("{0},BSLinksetConstraint.RemoveBodyDependencies,removeChildrenForRoot,rID={1},rBody={2}",
109 child.LocalID, LinksetRoot.LocalID, LinksetRoot.PhysBody.ptr.ToString("X")); 101 child.LocalID, LinksetRoot.LocalID, LinksetRoot.PhysBody.AddrString);
110 102
111 lock (m_linksetActivityLock) 103 lock (m_linksetActivityLock)
112 { 104 {
@@ -155,8 +147,8 @@ public sealed class BSLinksetConstraints : BSLinkset
155 147
156 DetailLog("{0},BSLinksetConstraints.RemoveChildFromLinkset,call,rID={1},rBody={2},cID={3},cBody={4}", 148 DetailLog("{0},BSLinksetConstraints.RemoveChildFromLinkset,call,rID={1},rBody={2},cID={3},cBody={4}",
157 childx.LocalID, 149 childx.LocalID,
158 rootx.LocalID, rootx.PhysBody.ptr.ToString("X"), 150 rootx.LocalID, rootx.PhysBody.AddrString,
159 childx.LocalID, childx.PhysBody.ptr.ToString("X")); 151 childx.LocalID, childx.PhysBody.AddrString);
160 152
161 PhysicsScene.TaintedObject("BSLinksetConstraints.RemoveChildFromLinkset", delegate() 153 PhysicsScene.TaintedObject("BSLinksetConstraints.RemoveChildFromLinkset", delegate()
162 { 154 {
@@ -195,8 +187,8 @@ public sealed class BSLinksetConstraints : BSLinkset
195 187
196 DetailLog("{0},BSLinksetConstraint.BuildConstraint,taint,root={1},rBody={2},child={3},cBody={4},rLoc={5},cLoc={6},midLoc={7}", 188 DetailLog("{0},BSLinksetConstraint.BuildConstraint,taint,root={1},rBody={2},child={3},cBody={4},rLoc={5},cLoc={6},midLoc={7}",
197 rootPrim.LocalID, 189 rootPrim.LocalID,
198 rootPrim.LocalID, rootPrim.PhysBody.ptr.ToString("X"), 190 rootPrim.LocalID, rootPrim.PhysBody.AddrString,
199 childPrim.LocalID, childPrim.PhysBody.ptr.ToString("X"), 191 childPrim.LocalID, childPrim.PhysBody.AddrString,
200 rootPrim.Position, childPrim.Position, midPoint); 192 rootPrim.Position, childPrim.Position, midPoint);
201 193
202 // create a constraint that allows no freedom of movement between the two objects 194 // create a constraint that allows no freedom of movement between the two objects
@@ -239,14 +231,14 @@ public sealed class BSLinksetConstraints : BSLinkset
239 constrain.SetAngularLimits(OMV.Vector3.Zero, OMV.Vector3.Zero); 231 constrain.SetAngularLimits(OMV.Vector3.Zero, OMV.Vector3.Zero);
240 232
241 // tweek the constraint to increase stability 233 // tweek the constraint to increase stability
242 constrain.UseFrameOffset(PhysicsScene.BoolNumeric(PhysicsScene.Params.linkConstraintUseFrameOffset)); 234 constrain.UseFrameOffset(BSParam.BoolNumeric(BSParam.LinkConstraintUseFrameOffset));
243 constrain.TranslationalLimitMotor(PhysicsScene.BoolNumeric(PhysicsScene.Params.linkConstraintEnableTransMotor), 235 constrain.TranslationalLimitMotor(BSParam.BoolNumeric(BSParam.LinkConstraintEnableTransMotor),
244 PhysicsScene.Params.linkConstraintTransMotorMaxVel, 236 BSParam.LinkConstraintTransMotorMaxVel,
245 PhysicsScene.Params.linkConstraintTransMotorMaxForce); 237 BSParam.LinkConstraintTransMotorMaxForce);
246 constrain.SetCFMAndERP(PhysicsScene.Params.linkConstraintCFM, PhysicsScene.Params.linkConstraintERP); 238 constrain.SetCFMAndERP(BSParam.LinkConstraintCFM, BSParam.LinkConstraintERP);
247 if (PhysicsScene.Params.linkConstraintSolverIterations != 0f) 239 if (BSParam.LinkConstraintSolverIterations != 0f)
248 { 240 {
249 constrain.SetSolverIterations(PhysicsScene.Params.linkConstraintSolverIterations); 241 constrain.SetSolverIterations(BSParam.LinkConstraintSolverIterations);
250 } 242 }
251 return constrain; 243 return constrain;
252 } 244 }
@@ -260,14 +252,14 @@ public sealed class BSLinksetConstraints : BSLinkset
260 bool ret = false; 252 bool ret = false;
261 DetailLog("{0},BSLinksetConstraint.PhysicallyUnlinkAChildFromRoot,taint,root={1},rBody={2},child={3},cBody={4}", 253 DetailLog("{0},BSLinksetConstraint.PhysicallyUnlinkAChildFromRoot,taint,root={1},rBody={2},child={3},cBody={4}",
262 rootPrim.LocalID, 254 rootPrim.LocalID,
263 rootPrim.LocalID, rootPrim.PhysBody.ptr.ToString("X"), 255 rootPrim.LocalID, rootPrim.PhysBody.AddrString,
264 childPrim.LocalID, childPrim.PhysBody.ptr.ToString("X")); 256 childPrim.LocalID, childPrim.PhysBody.AddrString);
265 257
266 // Find the constraint for this link and get rid of it from the overall collection and from my list 258 // Find the constraint for this link and get rid of it from the overall collection and from my list
267 if (PhysicsScene.Constraints.RemoveAndDestroyConstraint(rootPrim.PhysBody, childPrim.PhysBody)) 259 if (PhysicsScene.Constraints.RemoveAndDestroyConstraint(rootPrim.PhysBody, childPrim.PhysBody))
268 { 260 {
269 // Make the child refresh its location 261 // Make the child refresh its location
270 BulletSimAPI.PushUpdate2(childPrim.PhysBody.ptr); 262 PhysicsScene.PE.PushUpdate(childPrim.PhysBody);
271 ret = true; 263 ret = true;
272 } 264 }
273 265
@@ -292,20 +284,17 @@ public sealed class BSLinksetConstraints : BSLinkset
292 private void RecomputeLinksetConstraints() 284 private void RecomputeLinksetConstraints()
293 { 285 {
294 float linksetMass = LinksetMass; 286 float linksetMass = LinksetMass;
295 LinksetRoot.UpdatePhysicalMassProperties(linksetMass); 287 LinksetRoot.UpdatePhysicalMassProperties(linksetMass, true);
296 288
297 // DEBUG: see of inter-linkset collisions are causing problems
298 // BulletSimAPI.SetCollisionFilterMask2(LinksetRoot.BSBody.ptr,
299 // (uint)CollisionFilterGroups.LinksetFilter, (uint)CollisionFilterGroups.LinksetMask);
300 DetailLog("{0},BSLinksetConstraint.RecomputeLinksetConstraints,set,rBody={1},linksetMass={2}", 289 DetailLog("{0},BSLinksetConstraint.RecomputeLinksetConstraints,set,rBody={1},linksetMass={2}",
301 LinksetRoot.LocalID, LinksetRoot.PhysBody.ptr.ToString("X"), linksetMass); 290 LinksetRoot.LocalID, LinksetRoot.PhysBody.AddrString, linksetMass);
302 291
303 foreach (BSPhysObject child in m_children) 292 foreach (BSPhysObject child in m_children)
304 { 293 {
305 // A child in the linkset physically shows the mass of the whole linkset. 294 // A child in the linkset physically shows the mass of the whole linkset.
306 // This allows Bullet to apply enough force on the child to move the whole linkset. 295 // This allows Bullet to apply enough force on the child to move the whole linkset.
307 // (Also do the mass stuff before recomputing the constraint so mass is not zero.) 296 // (Also do the mass stuff before recomputing the constraint so mass is not zero.)
308 child.UpdatePhysicalMassProperties(linksetMass); 297 child.UpdatePhysicalMassProperties(linksetMass, true);
309 298
310 BSConstraint constrain; 299 BSConstraint constrain;
311 if (!PhysicsScene.Constraints.TryGetConstraint(LinksetRoot.PhysBody, child.PhysBody, out constrain)) 300 if (!PhysicsScene.Constraints.TryGetConstraint(LinksetRoot.PhysBody, child.PhysBody, out constrain))
@@ -315,11 +304,7 @@ public sealed class BSLinksetConstraints : BSLinkset
315 } 304 }
316 constrain.RecomputeConstraintVariables(linksetMass); 305 constrain.RecomputeConstraintVariables(linksetMass);
317 306
318 // DEBUG: see of inter-linkset collisions are causing problems 307 // PhysicsScene.PE.DumpConstraint(PhysicsScene.World, constrain.Constraint); // DEBUG DEBUG
319 // BulletSimAPI.SetCollisionFilterMask2(child.BSBody.ptr,
320 // (uint)CollisionFilterGroups.LinksetFilter, (uint)CollisionFilterGroups.LinksetMask);
321
322 // BulletSimAPI.DumpConstraint2(PhysicsScene.World.ptr, constrain.Constraint.ptr); // DEBUG DEBUG
323 } 308 }
324 309
325 } 310 }
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSMaterials.cs b/OpenSim/Region/Physics/BulletSPlugin/BSMaterials.cs
new file mode 100755
index 0000000..92d62ff
--- /dev/null
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSMaterials.cs
@@ -0,0 +1,200 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyrightD
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27using System;
28using System.Collections.Generic;
29using System.Text;
30using System.Reflection;
31using Nini.Config;
32
33namespace OpenSim.Region.Physics.BulletSPlugin
34{
35
36public struct MaterialAttributes
37{
38 // Material type values that correspond with definitions for LSL
39 public enum Material : int
40 {
41 Stone = 0,
42 Metal,
43 Glass,
44 Wood,
45 Flesh,
46 Plastic,
47 Rubber,
48 Light,
49 // Hereafter are BulletSim additions
50 Avatar,
51 NumberOfTypes // the count of types in the enum.
52 }
53
54 // Names must be in the order of the above enum.
55 // These names must coorespond to the lower case field names in the MaterialAttributes
56 // structure as reflection is used to select the field to put the value in.
57 public static readonly string[] MaterialAttribs = { "Density", "Friction", "Restitution"};
58
59 public MaterialAttributes(string t, float d, float f, float r)
60 {
61 type = t;
62 density = d;
63 friction = f;
64 restitution = r;
65 }
66 public string type;
67 public float density;
68 public float friction;
69 public float restitution;
70}
71
72public static class BSMaterials
73{
74 // Attributes for each material type
75 private static readonly MaterialAttributes[] Attributes;
76
77 // Map of material name to material type code
78 public static readonly Dictionary<string, MaterialAttributes.Material> MaterialMap;
79
80 static BSMaterials()
81 {
82 // Attribute sets for both the non-physical and physical instances of materials.
83 Attributes = new MaterialAttributes[(int)MaterialAttributes.Material.NumberOfTypes * 2];
84
85 // Map of name to type code.
86 MaterialMap = new Dictionary<string, MaterialAttributes.Material>();
87 MaterialMap.Add("Stone", MaterialAttributes.Material.Stone);
88 MaterialMap.Add("Metal", MaterialAttributes.Material.Metal);
89 MaterialMap.Add("Glass", MaterialAttributes.Material.Glass);
90 MaterialMap.Add("Wood", MaterialAttributes.Material.Wood);
91 MaterialMap.Add("Flesh", MaterialAttributes.Material.Flesh);
92 MaterialMap.Add("Plastic", MaterialAttributes.Material.Plastic);
93 MaterialMap.Add("Rubber", MaterialAttributes.Material.Rubber);
94 MaterialMap.Add("Light", MaterialAttributes.Material.Light);
95 MaterialMap.Add("Avatar", MaterialAttributes.Material.Avatar);
96 }
97
98 // This is where all the default material attributes are defined.
99 public static void InitializeFromDefaults(ConfigurationParameters parms)
100 {
101 // Values from http://wiki.secondlife.com/wiki/PRIM_MATERIAL
102 float dDensity = parms.defaultDensity;
103 float dFriction = parms.defaultFriction;
104 float dRestitution = parms.defaultRestitution;
105 Attributes[(int)MaterialAttributes.Material.Stone] =
106 new MaterialAttributes("stone",dDensity, 0.8f, 0.4f);
107 Attributes[(int)MaterialAttributes.Material.Metal] =
108 new MaterialAttributes("metal",dDensity, 0.3f, 0.4f);
109 Attributes[(int)MaterialAttributes.Material.Glass] =
110 new MaterialAttributes("glass",dDensity, 0.2f, 0.7f);
111 Attributes[(int)MaterialAttributes.Material.Wood] =
112 new MaterialAttributes("wood",dDensity, 0.6f, 0.5f);
113 Attributes[(int)MaterialAttributes.Material.Flesh] =
114 new MaterialAttributes("flesh",dDensity, 0.9f, 0.3f);
115 Attributes[(int)MaterialAttributes.Material.Plastic] =
116 new MaterialAttributes("plastic",dDensity, 0.4f, 0.7f);
117 Attributes[(int)MaterialAttributes.Material.Rubber] =
118 new MaterialAttributes("rubber",dDensity, 0.9f, 0.9f);
119 Attributes[(int)MaterialAttributes.Material.Light] =
120 new MaterialAttributes("light",dDensity, dFriction, dRestitution);
121 Attributes[(int)MaterialAttributes.Material.Avatar] =
122 new MaterialAttributes("avatar",3.5f, 0.2f, 0f);
123
124 Attributes[(int)MaterialAttributes.Material.Stone + (int)MaterialAttributes.Material.NumberOfTypes] =
125 new MaterialAttributes("stonePhysical",dDensity, 0.8f, 0.4f);
126 Attributes[(int)MaterialAttributes.Material.Metal + (int)MaterialAttributes.Material.NumberOfTypes] =
127 new MaterialAttributes("metalPhysical",dDensity, 0.3f, 0.4f);
128 Attributes[(int)MaterialAttributes.Material.Glass + (int)MaterialAttributes.Material.NumberOfTypes] =
129 new MaterialAttributes("glassPhysical",dDensity, 0.2f, 0.7f);
130 Attributes[(int)MaterialAttributes.Material.Wood + (int)MaterialAttributes.Material.NumberOfTypes] =
131 new MaterialAttributes("woodPhysical",dDensity, 0.6f, 0.5f);
132 Attributes[(int)MaterialAttributes.Material.Flesh + (int)MaterialAttributes.Material.NumberOfTypes] =
133 new MaterialAttributes("fleshPhysical",dDensity, 0.9f, 0.3f);
134 Attributes[(int)MaterialAttributes.Material.Plastic + (int)MaterialAttributes.Material.NumberOfTypes] =
135 new MaterialAttributes("plasticPhysical",dDensity, 0.4f, 0.7f);
136 Attributes[(int)MaterialAttributes.Material.Rubber + (int)MaterialAttributes.Material.NumberOfTypes] =
137 new MaterialAttributes("rubberPhysical",dDensity, 0.9f, 0.9f);
138 Attributes[(int)MaterialAttributes.Material.Light + (int)MaterialAttributes.Material.NumberOfTypes] =
139 new MaterialAttributes("lightPhysical",dDensity, dFriction, dRestitution);
140 Attributes[(int)MaterialAttributes.Material.Avatar + (int)MaterialAttributes.Material.NumberOfTypes] =
141 new MaterialAttributes("avatarPhysical",3.5f, 0.2f, 0f);
142 }
143
144 // Under the [BulletSim] section, one can change the individual material
145 // attribute values. The format of the configuration parameter is:
146 // <materialName><Attribute>["Physical"] = floatValue
147 // For instance:
148 // [BulletSim]
149 // StoneFriction = 0.2
150 // FleshRestitutionPhysical = 0.8
151 // Materials can have different parameters for their static and
152 // physical instantiations. When setting the non-physical value,
153 // both values are changed. Setting the physical value only changes
154 // the physical value.
155 public static void InitializefromParameters(IConfig pConfig)
156 {
157 foreach (KeyValuePair<string, MaterialAttributes.Material> kvp in MaterialMap)
158 {
159 string matName = kvp.Key;
160 foreach (string attribName in MaterialAttributes.MaterialAttribs)
161 {
162 string paramName = matName + attribName;
163 if (pConfig.Contains(paramName))
164 {
165 float paramValue = pConfig.GetFloat(paramName);
166 SetAttributeValue((int)kvp.Value, attribName, paramValue);
167 // set the physical value also
168 SetAttributeValue((int)kvp.Value + (int)MaterialAttributes.Material.NumberOfTypes, attribName, paramValue);
169 }
170 paramName += "Physical";
171 if (pConfig.Contains(paramName))
172 {
173 float paramValue = pConfig.GetFloat(paramName);
174 SetAttributeValue((int)kvp.Value + (int)MaterialAttributes.Material.NumberOfTypes, attribName, paramValue);
175 }
176 }
177 }
178 }
179
180 // Use reflection to set the value in the attribute structure.
181 private static void SetAttributeValue(int matType, string attribName, float val)
182 {
183 MaterialAttributes thisAttrib = Attributes[matType];
184 FieldInfo fieldInfo = thisAttrib.GetType().GetField(attribName.ToLower());
185 if (fieldInfo != null)
186 {
187 fieldInfo.SetValue(thisAttrib, val);
188 Attributes[matType] = thisAttrib;
189 }
190 }
191
192 // Given a material type, return a structure of attributes.
193 public static MaterialAttributes GetAttributes(MaterialAttributes.Material type, bool isPhysical)
194 {
195 int ind = (int)type;
196 if (isPhysical) ind += (int)MaterialAttributes.Material.NumberOfTypes;
197 return Attributes[ind];
198 }
199}
200}
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSMotors.cs b/OpenSim/Region/Physics/BulletSPlugin/BSMotors.cs
index bc6e4c4..6d0db2e 100755
--- a/OpenSim/Region/Physics/BulletSPlugin/BSMotors.cs
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSMotors.cs
@@ -1,104 +1,467 @@
1using System; 1/*
2using System.Collections.Generic; 2 * Copyright (c) Contributors, http://opensimulator.org/
3using System.Text; 3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4using OpenMetaverse; 4 *
5 5 * Redistribution and use in source and binary forms, with or without
6namespace OpenSim.Region.Physics.BulletSPlugin 6 * modification, are permitted provided that the following conditions are met:
7{ 7 * * Redistributions of source code must retain the above copyright
8public abstract class BSMotor 8 * notice, this list of conditions and the following disclaimer.
9{ 9 * * Redistributions in binary form must reproduce the above copyright
10 public virtual void Reset() { } 10 * notice, this list of conditions and the following disclaimer in the
11 public virtual void Zero() { } 11 * documentation and/or other materials provided with the distribution.
12} 12 * * Neither the name of the OpenSimulator Project nor the
13// Can all the incremental stepping be replaced with motor classes? 13 * names of its contributors may be used to endorse or promote products
14public class BSVMotor : BSMotor 14 * derived from this software without specific prior written permission.
15{ 15 *
16 public Vector3 FrameOfReference { get; set; } 16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 public Vector3 Offset { get; set; } 17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 public float TimeScale { get; set; } 19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 public float TargetValueDecayTimeScale { get; set; } 20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 public Vector3 CurrentValueReductionTimescale { get; set; } 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 public float Efficiency { get; set; } 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 public Vector3 TargetValue { get; private set; } 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 public Vector3 CurrentValue { get; private set; } 25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 26 *
27 27 */
28 28using System;
29 BSVMotor(float timeScale, float decayTimeScale, Vector3 frictionTimeScale, float efficiency) 29using System.Collections.Generic;
30 { 30using System.Text;
31 TimeScale = timeScale; 31using OpenMetaverse;
32 TargetValueDecayTimeScale = decayTimeScale; 32using OpenSim.Framework;
33 CurrentValueReductionTimescale = frictionTimeScale; 33
34 Efficiency = efficiency; 34namespace OpenSim.Region.Physics.BulletSPlugin
35 } 35{
36 public void SetCurrent(Vector3 current) 36public abstract class BSMotor
37 { 37{
38 CurrentValue = current; 38 // Timescales and other things can be turned off by setting them to 'infinite'.
39 } 39 public const float Infinite = 12345.6f;
40 public void SetTarget(Vector3 target) 40 public readonly static Vector3 InfiniteVector = new Vector3(BSMotor.Infinite, BSMotor.Infinite, BSMotor.Infinite);
41 { 41
42 TargetValue = target; 42 public BSMotor(string useName)
43 } 43 {
44 public Vector3 Step(float timeStep) 44 UseName = useName;
45 { 45 PhysicsScene = null;
46 if (CurrentValue.LengthSquared() > 0.001f) 46 Enabled = true;
47 { 47 }
48 // Vector3 origDir = Target; // DEBUG 48 public virtual bool Enabled { get; set; }
49 // Vector3 origVel = CurrentValue; // DEBUG 49 public virtual void Reset() { }
50 50 public virtual void Zero() { }
51 // Add (desiredVelocity - currentAppliedVelocity) / howLongItShouldTakeToComplete 51 public virtual void GenerateTestOutput(float timeStep) { }
52 Vector3 addAmount = (TargetValue - CurrentValue)/(TargetValue) * timeStep; 52
53 CurrentValue += addAmount; 53 // A name passed at motor creation for easily identifyable debugging messages.
54 54 public string UseName { get; private set; }
55 float decayFactor = (1.0f / TargetValueDecayTimeScale) * timeStep; 55
56 TargetValue *= (1f - decayFactor); 56 // Used only for outputting debug information. Might not be set so check for null.
57 57 public BSScene PhysicsScene { get; set; }
58 Vector3 frictionFactor = (Vector3.One / CurrentValueReductionTimescale) * timeStep; 58 protected void MDetailLog(string msg, params Object[] parms)
59 CurrentValue *= (Vector3.One - frictionFactor); 59 {
60 } 60 if (PhysicsScene != null)
61 else 61 {
62 { 62 PhysicsScene.DetailLog(msg, parms);
63 // if what remains of direction is very small, zero it. 63 }
64 TargetValue = Vector3.Zero; 64 }
65 CurrentValue = Vector3.Zero; 65}
66 66
67 // VDetailLog("{0},MoveLinear,zeroed", Prim.LocalID); 67// Motor which moves CurrentValue to TargetValue over TimeScale seconds.
68 } 68// The TargetValue decays in TargetValueDecayTimeScale and
69 return CurrentValue; 69// the CurrentValue will be held back by FrictionTimeScale.
70 } 70// This motor will "zero itself" over time in that the targetValue will
71} 71// decay to zero and the currentValue will follow it to that zero.
72 72// The overall effect is for the returned correction value to go from large
73public class BSFMotor : BSMotor 73// values (the total difference between current and target minus friction)
74{ 74// to small and eventually zero values.
75 public float TimeScale { get; set; } 75// TimeScale and TargetDelayTimeScale may be 'infinite' which means no decay.
76 public float DecayTimeScale { get; set; } 76
77 public float Friction { get; set; } 77// For instance, if something is moving at speed X and the desired speed is Y,
78 public float Efficiency { get; set; } 78// CurrentValue is X and TargetValue is Y. As the motor is stepped, new
79 79// values of CurrentValue are returned that approach the TargetValue.
80 public float Target { get; private set; } 80// The feature of decaying TargetValue is so vehicles will eventually
81 public float CurrentValue { get; private set; } 81// come to a stop rather than run forever. This can be disabled by
82 82// setting TargetValueDecayTimescale to 'infinite'.
83 BSFMotor(float timeScale, float decayTimescale, float friction, float efficiency) 83// The change from CurrentValue to TargetValue is linear over TimeScale seconds.
84 { 84public class BSVMotor : BSMotor
85 } 85{
86 public void SetCurrent(float target) 86 // public Vector3 FrameOfReference { get; set; }
87 { 87 // public Vector3 Offset { get; set; }
88 } 88
89 public void SetTarget(float target) 89 public virtual float TimeScale { get; set; }
90 { 90 public virtual float TargetValueDecayTimeScale { get; set; }
91 } 91 public virtual Vector3 FrictionTimescale { get; set; }
92 public float Step(float timeStep) 92 public virtual float Efficiency { get; set; }
93 { 93
94 return 0f; 94 public virtual float ErrorZeroThreshold { get; set; }
95 } 95
96} 96 public virtual Vector3 TargetValue { get; protected set; }
97public class BSPIDMotor : BSMotor 97 public virtual Vector3 CurrentValue { get; protected set; }
98{ 98 public virtual Vector3 LastError { get; protected set; }
99 // TODO: write and use this one 99
100 BSPIDMotor() 100 public virtual bool ErrorIsZero()
101 { 101 {
102 } 102 return ErrorIsZero(LastError);
103} 103 }
104} 104 public virtual bool ErrorIsZero(Vector3 err)
105 {
106 return (err == Vector3.Zero || err.ApproxEquals(Vector3.Zero, ErrorZeroThreshold));
107 }
108
109 public BSVMotor(string useName)
110 : base(useName)
111 {
112 TimeScale = TargetValueDecayTimeScale = BSMotor.Infinite;
113 Efficiency = 1f;
114 FrictionTimescale = BSMotor.InfiniteVector;
115 CurrentValue = TargetValue = Vector3.Zero;
116 ErrorZeroThreshold = 0.001f;
117 }
118 public BSVMotor(string useName, float timeScale, float decayTimeScale, Vector3 frictionTimeScale, float efficiency)
119 : this(useName)
120 {
121 TimeScale = timeScale;
122 TargetValueDecayTimeScale = decayTimeScale;
123 FrictionTimescale = frictionTimeScale;
124 Efficiency = efficiency;
125 CurrentValue = TargetValue = Vector3.Zero;
126 }
127 public void SetCurrent(Vector3 current)
128 {
129 CurrentValue = current;
130 }
131 public void SetTarget(Vector3 target)
132 {
133 TargetValue = target;
134 }
135 public override void Zero()
136 {
137 base.Zero();
138 CurrentValue = TargetValue = Vector3.Zero;
139 }
140
141 // Compute the next step and return the new current value
142 public virtual Vector3 Step(float timeStep)
143 {
144 if (!Enabled) return TargetValue;
145
146 Vector3 origTarget = TargetValue; // DEBUG
147 Vector3 origCurrVal = CurrentValue; // DEBUG
148
149 Vector3 correction = Vector3.Zero;
150 Vector3 error = TargetValue - CurrentValue;
151 if (!ErrorIsZero(error))
152 {
153 correction = Step(timeStep, error);
154
155 CurrentValue += correction;
156
157 // The desired value reduces to zero which also reduces the difference with current.
158 // If the decay time is infinite, don't decay at all.
159 float decayFactor = 0f;
160 if (TargetValueDecayTimeScale != BSMotor.Infinite)
161 {
162 decayFactor = (1.0f / TargetValueDecayTimeScale) * timeStep;
163 TargetValue *= (1f - decayFactor);
164 }
165
166 // The amount we can correct the error is reduced by the friction
167 Vector3 frictionFactor = Vector3.Zero;
168 if (FrictionTimescale != BSMotor.InfiniteVector)
169 {
170 // frictionFactor = (Vector3.One / FrictionTimescale) * timeStep;
171 // Individual friction components can be 'infinite' so compute each separately.
172 frictionFactor.X = (FrictionTimescale.X == BSMotor.Infinite) ? 0f : (1f / FrictionTimescale.X);
173 frictionFactor.Y = (FrictionTimescale.Y == BSMotor.Infinite) ? 0f : (1f / FrictionTimescale.Y);
174 frictionFactor.Z = (FrictionTimescale.Z == BSMotor.Infinite) ? 0f : (1f / FrictionTimescale.Z);
175 frictionFactor *= timeStep;
176 CurrentValue *= (Vector3.One - frictionFactor);
177 }
178
179 MDetailLog("{0}, BSVMotor.Step,nonZero,{1},origCurr={2},origTarget={3},timeStep={4},err={5},corr={6}",
180 BSScene.DetailLogZero, UseName, origCurrVal, origTarget,
181 timeStep, error, correction);
182 MDetailLog("{0}, BSVMotor.Step,nonZero,{1},tgtDecayTS={2},decayFact={3},frictTS={4},frictFact={5},tgt={6},curr={7}",
183 BSScene.DetailLogZero, UseName,
184 TargetValueDecayTimeScale, decayFactor, FrictionTimescale, frictionFactor,
185 TargetValue, CurrentValue);
186 }
187 else
188 {
189 // Difference between what we have and target is small. Motor is done.
190 CurrentValue = TargetValue;
191 MDetailLog("{0}, BSVMotor.Step,zero,{1},origTgt={2},origCurr={3},ret={4}",
192 BSScene.DetailLogZero, UseName, origCurrVal, origTarget, CurrentValue);
193 }
194
195 return CurrentValue;
196 }
197 public virtual Vector3 Step(float timeStep, Vector3 error)
198 {
199 if (!Enabled) return Vector3.Zero;
200
201 LastError = error;
202 Vector3 returnCorrection = Vector3.Zero;
203 if (!ErrorIsZero())
204 {
205 // correction = error / secondsItShouldTakeToCorrect
206 Vector3 correctionAmount;
207 if (TimeScale == 0f || TimeScale == BSMotor.Infinite)
208 correctionAmount = error * timeStep;
209 else
210 correctionAmount = error / TimeScale * timeStep;
211
212 returnCorrection = correctionAmount;
213 MDetailLog("{0}, BSVMotor.Step,nonZero,{1},timeStep={2},timeScale={3},err={4},corr={5}",
214 BSScene.DetailLogZero, UseName, timeStep, TimeScale, error, correctionAmount);
215 }
216 return returnCorrection;
217 }
218
219 // The user sets all the parameters and calls this which outputs values until error is zero.
220 public override void GenerateTestOutput(float timeStep)
221 {
222 // maximum number of outputs to generate.
223 int maxOutput = 50;
224 MDetailLog("{0},BSVMotor.Test,{1},===================================== BEGIN Test Output", BSScene.DetailLogZero, UseName);
225 MDetailLog("{0},BSVMotor.Test,{1},timeScale={2},targDlyTS={3},frictTS={4},eff={5},curr={6},tgt={7}",
226 BSScene.DetailLogZero, UseName,
227 TimeScale, TargetValueDecayTimeScale, FrictionTimescale, Efficiency,
228 CurrentValue, TargetValue);
229
230 LastError = BSMotor.InfiniteVector;
231 while (maxOutput-- > 0 && !LastError.ApproxEquals(Vector3.Zero, ErrorZeroThreshold))
232 {
233 Vector3 lastStep = Step(timeStep);
234 MDetailLog("{0},BSVMotor.Test,{1},cur={2},tgt={3},lastError={4},lastStep={5}",
235 BSScene.DetailLogZero, UseName, CurrentValue, TargetValue, LastError, lastStep);
236 }
237 MDetailLog("{0},BSVMotor.Test,{1},===================================== END Test Output", BSScene.DetailLogZero, UseName);
238
239
240 }
241
242 public override string ToString()
243 {
244 return String.Format("<{0},curr={1},targ={2},lastErr={3},decayTS={4},frictTS={5}>",
245 UseName, CurrentValue, TargetValue, LastError, TargetValueDecayTimeScale, FrictionTimescale);
246 }
247}
248
249// ============================================================================
250// ============================================================================
251public class BSFMotor : BSMotor
252{
253 public virtual float TimeScale { get; set; }
254 public virtual float TargetValueDecayTimeScale { get; set; }
255 public virtual float FrictionTimescale { get; set; }
256 public virtual float Efficiency { get; set; }
257
258 public virtual float ErrorZeroThreshold { get; set; }
259
260 public virtual float TargetValue { get; protected set; }
261 public virtual float CurrentValue { get; protected set; }
262 public virtual float LastError { get; protected set; }
263
264 public virtual bool ErrorIsZero()
265 {
266 return ErrorIsZero(LastError);
267 }
268 public virtual bool ErrorIsZero(float err)
269 {
270 return (err >= -ErrorZeroThreshold && err <= ErrorZeroThreshold);
271 }
272
273 public BSFMotor(string useName, float timeScale, float decayTimescale, float friction, float efficiency)
274 : base(useName)
275 {
276 TimeScale = TargetValueDecayTimeScale = BSMotor.Infinite;
277 Efficiency = 1f;
278 FrictionTimescale = BSMotor.Infinite;
279 CurrentValue = TargetValue = 0f;
280 ErrorZeroThreshold = 0.01f;
281 }
282 public void SetCurrent(float current)
283 {
284 CurrentValue = current;
285 }
286 public void SetTarget(float target)
287 {
288 TargetValue = target;
289 }
290 public override void Zero()
291 {
292 base.Zero();
293 CurrentValue = TargetValue = 0f;
294 }
295
296 public virtual float Step(float timeStep)
297 {
298 if (!Enabled) return TargetValue;
299
300 float origTarget = TargetValue; // DEBUG
301 float origCurrVal = CurrentValue; // DEBUG
302
303 float correction = 0f;
304 float error = TargetValue - CurrentValue;
305 if (!ErrorIsZero(error))
306 {
307 correction = Step(timeStep, error);
308
309 CurrentValue += correction;
310
311 // The desired value reduces to zero which also reduces the difference with current.
312 // If the decay time is infinite, don't decay at all.
313 float decayFactor = 0f;
314 if (TargetValueDecayTimeScale != BSMotor.Infinite)
315 {
316 decayFactor = (1.0f / TargetValueDecayTimeScale) * timeStep;
317 TargetValue *= (1f - decayFactor);
318 }
319
320 // The amount we can correct the error is reduced by the friction
321 float frictionFactor = 0f;
322 if (FrictionTimescale != BSMotor.Infinite)
323 {
324 // frictionFactor = (Vector3.One / FrictionTimescale) * timeStep;
325 // Individual friction components can be 'infinite' so compute each separately.
326 frictionFactor = 1f / FrictionTimescale;
327 frictionFactor *= timeStep;
328 CurrentValue *= (1f - frictionFactor);
329 }
330
331 MDetailLog("{0}, BSFMotor.Step,nonZero,{1},origCurr={2},origTarget={3},timeStep={4},err={5},corr={6}",
332 BSScene.DetailLogZero, UseName, origCurrVal, origTarget,
333 timeStep, error, correction);
334 MDetailLog("{0}, BSFMotor.Step,nonZero,{1},tgtDecayTS={2},decayFact={3},frictTS={4},frictFact={5},tgt={6},curr={7}",
335 BSScene.DetailLogZero, UseName,
336 TargetValueDecayTimeScale, decayFactor, FrictionTimescale, frictionFactor,
337 TargetValue, CurrentValue);
338 }
339 else
340 {
341 // Difference between what we have and target is small. Motor is done.
342 CurrentValue = TargetValue;
343 MDetailLog("{0}, BSFMotor.Step,zero,{1},origTgt={2},origCurr={3},ret={4}",
344 BSScene.DetailLogZero, UseName, origCurrVal, origTarget, CurrentValue);
345 }
346
347 return CurrentValue;
348 }
349
350 public virtual float Step(float timeStep, float error)
351 {
352 if (!Enabled) return 0f;
353
354 LastError = error;
355 float returnCorrection = 0f;
356 if (!ErrorIsZero())
357 {
358 // correction = error / secondsItShouldTakeToCorrect
359 float correctionAmount;
360 if (TimeScale == 0f || TimeScale == BSMotor.Infinite)
361 correctionAmount = error * timeStep;
362 else
363 correctionAmount = error / TimeScale * timeStep;
364
365 returnCorrection = correctionAmount;
366 MDetailLog("{0}, BSFMotor.Step,nonZero,{1},timeStep={2},timeScale={3},err={4},corr={5}",
367 BSScene.DetailLogZero, UseName, timeStep, TimeScale, error, correctionAmount);
368 }
369 return returnCorrection;
370 }
371
372 public override string ToString()
373 {
374 return String.Format("<{0},curr={1},targ={2},lastErr={3},decayTS={4},frictTS={5}>",
375 UseName, CurrentValue, TargetValue, LastError, TargetValueDecayTimeScale, FrictionTimescale);
376 }
377
378}
379
380// ============================================================================
381// ============================================================================
382// Proportional, Integral, Derivitive Motor
383// Good description at http://www.answers.com/topic/pid-controller . Includes processes for choosing p, i and d factors.
384public class BSPIDVMotor : BSVMotor
385{
386 // Larger makes more overshoot, smaller means converge quicker. Range of 0.1 to 10.
387 public Vector3 proportionFactor { get; set; }
388 public Vector3 integralFactor { get; set; }
389 public Vector3 derivFactor { get; set; }
390
391 // The factors are vectors for the three dimensions. This is the proportional of each
392 // that is applied. This could be multiplied through the actual factors but it
393 // is sometimes easier to manipulate the factors and their mix separately.
394 // to
395 public Vector3 FactorMix;
396
397 // Arbritrary factor range.
398 // EfficiencyHigh means move quickly to the correct number. EfficiencyLow means might over correct.
399 public float EfficiencyHigh = 0.4f;
400 public float EfficiencyLow = 4.0f;
401
402 // Running integration of the error
403 Vector3 RunningIntegration { get; set; }
404
405 public BSPIDVMotor(string useName)
406 : base(useName)
407 {
408 proportionFactor = new Vector3(1.00f, 1.00f, 1.00f);
409 integralFactor = new Vector3(1.00f, 1.00f, 1.00f);
410 derivFactor = new Vector3(1.00f, 1.00f, 1.00f);
411 FactorMix = new Vector3(0.5f, 0.25f, 0.25f);
412 RunningIntegration = Vector3.Zero;
413 LastError = Vector3.Zero;
414 }
415
416 public override void Zero()
417 {
418 base.Zero();
419 }
420
421 public override float Efficiency
422 {
423 get { return base.Efficiency; }
424 set
425 {
426 base.Efficiency = Util.Clamp(value, 0f, 1f);
427
428 // Compute factors based on efficiency.
429 // If efficiency is high (1f), use a factor value that moves the error value to zero with little overshoot.
430 // If efficiency is low (0f), use a factor value that overcorrects.
431 // TODO: might want to vary contribution of different factor depending on efficiency.
432 float factor = ((1f - this.Efficiency) * EfficiencyHigh + EfficiencyLow) / 3f;
433 // float factor = (1f - this.Efficiency) * EfficiencyHigh + EfficiencyLow;
434
435 proportionFactor = new Vector3(factor, factor, factor);
436 integralFactor = new Vector3(factor, factor, factor);
437 derivFactor = new Vector3(factor, factor, factor);
438
439 MDetailLog("{0},BSPIDVMotor.setEfficiency,eff={1},factor={2}", BSScene.DetailLogZero, Efficiency, factor);
440 }
441 }
442
443 // Ignore Current and Target Values and just advance the PID computation on this error.
444 public override Vector3 Step(float timeStep, Vector3 error)
445 {
446 if (!Enabled) return Vector3.Zero;
447
448 // Add up the error so we can integrate over the accumulated errors
449 RunningIntegration += error * timeStep;
450
451 // A simple derivitive is the rate of change from the last error.
452 Vector3 derivitive = (error - LastError) * timeStep;
453 LastError = error;
454
455 // Correction = (proportionOfPresentError + accumulationOfPastError + rateOfChangeOfError)
456 Vector3 ret = error * timeStep * proportionFactor * FactorMix.X
457 + RunningIntegration * integralFactor * FactorMix.Y
458 + derivitive * derivFactor * FactorMix.Z
459 ;
460
461 MDetailLog("{0},BSPIDVMotor.step,ts={1},err={2},runnInt={3},deriv={4},ret={5}",
462 BSScene.DetailLogZero, timeStep, error, RunningIntegration, derivitive, ret);
463
464 return ret;
465 }
466}
467}
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSParam.cs b/OpenSim/Region/Physics/BulletSPlugin/BSParam.cs
new file mode 100755
index 0000000..862dbf6
--- /dev/null
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSParam.cs
@@ -0,0 +1,634 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyrightD
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27using System;
28using System.Collections.Generic;
29using System.Text;
30
31using OpenSim.Region.Physics.Manager;
32
33using OpenMetaverse;
34using Nini.Config;
35
36namespace OpenSim.Region.Physics.BulletSPlugin
37{
38public static class BSParam
39{
40 // Level of Detail values kept as float because that's what the Meshmerizer wants
41 public static float MeshLOD { get; private set; }
42 public static float MeshMegaPrimLOD { get; private set; }
43 public static float MeshMegaPrimThreshold { get; private set; }
44 public static float SculptLOD { get; private set; }
45
46 public static float MinimumObjectMass { get; private set; }
47 public static float MaximumObjectMass { get; private set; }
48
49 public static float LinearDamping { get; private set; }
50 public static float AngularDamping { get; private set; }
51 public static float DeactivationTime { get; private set; }
52 public static float LinearSleepingThreshold { get; private set; }
53 public static float AngularSleepingThreshold { get; private set; }
54 public static float CcdMotionThreshold { get; private set; }
55 public static float CcdSweptSphereRadius { get; private set; }
56 public static float ContactProcessingThreshold { get; private set; }
57
58 public static bool ShouldMeshSculptedPrim { get; private set; } // cause scuplted prims to get meshed
59 public static bool ShouldForceSimplePrimMeshing { get; private set; } // if a cube or sphere, let Bullet do internal shapes
60 public static bool ShouldUseHullsForPhysicalObjects { get; private set; } // 'true' if should create hulls for physical objects
61
62 public static float TerrainImplementation { get; private set; }
63 public static float TerrainFriction { get; private set; }
64 public static float TerrainHitFraction { get; private set; }
65 public static float TerrainRestitution { get; private set; }
66 public static float TerrainCollisionMargin { get; private set; }
67
68 // Avatar parameters
69 public static float AvatarFriction { get; private set; }
70 public static float AvatarStandingFriction { get; private set; }
71 public static float AvatarAlwaysRunFactor { get; private set; }
72 public static float AvatarDensity { get; private set; }
73 public static float AvatarRestitution { get; private set; }
74 public static float AvatarCapsuleWidth { get; private set; }
75 public static float AvatarCapsuleDepth { get; private set; }
76 public static float AvatarCapsuleHeight { get; private set; }
77 public static float AvatarContactProcessingThreshold { get; private set; }
78 public static float AvatarStepHeight { get; private set; }
79 public static float AvatarStepApproachFactor { get; private set; }
80 public static float AvatarStepForceFactor { get; private set; }
81
82 public static float VehicleAngularDamping { get; private set; }
83 public static float VehicleDebuggingEnabled { get; private set; }
84
85 public static float LinksetImplementation { get; private set; }
86 public static float LinkConstraintUseFrameOffset { get; private set; }
87 public static float LinkConstraintEnableTransMotor { get; private set; }
88 public static float LinkConstraintTransMotorMaxVel { get; private set; }
89 public static float LinkConstraintTransMotorMaxForce { get; private set; }
90 public static float LinkConstraintERP { get; private set; }
91 public static float LinkConstraintCFM { get; private set; }
92 public static float LinkConstraintSolverIterations { get; private set; }
93
94 public static float PID_D { get; private set; } // derivative
95 public static float PID_P { get; private set; } // proportional
96
97 // Various constants that come from that other virtual world that shall not be named
98 public const float MinGravityZ = -1f;
99 public const float MaxGravityZ = 28f;
100 public const float MinFriction = 0f;
101 public const float MaxFriction = 255f;
102 public const float MinDensity = 0f;
103 public const float MaxDensity = 22587f;
104 public const float MinRestitution = 0f;
105 public const float MaxRestitution = 1f;
106 public const float MaxAddForceMagnitude = 20000f;
107
108 // ===========================================================================
109 public delegate void ParamUser(BSScene scene, IConfig conf, string paramName, float val);
110 public delegate float ParamGet(BSScene scene);
111 public delegate void ParamSet(BSScene scene, string paramName, uint localID, float val);
112 public delegate void SetOnObject(BSScene scene, BSPhysObject obj, float val);
113
114 public struct ParameterDefn
115 {
116 public string name; // string name of the parameter
117 public string desc; // a short description of what the parameter means
118 public float defaultValue; // default value if not specified anywhere else
119 public ParamUser userParam; // get the value from the configuration file
120 public ParamGet getter; // return the current value stored for this parameter
121 public ParamSet setter; // set the current value for this parameter
122 public SetOnObject onObject; // set the value on an object in the physical domain
123 public ParameterDefn(string n, string d, float v, ParamUser u, ParamGet g, ParamSet s)
124 {
125 name = n;
126 desc = d;
127 defaultValue = v;
128 userParam = u;
129 getter = g;
130 setter = s;
131 onObject = null;
132 }
133 public ParameterDefn(string n, string d, float v, ParamUser u, ParamGet g, ParamSet s, SetOnObject o)
134 {
135 name = n;
136 desc = d;
137 defaultValue = v;
138 userParam = u;
139 getter = g;
140 setter = s;
141 onObject = o;
142 }
143 }
144
145 // List of all of the externally visible parameters.
146 // For each parameter, this table maps a text name to getter and setters.
147 // To add a new externally referencable/settable parameter, add the paramter storage
148 // location somewhere in the program and make an entry in this table with the
149 // getters and setters.
150 // It is easiest to find an existing definition and copy it.
151 // Parameter values are floats. Booleans are converted to a floating value.
152 //
153 // A ParameterDefn() takes the following parameters:
154 // -- the text name of the parameter. This is used for console input and ini file.
155 // -- a short text description of the parameter. This shows up in the console listing.
156 // -- a default value (float)
157 // -- a delegate for fetching the parameter from the ini file.
158 // Should handle fetching the right type from the ini file and converting it.
159 // -- a delegate for getting the value as a float
160 // -- a delegate for setting the value from a float
161 // -- an optional delegate to update the value in the world. Most often used to
162 // push the new value to an in-world object.
163 //
164 // The single letter parameters for the delegates are:
165 // s = BSScene
166 // o = BSPhysObject
167 // p = string parameter name
168 // l = localID of referenced object
169 // v = value (float)
170 // cf = parameter configuration class (for fetching values from ini file)
171 private static ParameterDefn[] ParameterDefinitions =
172 {
173 new ParameterDefn("MeshSculptedPrim", "Whether to create meshes for sculpties",
174 ConfigurationParameters.numericTrue,
175 (s,cf,p,v) => { ShouldMeshSculptedPrim = cf.GetBoolean(p, BSParam.BoolNumeric(v)); },
176 (s) => { return BSParam.NumericBool(ShouldMeshSculptedPrim); },
177 (s,p,l,v) => { ShouldMeshSculptedPrim = BSParam.BoolNumeric(v); } ),
178 new ParameterDefn("ForceSimplePrimMeshing", "If true, only use primitive meshes for objects",
179 ConfigurationParameters.numericFalse,
180 (s,cf,p,v) => { ShouldForceSimplePrimMeshing = cf.GetBoolean(p, BSParam.BoolNumeric(v)); },
181 (s) => { return BSParam.NumericBool(ShouldForceSimplePrimMeshing); },
182 (s,p,l,v) => { ShouldForceSimplePrimMeshing = BSParam.BoolNumeric(v); } ),
183 new ParameterDefn("UseHullsForPhysicalObjects", "If true, create hulls for physical objects",
184 ConfigurationParameters.numericTrue,
185 (s,cf,p,v) => { ShouldUseHullsForPhysicalObjects = cf.GetBoolean(p, BSParam.BoolNumeric(v)); },
186 (s) => { return BSParam.NumericBool(ShouldUseHullsForPhysicalObjects); },
187 (s,p,l,v) => { ShouldUseHullsForPhysicalObjects = BSParam.BoolNumeric(v); } ),
188
189 new ParameterDefn("MeshLevelOfDetail", "Level of detail to render meshes (32, 16, 8 or 4. 32=most detailed)",
190 8f,
191 (s,cf,p,v) => { MeshLOD = (float)cf.GetInt(p, (int)v); },
192 (s) => { return MeshLOD; },
193 (s,p,l,v) => { MeshLOD = v; } ),
194 new ParameterDefn("MeshLevelOfDetailMegaPrim", "Level of detail to render meshes larger than threshold meters",
195 16f,
196 (s,cf,p,v) => { MeshMegaPrimLOD = (float)cf.GetInt(p, (int)v); },
197 (s) => { return MeshMegaPrimLOD; },
198 (s,p,l,v) => { MeshMegaPrimLOD = v; } ),
199 new ParameterDefn("MeshLevelOfDetailMegaPrimThreshold", "Size (in meters) of a mesh before using MeshMegaPrimLOD",
200 10f,
201 (s,cf,p,v) => { MeshMegaPrimThreshold = (float)cf.GetInt(p, (int)v); },
202 (s) => { return MeshMegaPrimThreshold; },
203 (s,p,l,v) => { MeshMegaPrimThreshold = v; } ),
204 new ParameterDefn("SculptLevelOfDetail", "Level of detail to render sculpties (32, 16, 8 or 4. 32=most detailed)",
205 32f,
206 (s,cf,p,v) => { SculptLOD = (float)cf.GetInt(p, (int)v); },
207 (s) => { return SculptLOD; },
208 (s,p,l,v) => { SculptLOD = v; } ),
209
210 new ParameterDefn("MaxSubStep", "In simulation step, maximum number of substeps",
211 10f,
212 (s,cf,p,v) => { s.m_maxSubSteps = cf.GetInt(p, (int)v); },
213 (s) => { return (float)s.m_maxSubSteps; },
214 (s,p,l,v) => { s.m_maxSubSteps = (int)v; } ),
215 new ParameterDefn("FixedTimeStep", "In simulation step, seconds of one substep (1/60)",
216 1f / 60f,
217 (s,cf,p,v) => { s.m_fixedTimeStep = cf.GetFloat(p, v); },
218 (s) => { return (float)s.m_fixedTimeStep; },
219 (s,p,l,v) => { s.m_fixedTimeStep = v; } ),
220 new ParameterDefn("NominalFrameRate", "The base frame rate we claim",
221 55f,
222 (s,cf,p,v) => { s.NominalFrameRate = cf.GetInt(p, (int)v); },
223 (s) => { return (float)s.NominalFrameRate; },
224 (s,p,l,v) => { s.NominalFrameRate = (int)v; } ),
225 new ParameterDefn("MaxCollisionsPerFrame", "Max collisions returned at end of each frame",
226 2048f,
227 (s,cf,p,v) => { s.m_maxCollisionsPerFrame = cf.GetInt(p, (int)v); },
228 (s) => { return (float)s.m_maxCollisionsPerFrame; },
229 (s,p,l,v) => { s.m_maxCollisionsPerFrame = (int)v; } ),
230 new ParameterDefn("MaxUpdatesPerFrame", "Max updates returned at end of each frame",
231 8000f,
232 (s,cf,p,v) => { s.m_maxUpdatesPerFrame = cf.GetInt(p, (int)v); },
233 (s) => { return (float)s.m_maxUpdatesPerFrame; },
234 (s,p,l,v) => { s.m_maxUpdatesPerFrame = (int)v; } ),
235 new ParameterDefn("MaxTaintsToProcessPerStep", "Number of update taints to process before each simulation step",
236 500f,
237 (s,cf,p,v) => { s.m_taintsToProcessPerStep = cf.GetInt(p, (int)v); },
238 (s) => { return (float)s.m_taintsToProcessPerStep; },
239 (s,p,l,v) => { s.m_taintsToProcessPerStep = (int)v; } ),
240 new ParameterDefn("MinObjectMass", "Minimum object mass (0.0001)",
241 0.0001f,
242 (s,cf,p,v) => { MinimumObjectMass = cf.GetFloat(p, v); },
243 (s) => { return (float)MinimumObjectMass; },
244 (s,p,l,v) => { MinimumObjectMass = v; } ),
245 new ParameterDefn("MaxObjectMass", "Maximum object mass (10000.01)",
246 10000.01f,
247 (s,cf,p,v) => { MaximumObjectMass = cf.GetFloat(p, v); },
248 (s) => { return (float)MaximumObjectMass; },
249 (s,p,l,v) => { MaximumObjectMass = v; } ),
250
251 new ParameterDefn("PID_D", "Derivitive factor for motion smoothing",
252 2200f,
253 (s,cf,p,v) => { PID_D = cf.GetFloat(p, v); },
254 (s) => { return (float)PID_D; },
255 (s,p,l,v) => { PID_D = v; } ),
256 new ParameterDefn("PID_P", "Parameteric factor for motion smoothing",
257 900f,
258 (s,cf,p,v) => { PID_P = cf.GetFloat(p, v); },
259 (s) => { return (float)PID_P; },
260 (s,p,l,v) => { PID_P = v; } ),
261
262 new ParameterDefn("DefaultFriction", "Friction factor used on new objects",
263 0.2f,
264 (s,cf,p,v) => { s.UnmanagedParams[0].defaultFriction = cf.GetFloat(p, v); },
265 (s) => { return s.UnmanagedParams[0].defaultFriction; },
266 (s,p,l,v) => { s.UnmanagedParams[0].defaultFriction = v; } ),
267 new ParameterDefn("DefaultDensity", "Density for new objects" ,
268 10.000006836f, // Aluminum g/cm3
269 (s,cf,p,v) => { s.UnmanagedParams[0].defaultDensity = cf.GetFloat(p, v); },
270 (s) => { return s.UnmanagedParams[0].defaultDensity; },
271 (s,p,l,v) => { s.UnmanagedParams[0].defaultDensity = v; } ),
272 new ParameterDefn("DefaultRestitution", "Bouncyness of an object" ,
273 0f,
274 (s,cf,p,v) => { s.UnmanagedParams[0].defaultRestitution = cf.GetFloat(p, v); },
275 (s) => { return s.UnmanagedParams[0].defaultRestitution; },
276 (s,p,l,v) => { s.UnmanagedParams[0].defaultRestitution = v; } ),
277 new ParameterDefn("CollisionMargin", "Margin around objects before collisions are calculated (must be zero!)",
278 0.04f,
279 (s,cf,p,v) => { s.UnmanagedParams[0].collisionMargin = cf.GetFloat(p, v); },
280 (s) => { return s.UnmanagedParams[0].collisionMargin; },
281 (s,p,l,v) => { s.UnmanagedParams[0].collisionMargin = v; } ),
282 new ParameterDefn("Gravity", "Vertical force of gravity (negative means down)",
283 -9.80665f,
284 (s,cf,p,v) => { s.UnmanagedParams[0].gravity = cf.GetFloat(p, v); },
285 (s) => { return s.UnmanagedParams[0].gravity; },
286 (s,p,l,v) => { s.UpdateParameterObject((x)=>{s.UnmanagedParams[0].gravity=x;}, p, PhysParameterEntry.APPLY_TO_NONE, v); },
287 (s,o,v) => { s.PE.SetGravity(o.PhysBody, new Vector3(0f,0f,v)); } ),
288
289
290 new ParameterDefn("LinearDamping", "Factor to damp linear movement per second (0.0 - 1.0)",
291 0f,
292 (s,cf,p,v) => { LinearDamping = cf.GetFloat(p, v); },
293 (s) => { return LinearDamping; },
294 (s,p,l,v) => { s.UpdateParameterObject((x)=>{LinearDamping=x;}, p, l, v); },
295 (s,o,v) => { s.PE.SetDamping(o.PhysBody, v, AngularDamping); } ),
296 new ParameterDefn("AngularDamping", "Factor to damp angular movement per second (0.0 - 1.0)",
297 0f,
298 (s,cf,p,v) => { AngularDamping = cf.GetFloat(p, v); },
299 (s) => { return AngularDamping; },
300 (s,p,l,v) => { s.UpdateParameterObject((x)=>{AngularDamping=x;}, p, l, v); },
301 (s,o,v) => { s.PE.SetDamping(o.PhysBody, LinearDamping, v); } ),
302 new ParameterDefn("DeactivationTime", "Seconds before considering an object potentially static",
303 0.2f,
304 (s,cf,p,v) => { DeactivationTime = cf.GetFloat(p, v); },
305 (s) => { return DeactivationTime; },
306 (s,p,l,v) => { s.UpdateParameterObject((x)=>{DeactivationTime=x;}, p, l, v); },
307 (s,o,v) => { s.PE.SetDeactivationTime(o.PhysBody, v); } ),
308 new ParameterDefn("LinearSleepingThreshold", "Seconds to measure linear movement before considering static",
309 0.8f,
310 (s,cf,p,v) => { LinearSleepingThreshold = cf.GetFloat(p, v); },
311 (s) => { return LinearSleepingThreshold; },
312 (s,p,l,v) => { s.UpdateParameterObject((x)=>{LinearSleepingThreshold=x;}, p, l, v); },
313 (s,o,v) => { s.PE.SetSleepingThresholds(o.PhysBody, v, v); } ),
314 new ParameterDefn("AngularSleepingThreshold", "Seconds to measure angular movement before considering static",
315 1.0f,
316 (s,cf,p,v) => { AngularSleepingThreshold = cf.GetFloat(p, v); },
317 (s) => { return AngularSleepingThreshold; },
318 (s,p,l,v) => { s.UpdateParameterObject((x)=>{AngularSleepingThreshold=x;}, p, l, v); },
319 (s,o,v) => { s.PE.SetSleepingThresholds(o.PhysBody, v, v); } ),
320 new ParameterDefn("CcdMotionThreshold", "Continuious collision detection threshold (0 means no CCD)" ,
321 0.3f, // set to zero to disable
322 (s,cf,p,v) => { CcdMotionThreshold = cf.GetFloat(p, v); },
323 (s) => { return CcdMotionThreshold; },
324 (s,p,l,v) => { s.UpdateParameterObject((x)=>{CcdMotionThreshold=x;}, p, l, v); },
325 (s,o,v) => { s.PE.SetCcdMotionThreshold(o.PhysBody, v); } ),
326 new ParameterDefn("CcdSweptSphereRadius", "Continuious collision detection test radius" ,
327 0.2f,
328 (s,cf,p,v) => { CcdSweptSphereRadius = cf.GetFloat(p, v); },
329 (s) => { return CcdSweptSphereRadius; },
330 (s,p,l,v) => { s.UpdateParameterObject((x)=>{CcdSweptSphereRadius=x;}, p, l, v); },
331 (s,o,v) => { s.PE.SetCcdSweptSphereRadius(o.PhysBody, v); } ),
332 new ParameterDefn("ContactProcessingThreshold", "Distance between contacts before doing collision check" ,
333 0.1f,
334 (s,cf,p,v) => { ContactProcessingThreshold = cf.GetFloat(p, v); },
335 (s) => { return ContactProcessingThreshold; },
336 (s,p,l,v) => { s.UpdateParameterObject((x)=>{ContactProcessingThreshold=x;}, p, l, v); },
337 (s,o,v) => { s.PE.SetContactProcessingThreshold(o.PhysBody, v); } ),
338
339 new ParameterDefn("TerrainImplementation", "Type of shape to use for terrain (0=heightmap, 1=mesh)",
340 (float)BSTerrainPhys.TerrainImplementation.Mesh,
341 (s,cf,p,v) => { TerrainImplementation = cf.GetFloat(p,v); },
342 (s) => { return TerrainImplementation; },
343 (s,p,l,v) => { TerrainImplementation = v; } ),
344 new ParameterDefn("TerrainFriction", "Factor to reduce movement against terrain surface" ,
345 0.3f,
346 (s,cf,p,v) => { TerrainFriction = cf.GetFloat(p, v); },
347 (s) => { return TerrainFriction; },
348 (s,p,l,v) => { TerrainFriction = v; /* TODO: set on real terrain */} ),
349 new ParameterDefn("TerrainHitFraction", "Distance to measure hit collisions" ,
350 0.8f,
351 (s,cf,p,v) => { TerrainHitFraction = cf.GetFloat(p, v); },
352 (s) => { return TerrainHitFraction; },
353 (s,p,l,v) => { TerrainHitFraction = v; /* TODO: set on real terrain */ } ),
354 new ParameterDefn("TerrainRestitution", "Bouncyness" ,
355 0f,
356 (s,cf,p,v) => { TerrainRestitution = cf.GetFloat(p, v); },
357 (s) => { return TerrainRestitution; },
358 (s,p,l,v) => { TerrainRestitution = v; /* TODO: set on real terrain */ } ),
359 new ParameterDefn("TerrainCollisionMargin", "Margin where collision checking starts" ,
360 0.04f,
361 (s,cf,p,v) => { TerrainCollisionMargin = cf.GetFloat(p, v); },
362 (s) => { return TerrainCollisionMargin; },
363 (s,p,l,v) => { TerrainCollisionMargin = v; /* TODO: set on real terrain */ } ),
364
365 new ParameterDefn("AvatarFriction", "Factor to reduce movement against an avatar. Changed on avatar recreation.",
366 0.2f,
367 (s,cf,p,v) => { AvatarFriction = cf.GetFloat(p, v); },
368 (s) => { return AvatarFriction; },
369 (s,p,l,v) => { s.UpdateParameterObject((x)=>{AvatarFriction=x;}, p, l, v); } ),
370 new ParameterDefn("AvatarStandingFriction", "Avatar friction when standing. Changed on avatar recreation.",
371 10.0f,
372 (s,cf,p,v) => { AvatarStandingFriction = cf.GetFloat(p, v); },
373 (s) => { return AvatarStandingFriction; },
374 (s,p,l,v) => { AvatarStandingFriction = v; } ),
375 new ParameterDefn("AvatarAlwaysRunFactor", "Speed multiplier if avatar is set to always run",
376 1.3f,
377 (s,cf,p,v) => { AvatarAlwaysRunFactor = cf.GetFloat(p, v); },
378 (s) => { return AvatarAlwaysRunFactor; },
379 (s,p,l,v) => { AvatarAlwaysRunFactor = v; } ),
380 new ParameterDefn("AvatarDensity", "Density of an avatar. Changed on avatar recreation.",
381 3.5f,
382 (s,cf,p,v) => { AvatarDensity = cf.GetFloat(p, v); },
383 (s) => { return AvatarDensity; },
384 (s,p,l,v) => { s.UpdateParameterObject((x)=>{AvatarDensity=x;}, p, l, v); } ),
385 new ParameterDefn("AvatarRestitution", "Bouncyness. Changed on avatar recreation.",
386 0f,
387 (s,cf,p,v) => { AvatarRestitution = cf.GetFloat(p, v); },
388 (s) => { return AvatarRestitution; },
389 (s,p,l,v) => { s.UpdateParameterObject((x)=>{AvatarRestitution=x;}, p, l, v); } ),
390 new ParameterDefn("AvatarCapsuleWidth", "The distance between the sides of the avatar capsule",
391 0.6f,
392 (s,cf,p,v) => { AvatarCapsuleWidth = cf.GetFloat(p, v); },
393 (s) => { return AvatarCapsuleWidth; },
394 (s,p,l,v) => { s.UpdateParameterObject((x)=>{AvatarCapsuleWidth=x;}, p, l, v); } ),
395 new ParameterDefn("AvatarCapsuleDepth", "The distance between the front and back of the avatar capsule",
396 0.45f,
397 (s,cf,p,v) => { AvatarCapsuleDepth = cf.GetFloat(p, v); },
398 (s) => { return AvatarCapsuleDepth; },
399 (s,p,l,v) => { s.UpdateParameterObject((x)=>{AvatarCapsuleDepth=x;}, p, l, v); } ),
400 new ParameterDefn("AvatarCapsuleHeight", "Default height of space around avatar",
401 1.5f,
402 (s,cf,p,v) => { AvatarCapsuleHeight = cf.GetFloat(p, v); },
403 (s) => { return AvatarCapsuleHeight; },
404 (s,p,l,v) => { s.UpdateParameterObject((x)=>{AvatarCapsuleHeight=x;}, p, l, v); } ),
405 new ParameterDefn("AvatarContactProcessingThreshold", "Distance from capsule to check for collisions",
406 0.1f,
407 (s,cf,p,v) => { AvatarContactProcessingThreshold = cf.GetFloat(p, v); },
408 (s) => { return AvatarContactProcessingThreshold; },
409 (s,p,l,v) => { s.UpdateParameterObject((x)=>{AvatarContactProcessingThreshold=x;}, p, l, v); } ),
410 new ParameterDefn("AvatarStepHeight", "Height of a step obstacle to consider step correction",
411 0.3f,
412 (s,cf,p,v) => { AvatarStepHeight = cf.GetFloat(p, v); },
413 (s) => { return AvatarStepHeight; },
414 (s,p,l,v) => { AvatarStepHeight = v; } ),
415 new ParameterDefn("AvatarStepApproachFactor", "Factor to control angle of approach to step (0=straight on)",
416 0.6f,
417 (s,cf,p,v) => { AvatarStepApproachFactor = cf.GetFloat(p, v); },
418 (s) => { return AvatarStepApproachFactor; },
419 (s,p,l,v) => { AvatarStepApproachFactor = v; } ),
420 new ParameterDefn("AvatarStepForceFactor", "Controls the amount of force up applied to step up onto a step",
421 2.0f,
422 (s,cf,p,v) => { AvatarStepForceFactor = cf.GetFloat(p, v); },
423 (s) => { return AvatarStepForceFactor; },
424 (s,p,l,v) => { AvatarStepForceFactor = v; } ),
425
426 new ParameterDefn("VehicleAngularDamping", "Factor to damp vehicle angular movement per second (0.0 - 1.0)",
427 0.95f,
428 (s,cf,p,v) => { VehicleAngularDamping = cf.GetFloat(p, v); },
429 (s) => { return VehicleAngularDamping; },
430 (s,p,l,v) => { VehicleAngularDamping = v; } ),
431 new ParameterDefn("VehicleDebuggingEnable", "Turn on/off vehicle debugging",
432 ConfigurationParameters.numericFalse,
433 (s,cf,p,v) => { VehicleDebuggingEnabled = BSParam.NumericBool(cf.GetBoolean(p, BSParam.BoolNumeric(v))); },
434 (s) => { return VehicleDebuggingEnabled; },
435 (s,p,l,v) => { VehicleDebuggingEnabled = v; } ),
436
437 new ParameterDefn("MaxPersistantManifoldPoolSize", "Number of manifolds pooled (0 means default of 4096)",
438 0f,
439 (s,cf,p,v) => { s.UnmanagedParams[0].maxPersistantManifoldPoolSize = cf.GetFloat(p, v); },
440 (s) => { return s.UnmanagedParams[0].maxPersistantManifoldPoolSize; },
441 (s,p,l,v) => { s.UnmanagedParams[0].maxPersistantManifoldPoolSize = v; } ),
442 new ParameterDefn("MaxCollisionAlgorithmPoolSize", "Number of collisions pooled (0 means default of 4096)",
443 0f,
444 (s,cf,p,v) => { s.UnmanagedParams[0].maxCollisionAlgorithmPoolSize = cf.GetFloat(p, v); },
445 (s) => { return s.UnmanagedParams[0].maxCollisionAlgorithmPoolSize; },
446 (s,p,l,v) => { s.UnmanagedParams[0].maxCollisionAlgorithmPoolSize = v; } ),
447 new ParameterDefn("ShouldDisableContactPoolDynamicAllocation", "Enable to allow large changes in object count",
448 ConfigurationParameters.numericFalse,
449 (s,cf,p,v) => { s.UnmanagedParams[0].shouldDisableContactPoolDynamicAllocation = BSParam.NumericBool(cf.GetBoolean(p, BSParam.BoolNumeric(v))); },
450 (s) => { return s.UnmanagedParams[0].shouldDisableContactPoolDynamicAllocation; },
451 (s,p,l,v) => { s.UnmanagedParams[0].shouldDisableContactPoolDynamicAllocation = v; } ),
452 new ParameterDefn("ShouldForceUpdateAllAabbs", "Enable to recomputer AABBs every simulator step",
453 ConfigurationParameters.numericFalse,
454 (s,cf,p,v) => { s.UnmanagedParams[0].shouldForceUpdateAllAabbs = BSParam.NumericBool(cf.GetBoolean(p, BSParam.BoolNumeric(v))); },
455 (s) => { return s.UnmanagedParams[0].shouldForceUpdateAllAabbs; },
456 (s,p,l,v) => { s.UnmanagedParams[0].shouldForceUpdateAllAabbs = v; } ),
457 new ParameterDefn("ShouldRandomizeSolverOrder", "Enable for slightly better stacking interaction",
458 ConfigurationParameters.numericTrue,
459 (s,cf,p,v) => { s.UnmanagedParams[0].shouldRandomizeSolverOrder = BSParam.NumericBool(cf.GetBoolean(p, BSParam.BoolNumeric(v))); },
460 (s) => { return s.UnmanagedParams[0].shouldRandomizeSolverOrder; },
461 (s,p,l,v) => { s.UnmanagedParams[0].shouldRandomizeSolverOrder = v; } ),
462 new ParameterDefn("ShouldSplitSimulationIslands", "Enable splitting active object scanning islands",
463 ConfigurationParameters.numericTrue,
464 (s,cf,p,v) => { s.UnmanagedParams[0].shouldSplitSimulationIslands = BSParam.NumericBool(cf.GetBoolean(p, BSParam.BoolNumeric(v))); },
465 (s) => { return s.UnmanagedParams[0].shouldSplitSimulationIslands; },
466 (s,p,l,v) => { s.UnmanagedParams[0].shouldSplitSimulationIslands = v; } ),
467 new ParameterDefn("ShouldEnableFrictionCaching", "Enable friction computation caching",
468 ConfigurationParameters.numericTrue,
469 (s,cf,p,v) => { s.UnmanagedParams[0].shouldEnableFrictionCaching = BSParam.NumericBool(cf.GetBoolean(p, BSParam.BoolNumeric(v))); },
470 (s) => { return s.UnmanagedParams[0].shouldEnableFrictionCaching; },
471 (s,p,l,v) => { s.UnmanagedParams[0].shouldEnableFrictionCaching = v; } ),
472 new ParameterDefn("NumberOfSolverIterations", "Number of internal iterations (0 means default)",
473 0f, // zero says use Bullet default
474 (s,cf,p,v) => { s.UnmanagedParams[0].numberOfSolverIterations = cf.GetFloat(p, v); },
475 (s) => { return s.UnmanagedParams[0].numberOfSolverIterations; },
476 (s,p,l,v) => { s.UnmanagedParams[0].numberOfSolverIterations = v; } ),
477
478 new ParameterDefn("LinksetImplementation", "Type of linkset implementation (0=Constraint, 1=Compound, 2=Manual)",
479 (float)BSLinkset.LinksetImplementation.Compound,
480 (s,cf,p,v) => { LinksetImplementation = cf.GetFloat(p,v); },
481 (s) => { return LinksetImplementation; },
482 (s,p,l,v) => { LinksetImplementation = v; } ),
483 new ParameterDefn("LinkConstraintUseFrameOffset", "For linksets built with constraints, enable frame offsetFor linksets built with constraints, enable frame offset.",
484 ConfigurationParameters.numericFalse,
485 (s,cf,p,v) => { LinkConstraintUseFrameOffset = BSParam.NumericBool(cf.GetBoolean(p, BSParam.BoolNumeric(v))); },
486 (s) => { return LinkConstraintUseFrameOffset; },
487 (s,p,l,v) => { LinkConstraintUseFrameOffset = v; } ),
488 new ParameterDefn("LinkConstraintEnableTransMotor", "Whether to enable translational motor on linkset constraints",
489 ConfigurationParameters.numericTrue,
490 (s,cf,p,v) => { LinkConstraintEnableTransMotor = BSParam.NumericBool(cf.GetBoolean(p, BSParam.BoolNumeric(v))); },
491 (s) => { return LinkConstraintEnableTransMotor; },
492 (s,p,l,v) => { LinkConstraintEnableTransMotor = v; } ),
493 new ParameterDefn("LinkConstraintTransMotorMaxVel", "Maximum velocity to be applied by translational motor in linkset constraints",
494 5.0f,
495 (s,cf,p,v) => { LinkConstraintTransMotorMaxVel = cf.GetFloat(p, v); },
496 (s) => { return LinkConstraintTransMotorMaxVel; },
497 (s,p,l,v) => { LinkConstraintTransMotorMaxVel = v; } ),
498 new ParameterDefn("LinkConstraintTransMotorMaxForce", "Maximum force to be applied by translational motor in linkset constraints",
499 0.1f,
500 (s,cf,p,v) => { LinkConstraintTransMotorMaxForce = cf.GetFloat(p, v); },
501 (s) => { return LinkConstraintTransMotorMaxForce; },
502 (s,p,l,v) => { LinkConstraintTransMotorMaxForce = v; } ),
503 new ParameterDefn("LinkConstraintCFM", "Amount constraint can be violated. 0=no violation, 1=infinite. Default=0.1",
504 0.1f,
505 (s,cf,p,v) => { LinkConstraintCFM = cf.GetFloat(p, v); },
506 (s) => { return LinkConstraintCFM; },
507 (s,p,l,v) => { LinkConstraintCFM = v; } ),
508 new ParameterDefn("LinkConstraintERP", "Amount constraint is corrected each tick. 0=none, 1=all. Default = 0.2",
509 0.1f,
510 (s,cf,p,v) => { LinkConstraintERP = cf.GetFloat(p, v); },
511 (s) => { return LinkConstraintERP; },
512 (s,p,l,v) => { LinkConstraintERP = v; } ),
513 new ParameterDefn("LinkConstraintSolverIterations", "Number of solver iterations when computing constraint. (0 = Bullet default)",
514 40,
515 (s,cf,p,v) => { LinkConstraintSolverIterations = cf.GetFloat(p, v); },
516 (s) => { return LinkConstraintSolverIterations; },
517 (s,p,l,v) => { LinkConstraintSolverIterations = v; } ),
518
519 new ParameterDefn("PhysicsMetricFrames", "Frames between outputting detailed phys metrics. (0 is off)",
520 0f,
521 (s,cf,p,v) => { s.PhysicsMetricDumpFrames = cf.GetFloat(p, (int)v); },
522 (s) => { return (float)s.PhysicsMetricDumpFrames; },
523 (s,p,l,v) => { s.PhysicsMetricDumpFrames = (int)v; } ),
524 new ParameterDefn("ResetBroadphasePool", "Setting this is any value resets the broadphase collision pool",
525 0f,
526 (s,cf,p,v) => { ; },
527 (s) => { return 0f; },
528 (s,p,l,v) => { BSParam.ResetBroadphasePoolTainted(s, v); } ),
529 new ParameterDefn("ResetConstraintSolver", "Setting this is any value resets the constraint solver",
530 0f,
531 (s,cf,p,v) => { ; },
532 (s) => { return 0f; },
533 (s,p,l,v) => { BSParam.ResetConstraintSolverTainted(s, v); } ),
534 };
535
536 // Convert a boolean to our numeric true and false values
537 public static float NumericBool(bool b)
538 {
539 return (b ? ConfigurationParameters.numericTrue : ConfigurationParameters.numericFalse);
540 }
541
542 // Convert numeric true and false values to a boolean
543 public static bool BoolNumeric(float b)
544 {
545 return (b == ConfigurationParameters.numericTrue ? true : false);
546 }
547
548 private static void ResetBroadphasePoolTainted(BSScene pPhysScene, float v)
549 {
550 BSScene physScene = pPhysScene;
551 physScene.TaintedObject("BSParam.ResetBroadphasePoolTainted", delegate()
552 {
553 physScene.PE.ResetBroadphasePool(physScene.World);
554 });
555 }
556
557 private static void ResetConstraintSolverTainted(BSScene pPhysScene, float v)
558 {
559 BSScene physScene = pPhysScene;
560 physScene.TaintedObject("BSParam.ResetConstraintSolver", delegate()
561 {
562 physScene.PE.ResetConstraintSolver(physScene.World);
563 });
564 }
565
566 // Search through the parameter definitions and return the matching
567 // ParameterDefn structure.
568 // Case does not matter as names are compared after converting to lower case.
569 // Returns 'false' if the parameter is not found.
570 internal static bool TryGetParameter(string paramName, out ParameterDefn defn)
571 {
572 bool ret = false;
573 ParameterDefn foundDefn = new ParameterDefn();
574 string pName = paramName.ToLower();
575
576 foreach (ParameterDefn parm in ParameterDefinitions)
577 {
578 if (pName == parm.name.ToLower())
579 {
580 foundDefn = parm;
581 ret = true;
582 break;
583 }
584 }
585 defn = foundDefn;
586 return ret;
587 }
588
589 // Pass through the settable parameters and set the default values
590 internal static void SetParameterDefaultValues(BSScene physicsScene)
591 {
592 foreach (ParameterDefn parm in ParameterDefinitions)
593 {
594 parm.setter(physicsScene, parm.name, PhysParameterEntry.APPLY_TO_NONE, parm.defaultValue);
595 }
596 }
597
598 // Get user set values out of the ini file.
599 internal static void SetParameterConfigurationValues(BSScene physicsScene, IConfig cfg)
600 {
601 foreach (ParameterDefn parm in ParameterDefinitions)
602 {
603 parm.userParam(physicsScene, cfg, parm.name, parm.defaultValue);
604 }
605 }
606
607 internal static PhysParameterEntry[] SettableParameters = new PhysParameterEntry[1];
608
609 // This creates an array in the correct format for returning the list of
610 // parameters. This is used by the 'list' option of the 'physics' command.
611 internal static void BuildParameterTable()
612 {
613 if (SettableParameters.Length < ParameterDefinitions.Length)
614 {
615 List<PhysParameterEntry> entries = new List<PhysParameterEntry>();
616 for (int ii = 0; ii < ParameterDefinitions.Length; ii++)
617 {
618 ParameterDefn pd = ParameterDefinitions[ii];
619 entries.Add(new PhysParameterEntry(pd.name, pd.desc));
620 }
621
622 // make the list in alphabetical order for estetic reasons
623 entries.Sort(delegate(PhysParameterEntry ppe1, PhysParameterEntry ppe2)
624 {
625 return ppe1.name.CompareTo(ppe2.name);
626 });
627
628 SettableParameters = entries.ToArray();
629 }
630 }
631
632
633}
634}
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSPhysObject.cs b/OpenSim/Region/Physics/BulletSPlugin/BSPhysObject.cs
index f6a890e..821f470 100755
--- a/OpenSim/Region/Physics/BulletSPlugin/BSPhysObject.cs
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSPhysObject.cs
@@ -45,6 +45,16 @@ namespace OpenSim.Region.Physics.BulletSPlugin
45 * ForceVariableName: direct reference (store and fetch) to the value in the physics engine. 45 * ForceVariableName: direct reference (store and fetch) to the value in the physics engine.
46 * The last two (and certainly the last one) should be referenced only in taint-time. 46 * The last two (and certainly the last one) should be referenced only in taint-time.
47 */ 47 */
48
49/*
50 * As of 20121221, the following are the call sequences (going down) for different script physical functions:
51 * llApplyImpulse llApplyRotImpulse llSetTorque llSetForce
52 * SOP.ApplyImpulse SOP.ApplyAngularImpulse SOP.SetAngularImpulse SOP.SetForce
53 * SOG.ApplyImpulse SOG.ApplyAngularImpulse SOG.SetAngularImpulse
54 * PA.AddForce PA.AddAngularForce PA.Torque = v PA.Force = v
55 * BS.ApplyCentralForce BS.ApplyTorque
56 */
57
48public abstract class BSPhysObject : PhysicsActor 58public abstract class BSPhysObject : PhysicsActor
49{ 59{
50 protected BSPhysObject() 60 protected BSPhysObject()
@@ -57,26 +67,45 @@ public abstract class BSPhysObject : PhysicsActor
57 PhysObjectName = name; 67 PhysObjectName = name;
58 TypeName = typeName; 68 TypeName = typeName;
59 69
70 // We don't have any physical representation yet.
71 PhysBody = new BulletBody(localID);
72 PhysShape = new BulletShape();
73
74 // A linkset of just me
60 Linkset = BSLinkset.Factory(PhysicsScene, this); 75 Linkset = BSLinkset.Factory(PhysicsScene, this);
76 PositionDisplacement = OMV.Vector3.Zero;
77
61 LastAssetBuildFailed = false; 78 LastAssetBuildFailed = false;
62 79
80 // Default material type
81 Material = MaterialAttributes.Material.Wood;
82
63 CollisionCollection = new CollisionEventUpdate(); 83 CollisionCollection = new CollisionEventUpdate();
84 CollisionsLastTick = CollisionCollection;
64 SubscribedEventsMs = 0; 85 SubscribedEventsMs = 0;
65 CollidingStep = 0; 86 CollidingStep = 0;
66 CollidingGroundStep = 0; 87 CollidingGroundStep = 0;
67 } 88 }
68 89
90 // Tell the object to clean up.
91 public virtual void Destroy()
92 {
93 UnRegisterAllPreStepActions();
94 }
95
69 public BSScene PhysicsScene { get; protected set; } 96 public BSScene PhysicsScene { get; protected set; }
70 // public override uint LocalID { get; set; } // Use the LocalID definition in PhysicsActor 97 // public override uint LocalID { get; set; } // Use the LocalID definition in PhysicsActor
71 public string PhysObjectName { get; protected set; } 98 public string PhysObjectName { get; protected set; }
72 public string TypeName { get; protected set; } 99 public string TypeName { get; protected set; }
73 100
74 public BSLinkset Linkset { get; set; } 101 public BSLinkset Linkset { get; set; }
102 public BSLinksetInfo LinksetInfo { get; set; }
75 103
76 // Return the object mass without calculating it or having side effects 104 // Return the object mass without calculating it or having side effects
77 public abstract float RawMass { get; } 105 public abstract float RawMass { get; }
78 // Set the raw mass but also update physical mass properties (inertia, ...) 106 // Set the raw mass but also update physical mass properties (inertia, ...)
79 public abstract void UpdatePhysicalMassProperties(float mass); 107 // 'inWorld' true if the object has already been added to the dynamic world.
108 public abstract void UpdatePhysicalMassProperties(float mass, bool inWorld);
80 109
81 // The last value calculated for the prim's inertia 110 // The last value calculated for the prim's inertia
82 public OMV.Vector3 Inertia { get; set; } 111 public OMV.Vector3 Inertia { get; set; }
@@ -105,9 +134,17 @@ public abstract class BSPhysObject : PhysicsActor
105 public EntityProperties CurrentEntityProperties { get; set; } 134 public EntityProperties CurrentEntityProperties { get; set; }
106 public EntityProperties LastEntityProperties { get; set; } 135 public EntityProperties LastEntityProperties { get; set; }
107 136
108 public abstract OMV.Vector3 Scale { get; set; } 137 public virtual OMV.Vector3 Scale { get; set; }
109 public abstract bool IsSolid { get; } 138 public abstract bool IsSolid { get; }
110 public abstract bool IsStatic { get; } 139 public abstract bool IsStatic { get; }
140 public abstract bool IsSelected { get; }
141
142 // Materialness
143 public MaterialAttributes.Material Material { get; private set; }
144 public override void SetMaterial(int material)
145 {
146 Material = (MaterialAttributes.Material)material;
147 }
111 148
112 // Stop all physical motion. 149 // Stop all physical motion.
113 public abstract void ZeroMotion(bool inTaintTime); 150 public abstract void ZeroMotion(bool inTaintTime);
@@ -119,15 +156,41 @@ public abstract class BSPhysObject : PhysicsActor
119 // Update the physical location and motion of the object. Called with data from Bullet. 156 // Update the physical location and motion of the object. Called with data from Bullet.
120 public abstract void UpdateProperties(EntityProperties entprop); 157 public abstract void UpdateProperties(EntityProperties entprop);
121 158
122 // Tell the object to clean up.
123 public abstract void Destroy();
124
125 public abstract OMV.Vector3 RawPosition { get; set; } 159 public abstract OMV.Vector3 RawPosition { get; set; }
126 public abstract OMV.Vector3 ForcePosition { get; set; } 160 public abstract OMV.Vector3 ForcePosition { get; set; }
127 161
162 // Position is what the simulator thinks the positions of the prim is.
163 // Because Bullet needs the zero coordinate to be the center of mass of the linkset,
164 // sometimes it is necessary to displace the position the physics engine thinks
165 // the position is. PositionDisplacement must be added and removed from the
166 // position as the simulator position is stored and fetched from the physics
167 // engine.
168 public virtual OMV.Vector3 PositionDisplacement { get; set; }
169
128 public abstract OMV.Quaternion RawOrientation { get; set; } 170 public abstract OMV.Quaternion RawOrientation { get; set; }
129 public abstract OMV.Quaternion ForceOrientation { get; set; } 171 public abstract OMV.Quaternion ForceOrientation { get; set; }
130 172
173 // The system is telling us the velocity it wants to move at.
174 // Velocity in world coordinates.
175 // protected OMV.Vector3 m_targetVelocity; // use the definition in PhysicsActor
176 public override OMV.Vector3 TargetVelocity
177 {
178 get { return m_targetVelocity; }
179 set
180 {
181 m_targetVelocity = value;
182 Velocity = value;
183 }
184 }
185 public virtual float TargetSpeed
186 {
187 get
188 {
189 OMV.Vector3 characterOrientedVelocity = TargetVelocity * OMV.Quaternion.Inverse(OMV.Quaternion.Normalize(RawOrientation));
190 return characterOrientedVelocity.X;
191 }
192 }
193 public abstract OMV.Vector3 RawVelocity { get; set; }
131 public abstract OMV.Vector3 ForceVelocity { get; set; } 194 public abstract OMV.Vector3 ForceVelocity { get; set; }
132 195
133 public abstract OMV.Vector3 ForceRotationalVelocity { get; set; } 196 public abstract OMV.Vector3 ForceRotationalVelocity { get; set; }
@@ -136,6 +199,15 @@ public abstract class BSPhysObject : PhysicsActor
136 199
137 public virtual bool ForceBodyShapeRebuild(bool inTaintTime) { return false; } 200 public virtual bool ForceBodyShapeRebuild(bool inTaintTime) { return false; }
138 201
202 public virtual float ForwardSpeed
203 {
204 get
205 {
206 OMV.Vector3 characterOrientedVelocity = RawVelocity * OMV.Quaternion.Inverse(OMV.Quaternion.Normalize(RawOrientation));
207 return characterOrientedVelocity.X;
208 }
209 }
210
139 #region Collisions 211 #region Collisions
140 212
141 // Requested number of milliseconds between collision events. Zero means disabled. 213 // Requested number of milliseconds between collision events. Zero means disabled.
@@ -146,26 +218,65 @@ public abstract class BSPhysObject : PhysicsActor
146 protected long CollidingStep { get; set; } 218 protected long CollidingStep { get; set; }
147 // The simulation step that last had a collision with the ground 219 // The simulation step that last had a collision with the ground
148 protected long CollidingGroundStep { get; set; } 220 protected long CollidingGroundStep { get; set; }
221 // The simulation step that last collided with an object
222 protected long CollidingObjectStep { get; set; }
149 // The collision flags we think are set in Bullet 223 // The collision flags we think are set in Bullet
150 protected CollisionFlags CurrentCollisionFlags { get; set; } 224 protected CollisionFlags CurrentCollisionFlags { get; set; }
151 225
226 public override bool IsColliding {
227 get { return (CollidingStep == PhysicsScene.SimulationStep); }
228 set {
229 if (value)
230 CollidingStep = PhysicsScene.SimulationStep;
231 else
232 CollidingStep = 0;
233 }
234 }
235 public override bool CollidingGround {
236 get { return (CollidingGroundStep == PhysicsScene.SimulationStep); }
237 set
238 {
239 if (value)
240 CollidingGroundStep = PhysicsScene.SimulationStep;
241 else
242 CollidingGroundStep = 0;
243 }
244 }
245 public override bool CollidingObj {
246 get { return (CollidingObjectStep == PhysicsScene.SimulationStep); }
247 set {
248 if (value)
249 CollidingObjectStep = PhysicsScene.SimulationStep;
250 else
251 CollidingObjectStep = 0;
252 }
253 }
254
152 // The collisions that have been collected this tick 255 // The collisions that have been collected this tick
153 protected CollisionEventUpdate CollisionCollection; 256 protected CollisionEventUpdate CollisionCollection;
257 // Remember collisions from last tick for fancy collision based actions
258 // (like a BSCharacter walking up stairs).
259 protected CollisionEventUpdate CollisionsLastTick;
154 260
155 // The simulation step is telling this object about a collision. 261 // The simulation step is telling this object about a collision.
156 // Return 'true' if a collision was processed and should be sent up. 262 // Return 'true' if a collision was processed and should be sent up.
263 // Return 'false' if this object is not enabled/subscribed/appropriate for or has already seen this collision.
157 // Called at taint time from within the Step() function 264 // Called at taint time from within the Step() function
158 public virtual bool Collide(uint collidingWith, BSPhysObject collidee, 265 public virtual bool Collide(uint collidingWith, BSPhysObject collidee,
159 OMV.Vector3 contactPoint, OMV.Vector3 contactNormal, float pentrationDepth) 266 OMV.Vector3 contactPoint, OMV.Vector3 contactNormal, float pentrationDepth)
160 { 267 {
161 bool ret = false; 268 bool ret = false;
162 269
163 // The following lines make IsColliding() and IsCollidingGround() work 270 // The following lines make IsColliding(), CollidingGround() and CollidingObj work
164 CollidingStep = PhysicsScene.SimulationStep; 271 CollidingStep = PhysicsScene.SimulationStep;
165 if (collidingWith <= PhysicsScene.TerrainManager.HighestTerrainID) 272 if (collidingWith <= PhysicsScene.TerrainManager.HighestTerrainID)
166 { 273 {
167 CollidingGroundStep = PhysicsScene.SimulationStep; 274 CollidingGroundStep = PhysicsScene.SimulationStep;
168 } 275 }
276 else
277 {
278 CollidingObjectStep = PhysicsScene.SimulationStep;
279 }
169 280
170 // prims in the same linkset cannot collide with each other 281 // prims in the same linkset cannot collide with each other
171 if (collidee != null && (this.Linkset.LinksetID == collidee.Linkset.LinksetID)) 282 if (collidee != null && (this.Linkset.LinksetID == collidee.Linkset.LinksetID))
@@ -192,7 +303,7 @@ public abstract class BSPhysObject : PhysicsActor
192 { 303 {
193 bool ret = true; 304 bool ret = true;
194 // If the 'no collision' call, force it to happen right now so quick collision_end 305 // If the 'no collision' call, force it to happen right now so quick collision_end
195 bool force = CollisionCollection.Count == 0; 306 bool force = (CollisionCollection.Count == 0);
196 307
197 // throttle the collisions to the number of milliseconds specified in the subscription 308 // throttle the collisions to the number of milliseconds specified in the subscription
198 if (force || (PhysicsScene.SimulationNowTime >= NextCollisionOkTime)) 309 if (force || (PhysicsScene.SimulationNowTime >= NextCollisionOkTime))
@@ -210,8 +321,13 @@ public abstract class BSPhysObject : PhysicsActor
210 // DetailLog("{0},{1}.SendCollisionUpdate,call,numCollisions={2}", LocalID, TypeName, CollisionCollection.Count); 321 // DetailLog("{0},{1}.SendCollisionUpdate,call,numCollisions={2}", LocalID, TypeName, CollisionCollection.Count);
211 base.SendCollisionUpdate(CollisionCollection); 322 base.SendCollisionUpdate(CollisionCollection);
212 323
213 // The collisionCollection structure is passed around in the simulator. 324 // Remember the collisions from this tick for some collision specific processing.
325 CollisionsLastTick = CollisionCollection;
326
327 // The CollisionCollection instance is passed around in the simulator.
214 // Make sure we don't have a handle to that one and that a new one is used for next time. 328 // Make sure we don't have a handle to that one and that a new one is used for next time.
329 // This fixes an interesting 'gotcha'. If we call CollisionCollection.Clear() here,
330 // a race condition is created for the other users of this instance.
215 CollisionCollection = new CollisionEventUpdate(); 331 CollisionCollection = new CollisionEventUpdate();
216 } 332 }
217 return ret; 333 return ret;
@@ -229,7 +345,8 @@ public abstract class BSPhysObject : PhysicsActor
229 345
230 PhysicsScene.TaintedObject(TypeName+".SubscribeEvents", delegate() 346 PhysicsScene.TaintedObject(TypeName+".SubscribeEvents", delegate()
231 { 347 {
232 CurrentCollisionFlags = BulletSimAPI.AddToCollisionFlags2(PhysBody.ptr, CollisionFlags.BS_SUBSCRIBE_COLLISION_EVENTS); 348 if (PhysBody.HasPhysicalBody)
349 CurrentCollisionFlags = PhysicsScene.PE.AddToCollisionFlags(PhysBody, CollisionFlags.BS_SUBSCRIBE_COLLISION_EVENTS);
233 }); 350 });
234 } 351 }
235 else 352 else
@@ -243,7 +360,9 @@ public abstract class BSPhysObject : PhysicsActor
243 SubscribedEventsMs = 0; 360 SubscribedEventsMs = 0;
244 PhysicsScene.TaintedObject(TypeName+".UnSubscribeEvents", delegate() 361 PhysicsScene.TaintedObject(TypeName+".UnSubscribeEvents", delegate()
245 { 362 {
246 CurrentCollisionFlags = BulletSimAPI.RemoveFromCollisionFlags2(PhysBody.ptr, CollisionFlags.BS_SUBSCRIBE_COLLISION_EVENTS); 363 // Make sure there is a body there because sometimes destruction happens in an un-ideal order.
364 if (PhysBody.HasPhysicalBody)
365 CurrentCollisionFlags = PhysicsScene.PE.RemoveFromCollisionFlags(PhysBody, CollisionFlags.BS_SUBSCRIBE_COLLISION_EVENTS);
247 }); 366 });
248 } 367 }
249 // Return 'true' if the simulator wants collision events 368 // Return 'true' if the simulator wants collision events
@@ -253,11 +372,66 @@ public abstract class BSPhysObject : PhysicsActor
253 372
254 #endregion // Collisions 373 #endregion // Collisions
255 374
375 #region Per Simulation Step actions
376 // There are some actions that must be performed for a physical object before each simulation step.
377 // These actions are optional so, rather than scanning all the physical objects and asking them
378 // if they have anything to do, a physical object registers for an event call before the step is performed.
379 // This bookkeeping makes it easy to add, remove and clean up after all these registrations.
380 private Dictionary<string, BSScene.PreStepAction> RegisteredActions = new Dictionary<string, BSScene.PreStepAction>();
381 protected void RegisterPreStepAction(string op, uint id, BSScene.PreStepAction actn)
382 {
383 string identifier = op + "-" + id.ToString();
384
385 lock (RegisteredActions)
386 {
387 // Clean out any existing action
388 UnRegisterPreStepAction(op, id);
389
390 RegisteredActions[identifier] = actn;
391 }
392 PhysicsScene.BeforeStep += actn;
393 DetailLog("{0},BSPhysObject.RegisterPreStepAction,id={1}", LocalID, identifier);
394 }
395
396 // Unregister a pre step action. Safe to call if the action has not been registered.
397 protected void UnRegisterPreStepAction(string op, uint id)
398 {
399 string identifier = op + "-" + id.ToString();
400 bool removed = false;
401 lock (RegisteredActions)
402 {
403 if (RegisteredActions.ContainsKey(identifier))
404 {
405 PhysicsScene.BeforeStep -= RegisteredActions[identifier];
406 RegisteredActions.Remove(identifier);
407 removed = true;
408 }
409 }
410 DetailLog("{0},BSPhysObject.UnRegisterPreStepAction,id={1},removed={2}", LocalID, identifier, removed);
411 }
412
413 protected void UnRegisterAllPreStepActions()
414 {
415 lock (RegisteredActions)
416 {
417 foreach (KeyValuePair<string, BSScene.PreStepAction> kvp in RegisteredActions)
418 {
419 PhysicsScene.BeforeStep -= kvp.Value;
420 }
421 RegisteredActions.Clear();
422 }
423 DetailLog("{0},BSPhysObject.UnRegisterAllPreStepActions,", LocalID);
424 }
425
426
427 #endregion // Per Simulation Step actions
428
256 // High performance detailed logging routine used by the physical objects. 429 // High performance detailed logging routine used by the physical objects.
257 protected void DetailLog(string msg, params Object[] args) 430 protected void DetailLog(string msg, params Object[] args)
258 { 431 {
259 if (PhysicsScene.PhysicsLogging.Enabled) 432 if (PhysicsScene.PhysicsLogging.Enabled)
260 PhysicsScene.DetailLog(msg, args); 433 PhysicsScene.DetailLog(msg, args);
261 } 434 }
435
262} 436}
263} 437}
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSPlugin.cs b/OpenSim/Region/Physics/BulletSPlugin/BSPlugin.cs
index 20f5180..9442854 100644
--- a/OpenSim/Region/Physics/BulletSPlugin/BSPlugin.cs
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSPlugin.cs
@@ -59,13 +59,7 @@ public class BSPlugin : IPhysicsPlugin
59 { 59 {
60 if (_mScene == null) 60 if (_mScene == null)
61 { 61 {
62 if (Util.IsWindows()) 62 _mScene = new BSScene(GetName(), sceneIdentifier);
63 Util.LoadArchSpecificWindowsDll("BulletSim.dll");
64 // If not Windows, loading is performed by the
65 // Mono loader as specified in
66 // "bin/Physics/OpenSim.Region.Physics.BulletSPlugin.dll.config".
67
68 _mScene = new BSScene(sceneIdentifier);
69 } 63 }
70 return (_mScene); 64 return (_mScene);
71 } 65 }
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSPrim.cs b/OpenSim/Region/Physics/BulletSPlugin/BSPrim.cs
index 2b3fa25..79fe632 100644
--- a/OpenSim/Region/Physics/BulletSPlugin/BSPrim.cs
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSPrim.cs
@@ -45,13 +45,15 @@ public sealed class BSPrim : BSPhysObject
45 private static readonly string LogHeader = "[BULLETS PRIM]"; 45 private static readonly string LogHeader = "[BULLETS PRIM]";
46 46
47 // _size is what the user passed. Scale is what we pass to the physics engine with the mesh. 47 // _size is what the user passed. Scale is what we pass to the physics engine with the mesh.
48 // Often Scale is unity because the meshmerizer will apply _size when creating the mesh.
49 private OMV.Vector3 _size; // the multiplier for each mesh dimension as passed by the user 48 private OMV.Vector3 _size; // the multiplier for each mesh dimension as passed by the user
50 49
51 private bool _grabbed; 50 private bool _grabbed;
52 private bool _isSelected; 51 private bool _isSelected;
53 private bool _isVolumeDetect; 52 private bool _isVolumeDetect;
53
54 // _position is what the simulator thinks the positions of the prim is.
54 private OMV.Vector3 _position; 55 private OMV.Vector3 _position;
56
55 private float _mass; // the mass of this object 57 private float _mass; // the mass of this object
56 private float _density; 58 private float _density;
57 private OMV.Vector3 _force; 59 private OMV.Vector3 _force;
@@ -67,9 +69,6 @@ public sealed class BSPrim : BSPhysObject
67 private float _restitution; 69 private float _restitution;
68 private bool _setAlwaysRun; 70 private bool _setAlwaysRun;
69 private bool _throttleUpdates; 71 private bool _throttleUpdates;
70 private bool _isColliding;
71 private bool _collidingGround;
72 private bool _collidingObj;
73 private bool _floatOnWater; 72 private bool _floatOnWater;
74 private OMV.Vector3 _rotationalVelocity; 73 private OMV.Vector3 _rotationalVelocity;
75 private bool _kinematic; 74 private bool _kinematic;
@@ -77,13 +76,14 @@ public sealed class BSPrim : BSPhysObject
77 76
78 private BSDynamics _vehicle; 77 private BSDynamics _vehicle;
79 78
79 private BSVMotor _targetMotor;
80 private OMV.Vector3 _PIDTarget; 80 private OMV.Vector3 _PIDTarget;
81 private bool _usePID;
82 private float _PIDTau; 81 private float _PIDTau;
83 private bool _useHoverPID; 82
83 private BSFMotor _hoverMotor;
84 private float _PIDHoverHeight; 84 private float _PIDHoverHeight;
85 private PIDHoverType _PIDHoverType; 85 private PIDHoverType _PIDHoverType;
86 private float _PIDHoverTao; 86 private float _PIDHoverTau;
87 87
88 public BSPrim(uint localID, String primName, BSScene parent_scene, OMV.Vector3 pos, OMV.Vector3 size, 88 public BSPrim(uint localID, String primName, BSScene parent_scene, OMV.Vector3 pos, OMV.Vector3 size,
89 OMV.Quaternion rotation, PrimitiveBaseShape pbs, bool pisPhysical) 89 OMV.Quaternion rotation, PrimitiveBaseShape pbs, bool pisPhysical)
@@ -93,23 +93,27 @@ public sealed class BSPrim : BSPhysObject
93 _physicsActorType = (int)ActorTypes.Prim; 93 _physicsActorType = (int)ActorTypes.Prim;
94 _position = pos; 94 _position = pos;
95 _size = size; 95 _size = size;
96 Scale = size; // the scale will be set by CreateGeom depending on object type 96 Scale = size; // prims are the size the user wants them to be (different for BSCharactes).
97 _orientation = rotation; 97 _orientation = rotation;
98 _buoyancy = 1f; 98 _buoyancy = 0f;
99 _velocity = OMV.Vector3.Zero; 99 _velocity = OMV.Vector3.Zero;
100 _rotationalVelocity = OMV.Vector3.Zero; 100 _rotationalVelocity = OMV.Vector3.Zero;
101 BaseShape = pbs; 101 BaseShape = pbs;
102 _isPhysical = pisPhysical; 102 _isPhysical = pisPhysical;
103 _isVolumeDetect = false; 103 _isVolumeDetect = false;
104 _friction = PhysicsScene.Params.defaultFriction; // TODO: compute based on object material 104
105 _density = PhysicsScene.Params.defaultDensity; // TODO: compute based on object material 105 // Someday set default attributes based on the material but, for now, we don't know the prim material yet.
106 // MaterialAttributes primMat = BSMaterials.GetAttributes(Material, pisPhysical);
107 _density = PhysicsScene.Params.defaultDensity;
108 _friction = PhysicsScene.Params.defaultFriction;
106 _restitution = PhysicsScene.Params.defaultRestitution; 109 _restitution = PhysicsScene.Params.defaultRestitution;
110
107 _vehicle = new BSDynamics(PhysicsScene, this); // add vehicleness 111 _vehicle = new BSDynamics(PhysicsScene, this); // add vehicleness
112
108 _mass = CalculateMass(); 113 _mass = CalculateMass();
109 114
110 // No body or shape yet 115 // Cause linkset variables to be initialized (like mass)
111 PhysBody = new BulletBody(LocalID, IntPtr.Zero); 116 Linkset.Refresh(this);
112 PhysShape = new BulletShape(IntPtr.Zero);
113 117
114 DetailLog("{0},BSPrim.constructor,call", LocalID); 118 DetailLog("{0},BSPrim.constructor,call", LocalID);
115 // do the actual object creation at taint time 119 // do the actual object creation at taint time
@@ -117,7 +121,7 @@ public sealed class BSPrim : BSPhysObject
117 { 121 {
118 CreateGeomAndObject(true); 122 CreateGeomAndObject(true);
119 123
120 CurrentCollisionFlags = BulletSimAPI.GetCollisionFlags2(PhysBody.ptr); 124 CurrentCollisionFlags = PhysicsScene.PE.GetCollisionFlags(PhysBody);
121 }); 125 });
122 } 126 }
123 127
@@ -125,6 +129,7 @@ public sealed class BSPrim : BSPhysObject
125 public override void Destroy() 129 public override void Destroy()
126 { 130 {
127 // m_log.DebugFormat("{0}: Destroy, id={1}", LogHeader, LocalID); 131 // m_log.DebugFormat("{0}: Destroy, id={1}", LogHeader, LocalID);
132 base.Destroy();
128 133
129 // Undo any links between me and any other object 134 // Undo any links between me and any other object
130 BSPhysObject parentBefore = Linkset.LinksetRoot; 135 BSPhysObject parentBefore = Linkset.LinksetRoot;
@@ -143,7 +148,9 @@ public sealed class BSPrim : BSPhysObject
143 DetailLog("{0},BSPrim.Destroy,taint,", LocalID); 148 DetailLog("{0},BSPrim.Destroy,taint,", LocalID);
144 // If there are physical body and shape, release my use of same. 149 // If there are physical body and shape, release my use of same.
145 PhysicsScene.Shapes.DereferenceBody(PhysBody, true, null); 150 PhysicsScene.Shapes.DereferenceBody(PhysBody, true, null);
151 PhysBody.Clear();
146 PhysicsScene.Shapes.DereferenceShape(PhysShape, true, null); 152 PhysicsScene.Shapes.DereferenceShape(PhysShape, true, null);
153 PhysShape.Clear();
147 }); 154 });
148 } 155 }
149 156
@@ -157,16 +164,15 @@ public sealed class BSPrim : BSPhysObject
157 // We presume the scale and size are the same. If scale must be changed for 164 // We presume the scale and size are the same. If scale must be changed for
158 // the physical shape, that is done when the geometry is built. 165 // the physical shape, that is done when the geometry is built.
159 _size = value; 166 _size = value;
167 Scale = _size;
160 ForceBodyShapeRebuild(false); 168 ForceBodyShapeRebuild(false);
161 } 169 }
162 } 170 }
163 // Scale is what we set in the physics engine. It is different than 'size' in that
164 // 'size' can be encorporated into the mesh. In that case, the scale is <1,1,1>.
165 public override OMV.Vector3 Scale { get; set; }
166 171
167 public override PrimitiveBaseShape Shape { 172 public override PrimitiveBaseShape Shape {
168 set { 173 set {
169 BaseShape = value; 174 BaseShape = value;
175 LastAssetBuildFailed = false;
170 ForceBodyShapeRebuild(false); 176 ForceBodyShapeRebuild(false);
171 } 177 }
172 } 178 }
@@ -176,7 +182,6 @@ public sealed class BSPrim : BSPhysObject
176 182
177 public override bool ForceBodyShapeRebuild(bool inTaintTime) 183 public override bool ForceBodyShapeRebuild(bool inTaintTime)
178 { 184 {
179 LastAssetBuildFailed = false;
180 PhysicsScene.TaintedObject(inTaintTime, "BSPrim.ForceBodyShapeRebuild", delegate() 185 PhysicsScene.TaintedObject(inTaintTime, "BSPrim.ForceBodyShapeRebuild", delegate()
181 { 186 {
182 _mass = CalculateMass(); // changing the shape changes the mass 187 _mass = CalculateMass(); // changing the shape changes the mass
@@ -189,15 +194,23 @@ public sealed class BSPrim : BSPhysObject
189 } 194 }
190 } 195 }
191 public override bool Selected { 196 public override bool Selected {
192 set { 197 set
193 _isSelected = value; 198 {
194 PhysicsScene.TaintedObject("BSPrim.setSelected", delegate() 199 if (value != _isSelected)
195 { 200 {
196 DetailLog("{0},BSPrim.selected,taint,selected={1}", LocalID, _isSelected); 201 _isSelected = value;
197 SetObjectDynamic(false); 202 PhysicsScene.TaintedObject("BSPrim.setSelected", delegate()
198 }); 203 {
204 DetailLog("{0},BSPrim.selected,taint,selected={1}", LocalID, _isSelected);
205 SetObjectDynamic(false);
206 });
207 }
199 } 208 }
200 } 209 }
210 public override bool IsSelected
211 {
212 get { return _isSelected; }
213 }
201 public override void CrossingFailure() { return; } 214 public override void CrossingFailure() { return; }
202 215
203 // link me to the specified parent 216 // link me to the specified parent
@@ -244,7 +257,8 @@ public sealed class BSPrim : BSPhysObject
244 // Zero some other properties in the physics engine 257 // Zero some other properties in the physics engine
245 PhysicsScene.TaintedObject(inTaintTime, "BSPrim.ZeroMotion", delegate() 258 PhysicsScene.TaintedObject(inTaintTime, "BSPrim.ZeroMotion", delegate()
246 { 259 {
247 BulletSimAPI.ClearAllForces2(PhysBody.ptr); 260 if (PhysBody.HasPhysicalBody)
261 PhysicsScene.PE.ClearAllForces(PhysBody);
248 }); 262 });
249 } 263 }
250 public override void ZeroAngularMotion(bool inTaintTime) 264 public override void ZeroAngularMotion(bool inTaintTime)
@@ -253,8 +267,12 @@ public sealed class BSPrim : BSPhysObject
253 // Zero some other properties in the physics engine 267 // Zero some other properties in the physics engine
254 PhysicsScene.TaintedObject(inTaintTime, "BSPrim.ZeroMotion", delegate() 268 PhysicsScene.TaintedObject(inTaintTime, "BSPrim.ZeroMotion", delegate()
255 { 269 {
256 BulletSimAPI.SetInterpolationAngularVelocity2(PhysBody.ptr, OMV.Vector3.Zero); 270 // DetailLog("{0},BSPrim.ZeroAngularMotion,call,rotVel={1}", LocalID, _rotationalVelocity);
257 BulletSimAPI.SetAngularVelocity2(PhysBody.ptr, OMV.Vector3.Zero); 271 if (PhysBody.HasPhysicalBody)
272 {
273 PhysicsScene.PE.SetInterpolationAngularVelocity(PhysBody, _rotationalVelocity);
274 PhysicsScene.PE.SetAngularVelocity(PhysBody, _rotationalVelocity);
275 }
258 }); 276 });
259 } 277 }
260 278
@@ -271,41 +289,69 @@ public sealed class BSPrim : BSPhysObject
271 } 289 }
272 public override OMV.Vector3 Position { 290 public override OMV.Vector3 Position {
273 get { 291 get {
292 /* NOTE: this refetch is not necessary. The simulator knows about linkset children
293 * and does not fetch this position info for children. Thus this is commented out.
274 // child prims move around based on their parent. Need to get the latest location 294 // child prims move around based on their parent. Need to get the latest location
275 if (!Linkset.IsRoot(this)) 295 if (!Linkset.IsRoot(this))
276 _position = Linkset.Position(this); 296 _position = Linkset.PositionGet(this);
297 */
277 298
278 // don't do the GetObjectPosition for root elements because this function is called a zillion times. 299 // don't do the GetObjectPosition for root elements because this function is called a zillion times.
279 // _position = BulletSimAPI.GetObjectPosition2(PhysicsScene.World.ptr, BSBody.ptr); 300 // _position = PhysicsScene.PE.GetObjectPosition2(PhysicsScene.World, BSBody) - PositionDisplacement;
280 return _position; 301 return _position;
281 } 302 }
282 set { 303 set {
283 // If the position must be forced into the physics engine, use ForcePosition. 304 // If the position must be forced into the physics engine, use ForcePosition.
305 // All positions are given in world positions.
284 if (_position == value) 306 if (_position == value)
285 { 307 {
308 DetailLog("{0},BSPrim.setPosition,call,positionNotChanging,pos={1},orient={2}", LocalID, _position, _orientation);
286 return; 309 return;
287 } 310 }
288 _position = value; 311 _position = value;
289 // TODO: what does it mean to set the position of a child prim?? Rebuild the constraint?
290 PositionSanityCheck(false); 312 PositionSanityCheck(false);
313
314 // A linkset might need to know if a component information changed.
315 Linkset.UpdateProperties(this, false);
316
291 PhysicsScene.TaintedObject("BSPrim.setPosition", delegate() 317 PhysicsScene.TaintedObject("BSPrim.setPosition", delegate()
292 { 318 {
293 // DetailLog("{0},BSPrim.SetPosition,taint,pos={1},orient={2}", LocalID, _position, _orientation); 319 DetailLog("{0},BSPrim.SetPosition,taint,pos={1},orient={2}", LocalID, _position, _orientation);
294 BulletSimAPI.SetTranslation2(PhysBody.ptr, _position, _orientation); 320 ForcePosition = _position;
295 ActivateIfPhysical(false);
296 }); 321 });
297 } 322 }
298 } 323 }
299 public override OMV.Vector3 ForcePosition { 324 public override OMV.Vector3 ForcePosition {
300 get { 325 get {
301 _position = BulletSimAPI.GetPosition2(PhysBody.ptr); 326 _position = PhysicsScene.PE.GetPosition(PhysBody) - PositionDisplacement;
302 return _position; 327 return _position;
303 } 328 }
304 set { 329 set {
305 _position = value; 330 _position = value;
306 // PositionSanityCheck(); // Don't do this! Causes a loop and caller should know better. 331 if (PhysBody.HasPhysicalBody)
307 BulletSimAPI.SetTranslation2(PhysBody.ptr, _position, _orientation); 332 {
308 ActivateIfPhysical(false); 333 PhysicsScene.PE.SetTranslation(PhysBody, _position + PositionDisplacement, _orientation);
334 ActivateIfPhysical(false);
335 }
336 }
337 }
338 // Override to have position displacement immediately update the physical position.
339 // A feeble attempt to keep the sim and physical positions in sync
340 // Must be called at taint time.
341 public override OMV.Vector3 PositionDisplacement
342 {
343 get
344 {
345 return base.PositionDisplacement;
346 }
347 set
348 {
349 base.PositionDisplacement = value;
350 PhysicsScene.TaintedObject(PhysicsScene.InTaintTime, "BSPrim.setPosition", delegate()
351 {
352 if (PhysBody.HasPhysicalBody)
353 PhysicsScene.PE.SetTranslation(PhysBody, _position + base.PositionDisplacement, _orientation);
354 });
309 } 355 }
310 } 356 }
311 357
@@ -316,51 +362,56 @@ public sealed class BSPrim : BSPhysObject
316 { 362 {
317 bool ret = false; 363 bool ret = false;
318 364
365 if (!PhysicsScene.TerrainManager.IsWithinKnownTerrain(RawPosition))
366 {
367 // The physical object is out of the known/simulated area.
368 // Upper levels of code will handle the transition to other areas so, for
369 // the time, we just ignore the position.
370 return ret;
371 }
372
319 float terrainHeight = PhysicsScene.TerrainManager.GetTerrainHeightAtXYZ(_position); 373 float terrainHeight = PhysicsScene.TerrainManager.GetTerrainHeightAtXYZ(_position);
320 OMV.Vector3 upForce = OMV.Vector3.Zero; 374 OMV.Vector3 upForce = OMV.Vector3.Zero;
321 if (Position.Z < terrainHeight) 375 if (RawPosition.Z < terrainHeight)
322 { 376 {
323 DetailLog("{0},BSPrim.PositionAdjustUnderGround,call,pos={1},terrain={2}", LocalID, _position, terrainHeight); 377 DetailLog("{0},BSPrim.PositionAdjustUnderGround,call,pos={1},terrain={2}", LocalID, _position, terrainHeight);
324 float targetHeight = terrainHeight + (Size.Z / 2f); 378 float targetHeight = terrainHeight + (Size.Z / 2f);
325 // Upforce proportional to the distance away from the terrain. Correct the error in 1 sec. 379 // If the object is below ground it just has to be moved up because pushing will
326 upForce.Z = (terrainHeight - Position.Z) * 1f; 380 // not get it through the terrain
381 _position.Z = targetHeight;
382 if (inTaintTime)
383 ForcePosition = _position;
327 ret = true; 384 ret = true;
328 } 385 }
329 386
330 if ((CurrentCollisionFlags & CollisionFlags.BS_FLOATS_ON_WATER) != 0) 387 if ((CurrentCollisionFlags & CollisionFlags.BS_FLOATS_ON_WATER) != 0)
331 { 388 {
332 float waterHeight = PhysicsScene.GetWaterLevelAtXYZ(_position); 389 float waterHeight = PhysicsScene.TerrainManager.GetWaterLevelAtXYZ(_position);
333 // TODO: a floating motor so object will bob in the water 390 // TODO: a floating motor so object will bob in the water
334 if (Math.Abs(Position.Z - waterHeight) > 0.1f) 391 if (Math.Abs(RawPosition.Z - waterHeight) > 0.1f)
335 { 392 {
336 // Upforce proportional to the distance away from the water. Correct the error in 1 sec. 393 // Upforce proportional to the distance away from the water. Correct the error in 1 sec.
337 upForce.Z = (waterHeight - Position.Z) * 1f; 394 upForce.Z = (waterHeight - RawPosition.Z) * 1f;
395
396 // Apply upforce and overcome gravity.
397 OMV.Vector3 correctionForce = upForce - PhysicsScene.DefaultGravity;
398 DetailLog("{0},BSPrim.PositionSanityCheck,applyForce,pos={1},upForce={2},correctionForce={3}", LocalID, _position, upForce, correctionForce);
399 AddForce(correctionForce, false, inTaintTime);
338 ret = true; 400 ret = true;
339 } 401 }
340 } 402 }
341 403
342 // TODO: check for out of bounds
343
344 // The above code computes a force to apply to correct any out-of-bounds problems. Apply same.
345 if (ret)
346 {
347 PhysicsScene.TaintedObject(inTaintTime, "BSPrim.PositionSanityCheck:belowTerrain", delegate()
348 {
349 // Apply upforce and overcome gravity.
350 ForceVelocity = ForceVelocity + upForce - PhysicsScene.DefaultGravity;
351 });
352 }
353 return ret; 404 return ret;
354 } 405 }
355 406
356 // Return the effective mass of the object. 407 // Return the effective mass of the object.
357 // If there are multiple items in the linkset, add them together for the root 408 // The definition of this call is to return the mass of the prim.
409 // If the simulator cares about the mass of the linkset, it will sum it itself.
358 public override float Mass 410 public override float Mass
359 { 411 {
360 get 412 get
361 { 413 {
362 return Linkset.LinksetMass; 414 return _mass;
363 // return _mass;
364 } 415 }
365 } 416 }
366 417
@@ -370,25 +421,64 @@ public sealed class BSPrim : BSPhysObject
370 } 421 }
371 // Set the physical mass to the passed mass. 422 // Set the physical mass to the passed mass.
372 // Note that this does not change _mass! 423 // Note that this does not change _mass!
373 public override void UpdatePhysicalMassProperties(float physMass) 424 public override void UpdatePhysicalMassProperties(float physMass, bool inWorld)
374 { 425 {
375 if (IsStatic) 426 if (PhysBody.HasPhysicalBody)
376 {
377 Inertia = OMV.Vector3.Zero;
378 BulletSimAPI.SetMassProps2(PhysBody.ptr, 0f, Inertia);
379 BulletSimAPI.UpdateInertiaTensor2(PhysBody.ptr);
380 }
381 else
382 { 427 {
383 Inertia = BulletSimAPI.CalculateLocalInertia2(PhysShape.ptr, physMass); 428 if (IsStatic)
384 BulletSimAPI.SetMassProps2(PhysBody.ptr, physMass, Inertia); 429 {
385 BulletSimAPI.UpdateInertiaTensor2(PhysBody.ptr); 430 PhysicsScene.PE.SetGravity(PhysBody, PhysicsScene.DefaultGravity);
386 // center of mass is at the zero of the object 431 Inertia = OMV.Vector3.Zero;
387 // DEBUG DEBUG BulletSimAPI.SetCenterOfMassByPosRot2(PhysBody.ptr, ForcePosition, ForceOrientation); 432 PhysicsScene.PE.SetMassProps(PhysBody, 0f, Inertia);
388 DetailLog("{0},BSPrim.UpdateMassProperties,mass={1},localInertia={2}", LocalID, physMass, Inertia); 433 PhysicsScene.PE.UpdateInertiaTensor(PhysBody);
434 }
435 else
436 {
437 OMV.Vector3 grav = ComputeGravity(Buoyancy);
438
439 if (inWorld)
440 {
441 // Changing interesting properties doesn't change proxy and collision cache
442 // information. The Bullet solution is to re-add the object to the world
443 // after parameters are changed.
444 PhysicsScene.PE.RemoveObjectFromWorld(PhysicsScene.World, PhysBody);
445 }
446
447 // The computation of mass props requires gravity to be set on the object.
448 PhysicsScene.PE.SetGravity(PhysBody, grav);
449
450 Inertia = PhysicsScene.PE.CalculateLocalInertia(PhysShape, physMass);
451 PhysicsScene.PE.SetMassProps(PhysBody, physMass, Inertia);
452 PhysicsScene.PE.UpdateInertiaTensor(PhysBody);
453
454 // center of mass is at the zero of the object
455 // DEBUG DEBUG PhysicsScene.PE.SetCenterOfMassByPosRot(PhysBody, ForcePosition, ForceOrientation);
456 DetailLog("{0},BSPrim.UpdateMassProperties,mass={1},localInertia={2},grav={3},inWorld={4}", LocalID, physMass, Inertia, grav, inWorld);
457
458 if (inWorld)
459 {
460 AddObjectToPhysicalWorld();
461 }
462
463 // Must set gravity after it has been added to the world because, for unknown reasons,
464 // adding the object resets the object's gravity to world gravity
465 PhysicsScene.PE.SetGravity(PhysBody, grav);
466
467 }
389 } 468 }
390 } 469 }
391 470
471 // Return what gravity should be set to this very moment
472 public OMV.Vector3 ComputeGravity(float buoyancy)
473 {
474 OMV.Vector3 ret = PhysicsScene.DefaultGravity;
475
476 if (!IsStatic)
477 ret *= (1f - buoyancy);
478
479 return ret;
480 }
481
392 // Is this used? 482 // Is this used?
393 public override OMV.Vector3 CenterOfMass 483 public override OMV.Vector3 CenterOfMass
394 { 484 {
@@ -405,11 +495,26 @@ public sealed class BSPrim : BSPhysObject
405 get { return _force; } 495 get { return _force; }
406 set { 496 set {
407 _force = value; 497 _force = value;
408 PhysicsScene.TaintedObject("BSPrim.setForce", delegate() 498 if (_force != OMV.Vector3.Zero)
409 { 499 {
410 // DetailLog("{0},BSPrim.setForce,taint,force={1}", LocalID, _force); 500 // If the force is non-zero, it must be reapplied each tick because
411 BulletSimAPI.SetObjectForce2(PhysBody.ptr, _force); 501 // Bullet clears the forces applied last frame.
412 }); 502 RegisterPreStepAction("BSPrim.setForce", LocalID,
503 delegate(float timeStep)
504 {
505 DetailLog("{0},BSPrim.setForce,preStep,force={1}", LocalID, _force);
506 if (PhysBody.HasPhysicalBody)
507 {
508 PhysicsScene.PE.ApplyCentralForce(PhysBody, _force);
509 ActivateIfPhysical(false);
510 }
511 }
512 );
513 }
514 else
515 {
516 UnRegisterPreStepAction("BSPrim.setForce", LocalID);
517 }
413 } 518 }
414 } 519 }
415 520
@@ -420,15 +525,18 @@ public sealed class BSPrim : BSPhysObject
420 set { 525 set {
421 Vehicle type = (Vehicle)value; 526 Vehicle type = (Vehicle)value;
422 527
423 // Tell the scene about the vehicle so it will get processing each frame.
424 PhysicsScene.VehicleInSceneTypeChanged(this, type);
425
426 PhysicsScene.TaintedObject("setVehicleType", delegate() 528 PhysicsScene.TaintedObject("setVehicleType", delegate()
427 { 529 {
428 // Done at taint time so we're sure the physics engine is not using the variables 530 // Done at taint time so we're sure the physics engine is not using the variables
429 // Vehicle code changes the parameters for this vehicle type. 531 // Vehicle code changes the parameters for this vehicle type.
430 _vehicle.ProcessTypeChange(type); 532 _vehicle.ProcessTypeChange(type);
431 ActivateIfPhysical(false); 533 ActivateIfPhysical(false);
534
535 // If an active vehicle, register the vehicle code to be called before each step
536 if (_vehicle.Type == Vehicle.TYPE_NONE)
537 UnRegisterPreStepAction("BSPrim.Vehicle", LocalID);
538 else
539 RegisterPreStepAction("BSPrim.Vehicle", LocalID, _vehicle.Step);
432 }); 540 });
433 } 541 }
434 } 542 }
@@ -464,23 +572,6 @@ public sealed class BSPrim : BSPhysObject
464 }); 572 });
465 } 573 }
466 574
467 // Called each simulation step to advance vehicle characteristics.
468 // Called from Scene when doing simulation step so we're in taint processing time.
469 public override void StepVehicle(float timeStep)
470 {
471 if (IsPhysical && _vehicle.IsActive)
472 {
473 _vehicle.Step(timeStep);
474 /* // TEST TEST DEBUG DEBUG -- trying to reduce the extra action of Bullet simulation step
475 PhysicsScene.PostTaintObject("BSPrim.StepVehicles", LocalID, delegate()
476 {
477 // This resets the interpolation values and recomputes the tensor variables
478 BulletSimAPI.SetCenterOfMassByPosRot2(BSBody.ptr, ForcePosition, ForceOrientation);
479 });
480 */
481 }
482 }
483
484 // Allows the detection of collisions with inherently non-physical prims. see llVolumeDetect for more 575 // Allows the detection of collisions with inherently non-physical prims. see llVolumeDetect for more
485 public override void SetVolumeDetect(int param) { 576 public override void SetVolumeDetect(int param) {
486 bool newValue = (param != 0); 577 bool newValue = (param != 0);
@@ -495,6 +586,11 @@ public sealed class BSPrim : BSPhysObject
495 } 586 }
496 return; 587 return;
497 } 588 }
589 public override OMV.Vector3 RawVelocity
590 {
591 get { return _velocity; }
592 set { _velocity = value; }
593 }
498 public override OMV.Vector3 Velocity { 594 public override OMV.Vector3 Velocity {
499 get { return _velocity; } 595 get { return _velocity; }
500 set { 596 set {
@@ -502,22 +598,44 @@ public sealed class BSPrim : BSPhysObject
502 PhysicsScene.TaintedObject("BSPrim.setVelocity", delegate() 598 PhysicsScene.TaintedObject("BSPrim.setVelocity", delegate()
503 { 599 {
504 // DetailLog("{0},BSPrim.SetVelocity,taint,vel={1}", LocalID, _velocity); 600 // DetailLog("{0},BSPrim.SetVelocity,taint,vel={1}", LocalID, _velocity);
505 BulletSimAPI.SetLinearVelocity2(PhysBody.ptr, _velocity); 601 ForceVelocity = _velocity;
506 }); 602 });
507 } 603 }
508 } 604 }
509 public override OMV.Vector3 ForceVelocity { 605 public override OMV.Vector3 ForceVelocity {
510 get { return _velocity; } 606 get { return _velocity; }
511 set { 607 set {
608 PhysicsScene.AssertInTaintTime("BSPrim.ForceVelocity");
609
512 _velocity = value; 610 _velocity = value;
513 BulletSimAPI.SetLinearVelocity2(PhysBody.ptr, _velocity); 611 if (PhysBody.HasPhysicalBody)
612 {
613 DetailLog("{0},BSPrim.ForceVelocity,taint,vel={1}", LocalID, _velocity);
614 PhysicsScene.PE.SetLinearVelocity(PhysBody, _velocity);
615 ActivateIfPhysical(false);
616 }
514 } 617 }
515 } 618 }
516 public override OMV.Vector3 Torque { 619 public override OMV.Vector3 Torque {
517 get { return _torque; } 620 get { return _torque; }
518 set { 621 set {
519 _torque = value; 622 _torque = value;
520 AddAngularForce(_torque, false, false); 623 if (_torque != OMV.Vector3.Zero)
624 {
625 // If the torque is non-zero, it must be reapplied each tick because
626 // Bullet clears the forces applied last frame.
627 RegisterPreStepAction("BSPrim.setTorque", LocalID,
628 delegate(float timeStep)
629 {
630 if (PhysBody.HasPhysicalBody)
631 AddAngularForce(_torque, false, true);
632 }
633 );
634 }
635 else
636 {
637 UnRegisterPreStepAction("BSPrim.setTorque", LocalID);
638 }
521 // DetailLog("{0},BSPrim.SetTorque,call,torque={1}", LocalID, _torque); 639 // DetailLog("{0},BSPrim.SetTorque,call,torque={1}", LocalID, _torque);
522 } 640 }
523 } 641 }
@@ -537,23 +655,27 @@ public sealed class BSPrim : BSPhysObject
537 } 655 }
538 public override OMV.Quaternion Orientation { 656 public override OMV.Quaternion Orientation {
539 get { 657 get {
658 /* NOTE: this refetch is not necessary. The simulator knows about linkset children
659 * and does not fetch this position info for children. Thus this is commented out.
540 // Children move around because tied to parent. Get a fresh value. 660 // Children move around because tied to parent. Get a fresh value.
541 if (!Linkset.IsRoot(this)) 661 if (!Linkset.IsRoot(this))
542 { 662 {
543 _orientation = Linkset.Orientation(this); 663 _orientation = Linkset.OrientationGet(this);
544 } 664 }
665 */
545 return _orientation; 666 return _orientation;
546 } 667 }
547 set { 668 set {
548 if (_orientation == value) 669 if (_orientation == value)
549 return; 670 return;
550 _orientation = value; 671 _orientation = value;
551 // TODO: what does it mean if a child in a linkset changes its orientation? Rebuild the constraint? 672
673 // A linkset might need to know if a component information changed.
674 Linkset.UpdateProperties(this, false);
675
552 PhysicsScene.TaintedObject("BSPrim.setOrientation", delegate() 676 PhysicsScene.TaintedObject("BSPrim.setOrientation", delegate()
553 { 677 {
554 // _position = BulletSimAPI.GetObjectPosition2(PhysicsScene.World.ptr, BSBody.ptr); 678 ForceOrientation = _orientation;
555 // DetailLog("{0},BSPrim.setOrientation,taint,pos={1},orient={2}", LocalID, _position, _orientation);
556 BulletSimAPI.SetTranslation2(PhysBody.ptr, _position, _orientation);
557 }); 679 });
558 } 680 }
559 } 681 }
@@ -562,13 +684,14 @@ public sealed class BSPrim : BSPhysObject
562 { 684 {
563 get 685 get
564 { 686 {
565 _orientation = BulletSimAPI.GetOrientation2(PhysBody.ptr); 687 _orientation = PhysicsScene.PE.GetOrientation(PhysBody);
566 return _orientation; 688 return _orientation;
567 } 689 }
568 set 690 set
569 { 691 {
570 _orientation = value; 692 _orientation = value;
571 BulletSimAPI.SetTranslation2(PhysBody.ptr, _position, _orientation); 693 if (PhysBody.HasPhysicalBody)
694 PhysicsScene.PE.SetTranslation(PhysBody, _position + PositionDisplacement, _orientation);
572 } 695 }
573 } 696 }
574 public override int PhysicsActorType { 697 public override int PhysicsActorType {
@@ -583,7 +706,7 @@ public sealed class BSPrim : BSPhysObject
583 _isPhysical = value; 706 _isPhysical = value;
584 PhysicsScene.TaintedObject("BSPrim.setIsPhysical", delegate() 707 PhysicsScene.TaintedObject("BSPrim.setIsPhysical", delegate()
585 { 708 {
586 // DetailLog("{0},setIsPhysical,taint,isPhys={1}", LocalID, _isPhysical); 709 DetailLog("{0},setIsPhysical,taint,isPhys={1}", LocalID, _isPhysical);
587 SetObjectDynamic(true); 710 SetObjectDynamic(true);
588 // whether phys-to-static or static-to-phys, the object is not moving. 711 // whether phys-to-static or static-to-phys, the object is not moving.
589 ZeroMotion(true); 712 ZeroMotion(true);
@@ -624,7 +747,7 @@ public sealed class BSPrim : BSPhysObject
624 747
625 // Mangling all the physical properties requires the object not be in the physical world. 748 // Mangling all the physical properties requires the object not be in the physical world.
626 // This is a NOOP if the object is not in the world (BulletSim and Bullet ignore objects not found). 749 // This is a NOOP if the object is not in the world (BulletSim and Bullet ignore objects not found).
627 BulletSimAPI.RemoveObjectFromWorld2(PhysicsScene.World.ptr, PhysBody.ptr); 750 PhysicsScene.PE.RemoveObjectFromWorld(PhysicsScene.World, PhysBody);
628 751
629 // Set up the object physicalness (does gravity and collisions move this object) 752 // Set up the object physicalness (does gravity and collisions move this object)
630 MakeDynamic(IsStatic); 753 MakeDynamic(IsStatic);
@@ -638,16 +761,10 @@ public sealed class BSPrim : BSPhysObject
638 // Make solid or not (do things bounce off or pass through this object). 761 // Make solid or not (do things bounce off or pass through this object).
639 MakeSolid(IsSolid); 762 MakeSolid(IsSolid);
640 763
641 BulletSimAPI.AddObjectToWorld2(PhysicsScene.World.ptr, PhysBody.ptr); 764 AddObjectToPhysicalWorld();
642 765
643 // Rebuild its shape 766 // Rebuild its shape
644 BulletSimAPI.UpdateSingleAabb2(PhysicsScene.World.ptr, PhysBody.ptr); 767 PhysicsScene.PE.UpdateSingleAabb(PhysicsScene.World, PhysBody);
645
646 // Collision filter can be set only when the object is in the world
647 if (PhysBody.collisionFilter != 0 || PhysBody.collisionMask != 0)
648 {
649 BulletSimAPI.SetCollisionFilterMask2(PhysBody.ptr, (uint)PhysBody.collisionFilter, (uint)PhysBody.collisionMask);
650 }
651 768
652 // Recompute any linkset parameters. 769 // Recompute any linkset parameters.
653 // When going from non-physical to physical, this re-enables the constraints that 770 // When going from non-physical to physical, this re-enables the constraints that
@@ -655,8 +772,8 @@ public sealed class BSPrim : BSPhysObject
655 // For compound based linksets, this enables and disables interactions of the children. 772 // For compound based linksets, this enables and disables interactions of the children.
656 Linkset.Refresh(this); 773 Linkset.Refresh(this);
657 774
658 DetailLog("{0},BSPrim.UpdatePhysicalParameters,taintExit,static={1},solid={2},mass={3},collide={4},cf={5:X},body={6},shape={7}", 775 DetailLog("{0},BSPrim.UpdatePhysicalParameters,taintExit,static={1},solid={2},mass={3},collide={4},cf={5:X},cType={6},body={7},shape={8}",
659 LocalID, IsStatic, IsSolid, _mass, SubscribedEvents(), CurrentCollisionFlags, PhysBody, PhysShape); 776 LocalID, IsStatic, IsSolid, Mass, SubscribedEvents(), CurrentCollisionFlags, PhysBody.collisionType, PhysBody, PhysShape);
660 } 777 }
661 778
662 // "Making dynamic" means changing to and from static. 779 // "Making dynamic" means changing to and from static.
@@ -669,74 +786,80 @@ public sealed class BSPrim : BSPhysObject
669 if (makeStatic) 786 if (makeStatic)
670 { 787 {
671 // Become a Bullet 'static' object type 788 // Become a Bullet 'static' object type
672 CurrentCollisionFlags = BulletSimAPI.AddToCollisionFlags2(PhysBody.ptr, CollisionFlags.CF_STATIC_OBJECT); 789 CurrentCollisionFlags = PhysicsScene.PE.AddToCollisionFlags(PhysBody, CollisionFlags.CF_STATIC_OBJECT);
673 // Stop all movement 790 // Stop all movement
674 ZeroMotion(true); 791 ZeroMotion(true);
675 // Center of mass is at the center of the object 792
676 // DEBUG DEBUG BulletSimAPI.SetCenterOfMassByPosRot2(Linkset.LinksetRoot.PhysBody.ptr, _position, _orientation); 793 // Set various physical properties so other object interact properly
794 MaterialAttributes matAttrib = BSMaterials.GetAttributes(Material, false);
795 PhysicsScene.PE.SetFriction(PhysBody, matAttrib.friction);
796 PhysicsScene.PE.SetRestitution(PhysBody, matAttrib.restitution);
797
677 // Mass is zero which disables a bunch of physics stuff in Bullet 798 // Mass is zero which disables a bunch of physics stuff in Bullet
678 UpdatePhysicalMassProperties(0f); 799 UpdatePhysicalMassProperties(0f, false);
679 // Set collision detection parameters 800 // Set collision detection parameters
680 if (PhysicsScene.Params.ccdMotionThreshold > 0f) 801 if (BSParam.CcdMotionThreshold > 0f)
681 { 802 {
682 BulletSimAPI.SetCcdMotionThreshold2(PhysBody.ptr, PhysicsScene.Params.ccdMotionThreshold); 803 PhysicsScene.PE.SetCcdMotionThreshold(PhysBody, BSParam.CcdMotionThreshold);
683 BulletSimAPI.SetCcdSweptSphereRadius2(PhysBody.ptr, PhysicsScene.Params.ccdSweptSphereRadius); 804 PhysicsScene.PE.SetCcdSweptSphereRadius(PhysBody, BSParam.CcdSweptSphereRadius);
684 } 805 }
685 // There can be special things needed for implementing linksets 806
686 Linkset.MakeStatic(this);
687 // The activation state is 'disabled' so Bullet will not try to act on it. 807 // The activation state is 'disabled' so Bullet will not try to act on it.
688 BulletSimAPI.ForceActivationState2(PhysBody.ptr, ActivationState.DISABLE_SIMULATION); 808 // PhysicsScene.PE.ForceActivationState(PhysBody, ActivationState.DISABLE_SIMULATION);
689 // Start it out sleeping and physical actions could wake it up. 809 // Start it out sleeping and physical actions could wake it up.
690 // BulletSimAPI.ForceActivationState2(BSBody.ptr, ActivationState.ISLAND_SLEEPING); 810 PhysicsScene.PE.ForceActivationState(PhysBody, ActivationState.ISLAND_SLEEPING);
811
812 // This collides like a static object
813 PhysBody.collisionType = CollisionType.Static;
691 814
692 PhysBody.collisionFilter = CollisionFilterGroups.StaticObjectFilter; 815 // There can be special things needed for implementing linksets
693 PhysBody.collisionMask = CollisionFilterGroups.StaticObjectMask; 816 Linkset.MakeStatic(this);
694 } 817 }
695 else 818 else
696 { 819 {
697 // Not a Bullet static object 820 // Not a Bullet static object
698 CurrentCollisionFlags = BulletSimAPI.RemoveFromCollisionFlags2(PhysBody.ptr, CollisionFlags.CF_STATIC_OBJECT); 821 CurrentCollisionFlags = PhysicsScene.PE.RemoveFromCollisionFlags(PhysBody, CollisionFlags.CF_STATIC_OBJECT);
699 822
700 // Set various physical properties so internal dynamic properties will get computed correctly as they are set 823 // Set various physical properties so other object interact properly
701 BulletSimAPI.SetFriction2(PhysBody.ptr, PhysicsScene.Params.defaultFriction); 824 MaterialAttributes matAttrib = BSMaterials.GetAttributes(Material, true);
702 BulletSimAPI.SetRestitution2(PhysBody.ptr, PhysicsScene.Params.defaultRestitution); 825 PhysicsScene.PE.SetFriction(PhysBody, matAttrib.friction);
826 PhysicsScene.PE.SetRestitution(PhysBody, matAttrib.restitution);
703 827
704 // per http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?t=3382 828 // per http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?t=3382
705 // Since this can be called multiple times, only zero forces when becoming physical 829 // Since this can be called multiple times, only zero forces when becoming physical
706 // BulletSimAPI.ClearAllForces2(BSBody.ptr); 830 // PhysicsScene.PE.ClearAllForces(BSBody);
707 831
708 // For good measure, make sure the transform is set through to the motion state 832 // For good measure, make sure the transform is set through to the motion state
709 BulletSimAPI.SetTranslation2(PhysBody.ptr, _position, _orientation); 833 PhysicsScene.PE.SetTranslation(PhysBody, _position + PositionDisplacement, _orientation);
710 834
711 // Center of mass is at the center of the object 835 // Center of mass is at the center of the object
712 // DEBUG DEBUG BulletSimAPI.SetCenterOfMassByPosRot2(Linkset.LinksetRoot.PhysBody.ptr, _position, _orientation); 836 // DEBUG DEBUG PhysicsScene.PE.SetCenterOfMassByPosRot(Linkset.LinksetRoot.PhysBody, _position, _orientation);
713 837
714 // A dynamic object has mass 838 // A dynamic object has mass
715 UpdatePhysicalMassProperties(RawMass); 839 UpdatePhysicalMassProperties(RawMass, false);
716 840
717 // Set collision detection parameters 841 // Set collision detection parameters
718 if (PhysicsScene.Params.ccdMotionThreshold > 0f) 842 if (BSParam.CcdMotionThreshold > 0f)
719 { 843 {
720 BulletSimAPI.SetCcdMotionThreshold2(PhysBody.ptr, PhysicsScene.Params.ccdMotionThreshold); 844 PhysicsScene.PE.SetCcdMotionThreshold(PhysBody, BSParam.CcdMotionThreshold);
721 BulletSimAPI.SetCcdSweptSphereRadius2(PhysBody.ptr, PhysicsScene.Params.ccdSweptSphereRadius); 845 PhysicsScene.PE.SetCcdSweptSphereRadius(PhysBody, BSParam.CcdSweptSphereRadius);
722 } 846 }
723 847
724 // Various values for simulation limits 848 // Various values for simulation limits
725 BulletSimAPI.SetDamping2(PhysBody.ptr, PhysicsScene.Params.linearDamping, PhysicsScene.Params.angularDamping); 849 PhysicsScene.PE.SetDamping(PhysBody, BSParam.LinearDamping, BSParam.AngularDamping);
726 BulletSimAPI.SetDeactivationTime2(PhysBody.ptr, PhysicsScene.Params.deactivationTime); 850 PhysicsScene.PE.SetDeactivationTime(PhysBody, BSParam.DeactivationTime);
727 BulletSimAPI.SetSleepingThresholds2(PhysBody.ptr, PhysicsScene.Params.linearSleepingThreshold, PhysicsScene.Params.angularSleepingThreshold); 851 PhysicsScene.PE.SetSleepingThresholds(PhysBody, BSParam.LinearSleepingThreshold, BSParam.AngularSleepingThreshold);
728 BulletSimAPI.SetContactProcessingThreshold2(PhysBody.ptr, PhysicsScene.Params.contactProcessingThreshold); 852 PhysicsScene.PE.SetContactProcessingThreshold(PhysBody, BSParam.ContactProcessingThreshold);
729 853
730 // There might be special things needed for implementing linksets. 854 // This collides like an object.
731 Linkset.MakeDynamic(this); 855 PhysBody.collisionType = CollisionType.Dynamic;
732 856
733 // Force activation of the object so Bullet will act on it. 857 // Force activation of the object so Bullet will act on it.
734 // Must do the ForceActivationState2() to overcome the DISABLE_SIMULATION from static objects. 858 // Must do the ForceActivationState2() to overcome the DISABLE_SIMULATION from static objects.
735 BulletSimAPI.ForceActivationState2(PhysBody.ptr, ActivationState.ACTIVE_TAG); 859 PhysicsScene.PE.ForceActivationState(PhysBody, ActivationState.ACTIVE_TAG);
736 // BulletSimAPI.Activate2(BSBody.ptr, true);
737 860
738 PhysBody.collisionFilter = CollisionFilterGroups.ObjectFilter; 861 // There might be special things needed for implementing linksets.
739 PhysBody.collisionMask = CollisionFilterGroups.ObjectMask; 862 Linkset.MakeDynamic(this);
740 } 863 }
741 } 864 }
742 865
@@ -746,7 +869,7 @@ public sealed class BSPrim : BSPhysObject
746 // the functions after this one set up the state of a possibly newly created collision body. 869 // the functions after this one set up the state of a possibly newly created collision body.
747 private void MakeSolid(bool makeSolid) 870 private void MakeSolid(bool makeSolid)
748 { 871 {
749 CollisionObjectTypes bodyType = (CollisionObjectTypes)BulletSimAPI.GetBodyType2(PhysBody.ptr); 872 CollisionObjectTypes bodyType = (CollisionObjectTypes)PhysicsScene.PE.GetBodyType(PhysBody);
750 if (makeSolid) 873 if (makeSolid)
751 { 874 {
752 // Verify the previous code created the correct shape for this type of thing. 875 // Verify the previous code created the correct shape for this type of thing.
@@ -754,7 +877,7 @@ public sealed class BSPrim : BSPhysObject
754 { 877 {
755 m_log.ErrorFormat("{0} MakeSolid: physical body of wrong type for solidity. id={1}, type={2}", LogHeader, LocalID, bodyType); 878 m_log.ErrorFormat("{0} MakeSolid: physical body of wrong type for solidity. id={1}, type={2}", LogHeader, LocalID, bodyType);
756 } 879 }
757 CurrentCollisionFlags = BulletSimAPI.RemoveFromCollisionFlags2(PhysBody.ptr, CollisionFlags.CF_NO_CONTACT_RESPONSE); 880 CurrentCollisionFlags = PhysicsScene.PE.RemoveFromCollisionFlags(PhysBody, CollisionFlags.CF_NO_CONTACT_RESPONSE);
758 } 881 }
759 else 882 else
760 { 883 {
@@ -762,9 +885,10 @@ public sealed class BSPrim : BSPhysObject
762 { 885 {
763 m_log.ErrorFormat("{0} MakeSolid: physical body of wrong type for non-solidness. id={1}, type={2}", LogHeader, LocalID, bodyType); 886 m_log.ErrorFormat("{0} MakeSolid: physical body of wrong type for non-solidness. id={1}, type={2}", LogHeader, LocalID, bodyType);
764 } 887 }
765 CurrentCollisionFlags = BulletSimAPI.AddToCollisionFlags2(PhysBody.ptr, CollisionFlags.CF_NO_CONTACT_RESPONSE); 888 CurrentCollisionFlags = PhysicsScene.PE.AddToCollisionFlags(PhysBody, CollisionFlags.CF_NO_CONTACT_RESPONSE);
766 PhysBody.collisionFilter = CollisionFilterGroups.VolumeDetectFilter; 889
767 PhysBody.collisionMask = CollisionFilterGroups.VolumeDetectMask; 890 // Change collision info from a static object to a ghosty collision object
891 PhysBody.collisionType = CollisionType.VolumeDetect;
768 } 892 }
769 } 893 }
770 894
@@ -773,8 +897,8 @@ public sealed class BSPrim : BSPhysObject
773 // Called in taint-time!! 897 // Called in taint-time!!
774 private void ActivateIfPhysical(bool forceIt) 898 private void ActivateIfPhysical(bool forceIt)
775 { 899 {
776 if (IsPhysical) 900 if (IsPhysical && PhysBody.HasPhysicalBody)
777 BulletSimAPI.Activate2(PhysBody.ptr, forceIt); 901 PhysicsScene.PE.Activate(PhysBody, forceIt);
778 } 902 }
779 903
780 // Turn on or off the flag controlling whether collision events are returned to the simulator. 904 // Turn on or off the flag controlling whether collision events are returned to the simulator.
@@ -782,11 +906,27 @@ public sealed class BSPrim : BSPhysObject
782 { 906 {
783 if (wantsCollisionEvents) 907 if (wantsCollisionEvents)
784 { 908 {
785 CurrentCollisionFlags = BulletSimAPI.AddToCollisionFlags2(PhysBody.ptr, CollisionFlags.BS_SUBSCRIBE_COLLISION_EVENTS); 909 CurrentCollisionFlags = PhysicsScene.PE.AddToCollisionFlags(PhysBody, CollisionFlags.BS_SUBSCRIBE_COLLISION_EVENTS);
910 }
911 else
912 {
913 CurrentCollisionFlags = PhysicsScene.PE.RemoveFromCollisionFlags(PhysBody, CollisionFlags.BS_SUBSCRIBE_COLLISION_EVENTS);
914 }
915 }
916
917 // Add me to the physical world.
918 // Object MUST NOT already be in the world.
919 // This routine exists because some assorted properties get mangled by adding to the world.
920 internal void AddObjectToPhysicalWorld()
921 {
922 if (PhysBody.HasPhysicalBody)
923 {
924 PhysicsScene.PE.AddObjectToWorld(PhysicsScene.World, PhysBody);
786 } 925 }
787 else 926 else
788 { 927 {
789 CurrentCollisionFlags = BulletSimAPI.RemoveFromCollisionFlags2(PhysBody.ptr, CollisionFlags.BS_SUBSCRIBE_COLLISION_EVENTS); 928 m_log.ErrorFormat("{0} Attempt to add physical object without body. id={1}", LogHeader, LocalID);
929 DetailLog("{0},BSPrim.UpdatePhysicalParameters,addObjectWithoutBody,cType={1}", LocalID, PhysBody.collisionType);
790 } 930 }
791 } 931 }
792 932
@@ -805,18 +945,6 @@ public sealed class BSPrim : BSPhysObject
805 get { return _throttleUpdates; } 945 get { return _throttleUpdates; }
806 set { _throttleUpdates = value; } 946 set { _throttleUpdates = value; }
807 } 947 }
808 public override bool IsColliding {
809 get { return (CollidingStep == PhysicsScene.SimulationStep); }
810 set { _isColliding = value; }
811 }
812 public override bool CollidingGround {
813 get { return (CollidingGroundStep == PhysicsScene.SimulationStep); }
814 set { _collidingGround = value; }
815 }
816 public override bool CollidingObj {
817 get { return _collidingObj; }
818 set { _collidingObj = value; }
819 }
820 public bool IsPhantom { 948 public bool IsPhantom {
821 get { 949 get {
822 // SceneObjectPart removes phantom objects from the physics scene 950 // SceneObjectPart removes phantom objects from the physics scene
@@ -831,23 +959,14 @@ public sealed class BSPrim : BSPhysObject
831 PhysicsScene.TaintedObject("BSPrim.setFloatOnWater", delegate() 959 PhysicsScene.TaintedObject("BSPrim.setFloatOnWater", delegate()
832 { 960 {
833 if (_floatOnWater) 961 if (_floatOnWater)
834 CurrentCollisionFlags = BulletSimAPI.AddToCollisionFlags2(PhysBody.ptr, CollisionFlags.BS_FLOATS_ON_WATER); 962 CurrentCollisionFlags = PhysicsScene.PE.AddToCollisionFlags(PhysBody, CollisionFlags.BS_FLOATS_ON_WATER);
835 else 963 else
836 CurrentCollisionFlags = BulletSimAPI.RemoveFromCollisionFlags2(PhysBody.ptr, CollisionFlags.BS_FLOATS_ON_WATER); 964 CurrentCollisionFlags = PhysicsScene.PE.RemoveFromCollisionFlags(PhysBody, CollisionFlags.BS_FLOATS_ON_WATER);
837 }); 965 });
838 } 966 }
839 } 967 }
840 public override OMV.Vector3 RotationalVelocity { 968 public override OMV.Vector3 RotationalVelocity {
841 get { 969 get {
842 /*
843 OMV.Vector3 pv = OMV.Vector3.Zero;
844 // if close to zero, report zero
845 // This is copied from ODE but I'm not sure why it returns zero but doesn't
846 // zero the property in the physics engine.
847 if (_rotationalVelocity.ApproxEquals(pv, 0.2f))
848 return pv;
849 */
850
851 return _rotationalVelocity; 970 return _rotationalVelocity;
852 } 971 }
853 set { 972 set {
@@ -856,7 +975,7 @@ public sealed class BSPrim : BSPhysObject
856 PhysicsScene.TaintedObject("BSPrim.setRotationalVelocity", delegate() 975 PhysicsScene.TaintedObject("BSPrim.setRotationalVelocity", delegate()
857 { 976 {
858 DetailLog("{0},BSPrim.SetRotationalVel,taint,rotvel={1}", LocalID, _rotationalVelocity); 977 DetailLog("{0},BSPrim.SetRotationalVel,taint,rotvel={1}", LocalID, _rotationalVelocity);
859 BulletSimAPI.SetAngularVelocity2(PhysBody.ptr, _rotationalVelocity); 978 ForceRotationalVelocity = _rotationalVelocity;
860 }); 979 });
861 } 980 }
862 } 981 }
@@ -866,7 +985,11 @@ public sealed class BSPrim : BSPhysObject
866 } 985 }
867 set { 986 set {
868 _rotationalVelocity = value; 987 _rotationalVelocity = value;
869 BulletSimAPI.SetAngularVelocity2(PhysBody.ptr, _rotationalVelocity); 988 if (PhysBody.HasPhysicalBody)
989 {
990 PhysicsScene.PE.SetAngularVelocity(PhysBody, _rotationalVelocity);
991 ActivateIfPhysical(false);
992 }
870 } 993 }
871 } 994 }
872 public override bool Kinematic { 995 public override bool Kinematic {
@@ -890,9 +1013,10 @@ public sealed class BSPrim : BSPhysObject
890 set { 1013 set {
891 _buoyancy = value; 1014 _buoyancy = value;
892 // DetailLog("{0},BSPrim.setForceBuoyancy,taint,buoy={1}", LocalID, _buoyancy); 1015 // DetailLog("{0},BSPrim.setForceBuoyancy,taint,buoy={1}", LocalID, _buoyancy);
893 // Buoyancy is faked by changing the gravity applied to the object 1016 // Force the recalculation of the various inertia,etc variables in the object
894 float grav = PhysicsScene.Params.gravity * (1f - _buoyancy); 1017 DetailLog("{0},BSPrim.ForceBuoyancy,buoy={1},mass={2}", LocalID, _buoyancy, _mass);
895 BulletSimAPI.SetGravity2(PhysBody.ptr, new OMV.Vector3(0f, 0f, grav)); 1018 UpdatePhysicalMassProperties(_mass, true);
1019 ActivateIfPhysical(false);
896 } 1020 }
897 } 1021 }
898 1022
@@ -900,17 +1024,103 @@ public sealed class BSPrim : BSPhysObject
900 public override OMV.Vector3 PIDTarget { 1024 public override OMV.Vector3 PIDTarget {
901 set { _PIDTarget = value; } 1025 set { _PIDTarget = value; }
902 } 1026 }
903 public override bool PIDActive {
904 set { _usePID = value; }
905 }
906 public override float PIDTau { 1027 public override float PIDTau {
907 set { _PIDTau = value; } 1028 set { _PIDTau = value; }
908 } 1029 }
1030 public override bool PIDActive {
1031 set {
1032 if (value)
1033 {
1034 // We're taking over after this.
1035 ZeroMotion(true);
1036
1037 _targetMotor = new BSVMotor("BSPrim.PIDTarget",
1038 _PIDTau, // timeScale
1039 BSMotor.Infinite, // decay time scale
1040 BSMotor.InfiniteVector, // friction timescale
1041 1f // efficiency
1042 );
1043 _targetMotor.PhysicsScene = PhysicsScene; // DEBUG DEBUG so motor will output detail log messages.
1044 _targetMotor.SetTarget(_PIDTarget);
1045 _targetMotor.SetCurrent(RawPosition);
1046 /*
1047 _targetMotor = new BSPIDVMotor("BSPrim.PIDTarget");
1048 _targetMotor.PhysicsScene = PhysicsScene; // DEBUG DEBUG so motor will output detail log messages.
1049
1050 _targetMotor.SetTarget(_PIDTarget);
1051 _targetMotor.SetCurrent(RawPosition);
1052 _targetMotor.TimeScale = _PIDTau;
1053 _targetMotor.Efficiency = 1f;
1054 */
1055
1056 RegisterPreStepAction("BSPrim.PIDTarget", LocalID, delegate(float timeStep)
1057 {
1058 OMV.Vector3 origPosition = RawPosition; // DEBUG DEBUG (for printout below)
1059
1060 // 'movePosition' is where we'd like the prim to be at this moment.
1061 OMV.Vector3 movePosition = _targetMotor.Step(timeStep);
1062
1063 // If we are very close to our target, turn off the movement motor.
1064 if (_targetMotor.ErrorIsZero())
1065 {
1066 DetailLog("{0},BSPrim.PIDTarget,zeroMovement,movePos={1},pos={2},mass={3}",
1067 LocalID, movePosition, RawPosition, Mass);
1068 ForcePosition = _targetMotor.TargetValue;
1069 _targetMotor.Enabled = false;
1070 }
1071 else
1072 {
1073 ForcePosition = movePosition;
1074 }
1075 DetailLog("{0},BSPrim.PIDTarget,move,fromPos={1},movePos={2}", LocalID, origPosition, movePosition);
1076 });
1077 }
1078 else
1079 {
1080 // Stop any targetting
1081 UnRegisterPreStepAction("BSPrim.PIDTarget", LocalID);
1082 }
1083 }
1084 }
909 1085
910 // Used for llSetHoverHeight and maybe vehicle height 1086 // Used for llSetHoverHeight and maybe vehicle height
911 // Hover Height will override MoveTo target's Z 1087 // Hover Height will override MoveTo target's Z
912 public override bool PIDHoverActive { 1088 public override bool PIDHoverActive {
913 set { _useHoverPID = value; } 1089 set {
1090 if (value)
1091 {
1092 // Turning the target on
1093 _hoverMotor = new BSFMotor("BSPrim.Hover",
1094 _PIDHoverTau, // timeScale
1095 BSMotor.Infinite, // decay time scale
1096 BSMotor.Infinite, // friction timescale
1097 1f // efficiency
1098 );
1099 _hoverMotor.SetTarget(ComputeCurrentPIDHoverHeight());
1100 _hoverMotor.SetCurrent(RawPosition.Z);
1101 _hoverMotor.PhysicsScene = PhysicsScene; // DEBUG DEBUG so motor will output detail log messages.
1102
1103 RegisterPreStepAction("BSPrim.Hover", LocalID, delegate(float timeStep)
1104 {
1105 _hoverMotor.SetCurrent(RawPosition.Z);
1106 _hoverMotor.SetTarget(ComputeCurrentPIDHoverHeight());
1107 float targetHeight = _hoverMotor.Step(timeStep);
1108
1109 // 'targetHeight' is where we'd like the Z of the prim to be at this moment.
1110 // Compute the amount of force to push us there.
1111 float moveForce = (targetHeight - RawPosition.Z) * Mass;
1112 // Undo anything the object thinks it's doing at the moment
1113 moveForce = -RawVelocity.Z * Mass;
1114
1115 PhysicsScene.PE.ApplyCentralImpulse(PhysBody, new OMV.Vector3(0f, 0f, moveForce));
1116 DetailLog("{0},BSPrim.Hover,move,targHt={1},moveForce={2},mass={3}", LocalID, targetHeight, moveForce, Mass);
1117 });
1118 }
1119 else
1120 {
1121 UnRegisterPreStepAction("BSPrim.Hover", LocalID);
1122 }
1123 }
914 } 1124 }
915 public override float PIDHoverHeight { 1125 public override float PIDHoverHeight {
916 set { _PIDHoverHeight = value; } 1126 set { _PIDHoverHeight = value; }
@@ -919,8 +1129,35 @@ public sealed class BSPrim : BSPhysObject
919 set { _PIDHoverType = value; } 1129 set { _PIDHoverType = value; }
920 } 1130 }
921 public override float PIDHoverTau { 1131 public override float PIDHoverTau {
922 set { _PIDHoverTao = value; } 1132 set { _PIDHoverTau = value; }
923 } 1133 }
1134 // Based on current position, determine what we should be hovering at now.
1135 // Must recompute often. What if we walked offa cliff>
1136 private float ComputeCurrentPIDHoverHeight()
1137 {
1138 float ret = _PIDHoverHeight;
1139 float groundHeight = PhysicsScene.TerrainManager.GetTerrainHeightAtXYZ(RawPosition);
1140
1141 switch (_PIDHoverType)
1142 {
1143 case PIDHoverType.Ground:
1144 ret = groundHeight + _PIDHoverHeight;
1145 break;
1146 case PIDHoverType.GroundAndWater:
1147 float waterHeight = PhysicsScene.TerrainManager.GetWaterLevelAtXYZ(RawPosition);
1148 if (groundHeight > waterHeight)
1149 {
1150 ret = groundHeight + _PIDHoverHeight;
1151 }
1152 else
1153 {
1154 ret = waterHeight + _PIDHoverHeight;
1155 }
1156 break;
1157 }
1158 return ret;
1159 }
1160
924 1161
925 // For RotLookAt 1162 // For RotLookAt
926 public override OMV.Quaternion APIDTarget { set { return; } } 1163 public override OMV.Quaternion APIDTarget { set { return; } }
@@ -928,54 +1165,82 @@ public sealed class BSPrim : BSPhysObject
928 public override float APIDStrength { set { return; } } 1165 public override float APIDStrength { set { return; } }
929 public override float APIDDamping { set { return; } } 1166 public override float APIDDamping { set { return; } }
930 1167
931 private List<OMV.Vector3> m_accumulatedForces = new List<OMV.Vector3>();
932 public override void AddForce(OMV.Vector3 force, bool pushforce) { 1168 public override void AddForce(OMV.Vector3 force, bool pushforce) {
933 AddForce(force, pushforce, false); 1169 // Since this force is being applied in only one step, make this a force per second.
1170 OMV.Vector3 addForce = force / PhysicsScene.LastTimeStep;
1171 AddForce(addForce, pushforce, false);
934 } 1172 }
935 // Applying a force just adds this to the total force on the object. 1173 // Applying a force just adds this to the total force on the object.
1174 // This added force will only last the next simulation tick.
936 public void AddForce(OMV.Vector3 force, bool pushforce, bool inTaintTime) { 1175 public void AddForce(OMV.Vector3 force, bool pushforce, bool inTaintTime) {
937 // for an object, doesn't matter if force is a pushforce or not 1176 // for an object, doesn't matter if force is a pushforce or not
938 if (force.IsFinite()) 1177 if (!IsStatic)
939 { 1178 {
940 // _force += force; 1179 if (force.IsFinite())
941 lock (m_accumulatedForces)
942 m_accumulatedForces.Add(new OMV.Vector3(force));
943 }
944 else
945 {
946 m_log.WarnFormat("{0}: Got a NaN force applied to a prim. LocalID={1}", LogHeader, LocalID);
947 return;
948 }
949 PhysicsScene.TaintedObject(inTaintTime, "BSPrim.AddForce", delegate()
950 {
951 OMV.Vector3 fSum = OMV.Vector3.Zero;
952 lock (m_accumulatedForces)
953 { 1180 {
954 // Sum the accumulated additional forces for one big force to apply once. 1181 float magnitude = force.Length();
955 foreach (OMV.Vector3 v in m_accumulatedForces) 1182 if (magnitude > BSParam.MaxAddForceMagnitude)
956 { 1183 {
957 fSum += v; 1184 // Force has a limit
1185 force = force / magnitude * BSParam.MaxAddForceMagnitude;
958 } 1186 }
959 m_accumulatedForces.Clear(); 1187
1188 OMV.Vector3 addForce = force;
1189 // DetailLog("{0},BSPrim.addForce,call,force={1}", LocalID, addForce);
1190
1191 PhysicsScene.TaintedObject(inTaintTime, "BSPrim.AddForce", delegate()
1192 {
1193 // Bullet adds this central force to the total force for this tick
1194 DetailLog("{0},BSPrim.addForce,taint,force={1}", LocalID, addForce);
1195 if (PhysBody.HasPhysicalBody)
1196 {
1197 PhysicsScene.PE.ApplyCentralForce(PhysBody, addForce);
1198 ActivateIfPhysical(false);
1199 }
1200 });
960 } 1201 }
961 DetailLog("{0},BSPrim.AddForce,taint,force={1}", LocalID, fSum); 1202 else
962 if (fSum != OMV.Vector3.Zero) 1203 {
963 BulletSimAPI.ApplyCentralForce2(PhysBody.ptr, fSum); 1204 m_log.WarnFormat("{0}: AddForce: Got a NaN force applied to a prim. LocalID={1}", LogHeader, LocalID);
964 }); 1205 return;
1206 }
1207 }
965 } 1208 }
966 1209
967 // An impulse force is scaled by the mass of the object. 1210 public void AddForceImpulse(OMV.Vector3 impulse, bool pushforce, bool inTaintTime) {
968 public void ApplyForceImpulse(OMV.Vector3 impulse, bool inTaintTime) 1211 // for an object, doesn't matter if force is a pushforce or not
969 { 1212 if (!IsStatic)
970 OMV.Vector3 applyImpulse = impulse;
971 PhysicsScene.TaintedObject(inTaintTime, "BSPrim.ApplyForceImpulse", delegate()
972 { 1213 {
973 DetailLog("{0},BSPrim.ApplyForceImpulse,taint,tImpulse={1}", LocalID, applyImpulse); 1214 if (impulse.IsFinite())
974 BulletSimAPI.ApplyCentralImpulse2(PhysBody.ptr, applyImpulse); 1215 {
975 }); 1216 float magnitude = impulse.Length();
1217 if (magnitude > BSParam.MaxAddForceMagnitude)
1218 {
1219 // Force has a limit
1220 impulse = impulse / magnitude * BSParam.MaxAddForceMagnitude;
1221 }
1222
1223 // DetailLog("{0},BSPrim.addForceImpulse,call,impulse={1}", LocalID, impulse);
1224 OMV.Vector3 addImpulse = impulse;
1225 PhysicsScene.TaintedObject(inTaintTime, "BSPrim.AddImpulse", delegate()
1226 {
1227 // Bullet adds this impulse immediately to the velocity
1228 DetailLog("{0},BSPrim.addForceImpulse,taint,impulseforce={1}", LocalID, addImpulse);
1229 if (PhysBody.HasPhysicalBody)
1230 {
1231 PhysicsScene.PE.ApplyCentralImpulse(PhysBody, addImpulse);
1232 ActivateIfPhysical(false);
1233 }
1234 });
1235 }
1236 else
1237 {
1238 m_log.WarnFormat("{0}: AddForceImpulse: Got a NaN impulse applied to a prim. LocalID={1}", LogHeader, LocalID);
1239 return;
1240 }
1241 }
976 } 1242 }
977 1243
978 private List<OMV.Vector3> m_accumulatedAngularForces = new List<OMV.Vector3>();
979 public override void AddAngularForce(OMV.Vector3 force, bool pushforce) { 1244 public override void AddAngularForce(OMV.Vector3 force, bool pushforce) {
980 AddAngularForce(force, pushforce, false); 1245 AddAngularForce(force, pushforce, false);
981 } 1246 }
@@ -983,42 +1248,37 @@ public sealed class BSPrim : BSPhysObject
983 { 1248 {
984 if (force.IsFinite()) 1249 if (force.IsFinite())
985 { 1250 {
986 // _force += force; 1251 OMV.Vector3 angForce = force;
987 lock (m_accumulatedAngularForces) 1252 PhysicsScene.TaintedObject(inTaintTime, "BSPrim.AddAngularForce", delegate()
988 m_accumulatedAngularForces.Add(new OMV.Vector3(force)); 1253 {
1254 if (PhysBody.HasPhysicalBody)
1255 {
1256 PhysicsScene.PE.ApplyTorque(PhysBody, angForce);
1257 ActivateIfPhysical(false);
1258 }
1259 });
989 } 1260 }
990 else 1261 else
991 { 1262 {
992 m_log.WarnFormat("{0}: Got a NaN force applied to a prim. LocalID={1}", LogHeader, LocalID); 1263 m_log.WarnFormat("{0}: Got a NaN force applied to a prim. LocalID={1}", LogHeader, LocalID);
993 return; 1264 return;
994 } 1265 }
995 PhysicsScene.TaintedObject(inTaintTime, "BSPrim.AddAngularForce", delegate()
996 {
997 OMV.Vector3 fSum = OMV.Vector3.Zero;
998 lock (m_accumulatedAngularForces)
999 {
1000 // Sum the accumulated additional forces for one big force to apply once.
1001 foreach (OMV.Vector3 v in m_accumulatedAngularForces)
1002 {
1003 fSum += v;
1004 }
1005 m_accumulatedAngularForces.Clear();
1006 }
1007 DetailLog("{0},BSPrim.AddAngularForce,taint,aForce={1}", LocalID, fSum);
1008 if (fSum != OMV.Vector3.Zero)
1009 {
1010 BulletSimAPI.ApplyTorque2(PhysBody.ptr, fSum);
1011 _torque = fSum;
1012 }
1013 });
1014 } 1266 }
1267
1015 // A torque impulse. 1268 // A torque impulse.
1269 // ApplyTorqueImpulse adds torque directly to the angularVelocity.
1270 // AddAngularForce accumulates the force and applied it to the angular velocity all at once.
1271 // Computed as: angularVelocity += impulse * inertia;
1016 public void ApplyTorqueImpulse(OMV.Vector3 impulse, bool inTaintTime) 1272 public void ApplyTorqueImpulse(OMV.Vector3 impulse, bool inTaintTime)
1017 { 1273 {
1018 OMV.Vector3 applyImpulse = impulse; 1274 OMV.Vector3 applyImpulse = impulse;
1019 PhysicsScene.TaintedObject(inTaintTime, "BSPrim.ApplyTorqueImpulse", delegate() 1275 PhysicsScene.TaintedObject(inTaintTime, "BSPrim.ApplyTorqueImpulse", delegate()
1020 { 1276 {
1021 BulletSimAPI.ApplyTorqueImpulse2(PhysBody.ptr, applyImpulse); 1277 if (PhysBody.HasPhysicalBody)
1278 {
1279 PhysicsScene.PE.ApplyTorqueImpulse(PhysBody, applyImpulse);
1280 ActivateIfPhysical(false);
1281 }
1022 }); 1282 });
1023 } 1283 }
1024 1284
@@ -1313,11 +1573,7 @@ public sealed class BSPrim : BSPhysObject
1313 } 1573 }
1314 */ 1574 */
1315 1575
1316 if (returnMass <= 0) 1576 returnMass = Util.Clamp(returnMass, BSParam.MinimumObjectMass, BSParam.MaximumObjectMass);
1317 returnMass = 0.0001f;
1318
1319 if (returnMass > PhysicsScene.MaximumObjectMass)
1320 returnMass = PhysicsScene.MaximumObjectMass;
1321 1577
1322 return returnMass; 1578 return returnMass;
1323 }// end CalculateMass 1579 }// end CalculateMass
@@ -1326,7 +1582,7 @@ public sealed class BSPrim : BSPhysObject
1326 // Rebuild the geometry and object. 1582 // Rebuild the geometry and object.
1327 // This is called when the shape changes so we need to recreate the mesh/hull. 1583 // This is called when the shape changes so we need to recreate the mesh/hull.
1328 // Called at taint-time!!! 1584 // Called at taint-time!!!
1329 private void CreateGeomAndObject(bool forceRebuild) 1585 public void CreateGeomAndObject(bool forceRebuild)
1330 { 1586 {
1331 // If this prim is part of a linkset, we must remove and restore the physical 1587 // If this prim is part of a linkset, we must remove and restore the physical
1332 // links if the body is rebuilt. 1588 // links if the body is rebuilt.
@@ -1336,12 +1592,11 @@ public sealed class BSPrim : BSPhysObject
1336 // Create the correct physical representation for this type of object. 1592 // Create the correct physical representation for this type of object.
1337 // Updates PhysBody and PhysShape with the new information. 1593 // Updates PhysBody and PhysShape with the new information.
1338 // Ignore 'forceRebuild'. This routine makes the right choices and changes of necessary. 1594 // Ignore 'forceRebuild'. This routine makes the right choices and changes of necessary.
1339 // Returns 'true' if either the body or the shape was changed.
1340 PhysicsScene.Shapes.GetBodyAndShape(false, PhysicsScene.World, this, null, delegate(BulletBody dBody) 1595 PhysicsScene.Shapes.GetBodyAndShape(false, PhysicsScene.World, this, null, delegate(BulletBody dBody)
1341 { 1596 {
1342 // Called if the current prim body is about to be destroyed. 1597 // Called if the current prim body is about to be destroyed.
1343 // Remove all the physical dependencies on the old body. 1598 // Remove all the physical dependencies on the old body.
1344 // (Maybe someday make the changing of BSShape an event handled by BSLinkset.) 1599 // (Maybe someday make the changing of BSShape an event to be subscribed to by BSLinkset, ...)
1345 needToRestoreLinkset = Linkset.RemoveBodyDependencies(this); 1600 needToRestoreLinkset = Linkset.RemoveBodyDependencies(this);
1346 needToRestoreVehicle = _vehicle.RemoveBodyDependencies(this); 1601 needToRestoreVehicle = _vehicle.RemoveBodyDependencies(this);
1347 }); 1602 });
@@ -1364,72 +1619,20 @@ public sealed class BSPrim : BSPhysObject
1364 1619
1365 // The physics engine says that properties have updated. Update same and inform 1620 // The physics engine says that properties have updated. Update same and inform
1366 // the world that things have changed. 1621 // the world that things have changed.
1367 // TODO: do we really need to check for changed? Maybe just copy values and call RequestPhysicsterseUpdate()
1368 enum UpdatedProperties {
1369 Position = 1 << 0,
1370 Rotation = 1 << 1,
1371 Velocity = 1 << 2,
1372 Acceleration = 1 << 3,
1373 RotationalVel = 1 << 4
1374 }
1375
1376 const float ROTATION_TOLERANCE = 0.01f;
1377 const float VELOCITY_TOLERANCE = 0.001f;
1378 const float POSITION_TOLERANCE = 0.05f;
1379 const float ACCELERATION_TOLERANCE = 0.01f;
1380 const float ROTATIONAL_VELOCITY_TOLERANCE = 0.01f;
1381
1382 public override void UpdateProperties(EntityProperties entprop) 1622 public override void UpdateProperties(EntityProperties entprop)
1383 { 1623 {
1384 /* 1624 // Updates only for individual prims and for the root object of a linkset.
1385 UpdatedProperties changed = 0; 1625 if (Linkset.IsRoot(this))
1386 // assign to the local variables so the normal set action does not happen
1387 // if (_position != entprop.Position)
1388 if (!_position.ApproxEquals(entprop.Position, POSITION_TOLERANCE))
1389 {
1390 _position = entprop.Position;
1391 changed |= UpdatedProperties.Position;
1392 }
1393 // if (_orientation != entprop.Rotation)
1394 if (!_orientation.ApproxEquals(entprop.Rotation, ROTATION_TOLERANCE))
1395 {
1396 _orientation = entprop.Rotation;
1397 changed |= UpdatedProperties.Rotation;
1398 }
1399 // if (_velocity != entprop.Velocity)
1400 if (!_velocity.ApproxEquals(entprop.Velocity, VELOCITY_TOLERANCE))
1401 {
1402 _velocity = entprop.Velocity;
1403 changed |= UpdatedProperties.Velocity;
1404 }
1405 // if (_acceleration != entprop.Acceleration)
1406 if (!_acceleration.ApproxEquals(entprop.Acceleration, ACCELERATION_TOLERANCE))
1407 {
1408 _acceleration = entprop.Acceleration;
1409 changed |= UpdatedProperties.Acceleration;
1410 }
1411 // if (_rotationalVelocity != entprop.RotationalVelocity)
1412 if (!_rotationalVelocity.ApproxEquals(entprop.RotationalVelocity, ROTATIONAL_VELOCITY_TOLERANCE))
1413 {
1414 _rotationalVelocity = entprop.RotationalVelocity;
1415 changed |= UpdatedProperties.RotationalVel;
1416 }
1417 if (changed != 0)
1418 { 1626 {
1419 // Only update the position of single objects and linkset roots 1627 // A temporary kludge to suppress the rotational effects introduced on vehicles by Bullet
1420 if (Linkset.IsRoot(this)) 1628 // TODO: handle physics introduced by Bullet with computed vehicle physics.
1629 if (_vehicle.IsActive)
1421 { 1630 {
1422 base.RequestPhysicsterseUpdate(); 1631 entprop.RotationalVelocity = OMV.Vector3.Zero;
1423 } 1632 }
1424 }
1425 */
1426
1427 // Don't check for damping here -- it's done in BulletSim and SceneObjectPart.
1428 1633
1429 // Updates only for individual prims and for the root object of a linkset. 1634 // Assign directly to the local variables so the normal set actions do not happen
1430 if (Linkset.IsRoot(this)) 1635 entprop.Position -= PositionDisplacement;
1431 {
1432 // Assign directly to the local variables so the normal set action does not happen
1433 _position = entprop.Position; 1636 _position = entprop.Position;
1434 _orientation = entprop.Rotation; 1637 _orientation = entprop.Rotation;
1435 _velocity = entprop.Velocity; 1638 _velocity = entprop.Velocity;
@@ -1437,21 +1640,19 @@ public sealed class BSPrim : BSPhysObject
1437 _rotationalVelocity = entprop.RotationalVelocity; 1640 _rotationalVelocity = entprop.RotationalVelocity;
1438 1641
1439 // The sanity check can change the velocity and/or position. 1642 // The sanity check can change the velocity and/or position.
1440 if (PositionSanityCheck(true)) 1643 if (IsPhysical && PositionSanityCheck(true))
1441 { 1644 {
1442 entprop.Position = _position; 1645 entprop.Position = _position;
1443 entprop.Velocity = _velocity; 1646 entprop.Velocity = _velocity;
1444 } 1647 }
1445 1648
1446 // remember the current and last set values 1649 OMV.Vector3 direction = OMV.Vector3.UnitX * _orientation; // DEBUG DEBUG DEBUG
1447 LastEntityProperties = CurrentEntityProperties;
1448 CurrentEntityProperties = entprop;
1449
1450 OMV.Vector3 direction = OMV.Vector3.UnitX * _orientation;
1451 DetailLog("{0},BSPrim.UpdateProperties,call,pos={1},orient={2},dir={3},vel={4},rotVel={5}", 1650 DetailLog("{0},BSPrim.UpdateProperties,call,pos={1},orient={2},dir={3},vel={4},rotVel={5}",
1452 LocalID, _position, _orientation, direction, _velocity, _rotationalVelocity); 1651 LocalID, _position, _orientation, direction, _velocity, _rotationalVelocity);
1453 1652
1454 // BulletSimAPI.DumpRigidBody2(PhysicsScene.World.ptr, BSBody.ptr); // DEBUG DEBUG DEBUG 1653 // remember the current and last set values
1654 LastEntityProperties = CurrentEntityProperties;
1655 CurrentEntityProperties = entprop;
1455 1656
1456 base.RequestPhysicsterseUpdate(); 1657 base.RequestPhysicsterseUpdate();
1457 } 1658 }
@@ -1466,7 +1667,7 @@ public sealed class BSPrim : BSPhysObject
1466 */ 1667 */
1467 1668
1468 // The linkset implimentation might want to know about this. 1669 // The linkset implimentation might want to know about this.
1469 Linkset.UpdateProperties(this); 1670 Linkset.UpdateProperties(this, true);
1470 } 1671 }
1471} 1672}
1472} 1673}
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSScene.cs b/OpenSim/Region/Physics/BulletSPlugin/BSScene.cs
index 27a78d1..e0b4992 100644
--- a/OpenSim/Region/Physics/BulletSPlugin/BSScene.cs
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSScene.cs
@@ -26,6 +26,7 @@
26 */ 26 */
27using System; 27using System;
28using System.Collections.Generic; 28using System.Collections.Generic;
29using System.Reflection;
29using System.Runtime.InteropServices; 30using System.Runtime.InteropServices;
30using System.Text; 31using System.Text;
31using System.Threading; 32using System.Threading;
@@ -38,40 +39,22 @@ using Nini.Config;
38using log4net; 39using log4net;
39using OpenMetaverse; 40using OpenMetaverse;
40 41
41// TODOs for BulletSim (for BSScene, BSPrim, BSCharacter and BulletSim)
42// Test sculpties (verified that they don't work)
43// Compute physics FPS reasonably
44// Based on material, set density and friction
45// Don't use constraints in linksets of non-physical objects. Means having to move children manually.
46// Four states of prim: Physical, regular, phantom and selected. Are we modeling these correctly?
47// In SL one can set both physical and phantom (gravity, does not effect others, makes collisions with ground)
48// At the moment, physical and phantom causes object to drop through the terrain
49// Physical phantom objects and related typing (collision options )
50// Check out llVolumeDetect. Must do something for that.
51// Use collision masks for collision with terrain and phantom objects
52// More efficient memory usage when passing hull information from BSPrim to BulletSim
53// Should prim.link() and prim.delink() membership checking happen at taint time?
54// Mesh sharing. Use meshHash to tell if we already have a hull of that shape and only create once.
55// Do attachments need to be handled separately? Need collision events. Do not collide with VolumeDetect
56// Implement LockAngularMotion
57// Decide if clearing forces is the right thing to do when setting position (BulletSim::SetObjectTranslation)
58// Remove mesh and Hull stuff. Use mesh passed to bullet and use convexdecom from bullet.
59// Add PID movement operations. What does ScenePresence.MoveToTarget do?
60// Check terrain size. 128 or 127?
61// Raycast
62//
63namespace OpenSim.Region.Physics.BulletSPlugin 42namespace OpenSim.Region.Physics.BulletSPlugin
64{ 43{
65public sealed class BSScene : PhysicsScene, IPhysicsParameters 44public sealed class BSScene : PhysicsScene, IPhysicsParameters
66{ 45{
67 private static readonly ILog m_log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 46 internal static readonly ILog m_log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
68 private static readonly string LogHeader = "[BULLETS SCENE]"; 47 internal static readonly string LogHeader = "[BULLETS SCENE]";
69 48
70 // The name of the region we're working for. 49 // The name of the region we're working for.
71 public string RegionName { get; private set; } 50 public string RegionName { get; private set; }
72 51
73 public string BulletSimVersion = "?"; 52 public string BulletSimVersion = "?";
74 53
54 // The handle to the underlying managed or unmanaged version of Bullet being used.
55 public string BulletEngineName { get; private set; }
56 public BSAPITemplate PE;
57
75 public Dictionary<uint, BSPhysObject> PhysObjects; 58 public Dictionary<uint, BSPhysObject> PhysObjects;
76 public BSShapeCollection Shapes; 59 public BSShapeCollection Shapes;
77 60
@@ -82,32 +65,30 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
82 // every tick so OpenSim will update its animation. 65 // every tick so OpenSim will update its animation.
83 private HashSet<BSPhysObject> m_avatars = new HashSet<BSPhysObject>(); 66 private HashSet<BSPhysObject> m_avatars = new HashSet<BSPhysObject>();
84 67
85 // List of all the objects that have vehicle properties and should be called
86 // to update each physics step.
87 private List<BSPhysObject> m_vehicles = new List<BSPhysObject>();
88
89 // let my minuions use my logger 68 // let my minuions use my logger
90 public ILog Logger { get { return m_log; } } 69 public ILog Logger { get { return m_log; } }
91 70
92 public IMesher mesher; 71 public IMesher mesher;
93 // Level of Detail values kept as float because that's what the Meshmerizer wants
94 public float MeshLOD { get; private set; }
95 public float MeshMegaPrimLOD { get; private set; }
96 public float MeshMegaPrimThreshold { get; private set; }
97 public float SculptLOD { get; private set; }
98
99 public uint WorldID { get; private set; } 72 public uint WorldID { get; private set; }
100 public BulletSim World { get; private set; } 73 public BulletWorld World { get; private set; }
101 74
102 // All the constraints that have been allocated in this instance. 75 // All the constraints that have been allocated in this instance.
103 public BSConstraintCollection Constraints { get; private set; } 76 public BSConstraintCollection Constraints { get; private set; }
104 77
105 // Simulation parameters 78 // Simulation parameters
106 private int m_maxSubSteps; 79 internal int m_maxSubSteps;
107 private float m_fixedTimeStep; 80 internal float m_fixedTimeStep;
108 private long m_simulationStep = 0; 81 internal long m_simulationStep = 0;
82 internal float NominalFrameRate { get; set; }
109 public long SimulationStep { get { return m_simulationStep; } } 83 public long SimulationStep { get { return m_simulationStep; } }
110 private int m_taintsToProcessPerStep; 84 internal int m_taintsToProcessPerStep;
85 internal float LastTimeStep { get; private set; }
86
87 // Physical objects can register for prestep or poststep events
88 public delegate void PreStepAction(float timeStep);
89 public delegate void PostStepAction(float timeStep);
90 public event PreStepAction BeforeStep;
91 public event PreStepAction AfterStep;
111 92
112 // A value of the time now so all the collision and update routines do not have to get their own 93 // A value of the time now so all the collision and update routines do not have to get their own
113 // Set to 'now' just before all the prims and actors are called for collisions and updates 94 // Set to 'now' just before all the prims and actors are called for collisions and updates
@@ -121,31 +102,22 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
121 public bool InTaintTime { get; private set; } 102 public bool InTaintTime { get; private set; }
122 103
123 // Pinned memory used to pass step information between managed and unmanaged 104 // Pinned memory used to pass step information between managed and unmanaged
124 private int m_maxCollisionsPerFrame; 105 internal int m_maxCollisionsPerFrame;
125 private CollisionDesc[] m_collisionArray; 106 internal CollisionDesc[] m_collisionArray;
126 private GCHandle m_collisionArrayPinnedHandle;
127 107
128 private int m_maxUpdatesPerFrame; 108 internal int m_maxUpdatesPerFrame;
129 private EntityProperties[] m_updateArray; 109 internal EntityProperties[] m_updateArray;
130 private GCHandle m_updateArrayPinnedHandle;
131
132 public bool ShouldMeshSculptedPrim { get; private set; } // cause scuplted prims to get meshed
133 public bool ShouldForceSimplePrimMeshing { get; private set; } // if a cube or sphere, let Bullet do internal shapes
134 public bool ShouldUseHullsForPhysicalObjects { get; private set; } // 'true' if should create hulls for physical objects
135
136 public float PID_D { get; private set; } // derivative
137 public float PID_P { get; private set; } // proportional
138 110
139 public const uint TERRAIN_ID = 0; // OpenSim senses terrain with a localID of zero 111 public const uint TERRAIN_ID = 0; // OpenSim senses terrain with a localID of zero
140 public const uint GROUNDPLANE_ID = 1; 112 public const uint GROUNDPLANE_ID = 1;
141 public const uint CHILDTERRAIN_ID = 2; // Terrain allocated based on our mega-prim childre start here 113 public const uint CHILDTERRAIN_ID = 2; // Terrain allocated based on our mega-prim childre start here
142 114
143 private float m_waterLevel; 115 public float SimpleWaterLevel { get; set; }
144 public BSTerrainManager TerrainManager { get; private set; } 116 public BSTerrainManager TerrainManager { get; private set; }
145 117
146 public ConfigurationParameters Params 118 public ConfigurationParameters Params
147 { 119 {
148 get { return m_params[0]; } 120 get { return UnmanagedParams[0]; }
149 } 121 }
150 public Vector3 DefaultGravity 122 public Vector3 DefaultGravity
151 { 123 {
@@ -157,8 +129,6 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
157 get { return Params.gravity; } 129 get { return Params.gravity; }
158 } 130 }
159 131
160 public float MaximumObjectMass { get; private set; }
161
162 // When functions in the unmanaged code must be called, it is only 132 // When functions in the unmanaged code must be called, it is only
163 // done at a known time just before the simulation step. The taint 133 // done at a known time just before the simulation step. The taint
164 // system saves all these function calls and executes them in 134 // system saves all these function calls and executes them in
@@ -181,13 +151,7 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
181 151
182 // A pointer to an instance if this structure is passed to the C++ code 152 // A pointer to an instance if this structure is passed to the C++ code
183 // Used to pass basic configuration values to the unmanaged code. 153 // Used to pass basic configuration values to the unmanaged code.
184 ConfigurationParameters[] m_params; 154 internal ConfigurationParameters[] UnmanagedParams;
185 GCHandle m_paramsHandle;
186
187 // Handle to the callback used by the unmanaged code to call into the managed code.
188 // Used for debug logging.
189 // Need to store the handle in a persistant variable so it won't be freed.
190 private BulletSimAPI.DebugLogCallback m_DebugLogCallbackHandle;
191 155
192 // Sometimes you just have to log everything. 156 // Sometimes you just have to log everything.
193 public Logging.LogWriter PhysicsLogging; 157 public Logging.LogWriter PhysicsLogging;
@@ -195,15 +159,24 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
195 private string m_physicsLoggingDir; 159 private string m_physicsLoggingDir;
196 private string m_physicsLoggingPrefix; 160 private string m_physicsLoggingPrefix;
197 private int m_physicsLoggingFileMinutes; 161 private int m_physicsLoggingFileMinutes;
162 private bool m_physicsLoggingDoFlush;
163 private bool m_physicsPhysicalDumpEnabled;
164 public float PhysicsMetricDumpFrames { get; set; }
198 // 'true' of the vehicle code is to log lots of details 165 // 'true' of the vehicle code is to log lots of details
199 public bool VehicleLoggingEnabled { get; private set; } 166 public bool VehicleLoggingEnabled { get; private set; }
167 public bool VehiclePhysicalLoggingEnabled { get; private set; }
200 168
201 #region Construction and Initialization 169 #region Construction and Initialization
202 public BSScene(string identifier) 170 public BSScene(string engineType, string identifier)
203 { 171 {
204 m_initialized = false; 172 m_initialized = false;
205 // we are passed the name of the region we're working for. 173
174 // The name of the region we're working for is passed to us. Keep for identification.
206 RegionName = identifier; 175 RegionName = identifier;
176
177 // Set identifying variables in the PhysicsScene interface.
178 EngineType = engineType;
179 Name = EngineType + "/" + RegionName;
207 } 180 }
208 181
209 public override void Initialise(IMesher meshmerizer, IConfigSource config) 182 public override void Initialise(IMesher meshmerizer, IConfigSource config)
@@ -216,17 +189,13 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
216 Shapes = new BSShapeCollection(this); 189 Shapes = new BSShapeCollection(this);
217 190
218 // Allocate pinned memory to pass parameters. 191 // Allocate pinned memory to pass parameters.
219 m_params = new ConfigurationParameters[1]; 192 UnmanagedParams = new ConfigurationParameters[1];
220 m_paramsHandle = GCHandle.Alloc(m_params, GCHandleType.Pinned);
221 193
222 // Set default values for physics parameters plus any overrides from the ini file 194 // Set default values for physics parameters plus any overrides from the ini file
223 GetInitialParameterValues(config); 195 GetInitialParameterValues(config);
224 196
225 // allocate more pinned memory close to the above in an attempt to get the memory all together 197 // Get the connection to the physics engine (could be native or one of many DLLs)
226 m_collisionArray = new CollisionDesc[m_maxCollisionsPerFrame]; 198 PE = SelectUnderlyingBulletEngine(BulletEngineName);
227 m_collisionArrayPinnedHandle = GCHandle.Alloc(m_collisionArray, GCHandleType.Pinned);
228 m_updateArray = new EntityProperties[m_maxUpdatesPerFrame];
229 m_updateArrayPinnedHandle = GCHandle.Alloc(m_updateArray, GCHandleType.Pinned);
230 199
231 // Enable very detailed logging. 200 // Enable very detailed logging.
232 // By creating an empty logger when not logging, the log message invocation code 201 // By creating an empty logger when not logging, the log message invocation code
@@ -234,28 +203,16 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
234 if (m_physicsLoggingEnabled) 203 if (m_physicsLoggingEnabled)
235 { 204 {
236 PhysicsLogging = new Logging.LogWriter(m_physicsLoggingDir, m_physicsLoggingPrefix, m_physicsLoggingFileMinutes); 205 PhysicsLogging = new Logging.LogWriter(m_physicsLoggingDir, m_physicsLoggingPrefix, m_physicsLoggingFileMinutes);
206 PhysicsLogging.ErrorLogger = m_log; // for DEBUG. Let's the logger output error messages.
237 } 207 }
238 else 208 else
239 { 209 {
240 PhysicsLogging = new Logging.LogWriter(); 210 PhysicsLogging = new Logging.LogWriter();
241 } 211 }
242 212
243 // If Debug logging level, enable logging from the unmanaged code 213 // Allocate memory for returning of the updates and collisions from the physics engine
244 m_DebugLogCallbackHandle = null; 214 m_collisionArray = new CollisionDesc[m_maxCollisionsPerFrame];
245 if (m_log.IsDebugEnabled || PhysicsLogging.Enabled) 215 m_updateArray = new EntityProperties[m_maxUpdatesPerFrame];
246 {
247 m_log.DebugFormat("{0}: Initialize: Setting debug callback for unmanaged code", LogHeader);
248 if (PhysicsLogging.Enabled)
249 // The handle is saved in a variable to make sure it doesn't get freed after this call
250 m_DebugLogCallbackHandle = new BulletSimAPI.DebugLogCallback(BulletLoggerPhysLog);
251 else
252 m_DebugLogCallbackHandle = new BulletSimAPI.DebugLogCallback(BulletLogger);
253 }
254
255 // Get the version of the DLL
256 // TODO: this doesn't work yet. Something wrong with marshaling the returned string.
257 // BulletSimVersion = BulletSimAPI.GetVersion();
258 // m_log.WarnFormat("{0}: BulletSim.dll version='{1}'", LogHeader, BulletSimVersion);
259 216
260 // The bounding box for the simulated world. The origin is 0,0,0 unless we're 217 // The bounding box for the simulated world. The origin is 0,0,0 unless we're
261 // a child in a mega-region. 218 // a child in a mega-region.
@@ -263,18 +220,14 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
263 // area. It tracks active objects no matter where they are. 220 // area. It tracks active objects no matter where they are.
264 Vector3 worldExtent = new Vector3(Constants.RegionSize, Constants.RegionSize, Constants.RegionHeight); 221 Vector3 worldExtent = new Vector3(Constants.RegionSize, Constants.RegionSize, Constants.RegionHeight);
265 222
266 // m_log.DebugFormat("{0}: Initialize: Calling BulletSimAPI.Initialize.", LogHeader); 223 World = PE.Initialize(worldExtent, Params, m_maxCollisionsPerFrame, ref m_collisionArray, m_maxUpdatesPerFrame, ref m_updateArray);
267 World = new BulletSim(0, this, BulletSimAPI.Initialize2(worldExtent, m_paramsHandle.AddrOfPinnedObject(),
268 m_maxCollisionsPerFrame, m_collisionArrayPinnedHandle.AddrOfPinnedObject(),
269 m_maxUpdatesPerFrame, m_updateArrayPinnedHandle.AddrOfPinnedObject(),
270 m_DebugLogCallbackHandle));
271 224
272 Constraints = new BSConstraintCollection(World); 225 Constraints = new BSConstraintCollection(World);
273 226
274 TerrainManager = new BSTerrainManager(this); 227 TerrainManager = new BSTerrainManager(this);
275 TerrainManager.CreateInitialGroundPlaneAndTerrain(); 228 TerrainManager.CreateInitialGroundPlaneAndTerrain();
276 229
277 m_log.WarnFormat("{0} Linksets implemented with {1}", LogHeader, (BSLinkset.LinksetImplementation)Params.linksetImplementation); 230 m_log.WarnFormat("{0} Linksets implemented with {1}", LogHeader, (BSLinkset.LinksetImplementation)BSParam.LinksetImplementation);
278 231
279 InTaintTime = false; 232 InTaintTime = false;
280 m_initialized = true; 233 m_initialized = true;
@@ -285,9 +238,9 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
285 private void GetInitialParameterValues(IConfigSource config) 238 private void GetInitialParameterValues(IConfigSource config)
286 { 239 {
287 ConfigurationParameters parms = new ConfigurationParameters(); 240 ConfigurationParameters parms = new ConfigurationParameters();
288 m_params[0] = parms; 241 UnmanagedParams[0] = parms;
289 242
290 SetParameterDefaultValues(); 243 BSParam.SetParameterDefaultValues(this);
291 244
292 if (config != null) 245 if (config != null)
293 { 246 {
@@ -295,19 +248,34 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
295 IConfig pConfig = config.Configs["BulletSim"]; 248 IConfig pConfig = config.Configs["BulletSim"];
296 if (pConfig != null) 249 if (pConfig != null)
297 { 250 {
298 SetParameterConfigurationValues(pConfig); 251 BSParam.SetParameterConfigurationValues(this, pConfig);
252
253 // There are two Bullet implementations to choose from
254 BulletEngineName = pConfig.GetString("BulletEngine", "BulletUnmanaged");
299 255
300 // Very detailed logging for physics debugging 256 // Very detailed logging for physics debugging
257 // TODO: the boolean values can be moved to the normal parameter processing.
301 m_physicsLoggingEnabled = pConfig.GetBoolean("PhysicsLoggingEnabled", false); 258 m_physicsLoggingEnabled = pConfig.GetBoolean("PhysicsLoggingEnabled", false);
302 m_physicsLoggingDir = pConfig.GetString("PhysicsLoggingDir", "."); 259 m_physicsLoggingDir = pConfig.GetString("PhysicsLoggingDir", ".");
303 m_physicsLoggingPrefix = pConfig.GetString("PhysicsLoggingPrefix", "physics-%REGIONNAME%-"); 260 m_physicsLoggingPrefix = pConfig.GetString("PhysicsLoggingPrefix", "physics-%REGIONNAME%-");
304 m_physicsLoggingFileMinutes = pConfig.GetInt("PhysicsLoggingFileMinutes", 5); 261 m_physicsLoggingFileMinutes = pConfig.GetInt("PhysicsLoggingFileMinutes", 5);
262 m_physicsLoggingDoFlush = pConfig.GetBoolean("PhysicsLoggingDoFlush", false);
263 m_physicsPhysicalDumpEnabled = pConfig.GetBoolean("PhysicsPhysicalDumpEnabled", false);
305 // Very detailed logging for vehicle debugging 264 // Very detailed logging for vehicle debugging
306 VehicleLoggingEnabled = pConfig.GetBoolean("VehicleLoggingEnabled", false); 265 VehicleLoggingEnabled = pConfig.GetBoolean("VehicleLoggingEnabled", false);
266 VehiclePhysicalLoggingEnabled = pConfig.GetBoolean("VehiclePhysicalLoggingEnabled", false);
307 267
308 // Do any replacements in the parameters 268 // Do any replacements in the parameters
309 m_physicsLoggingPrefix = m_physicsLoggingPrefix.Replace("%REGIONNAME%", RegionName); 269 m_physicsLoggingPrefix = m_physicsLoggingPrefix.Replace("%REGIONNAME%", RegionName);
310 } 270 }
271
272 // The material characteristics.
273 BSMaterials.InitializeFromDefaults(Params);
274 if (pConfig != null)
275 {
276 // Let the user add new and interesting material property values.
277 BSMaterials.InitializefromParameters(pConfig);
278 }
311 } 279 }
312 } 280 }
313 281
@@ -326,16 +294,41 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
326 return ret; 294 return ret;
327 } 295 }
328 296
329 // Called directly from unmanaged code so don't do much 297 // Select the connection to the actual Bullet implementation.
330 private void BulletLogger(string msg) 298 // The main engine selection is the engineName up to the first hypen.
299 // So "Bullet-2.80-OpenCL-Intel" specifies the 'bullet' class here and the whole name
300 // is passed to the engine to do its special selection, etc.
301 private BSAPITemplate SelectUnderlyingBulletEngine(string engineName)
331 { 302 {
332 m_log.Debug("[BULLETS UNMANAGED]:" + msg); 303 // For the moment, do a simple switch statement.
333 } 304 // Someday do fancyness with looking up the interfaces in the assembly.
305 BSAPITemplate ret = null;
334 306
335 // Called directly from unmanaged code so don't do much 307 string selectionName = engineName.ToLower();
336 private void BulletLoggerPhysLog(string msg) 308 int hyphenIndex = engineName.IndexOf("-");
337 { 309 if (hyphenIndex > 0)
338 DetailLog("[BULLETS UNMANAGED]:" + msg); 310 selectionName = engineName.ToLower().Substring(0, hyphenIndex - 1);
311
312 switch (selectionName)
313 {
314 case "bulletunmanaged":
315 ret = new BSAPIUnman(engineName, this);
316 break;
317 case "bulletxna":
318 ret = new BSAPIXNA(engineName, this);
319 break;
320 }
321
322 if (ret == null)
323 {
324 m_log.ErrorFormat("{0) COULD NOT SELECT BULLET ENGINE: '[BulletSim]PhysicsEngine' must be either 'BulletUnmanaged-*' or 'BulletXNA-*'", LogHeader);
325 }
326 else
327 {
328 m_log.WarnFormat("{0} Selected bullet engine {1} -> {2}/{3}", LogHeader, engineName, ret.BulletEngineName, ret.BulletEngineVersion);
329 }
330
331 return ret;
339 } 332 }
340 333
341 public override void Dispose() 334 public override void Dispose()
@@ -345,8 +338,6 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
345 // make sure no stepping happens while we're deleting stuff 338 // make sure no stepping happens while we're deleting stuff
346 m_initialized = false; 339 m_initialized = false;
347 340
348 TerrainManager.ReleaseGroundPlaneAndTerrain();
349
350 foreach (KeyValuePair<uint, BSPhysObject> kvp in PhysObjects) 341 foreach (KeyValuePair<uint, BSPhysObject> kvp in PhysObjects)
351 { 342 {
352 kvp.Value.Destroy(); 343 kvp.Value.Destroy();
@@ -366,8 +357,15 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
366 Shapes = null; 357 Shapes = null;
367 } 358 }
368 359
360 if (TerrainManager != null)
361 {
362 TerrainManager.ReleaseGroundPlaneAndTerrain();
363 TerrainManager.Dispose();
364 TerrainManager = null;
365 }
366
369 // Anything left in the unmanaged code should be cleaned out 367 // Anything left in the unmanaged code should be cleaned out
370 BulletSimAPI.Shutdown2(World.ptr); 368 PE.Shutdown(World);
371 369
372 // Not logging any more 370 // Not logging any more
373 PhysicsLogging.Close(); 371 PhysicsLogging.Close();
@@ -389,12 +387,14 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
389 if (!m_initialized) return null; 387 if (!m_initialized) return null;
390 388
391 BSCharacter actor = new BSCharacter(localID, avName, this, position, size, isFlying); 389 BSCharacter actor = new BSCharacter(localID, avName, this, position, size, isFlying);
392 lock (PhysObjects) PhysObjects.Add(localID, actor); 390 lock (PhysObjects)
391 PhysObjects.Add(localID, actor);
393 392
394 // TODO: Remove kludge someday. 393 // TODO: Remove kludge someday.
395 // We must generate a collision for avatars whether they collide or not. 394 // We must generate a collision for avatars whether they collide or not.
396 // This is required by OpenSim to update avatar animations, etc. 395 // This is required by OpenSim to update avatar animations, etc.
397 lock (m_avatars) m_avatars.Add(actor); 396 lock (m_avatars)
397 m_avatars.Add(actor);
398 398
399 return actor; 399 return actor;
400 } 400 }
@@ -410,9 +410,11 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
410 { 410 {
411 try 411 try
412 { 412 {
413 lock (PhysObjects) PhysObjects.Remove(actor.LocalID); 413 lock (PhysObjects)
414 PhysObjects.Remove(bsactor.LocalID);
414 // Remove kludge someday 415 // Remove kludge someday
415 lock (m_avatars) m_avatars.Remove(bsactor); 416 lock (m_avatars)
417 m_avatars.Remove(bsactor);
416 } 418 }
417 catch (Exception e) 419 catch (Exception e)
418 { 420 {
@@ -421,6 +423,11 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
421 bsactor.Destroy(); 423 bsactor.Destroy();
422 // bsactor.dispose(); 424 // bsactor.dispose();
423 } 425 }
426 else
427 {
428 m_log.ErrorFormat("{0}: Requested to remove avatar that is not a BSCharacter. ID={1}, type={2}",
429 LogHeader, actor.LocalID, actor.GetType().Name);
430 }
424 } 431 }
425 432
426 public override void RemovePrim(PhysicsActor prim) 433 public override void RemovePrim(PhysicsActor prim)
@@ -474,41 +481,56 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
474 // Simulate one timestep 481 // Simulate one timestep
475 public override float Simulate(float timeStep) 482 public override float Simulate(float timeStep)
476 { 483 {
484 // prevent simulation until we've been initialized
485 if (!m_initialized) return 5.0f;
486
487 LastTimeStep = timeStep;
488
477 int updatedEntityCount = 0; 489 int updatedEntityCount = 0;
478 IntPtr updatedEntitiesPtr;
479 int collidersCount = 0; 490 int collidersCount = 0;
480 IntPtr collidersPtr;
481 491
482 int beforeTime = 0; 492 int beforeTime = 0;
483 int simTime = 0; 493 int simTime = 0;
484 494
485 // prevent simulation until we've been initialized
486 if (!m_initialized) return 5.0f;
487
488 // update the prim states while we know the physics engine is not busy 495 // update the prim states while we know the physics engine is not busy
489 int numTaints = _taintOperations.Count; 496 int numTaints = _taintOperations.Count;
497
498 InTaintTime = true; // Only used for debugging so locking is not necessary.
499
500 ProcessTaints();
501
502 // Some of the physical objects requre individual, pre-step calls
503 // (vehicles and avatar movement, in particular)
504 TriggerPreStepEvent(timeStep);
505
506 // the prestep actions might have added taints
507 numTaints += _taintOperations.Count;
490 ProcessTaints(); 508 ProcessTaints();
491 509
492 // Some of the prims operate with special vehicle properties 510 InTaintTime = false; // Only used for debugging so locking is not necessary.
493 ProcessVehicles(timeStep); 511
494 ProcessTaints(); // the vehicles might have added taints 512 // The following causes the unmanaged code to output ALL the values found in ALL the objects in the world.
513 // Only enable this in a limited test world with few objects.
514 if (m_physicsPhysicalDumpEnabled)
515 PE.DumpAllInfo(World);
495 516
496 // step the physical world one interval 517 // step the physical world one interval
497 m_simulationStep++; 518 m_simulationStep++;
498 int numSubSteps = 0; 519 int numSubSteps = 0;
499
500 try 520 try
501 { 521 {
502 if (VehicleLoggingEnabled) DumpVehicles(); // DEBUG 522 if (PhysicsLogging.Enabled)
503 if (PhysicsLogging.Enabled) beforeTime = Util.EnvironmentTickCount(); 523 beforeTime = Util.EnvironmentTickCount();
504 524
505 numSubSteps = BulletSimAPI.PhysicsStep2(World.ptr, timeStep, m_maxSubSteps, m_fixedTimeStep, 525 numSubSteps = PE.PhysicsStep(World, timeStep, m_maxSubSteps, m_fixedTimeStep, out updatedEntityCount, out collidersCount);
506 out updatedEntityCount, out updatedEntitiesPtr, out collidersCount, out collidersPtr);
507 526
508 if (PhysicsLogging.Enabled) simTime = Util.EnvironmentTickCountSubtract(beforeTime); 527 if (PhysicsLogging.Enabled)
509 DetailLog("{0},Simulate,call, frame={1}, nTaints={2}, simTime={3}, substeps={4}, updates={5}, colliders={6}", 528 {
510 DetailLogZero, m_simulationStep, numTaints, simTime, numSubSteps, updatedEntityCount, collidersCount); 529 simTime = Util.EnvironmentTickCountSubtract(beforeTime);
511 if (VehicleLoggingEnabled) DumpVehicles(); // DEBUG 530 DetailLog("{0},Simulate,call, frame={1}, nTaints={2}, simTime={3}, substeps={4}, updates={5}, colliders={6}, objWColl={7}",
531 DetailLogZero, m_simulationStep, numTaints, simTime, numSubSteps,
532 updatedEntityCount, collidersCount, ObjectsWithCollisions.Count);
533 }
512 } 534 }
513 catch (Exception e) 535 catch (Exception e)
514 { 536 {
@@ -520,9 +542,10 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
520 collidersCount = 0; 542 collidersCount = 0;
521 } 543 }
522 544
523 // Don't have to use the pointers passed back since we know it is the same pinned memory we passed in 545 if ((m_simulationStep % PhysicsMetricDumpFrames) == 0)
546 PE.DumpPhysicsStatistics(World);
524 547
525 // Get a value for 'now' so all the collision and update routines don't have to get their own 548 // Get a value for 'now' so all the collision and update routines don't have to get their own.
526 SimulationNowTime = Util.EnvironmentTickCount(); 549 SimulationNowTime = Util.EnvironmentTickCount();
527 550
528 // If there were collisions, process them by sending the event to the prim. 551 // If there were collisions, process them by sending the event to the prim.
@@ -562,12 +585,16 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
562 585
563 // Objects that are done colliding are removed from the ObjectsWithCollisions list. 586 // Objects that are done colliding are removed from the ObjectsWithCollisions list.
564 // Not done above because it is inside an iteration of ObjectWithCollisions. 587 // Not done above because it is inside an iteration of ObjectWithCollisions.
588 // This complex collision processing is required to create an empty collision
589 // event call after all real collisions have happened on an object. This enables
590 // the simulator to generate the 'collision end' event.
565 if (ObjectsWithNoMoreCollisions.Count > 0) 591 if (ObjectsWithNoMoreCollisions.Count > 0)
566 { 592 {
567 foreach (BSPhysObject po in ObjectsWithNoMoreCollisions) 593 foreach (BSPhysObject po in ObjectsWithNoMoreCollisions)
568 ObjectsWithCollisions.Remove(po); 594 ObjectsWithCollisions.Remove(po);
569 ObjectsWithNoMoreCollisions.Clear(); 595 ObjectsWithNoMoreCollisions.Clear();
570 } 596 }
597 // Done with collisions.
571 598
572 // If any of the objects had updated properties, tell the object it has been changed by the physics engine 599 // If any of the objects had updated properties, tell the object it has been changed by the physics engine
573 if (updatedEntityCount > 0) 600 if (updatedEntityCount > 0)
@@ -583,17 +610,17 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
583 } 610 }
584 } 611 }
585 612
586 ProcessPostStepTaints(); 613 TriggerPostStepEvent(timeStep);
587 614
588 // This causes the unmanaged code to output ALL the values found in ALL the objects in the world. 615 // The following causes the unmanaged code to output ALL the values found in ALL the objects in the world.
589 // Only enable this in a limited test world with few objects. 616 // Only enable this in a limited test world with few objects.
590 // BulletSimAPI.DumpAllInfo2(World.ptr); // DEBUG DEBUG DEBUG 617 if (m_physicsPhysicalDumpEnabled)
618 PE.DumpAllInfo(World);
591 619
592 // The physics engine returns the number of milliseconds it simulated this call. 620 // The physics engine returns the number of milliseconds it simulated this call.
593 // These are summed and normalized to one second and divided by 1000 to give the reported physics FPS. 621 // These are summed and normalized to one second and divided by 1000 to give the reported physics FPS.
594 // We multiply by 55 to give a recognizable running rate (55 or less). 622 // Multiply by a fixed nominal frame rate to give a rate similar to the simulator (usually 55).
595 return numSubSteps * m_fixedTimeStep * 1000 * 55; 623 return (float)numSubSteps * m_fixedTimeStep * 1000f * NominalFrameRate;
596 // return timeStep * 1000 * 55;
597 } 624 }
598 625
599 // Something has collided 626 // Something has collided
@@ -639,12 +666,7 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
639 666
640 public override void SetWaterLevel(float baseheight) 667 public override void SetWaterLevel(float baseheight)
641 { 668 {
642 m_waterLevel = baseheight; 669 SimpleWaterLevel = baseheight;
643 }
644 // Someday....
645 public float GetWaterLevelAtXYZ(Vector3 loc)
646 {
647 return m_waterLevel;
648 } 670 }
649 671
650 public override void DeleteTerrain() 672 public override void DeleteTerrain()
@@ -681,6 +703,15 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
681 public override bool IsThreaded { get { return false; } } 703 public override bool IsThreaded { get { return false; } }
682 704
683 #region Taints 705 #region Taints
706 // The simulation execution order is:
707 // Simulate()
708 // DoOneTimeTaints
709 // TriggerPreStepEvent
710 // DoOneTimeTaints
711 // Step()
712 // ProcessAndForwardCollisions
713 // ProcessAndForwardPropertyUpdates
714 // TriggerPostStepEvent
684 715
685 // Calls to the PhysicsActors can't directly call into the physics engine 716 // Calls to the PhysicsActors can't directly call into the physics engine
686 // because it might be busy. We delay changes to a known time. 717 // because it might be busy. We delay changes to a known time.
@@ -707,58 +738,35 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
707 TaintedObject(ident, callback); 738 TaintedObject(ident, callback);
708 } 739 }
709 740
741 private void TriggerPreStepEvent(float timeStep)
742 {
743 PreStepAction actions = BeforeStep;
744 if (actions != null)
745 actions(timeStep);
746
747 }
748
749 private void TriggerPostStepEvent(float timeStep)
750 {
751 PreStepAction actions = AfterStep;
752 if (actions != null)
753 actions(timeStep);
754
755 }
756
710 // When someone tries to change a property on a BSPrim or BSCharacter, the object queues 757 // When someone tries to change a property on a BSPrim or BSCharacter, the object queues
711 // a callback into itself to do the actual property change. That callback is called 758 // a callback into itself to do the actual property change. That callback is called
712 // here just before the physics engine is called to step the simulation. 759 // here just before the physics engine is called to step the simulation.
713 public void ProcessTaints() 760 public void ProcessTaints()
714 { 761 {
715 InTaintTime = true; // Only used for debugging so locking is not necessary.
716 ProcessRegularTaints(); 762 ProcessRegularTaints();
717 ProcessPostTaintTaints(); 763 ProcessPostTaintTaints();
718 InTaintTime = false;
719 } 764 }
720 765
721 private void ProcessRegularTaints() 766 private void ProcessRegularTaints()
722 { 767 {
723 if (_taintOperations.Count > 0) // save allocating new list if there is nothing to process 768 if (_taintOperations.Count > 0) // save allocating new list if there is nothing to process
724 { 769 {
725 /*
726 // Code to limit the number of taints processed per step. Meant to limit step time.
727 // Unsure if a good idea as code assumes that taints are done before the step.
728 int taintCount = m_taintsToProcessPerStep;
729 TaintCallbackEntry oneCallback = new TaintCallbackEntry();
730 while (_taintOperations.Count > 0 && taintCount-- > 0)
731 {
732 bool gotOne = false;
733 lock (_taintLock)
734 {
735 if (_taintOperations.Count > 0)
736 {
737 oneCallback = _taintOperations[0];
738 _taintOperations.RemoveAt(0);
739 gotOne = true;
740 }
741 }
742 if (gotOne)
743 {
744 try
745 {
746 DetailLog("{0},BSScene.ProcessTaints,doTaint,id={1}", DetailLogZero, oneCallback.ident);
747 oneCallback.callback();
748 }
749 catch (Exception e)
750 {
751 DetailLog("{0},BSScene.ProcessTaints,doTaintException,id={1}", DetailLogZero, oneCallback.ident); // DEBUG DEBUG DEBUG
752 m_log.ErrorFormat("{0}: ProcessTaints: {1}: Exception: {2}", LogHeader, oneCallback.ident, e);
753 }
754 }
755 }
756 if (_taintOperations.Count > 0)
757 {
758 DetailLog("{0},BSScene.ProcessTaints,leftTaintsOnList,numNotProcessed={1}", DetailLogZero, _taintOperations.Count);
759 }
760 */
761
762 // swizzle a new list into the list location so we can process what's there 770 // swizzle a new list into the list location so we can process what's there
763 List<TaintCallbackEntry> oldList; 771 List<TaintCallbackEntry> oldList;
764 lock (_taintLock) 772 lock (_taintLock)
@@ -797,6 +805,7 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
797 return; 805 return;
798 } 806 }
799 807
808 // Taints that happen after the normal taint processing but before the simulation step.
800 private void ProcessPostTaintTaints() 809 private void ProcessPostTaintTaints()
801 { 810 {
802 if (_postTaintOperations.Count > 0) 811 if (_postTaintOperations.Count > 0)
@@ -824,45 +833,6 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
824 } 833 }
825 } 834 }
826 835
827 public void PostStepTaintObject(String ident, TaintCallback callback)
828 {
829 if (!m_initialized) return;
830
831 lock (_taintLock)
832 {
833 _postStepOperations.Add(new TaintCallbackEntry(ident, callback));
834 }
835
836 return;
837 }
838
839 private void ProcessPostStepTaints()
840 {
841 if (_postStepOperations.Count > 0)
842 {
843 List<TaintCallbackEntry> oldList;
844 lock (_taintLock)
845 {
846 oldList = _postStepOperations;
847 _postStepOperations = new List<TaintCallbackEntry>();
848 }
849
850 foreach (TaintCallbackEntry tcbe in oldList)
851 {
852 try
853 {
854 DetailLog("{0},BSScene.ProcessPostStepTaints,doTaint,id={1}", DetailLogZero, tcbe.ident); // DEBUG DEBUG DEBUG
855 tcbe.callback();
856 }
857 catch (Exception e)
858 {
859 m_log.ErrorFormat("{0}: ProcessPostStepTaints: {1}: Exception: {2}", LogHeader, tcbe.ident, e);
860 }
861 }
862 oldList.Clear();
863 }
864 }
865
866 // Only used for debugging. Does not change state of anything so locking is not necessary. 836 // Only used for debugging. Does not change state of anything so locking is not necessary.
867 public bool AssertInTaintTime(string whereFrom) 837 public bool AssertInTaintTime(string whereFrom)
868 { 838 {
@@ -870,517 +840,21 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
870 { 840 {
871 DetailLog("{0},BSScene.AssertInTaintTime,NOT IN TAINT TIME,Region={1},Where={2}", DetailLogZero, RegionName, whereFrom); 841 DetailLog("{0},BSScene.AssertInTaintTime,NOT IN TAINT TIME,Region={1},Where={2}", DetailLogZero, RegionName, whereFrom);
872 m_log.ErrorFormat("{0} NOT IN TAINT TIME!! Region={1}, Where={2}", LogHeader, RegionName, whereFrom); 842 m_log.ErrorFormat("{0} NOT IN TAINT TIME!! Region={1}, Where={2}", LogHeader, RegionName, whereFrom);
873 Util.PrintCallStack(); // Prints the stack into the DEBUG log file. 843 Util.PrintCallStack(DetailLog);
874 } 844 }
875 return InTaintTime; 845 return InTaintTime;
876 } 846 }
877 847
878 #endregion // Taints 848 #endregion // Taints
879 849
880 #region Vehicles
881
882 public void VehicleInSceneTypeChanged(BSPrim vehic, Vehicle newType)
883 {
884 RemoveVehiclePrim(vehic);
885 if (newType != Vehicle.TYPE_NONE)
886 {
887 // make it so the scene will call us each tick to do vehicle things
888 AddVehiclePrim(vehic);
889 }
890 }
891
892 // Make so the scene will call this prim for vehicle actions each tick.
893 // Safe to call if prim is already in the vehicle list.
894 public void AddVehiclePrim(BSPrim vehicle)
895 {
896 lock (m_vehicles)
897 {
898 if (!m_vehicles.Contains(vehicle))
899 {
900 m_vehicles.Add(vehicle);
901 }
902 }
903 }
904
905 // Remove a prim from our list of vehicles.
906 // Safe to call if the prim is not in the vehicle list.
907 public void RemoveVehiclePrim(BSPrim vehicle)
908 {
909 lock (m_vehicles)
910 {
911 if (m_vehicles.Contains(vehicle))
912 {
913 m_vehicles.Remove(vehicle);
914 }
915 }
916 }
917
918 // Some prims have extra vehicle actions
919 // Called at taint time!
920 private void ProcessVehicles(float timeStep)
921 {
922 foreach (BSPhysObject pobj in m_vehicles)
923 {
924 pobj.StepVehicle(timeStep);
925 }
926 }
927 #endregion Vehicles
928
929 #region INI and command line parameter processing 850 #region INI and command line parameter processing
930 851
931 delegate void ParamUser(BSScene scene, IConfig conf, string paramName, float val);
932 delegate float ParamGet(BSScene scene);
933 delegate void ParamSet(BSScene scene, string paramName, uint localID, float val);
934 delegate void SetOnObject(BSScene scene, BSPhysObject obj, float val);
935
936 private struct ParameterDefn
937 {
938 public string name; // string name of the parameter
939 public string desc; // a short description of what the parameter means
940 public float defaultValue; // default value if not specified anywhere else
941 public ParamUser userParam; // get the value from the configuration file
942 public ParamGet getter; // return the current value stored for this parameter
943 public ParamSet setter; // set the current value for this parameter
944 public SetOnObject onObject; // set the value on an object in the physical domain
945 public ParameterDefn(string n, string d, float v, ParamUser u, ParamGet g, ParamSet s)
946 {
947 name = n;
948 desc = d;
949 defaultValue = v;
950 userParam = u;
951 getter = g;
952 setter = s;
953 onObject = null;
954 }
955 public ParameterDefn(string n, string d, float v, ParamUser u, ParamGet g, ParamSet s, SetOnObject o)
956 {
957 name = n;
958 desc = d;
959 defaultValue = v;
960 userParam = u;
961 getter = g;
962 setter = s;
963 onObject = o;
964 }
965 }
966
967 // List of all of the externally visible parameters.
968 // For each parameter, this table maps a text name to getter and setters.
969 // To add a new externally referencable/settable parameter, add the paramter storage
970 // location somewhere in the program and make an entry in this table with the
971 // getters and setters.
972 // It is easiest to find an existing definition and copy it.
973 // Parameter values are floats. Booleans are converted to a floating value.
974 //
975 // A ParameterDefn() takes the following parameters:
976 // -- the text name of the parameter. This is used for console input and ini file.
977 // -- a short text description of the parameter. This shows up in the console listing.
978 // -- a delegate for fetching the parameter from the ini file.
979 // Should handle fetching the right type from the ini file and converting it.
980 // -- a delegate for getting the value as a float
981 // -- a delegate for setting the value from a float
982 //
983 // The single letter parameters for the delegates are:
984 // s = BSScene
985 // o = BSPhysObject
986 // p = string parameter name
987 // l = localID of referenced object
988 // v = float value
989 // cf = parameter configuration class (for fetching values from ini file)
990 private ParameterDefn[] ParameterDefinitions =
991 {
992 new ParameterDefn("MeshSculptedPrim", "Whether to create meshes for sculpties",
993 ConfigurationParameters.numericTrue,
994 (s,cf,p,v) => { s.ShouldMeshSculptedPrim = cf.GetBoolean(p, s.BoolNumeric(v)); },
995 (s) => { return s.NumericBool(s.ShouldMeshSculptedPrim); },
996 (s,p,l,v) => { s.ShouldMeshSculptedPrim = s.BoolNumeric(v); } ),
997 new ParameterDefn("ForceSimplePrimMeshing", "If true, only use primitive meshes for objects",
998 ConfigurationParameters.numericFalse,
999 (s,cf,p,v) => { s.ShouldForceSimplePrimMeshing = cf.GetBoolean(p, s.BoolNumeric(v)); },
1000 (s) => { return s.NumericBool(s.ShouldForceSimplePrimMeshing); },
1001 (s,p,l,v) => { s.ShouldForceSimplePrimMeshing = s.BoolNumeric(v); } ),
1002 new ParameterDefn("UseHullsForPhysicalObjects", "If true, create hulls for physical objects",
1003 ConfigurationParameters.numericTrue,
1004 (s,cf,p,v) => { s.ShouldUseHullsForPhysicalObjects = cf.GetBoolean(p, s.BoolNumeric(v)); },
1005 (s) => { return s.NumericBool(s.ShouldUseHullsForPhysicalObjects); },
1006 (s,p,l,v) => { s.ShouldUseHullsForPhysicalObjects = s.BoolNumeric(v); } ),
1007
1008 new ParameterDefn("MeshLevelOfDetail", "Level of detail to render meshes (32, 16, 8 or 4. 32=most detailed)",
1009 8f,
1010 (s,cf,p,v) => { s.MeshLOD = (float)cf.GetInt(p, (int)v); },
1011 (s) => { return s.MeshLOD; },
1012 (s,p,l,v) => { s.MeshLOD = v; } ),
1013 new ParameterDefn("MeshLevelOfDetailMegaPrim", "Level of detail to render meshes larger than threshold meters",
1014 16f,
1015 (s,cf,p,v) => { s.MeshMegaPrimLOD = (float)cf.GetInt(p, (int)v); },
1016 (s) => { return s.MeshMegaPrimLOD; },
1017 (s,p,l,v) => { s.MeshMegaPrimLOD = v; } ),
1018 new ParameterDefn("MeshLevelOfDetailMegaPrimThreshold", "Size (in meters) of a mesh before using MeshMegaPrimLOD",
1019 10f,
1020 (s,cf,p,v) => { s.MeshMegaPrimThreshold = (float)cf.GetInt(p, (int)v); },
1021 (s) => { return s.MeshMegaPrimThreshold; },
1022 (s,p,l,v) => { s.MeshMegaPrimThreshold = v; } ),
1023 new ParameterDefn("SculptLevelOfDetail", "Level of detail to render sculpties (32, 16, 8 or 4. 32=most detailed)",
1024 32f,
1025 (s,cf,p,v) => { s.SculptLOD = (float)cf.GetInt(p, (int)v); },
1026 (s) => { return s.SculptLOD; },
1027 (s,p,l,v) => { s.SculptLOD = v; } ),
1028
1029 new ParameterDefn("MaxSubStep", "In simulation step, maximum number of substeps",
1030 10f,
1031 (s,cf,p,v) => { s.m_maxSubSteps = cf.GetInt(p, (int)v); },
1032 (s) => { return (float)s.m_maxSubSteps; },
1033 (s,p,l,v) => { s.m_maxSubSteps = (int)v; } ),
1034 new ParameterDefn("FixedTimeStep", "In simulation step, seconds of one substep (1/60)",
1035 1f / 60f,
1036 (s,cf,p,v) => { s.m_fixedTimeStep = cf.GetFloat(p, v); },
1037 (s) => { return (float)s.m_fixedTimeStep; },
1038 (s,p,l,v) => { s.m_fixedTimeStep = v; } ),
1039 new ParameterDefn("MaxCollisionsPerFrame", "Max collisions returned at end of each frame",
1040 2048f,
1041 (s,cf,p,v) => { s.m_maxCollisionsPerFrame = cf.GetInt(p, (int)v); },
1042 (s) => { return (float)s.m_maxCollisionsPerFrame; },
1043 (s,p,l,v) => { s.m_maxCollisionsPerFrame = (int)v; } ),
1044 new ParameterDefn("MaxUpdatesPerFrame", "Max updates returned at end of each frame",
1045 8000f,
1046 (s,cf,p,v) => { s.m_maxUpdatesPerFrame = cf.GetInt(p, (int)v); },
1047 (s) => { return (float)s.m_maxUpdatesPerFrame; },
1048 (s,p,l,v) => { s.m_maxUpdatesPerFrame = (int)v; } ),
1049 new ParameterDefn("MaxTaintsToProcessPerStep", "Number of update taints to process before each simulation step",
1050 500f,
1051 (s,cf,p,v) => { s.m_taintsToProcessPerStep = cf.GetInt(p, (int)v); },
1052 (s) => { return (float)s.m_taintsToProcessPerStep; },
1053 (s,p,l,v) => { s.m_taintsToProcessPerStep = (int)v; } ),
1054 new ParameterDefn("MaxObjectMass", "Maximum object mass (10000.01)",
1055 10000.01f,
1056 (s,cf,p,v) => { s.MaximumObjectMass = cf.GetFloat(p, v); },
1057 (s) => { return (float)s.MaximumObjectMass; },
1058 (s,p,l,v) => { s.MaximumObjectMass = v; } ),
1059
1060 new ParameterDefn("PID_D", "Derivitive factor for motion smoothing",
1061 2200f,
1062 (s,cf,p,v) => { s.PID_D = cf.GetFloat(p, v); },
1063 (s) => { return (float)s.PID_D; },
1064 (s,p,l,v) => { s.PID_D = v; } ),
1065 new ParameterDefn("PID_P", "Parameteric factor for motion smoothing",
1066 900f,
1067 (s,cf,p,v) => { s.PID_P = cf.GetFloat(p, v); },
1068 (s) => { return (float)s.PID_P; },
1069 (s,p,l,v) => { s.PID_P = v; } ),
1070
1071 new ParameterDefn("DefaultFriction", "Friction factor used on new objects",
1072 0.5f,
1073 (s,cf,p,v) => { s.m_params[0].defaultFriction = cf.GetFloat(p, v); },
1074 (s) => { return s.m_params[0].defaultFriction; },
1075 (s,p,l,v) => { s.m_params[0].defaultFriction = v; } ),
1076 new ParameterDefn("DefaultDensity", "Density for new objects" ,
1077 10.000006836f, // Aluminum g/cm3
1078 (s,cf,p,v) => { s.m_params[0].defaultDensity = cf.GetFloat(p, v); },
1079 (s) => { return s.m_params[0].defaultDensity; },
1080 (s,p,l,v) => { s.m_params[0].defaultDensity = v; } ),
1081 new ParameterDefn("DefaultRestitution", "Bouncyness of an object" ,
1082 0f,
1083 (s,cf,p,v) => { s.m_params[0].defaultRestitution = cf.GetFloat(p, v); },
1084 (s) => { return s.m_params[0].defaultRestitution; },
1085 (s,p,l,v) => { s.m_params[0].defaultRestitution = v; } ),
1086 new ParameterDefn("CollisionMargin", "Margin around objects before collisions are calculated (must be zero!)",
1087 0f,
1088 (s,cf,p,v) => { s.m_params[0].collisionMargin = cf.GetFloat(p, v); },
1089 (s) => { return s.m_params[0].collisionMargin; },
1090 (s,p,l,v) => { s.m_params[0].collisionMargin = v; } ),
1091 new ParameterDefn("Gravity", "Vertical force of gravity (negative means down)",
1092 -9.80665f,
1093 (s,cf,p,v) => { s.m_params[0].gravity = cf.GetFloat(p, v); },
1094 (s) => { return s.m_params[0].gravity; },
1095 (s,p,l,v) => { s.UpdateParameterObject(ref s.m_params[0].gravity, p, PhysParameterEntry.APPLY_TO_NONE, v); },
1096 (s,o,v) => { BulletSimAPI.SetGravity2(s.World.ptr, new Vector3(0f,0f,v)); } ),
1097
1098
1099 new ParameterDefn("LinearDamping", "Factor to damp linear movement per second (0.0 - 1.0)",
1100 0f,
1101 (s,cf,p,v) => { s.m_params[0].linearDamping = cf.GetFloat(p, v); },
1102 (s) => { return s.m_params[0].linearDamping; },
1103 (s,p,l,v) => { s.UpdateParameterObject(ref s.m_params[0].linearDamping, p, l, v); },
1104 (s,o,v) => { BulletSimAPI.SetDamping2(o.PhysBody.ptr, v, s.m_params[0].angularDamping); } ),
1105 new ParameterDefn("AngularDamping", "Factor to damp angular movement per second (0.0 - 1.0)",
1106 0f,
1107 (s,cf,p,v) => { s.m_params[0].angularDamping = cf.GetFloat(p, v); },
1108 (s) => { return s.m_params[0].angularDamping; },
1109 (s,p,l,v) => { s.UpdateParameterObject(ref s.m_params[0].angularDamping, p, l, v); },
1110 (s,o,v) => { BulletSimAPI.SetDamping2(o.PhysBody.ptr, s.m_params[0].linearDamping, v); } ),
1111 new ParameterDefn("DeactivationTime", "Seconds before considering an object potentially static",
1112 0.2f,
1113 (s,cf,p,v) => { s.m_params[0].deactivationTime = cf.GetFloat(p, v); },
1114 (s) => { return s.m_params[0].deactivationTime; },
1115 (s,p,l,v) => { s.UpdateParameterObject(ref s.m_params[0].deactivationTime, p, l, v); },
1116 (s,o,v) => { BulletSimAPI.SetDeactivationTime2(o.PhysBody.ptr, v); } ),
1117 new ParameterDefn("LinearSleepingThreshold", "Seconds to measure linear movement before considering static",
1118 0.8f,
1119 (s,cf,p,v) => { s.m_params[0].linearSleepingThreshold = cf.GetFloat(p, v); },
1120 (s) => { return s.m_params[0].linearSleepingThreshold; },
1121 (s,p,l,v) => { s.UpdateParameterObject(ref s.m_params[0].linearSleepingThreshold, p, l, v); },
1122 (s,o,v) => { BulletSimAPI.SetSleepingThresholds2(o.PhysBody.ptr, v, v); } ),
1123 new ParameterDefn("AngularSleepingThreshold", "Seconds to measure angular movement before considering static",
1124 1.0f,
1125 (s,cf,p,v) => { s.m_params[0].angularSleepingThreshold = cf.GetFloat(p, v); },
1126 (s) => { return s.m_params[0].angularSleepingThreshold; },
1127 (s,p,l,v) => { s.UpdateParameterObject(ref s.m_params[0].angularSleepingThreshold, p, l, v); },
1128 (s,o,v) => { BulletSimAPI.SetSleepingThresholds2(o.PhysBody.ptr, v, v); } ),
1129 new ParameterDefn("CcdMotionThreshold", "Continuious collision detection threshold (0 means no CCD)" ,
1130 0f, // set to zero to disable
1131 (s,cf,p,v) => { s.m_params[0].ccdMotionThreshold = cf.GetFloat(p, v); },
1132 (s) => { return s.m_params[0].ccdMotionThreshold; },
1133 (s,p,l,v) => { s.UpdateParameterObject(ref s.m_params[0].ccdMotionThreshold, p, l, v); },
1134 (s,o,v) => { BulletSimAPI.SetCcdMotionThreshold2(o.PhysBody.ptr, v); } ),
1135 new ParameterDefn("CcdSweptSphereRadius", "Continuious collision detection test radius" ,
1136 0f,
1137 (s,cf,p,v) => { s.m_params[0].ccdSweptSphereRadius = cf.GetFloat(p, v); },
1138 (s) => { return s.m_params[0].ccdSweptSphereRadius; },
1139 (s,p,l,v) => { s.UpdateParameterObject(ref s.m_params[0].ccdSweptSphereRadius, p, l, v); },
1140 (s,o,v) => { BulletSimAPI.SetCcdSweptSphereRadius2(o.PhysBody.ptr, v); } ),
1141 new ParameterDefn("ContactProcessingThreshold", "Distance between contacts before doing collision check" ,
1142 0.1f,
1143 (s,cf,p,v) => { s.m_params[0].contactProcessingThreshold = cf.GetFloat(p, v); },
1144 (s) => { return s.m_params[0].contactProcessingThreshold; },
1145 (s,p,l,v) => { s.UpdateParameterObject(ref s.m_params[0].contactProcessingThreshold, p, l, v); },
1146 (s,o,v) => { BulletSimAPI.SetContactProcessingThreshold2(o.PhysBody.ptr, v); } ),
1147
1148 new ParameterDefn("TerrainImplementation", "Type of shape to use for terrain (0=heightmap, 1=mesh)",
1149 (float)BSTerrainPhys.TerrainImplementation.Mesh,
1150 (s,cf,p,v) => { s.m_params[0].terrainImplementation = cf.GetFloat(p,v); },
1151 (s) => { return s.m_params[0].terrainImplementation; },
1152 (s,p,l,v) => { s.m_params[0].terrainImplementation = v; } ),
1153 new ParameterDefn("TerrainFriction", "Factor to reduce movement against terrain surface" ,
1154 0.5f,
1155 (s,cf,p,v) => { s.m_params[0].terrainFriction = cf.GetFloat(p, v); },
1156 (s) => { return s.m_params[0].terrainFriction; },
1157 (s,p,l,v) => { s.m_params[0].terrainFriction = v; /* TODO: set on real terrain */} ),
1158 new ParameterDefn("TerrainHitFraction", "Distance to measure hit collisions" ,
1159 0.8f,
1160 (s,cf,p,v) => { s.m_params[0].terrainHitFraction = cf.GetFloat(p, v); },
1161 (s) => { return s.m_params[0].terrainHitFraction; },
1162 (s,p,l,v) => { s.m_params[0].terrainHitFraction = v; /* TODO: set on real terrain */ } ),
1163 new ParameterDefn("TerrainRestitution", "Bouncyness" ,
1164 0f,
1165 (s,cf,p,v) => { s.m_params[0].terrainRestitution = cf.GetFloat(p, v); },
1166 (s) => { return s.m_params[0].terrainRestitution; },
1167 (s,p,l,v) => { s.m_params[0].terrainRestitution = v; /* TODO: set on real terrain */ } ),
1168 new ParameterDefn("AvatarFriction", "Factor to reduce movement against an avatar. Changed on avatar recreation.",
1169 0.2f,
1170 (s,cf,p,v) => { s.m_params[0].avatarFriction = cf.GetFloat(p, v); },
1171 (s) => { return s.m_params[0].avatarFriction; },
1172 (s,p,l,v) => { s.UpdateParameterObject(ref s.m_params[0].avatarFriction, p, l, v); } ),
1173 new ParameterDefn("AvatarStandingFriction", "Avatar friction when standing. Changed on avatar recreation.",
1174 10f,
1175 (s,cf,p,v) => { s.m_params[0].avatarStandingFriction = cf.GetFloat(p, v); },
1176 (s) => { return s.m_params[0].avatarStandingFriction; },
1177 (s,p,l,v) => { s.m_params[0].avatarStandingFriction = v; } ),
1178 new ParameterDefn("AvatarDensity", "Density of an avatar. Changed on avatar recreation.",
1179 60f,
1180 (s,cf,p,v) => { s.m_params[0].avatarDensity = cf.GetFloat(p, v); },
1181 (s) => { return s.m_params[0].avatarDensity; },
1182 (s,p,l,v) => { s.UpdateParameterObject(ref s.m_params[0].avatarDensity, p, l, v); } ),
1183 new ParameterDefn("AvatarRestitution", "Bouncyness. Changed on avatar recreation.",
1184 0f,
1185 (s,cf,p,v) => { s.m_params[0].avatarRestitution = cf.GetFloat(p, v); },
1186 (s) => { return s.m_params[0].avatarRestitution; },
1187 (s,p,l,v) => { s.UpdateParameterObject(ref s.m_params[0].avatarRestitution, p, l, v); } ),
1188 new ParameterDefn("AvatarCapsuleWidth", "The distance between the sides of the avatar capsule",
1189 0.6f,
1190 (s,cf,p,v) => { s.m_params[0].avatarCapsuleWidth = cf.GetFloat(p, v); },
1191 (s) => { return s.m_params[0].avatarCapsuleWidth; },
1192 (s,p,l,v) => { s.UpdateParameterObject(ref s.m_params[0].avatarCapsuleWidth, p, l, v); } ),
1193 new ParameterDefn("AvatarCapsuleDepth", "The distance between the front and back of the avatar capsule",
1194 0.45f,
1195 (s,cf,p,v) => { s.m_params[0].avatarCapsuleDepth = cf.GetFloat(p, v); },
1196 (s) => { return s.m_params[0].avatarCapsuleDepth; },
1197 (s,p,l,v) => { s.UpdateParameterObject(ref s.m_params[0].avatarCapsuleDepth, p, l, v); } ),
1198 new ParameterDefn("AvatarCapsuleHeight", "Default height of space around avatar",
1199 1.5f,
1200 (s,cf,p,v) => { s.m_params[0].avatarCapsuleHeight = cf.GetFloat(p, v); },
1201 (s) => { return s.m_params[0].avatarCapsuleHeight; },
1202 (s,p,l,v) => { s.UpdateParameterObject(ref s.m_params[0].avatarCapsuleHeight, p, l, v); } ),
1203 new ParameterDefn("AvatarContactProcessingThreshold", "Distance from capsule to check for collisions",
1204 0.1f,
1205 (s,cf,p,v) => { s.m_params[0].avatarContactProcessingThreshold = cf.GetFloat(p, v); },
1206 (s) => { return s.m_params[0].avatarContactProcessingThreshold; },
1207 (s,p,l,v) => { s.UpdateParameterObject(ref s.m_params[0].avatarContactProcessingThreshold, p, l, v); } ),
1208
1209
1210 new ParameterDefn("MaxPersistantManifoldPoolSize", "Number of manifolds pooled (0 means default of 4096)",
1211 0f,
1212 (s,cf,p,v) => { s.m_params[0].maxPersistantManifoldPoolSize = cf.GetFloat(p, v); },
1213 (s) => { return s.m_params[0].maxPersistantManifoldPoolSize; },
1214 (s,p,l,v) => { s.m_params[0].maxPersistantManifoldPoolSize = v; } ),
1215 new ParameterDefn("MaxCollisionAlgorithmPoolSize", "Number of collisions pooled (0 means default of 4096)",
1216 0f,
1217 (s,cf,p,v) => { s.m_params[0].maxCollisionAlgorithmPoolSize = cf.GetFloat(p, v); },
1218 (s) => { return s.m_params[0].maxCollisionAlgorithmPoolSize; },
1219 (s,p,l,v) => { s.m_params[0].maxCollisionAlgorithmPoolSize = v; } ),
1220 new ParameterDefn("ShouldDisableContactPoolDynamicAllocation", "Enable to allow large changes in object count",
1221 ConfigurationParameters.numericFalse,
1222 (s,cf,p,v) => { s.m_params[0].shouldDisableContactPoolDynamicAllocation = s.NumericBool(cf.GetBoolean(p, s.BoolNumeric(v))); },
1223 (s) => { return s.m_params[0].shouldDisableContactPoolDynamicAllocation; },
1224 (s,p,l,v) => { s.m_params[0].shouldDisableContactPoolDynamicAllocation = v; } ),
1225 new ParameterDefn("ShouldForceUpdateAllAabbs", "Enable to recomputer AABBs every simulator step",
1226 ConfigurationParameters.numericFalse,
1227 (s,cf,p,v) => { s.m_params[0].shouldForceUpdateAllAabbs = s.NumericBool(cf.GetBoolean(p, s.BoolNumeric(v))); },
1228 (s) => { return s.m_params[0].shouldForceUpdateAllAabbs; },
1229 (s,p,l,v) => { s.m_params[0].shouldForceUpdateAllAabbs = v; } ),
1230 new ParameterDefn("ShouldRandomizeSolverOrder", "Enable for slightly better stacking interaction",
1231 ConfigurationParameters.numericTrue,
1232 (s,cf,p,v) => { s.m_params[0].shouldRandomizeSolverOrder = s.NumericBool(cf.GetBoolean(p, s.BoolNumeric(v))); },
1233 (s) => { return s.m_params[0].shouldRandomizeSolverOrder; },
1234 (s,p,l,v) => { s.m_params[0].shouldRandomizeSolverOrder = v; } ),
1235 new ParameterDefn("ShouldSplitSimulationIslands", "Enable splitting active object scanning islands",
1236 ConfigurationParameters.numericTrue,
1237 (s,cf,p,v) => { s.m_params[0].shouldSplitSimulationIslands = s.NumericBool(cf.GetBoolean(p, s.BoolNumeric(v))); },
1238 (s) => { return s.m_params[0].shouldSplitSimulationIslands; },
1239 (s,p,l,v) => { s.m_params[0].shouldSplitSimulationIslands = v; } ),
1240 new ParameterDefn("ShouldEnableFrictionCaching", "Enable friction computation caching",
1241 ConfigurationParameters.numericFalse,
1242 (s,cf,p,v) => { s.m_params[0].shouldEnableFrictionCaching = s.NumericBool(cf.GetBoolean(p, s.BoolNumeric(v))); },
1243 (s) => { return s.m_params[0].shouldEnableFrictionCaching; },
1244 (s,p,l,v) => { s.m_params[0].shouldEnableFrictionCaching = v; } ),
1245 new ParameterDefn("NumberOfSolverIterations", "Number of internal iterations (0 means default)",
1246 0f, // zero says use Bullet default
1247 (s,cf,p,v) => { s.m_params[0].numberOfSolverIterations = cf.GetFloat(p, v); },
1248 (s) => { return s.m_params[0].numberOfSolverIterations; },
1249 (s,p,l,v) => { s.m_params[0].numberOfSolverIterations = v; } ),
1250
1251 new ParameterDefn("LinksetImplementation", "Type of linkset implementation (0=Constraint, 1=Compound, 2=Manual)",
1252 (float)BSLinkset.LinksetImplementation.Compound,
1253 (s,cf,p,v) => { s.m_params[0].linksetImplementation = cf.GetFloat(p,v); },
1254 (s) => { return s.m_params[0].linksetImplementation; },
1255 (s,p,l,v) => { s.m_params[0].linksetImplementation = v; } ),
1256 new ParameterDefn("LinkConstraintUseFrameOffset", "For linksets built with constraints, enable frame offsetFor linksets built with constraints, enable frame offset.",
1257 ConfigurationParameters.numericFalse,
1258 (s,cf,p,v) => { s.m_params[0].linkConstraintUseFrameOffset = s.NumericBool(cf.GetBoolean(p, s.BoolNumeric(v))); },
1259 (s) => { return s.m_params[0].linkConstraintUseFrameOffset; },
1260 (s,p,l,v) => { s.m_params[0].linkConstraintUseFrameOffset = v; } ),
1261 new ParameterDefn("LinkConstraintEnableTransMotor", "Whether to enable translational motor on linkset constraints",
1262 ConfigurationParameters.numericTrue,
1263 (s,cf,p,v) => { s.m_params[0].linkConstraintEnableTransMotor = s.NumericBool(cf.GetBoolean(p, s.BoolNumeric(v))); },
1264 (s) => { return s.m_params[0].linkConstraintEnableTransMotor; },
1265 (s,p,l,v) => { s.m_params[0].linkConstraintEnableTransMotor = v; } ),
1266 new ParameterDefn("LinkConstraintTransMotorMaxVel", "Maximum velocity to be applied by translational motor in linkset constraints",
1267 5.0f,
1268 (s,cf,p,v) => { s.m_params[0].linkConstraintTransMotorMaxVel = cf.GetFloat(p, v); },
1269 (s) => { return s.m_params[0].linkConstraintTransMotorMaxVel; },
1270 (s,p,l,v) => { s.m_params[0].linkConstraintTransMotorMaxVel = v; } ),
1271 new ParameterDefn("LinkConstraintTransMotorMaxForce", "Maximum force to be applied by translational motor in linkset constraints",
1272 0.1f,
1273 (s,cf,p,v) => { s.m_params[0].linkConstraintTransMotorMaxForce = cf.GetFloat(p, v); },
1274 (s) => { return s.m_params[0].linkConstraintTransMotorMaxForce; },
1275 (s,p,l,v) => { s.m_params[0].linkConstraintTransMotorMaxForce = v; } ),
1276 new ParameterDefn("LinkConstraintCFM", "Amount constraint can be violated. 0=no violation, 1=infinite. Default=0.1",
1277 0.1f,
1278 (s,cf,p,v) => { s.m_params[0].linkConstraintCFM = cf.GetFloat(p, v); },
1279 (s) => { return s.m_params[0].linkConstraintCFM; },
1280 (s,p,l,v) => { s.m_params[0].linkConstraintCFM = v; } ),
1281 new ParameterDefn("LinkConstraintERP", "Amount constraint is corrected each tick. 0=none, 1=all. Default = 0.2",
1282 0.1f,
1283 (s,cf,p,v) => { s.m_params[0].linkConstraintERP = cf.GetFloat(p, v); },
1284 (s) => { return s.m_params[0].linkConstraintERP; },
1285 (s,p,l,v) => { s.m_params[0].linkConstraintERP = v; } ),
1286 new ParameterDefn("LinkConstraintSolverIterations", "Number of solver iterations when computing constraint. (0 = Bullet default)",
1287 40,
1288 (s,cf,p,v) => { s.m_params[0].linkConstraintSolverIterations = cf.GetFloat(p, v); },
1289 (s) => { return s.m_params[0].linkConstraintSolverIterations; },
1290 (s,p,l,v) => { s.m_params[0].linkConstraintSolverIterations = v; } ),
1291
1292 new ParameterDefn("LogPhysicsStatisticsFrames", "Frames between outputting detailed phys stats. (0 is off)",
1293 0f,
1294 (s,cf,p,v) => { s.m_params[0].physicsLoggingFrames = cf.GetInt(p, (int)v); },
1295 (s) => { return (float)s.m_params[0].physicsLoggingFrames; },
1296 (s,p,l,v) => { s.m_params[0].physicsLoggingFrames = (int)v; } ),
1297 };
1298
1299 // Convert a boolean to our numeric true and false values
1300 public float NumericBool(bool b)
1301 {
1302 return (b ? ConfigurationParameters.numericTrue : ConfigurationParameters.numericFalse);
1303 }
1304
1305 // Convert numeric true and false values to a boolean
1306 public bool BoolNumeric(float b)
1307 {
1308 return (b == ConfigurationParameters.numericTrue ? true : false);
1309 }
1310
1311 // Search through the parameter definitions and return the matching
1312 // ParameterDefn structure.
1313 // Case does not matter as names are compared after converting to lower case.
1314 // Returns 'false' if the parameter is not found.
1315 private bool TryGetParameter(string paramName, out ParameterDefn defn)
1316 {
1317 bool ret = false;
1318 ParameterDefn foundDefn = new ParameterDefn();
1319 string pName = paramName.ToLower();
1320
1321 foreach (ParameterDefn parm in ParameterDefinitions)
1322 {
1323 if (pName == parm.name.ToLower())
1324 {
1325 foundDefn = parm;
1326 ret = true;
1327 break;
1328 }
1329 }
1330 defn = foundDefn;
1331 return ret;
1332 }
1333
1334 // Pass through the settable parameters and set the default values
1335 private void SetParameterDefaultValues()
1336 {
1337 foreach (ParameterDefn parm in ParameterDefinitions)
1338 {
1339 parm.setter(this, parm.name, PhysParameterEntry.APPLY_TO_NONE, parm.defaultValue);
1340 }
1341 }
1342
1343 // Get user set values out of the ini file.
1344 private void SetParameterConfigurationValues(IConfig cfg)
1345 {
1346 foreach (ParameterDefn parm in ParameterDefinitions)
1347 {
1348 parm.userParam(this, cfg, parm.name, parm.defaultValue);
1349 }
1350 }
1351
1352 private PhysParameterEntry[] SettableParameters = new PhysParameterEntry[1];
1353
1354 // This creates an array in the correct format for returning the list of
1355 // parameters. This is used by the 'list' option of the 'physics' command.
1356 private void BuildParameterTable()
1357 {
1358 if (SettableParameters.Length < ParameterDefinitions.Length)
1359 {
1360 List<PhysParameterEntry> entries = new List<PhysParameterEntry>();
1361 for (int ii = 0; ii < ParameterDefinitions.Length; ii++)
1362 {
1363 ParameterDefn pd = ParameterDefinitions[ii];
1364 entries.Add(new PhysParameterEntry(pd.name, pd.desc));
1365 }
1366
1367 // make the list in alphabetical order for estetic reasons
1368 entries.Sort(delegate(PhysParameterEntry ppe1, PhysParameterEntry ppe2)
1369 {
1370 return ppe1.name.CompareTo(ppe2.name);
1371 });
1372
1373 SettableParameters = entries.ToArray();
1374 }
1375 }
1376
1377
1378 #region IPhysicsParameters 852 #region IPhysicsParameters
1379 // Get the list of parameters this physics engine supports 853 // Get the list of parameters this physics engine supports
1380 public PhysParameterEntry[] GetParameterList() 854 public PhysParameterEntry[] GetParameterList()
1381 { 855 {
1382 BuildParameterTable(); 856 BSParam.BuildParameterTable();
1383 return SettableParameters; 857 return BSParam.SettableParameters;
1384 } 858 }
1385 859
1386 // Set parameter on a specific or all instances. 860 // Set parameter on a specific or all instances.
@@ -1392,8 +866,8 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
1392 public bool SetPhysicsParameter(string parm, float val, uint localID) 866 public bool SetPhysicsParameter(string parm, float val, uint localID)
1393 { 867 {
1394 bool ret = false; 868 bool ret = false;
1395 ParameterDefn theParam; 869 BSParam.ParameterDefn theParam;
1396 if (TryGetParameter(parm, out theParam)) 870 if (BSParam.TryGetParameter(parm, out theParam))
1397 { 871 {
1398 theParam.setter(this, parm, localID, val); 872 theParam.setter(this, parm, localID, val);
1399 ret = true; 873 ret = true;
@@ -1405,19 +879,20 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
1405 // If the local ID is APPLY_TO_NONE, just change the default value 879 // If the local ID is APPLY_TO_NONE, just change the default value
1406 // If the localID is APPLY_TO_ALL change the default value and apply the new value to all the lIDs 880 // If the localID is APPLY_TO_ALL change the default value and apply the new value to all the lIDs
1407 // If the localID is a specific object, apply the parameter change to only that object 881 // If the localID is a specific object, apply the parameter change to only that object
1408 private void UpdateParameterObject(ref float defaultLoc, string parm, uint localID, float val) 882 internal delegate void AssignVal(float x);
883 internal void UpdateParameterObject(AssignVal setDefault, string parm, uint localID, float val)
1409 { 884 {
1410 List<uint> objectIDs = new List<uint>(); 885 List<uint> objectIDs = new List<uint>();
1411 switch (localID) 886 switch (localID)
1412 { 887 {
1413 case PhysParameterEntry.APPLY_TO_NONE: 888 case PhysParameterEntry.APPLY_TO_NONE:
1414 defaultLoc = val; // setting only the default value 889 setDefault(val); // setting only the default value
1415 // This will cause a call into the physical world if some operation is specified (SetOnObject). 890 // This will cause a call into the physical world if some operation is specified (SetOnObject).
1416 objectIDs.Add(TERRAIN_ID); 891 objectIDs.Add(TERRAIN_ID);
1417 TaintedUpdateParameter(parm, objectIDs, val); 892 TaintedUpdateParameter(parm, objectIDs, val);
1418 break; 893 break;
1419 case PhysParameterEntry.APPLY_TO_ALL: 894 case PhysParameterEntry.APPLY_TO_ALL:
1420 defaultLoc = val; // setting ALL also sets the default value 895 setDefault(val); // setting ALL also sets the default value
1421 lock (PhysObjects) objectIDs = new List<uint>(PhysObjects.Keys); 896 lock (PhysObjects) objectIDs = new List<uint>(PhysObjects.Keys);
1422 TaintedUpdateParameter(parm, objectIDs, val); 897 TaintedUpdateParameter(parm, objectIDs, val);
1423 break; 898 break;
@@ -1436,8 +911,8 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
1436 List<uint> xlIDs = lIDs; 911 List<uint> xlIDs = lIDs;
1437 string xparm = parm; 912 string xparm = parm;
1438 TaintedObject("BSScene.UpdateParameterSet", delegate() { 913 TaintedObject("BSScene.UpdateParameterSet", delegate() {
1439 ParameterDefn thisParam; 914 BSParam.ParameterDefn thisParam;
1440 if (TryGetParameter(xparm, out thisParam)) 915 if (BSParam.TryGetParameter(xparm, out thisParam))
1441 { 916 {
1442 if (thisParam.onObject != null) 917 if (thisParam.onObject != null)
1443 { 918 {
@@ -1458,8 +933,8 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
1458 { 933 {
1459 float val = 0f; 934 float val = 0f;
1460 bool ret = false; 935 bool ret = false;
1461 ParameterDefn theParam; 936 BSParam.ParameterDefn theParam;
1462 if (TryGetParameter(parm, out theParam)) 937 if (BSParam.TryGetParameter(parm, out theParam))
1463 { 938 {
1464 val = theParam.getter(this); 939 val = theParam.getter(this);
1465 ret = true; 940 ret = true;
@@ -1472,22 +947,12 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
1472 947
1473 #endregion Runtime settable parameters 948 #endregion Runtime settable parameters
1474 949
1475 // Debugging routine for dumping detailed physical information for vehicle prims
1476 private void DumpVehicles()
1477 {
1478 foreach (BSPrim prim in m_vehicles)
1479 {
1480 BulletSimAPI.DumpRigidBody2(World.ptr, prim.PhysBody.ptr);
1481 BulletSimAPI.DumpCollisionShape2(World.ptr, prim.PhysShape.ptr);
1482 }
1483 }
1484
1485 // Invoke the detailed logger and output something if it's enabled. 950 // Invoke the detailed logger and output something if it's enabled.
1486 public void DetailLog(string msg, params Object[] args) 951 public void DetailLog(string msg, params Object[] args)
1487 { 952 {
1488 PhysicsLogging.Write(msg, args); 953 PhysicsLogging.Write(msg, args);
1489 // Add the Flush() if debugging crashes. Gets all the messages written out. 954 // Add the Flush() if debugging crashes. Gets all the messages written out.
1490 // PhysicsLogging.Flush(); 955 if (m_physicsLoggingDoFlush) PhysicsLogging.Flush();
1491 } 956 }
1492 // Used to fill in the LocalID when there isn't one. It's the correct number of characters. 957 // Used to fill in the LocalID when there isn't one. It's the correct number of characters.
1493 public const string DetailLogZero = "0000000000"; 958 public const string DetailLogZero = "0000000000";
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSShapeCollection.cs b/OpenSim/Region/Physics/BulletSPlugin/BSShapeCollection.cs
index 892c34b..838c845 100755
--- a/OpenSim/Region/Physics/BulletSPlugin/BSShapeCollection.cs
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSShapeCollection.cs
@@ -45,7 +45,7 @@ public sealed class BSShapeCollection : IDisposable
45 // Description of a Mesh 45 // Description of a Mesh
46 private struct MeshDesc 46 private struct MeshDesc
47 { 47 {
48 public IntPtr ptr; 48 public BulletShape shape;
49 public int referenceCount; 49 public int referenceCount;
50 public DateTime lastReferenced; 50 public DateTime lastReferenced;
51 public UInt64 shapeKey; 51 public UInt64 shapeKey;
@@ -55,7 +55,7 @@ public sealed class BSShapeCollection : IDisposable
55 // Meshes and hulls have the same shape hash key but we only need hulls for efficient collision calculations. 55 // Meshes and hulls have the same shape hash key but we only need hulls for efficient collision calculations.
56 private struct HullDesc 56 private struct HullDesc
57 { 57 {
58 public IntPtr ptr; 58 public BulletShape shape;
59 public int referenceCount; 59 public int referenceCount;
60 public DateTime lastReferenced; 60 public DateTime lastReferenced;
61 public UInt64 shapeKey; 61 public UInt64 shapeKey;
@@ -65,9 +65,16 @@ public sealed class BSShapeCollection : IDisposable
65 private Dictionary<System.UInt64, MeshDesc> Meshes = new Dictionary<System.UInt64, MeshDesc>(); 65 private Dictionary<System.UInt64, MeshDesc> Meshes = new Dictionary<System.UInt64, MeshDesc>();
66 private Dictionary<System.UInt64, HullDesc> Hulls = new Dictionary<System.UInt64, HullDesc>(); 66 private Dictionary<System.UInt64, HullDesc> Hulls = new Dictionary<System.UInt64, HullDesc>();
67 67
68 private bool DDetail = false;
69
68 public BSShapeCollection(BSScene physScene) 70 public BSShapeCollection(BSScene physScene)
69 { 71 {
70 PhysicsScene = physScene; 72 PhysicsScene = physScene;
73 // Set the next to 'true' for very detailed shape update detailed logging (detailed details?)
74 // While detailed debugging is still active, this is better than commenting out all the
75 // DetailLog statements. When debugging slows down, this and the protected logging
76 // statements can be commented/removed.
77 DDetail = true;
71 } 78 }
72 79
73 public void Dispose() 80 public void Dispose()
@@ -91,7 +98,7 @@ public sealed class BSShapeCollection : IDisposable
91 // higher level dependencies on the shape or body. Mostly used for LinkSets to 98 // higher level dependencies on the shape or body. Mostly used for LinkSets to
92 // remove the physical constraints before the body is destroyed. 99 // remove the physical constraints before the body is destroyed.
93 // Called at taint-time!! 100 // Called at taint-time!!
94 public bool GetBodyAndShape(bool forceRebuild, BulletSim sim, BSPhysObject prim, 101 public bool GetBodyAndShape(bool forceRebuild, BulletWorld sim, BSPhysObject prim,
95 ShapeDestructionCallback shapeCallback, BodyDestructionCallback bodyCallback) 102 ShapeDestructionCallback shapeCallback, BodyDestructionCallback bodyCallback)
96 { 103 {
97 PhysicsScene.AssertInTaintTime("BSShapeCollection.GetBodyAndShape"); 104 PhysicsScene.AssertInTaintTime("BSShapeCollection.GetBodyAndShape");
@@ -119,6 +126,11 @@ public sealed class BSShapeCollection : IDisposable
119 return ret; 126 return ret;
120 } 127 }
121 128
129 public bool GetBodyAndShape(bool forceRebuild, BulletWorld sim, BSPhysObject prim)
130 {
131 return GetBodyAndShape(forceRebuild, sim, prim, null, null);
132 }
133
122 // Track another user of a body. 134 // Track another user of a body.
123 // We presume the caller has allocated the body. 135 // We presume the caller has allocated the body.
124 // Bodies only have one user so the body is just put into the world if not already there. 136 // Bodies only have one user so the body is just put into the world if not already there.
@@ -126,13 +138,13 @@ public sealed class BSShapeCollection : IDisposable
126 { 138 {
127 lock (m_collectionActivityLock) 139 lock (m_collectionActivityLock)
128 { 140 {
129 DetailLog("{0},BSShapeCollection.ReferenceBody,newBody,body={1}", body.ID, body); 141 if (DDetail) DetailLog("{0},BSShapeCollection.ReferenceBody,newBody,body={1}", body.ID, body);
130 PhysicsScene.TaintedObject(inTaintTime, "BSShapeCollection.ReferenceBody", delegate() 142 PhysicsScene.TaintedObject(inTaintTime, "BSShapeCollection.ReferenceBody", delegate()
131 { 143 {
132 if (!BulletSimAPI.IsInWorld2(body.ptr)) 144 if (!PhysicsScene.PE.IsInWorld(PhysicsScene.World, body))
133 { 145 {
134 BulletSimAPI.AddObjectToWorld2(PhysicsScene.World.ptr, body.ptr); 146 PhysicsScene.PE.AddObjectToWorld(PhysicsScene.World, body);
135 DetailLog("{0},BSShapeCollection.ReferenceBody,addedToWorld,ref={1}", body.ID, body); 147 if (DDetail) DetailLog("{0},BSShapeCollection.ReferenceBody,addedToWorld,ref={1}", body.ID, body);
136 } 148 }
137 }); 149 });
138 } 150 }
@@ -142,27 +154,27 @@ public sealed class BSShapeCollection : IDisposable
142 // Called when releasing use of a BSBody. BSShape is handled separately. 154 // Called when releasing use of a BSBody. BSShape is handled separately.
143 public void DereferenceBody(BulletBody body, bool inTaintTime, BodyDestructionCallback bodyCallback ) 155 public void DereferenceBody(BulletBody body, bool inTaintTime, BodyDestructionCallback bodyCallback )
144 { 156 {
145 if (body.ptr == IntPtr.Zero) 157 if (!body.HasPhysicalBody)
146 return; 158 return;
147 159
148 lock (m_collectionActivityLock) 160 lock (m_collectionActivityLock)
149 { 161 {
150 PhysicsScene.TaintedObject(inTaintTime, "BSShapeCollection.DereferenceBody", delegate() 162 PhysicsScene.TaintedObject(inTaintTime, "BSShapeCollection.DereferenceBody", delegate()
151 { 163 {
152 DetailLog("{0},BSShapeCollection.DereferenceBody,DestroyingBody,body={1},inTaintTime={2}", 164 if (DDetail) DetailLog("{0},BSShapeCollection.DereferenceBody,DestroyingBody,body={1},inTaintTime={2}",
153 body.ID, body, inTaintTime); 165 body.ID, body, inTaintTime);
154 // If the caller needs to know the old body is going away, pass the event up. 166 // If the caller needs to know the old body is going away, pass the event up.
155 if (bodyCallback != null) bodyCallback(body); 167 if (bodyCallback != null) bodyCallback(body);
156 168
157 if (BulletSimAPI.IsInWorld2(body.ptr)) 169 if (PhysicsScene.PE.IsInWorld(PhysicsScene.World, body))
158 { 170 {
159 BulletSimAPI.RemoveObjectFromWorld2(PhysicsScene.World.ptr, body.ptr); 171 PhysicsScene.PE.RemoveObjectFromWorld(PhysicsScene.World, body);
160 DetailLog("{0},BSShapeCollection.DereferenceBody,removingFromWorld. Body={1}", body.ID, body); 172 if (DDetail) DetailLog("{0},BSShapeCollection.DereferenceBody,removingFromWorld. Body={1}", body.ID, body);
161 } 173 }
162 174
163 // Zero any reference to the shape so it is not freed when the body is deleted. 175 // Zero any reference to the shape so it is not freed when the body is deleted.
164 BulletSimAPI.SetCollisionShape2(PhysicsScene.World.ptr, body.ptr, IntPtr.Zero); 176 PhysicsScene.PE.SetCollisionShape(PhysicsScene.World, body, null);
165 BulletSimAPI.DestroyObject2(PhysicsScene.World.ptr, body.ptr); 177 PhysicsScene.PE.DestroyObject(PhysicsScene.World, body);
166 }); 178 });
167 } 179 }
168 } 180 }
@@ -184,17 +196,17 @@ public sealed class BSShapeCollection : IDisposable
184 { 196 {
185 // There is an existing instance of this mesh. 197 // There is an existing instance of this mesh.
186 meshDesc.referenceCount++; 198 meshDesc.referenceCount++;
187 DetailLog("{0},BSShapeCollection.ReferenceShape,existingMesh,key={1},cnt={2}", 199 if (DDetail) DetailLog("{0},BSShapeCollection.ReferenceShape,existingMesh,key={1},cnt={2}",
188 BSScene.DetailLogZero, shape.shapeKey.ToString("X"), meshDesc.referenceCount); 200 BSScene.DetailLogZero, shape.shapeKey.ToString("X"), meshDesc.referenceCount);
189 } 201 }
190 else 202 else
191 { 203 {
192 // This is a new reference to a mesh 204 // This is a new reference to a mesh
193 meshDesc.ptr = shape.ptr; 205 meshDesc.shape = shape.Clone();
194 meshDesc.shapeKey = shape.shapeKey; 206 meshDesc.shapeKey = shape.shapeKey;
195 // We keep a reference to the underlying IMesh data so a hull can be built 207 // We keep a reference to the underlying IMesh data so a hull can be built
196 meshDesc.referenceCount = 1; 208 meshDesc.referenceCount = 1;
197 DetailLog("{0},BSShapeCollection.ReferenceShape,newMesh,key={1},cnt={2}", 209 if (DDetail) DetailLog("{0},BSShapeCollection.ReferenceShape,newMesh,key={1},cnt={2}",
198 BSScene.DetailLogZero, shape.shapeKey.ToString("X"), meshDesc.referenceCount); 210 BSScene.DetailLogZero, shape.shapeKey.ToString("X"), meshDesc.referenceCount);
199 ret = true; 211 ret = true;
200 } 212 }
@@ -207,16 +219,16 @@ public sealed class BSShapeCollection : IDisposable
207 { 219 {
208 // There is an existing instance of this hull. 220 // There is an existing instance of this hull.
209 hullDesc.referenceCount++; 221 hullDesc.referenceCount++;
210 DetailLog("{0},BSShapeCollection.ReferenceShape,existingHull,key={1},cnt={2}", 222 if (DDetail) DetailLog("{0},BSShapeCollection.ReferenceShape,existingHull,key={1},cnt={2}",
211 BSScene.DetailLogZero, shape.shapeKey.ToString("X"), hullDesc.referenceCount); 223 BSScene.DetailLogZero, shape.shapeKey.ToString("X"), hullDesc.referenceCount);
212 } 224 }
213 else 225 else
214 { 226 {
215 // This is a new reference to a hull 227 // This is a new reference to a hull
216 hullDesc.ptr = shape.ptr; 228 hullDesc.shape = shape.Clone();
217 hullDesc.shapeKey = shape.shapeKey; 229 hullDesc.shapeKey = shape.shapeKey;
218 hullDesc.referenceCount = 1; 230 hullDesc.referenceCount = 1;
219 DetailLog("{0},BSShapeCollection.ReferenceShape,newHull,key={1},cnt={2}", 231 if (DDetail) DetailLog("{0},BSShapeCollection.ReferenceShape,newHull,key={1},cnt={2}",
220 BSScene.DetailLogZero, shape.shapeKey.ToString("X"), hullDesc.referenceCount); 232 BSScene.DetailLogZero, shape.shapeKey.ToString("X"), hullDesc.referenceCount);
221 ret = true; 233 ret = true;
222 234
@@ -236,20 +248,20 @@ public sealed class BSShapeCollection : IDisposable
236 // Release the usage of a shape. 248 // Release the usage of a shape.
237 public void DereferenceShape(BulletShape shape, bool inTaintTime, ShapeDestructionCallback shapeCallback) 249 public void DereferenceShape(BulletShape shape, bool inTaintTime, ShapeDestructionCallback shapeCallback)
238 { 250 {
239 if (shape.ptr == IntPtr.Zero) 251 if (!shape.HasPhysicalShape)
240 return; 252 return;
241 253
242 PhysicsScene.TaintedObject(inTaintTime, "BSShapeCollection.DereferenceShape", delegate() 254 PhysicsScene.TaintedObject(inTaintTime, "BSShapeCollection.DereferenceShape", delegate()
243 { 255 {
244 if (shape.ptr != IntPtr.Zero) 256 if (shape.HasPhysicalShape)
245 { 257 {
246 if (shape.isNativeShape) 258 if (shape.isNativeShape)
247 { 259 {
248 // Native shapes are not tracked and are released immediately 260 // Native shapes are not tracked and are released immediately
249 DetailLog("{0},BSShapeCollection.DereferenceShape,deleteNativeShape,ptr={1},taintTime={2}", 261 if (DDetail) DetailLog("{0},BSShapeCollection.DereferenceShape,deleteNativeShape,ptr={1},taintTime={2}",
250 BSScene.DetailLogZero, shape.ptr.ToString("X"), inTaintTime); 262 BSScene.DetailLogZero, shape.AddrString, inTaintTime);
251 if (shapeCallback != null) shapeCallback(shape); 263 if (shapeCallback != null) shapeCallback(shape);
252 BulletSimAPI.DeleteCollisionShape2(PhysicsScene.World.ptr, shape.ptr); 264 PhysicsScene.PE.DeleteCollisionShape(PhysicsScene.World, shape);
253 } 265 }
254 else 266 else
255 { 267 {
@@ -286,7 +298,7 @@ public sealed class BSShapeCollection : IDisposable
286 if (shapeCallback != null) shapeCallback(shape); 298 if (shapeCallback != null) shapeCallback(shape);
287 meshDesc.lastReferenced = System.DateTime.Now; 299 meshDesc.lastReferenced = System.DateTime.Now;
288 Meshes[shape.shapeKey] = meshDesc; 300 Meshes[shape.shapeKey] = meshDesc;
289 DetailLog("{0},BSShapeCollection.DereferenceMesh,shape={1},refCnt={2}", 301 if (DDetail) DetailLog("{0},BSShapeCollection.DereferenceMesh,shape={1},refCnt={2}",
290 BSScene.DetailLogZero, shape, meshDesc.referenceCount); 302 BSScene.DetailLogZero, shape, meshDesc.referenceCount);
291 303
292 } 304 }
@@ -307,7 +319,7 @@ public sealed class BSShapeCollection : IDisposable
307 319
308 hullDesc.lastReferenced = System.DateTime.Now; 320 hullDesc.lastReferenced = System.DateTime.Now;
309 Hulls[shape.shapeKey] = hullDesc; 321 Hulls[shape.shapeKey] = hullDesc;
310 DetailLog("{0},BSShapeCollection.DereferenceHull,shape={1},refCnt={2}", 322 if (DDetail) DetailLog("{0},BSShapeCollection.DereferenceHull,shape={1},refCnt={2}",
311 BSScene.DetailLogZero, shape, hullDesc.referenceCount); 323 BSScene.DetailLogZero, shape, hullDesc.referenceCount);
312 } 324 }
313 } 325 }
@@ -320,57 +332,56 @@ public sealed class BSShapeCollection : IDisposable
320 // Called at taint-time. 332 // Called at taint-time.
321 private void DereferenceCompound(BulletShape shape, ShapeDestructionCallback shapeCallback) 333 private void DereferenceCompound(BulletShape shape, ShapeDestructionCallback shapeCallback)
322 { 334 {
323 if (!BulletSimAPI.IsCompound2(shape.ptr)) 335 if (!PhysicsScene.PE.IsCompound(shape))
324 { 336 {
325 // Failed the sanity check!! 337 // Failed the sanity check!!
326 PhysicsScene.Logger.ErrorFormat("{0} Attempt to free a compound shape that is not compound!! type={1}, ptr={2}", 338 PhysicsScene.Logger.ErrorFormat("{0} Attempt to free a compound shape that is not compound!! type={1}, ptr={2}",
327 LogHeader, shape.type, shape.ptr.ToString("X")); 339 LogHeader, shape.type, shape.AddrString);
328 DetailLog("{0},BSShapeCollection.DereferenceCompound,notACompoundShape,type={1},ptr={2}", 340 if (DDetail) DetailLog("{0},BSShapeCollection.DereferenceCompound,notACompoundShape,type={1},ptr={2}",
329 BSScene.DetailLogZero, shape.type, shape.ptr.ToString("X")); 341 BSScene.DetailLogZero, shape.type, shape.AddrString);
330 return; 342 return;
331 } 343 }
332 344
333 int numChildren = BulletSimAPI.GetNumberOfCompoundChildren2(shape.ptr); 345 int numChildren = PhysicsScene.PE.GetNumberOfCompoundChildren(shape);
334 DetailLog("{0},BSShapeCollection.DereferenceCompound,shape={1},children={2}", BSScene.DetailLogZero, shape, numChildren); 346 if (DDetail) DetailLog("{0},BSShapeCollection.DereferenceCompound,shape={1},children={2}", BSScene.DetailLogZero, shape, numChildren);
335 347
336 for (int ii = numChildren - 1; ii >= 0; ii--) 348 for (int ii = numChildren - 1; ii >= 0; ii--)
337 { 349 {
338 IntPtr childShape = BulletSimAPI.RemoveChildShapeFromCompoundShapeIndex2(shape.ptr, ii); 350 BulletShape childShape = PhysicsScene.PE.RemoveChildShapeFromCompoundShapeIndex(shape, ii);
339 DereferenceAnonCollisionShape(childShape); 351 DereferenceAnonCollisionShape(childShape);
340 } 352 }
341 BulletSimAPI.DeleteCollisionShape2(PhysicsScene.World.ptr, shape.ptr); 353 PhysicsScene.PE.DeleteCollisionShape(PhysicsScene.World, shape);
342 } 354 }
343 355
344 // Sometimes we have a pointer to a collision shape but don't know what type it is. 356 // Sometimes we have a pointer to a collision shape but don't know what type it is.
345 // Figure out type and call the correct dereference routine. 357 // Figure out type and call the correct dereference routine.
346 // Called at taint-time. 358 // Called at taint-time.
347 private void DereferenceAnonCollisionShape(IntPtr cShape) 359 private void DereferenceAnonCollisionShape(BulletShape shapeInfo)
348 { 360 {
349 MeshDesc meshDesc; 361 MeshDesc meshDesc;
350 HullDesc hullDesc; 362 HullDesc hullDesc;
351 363
352 BulletShape shapeInfo = new BulletShape(cShape); 364 if (TryGetMeshByPtr(shapeInfo, out meshDesc))
353 if (TryGetMeshByPtr(cShape, out meshDesc))
354 { 365 {
355 shapeInfo.type = BSPhysicsShapeType.SHAPE_MESH; 366 shapeInfo.type = BSPhysicsShapeType.SHAPE_MESH;
356 shapeInfo.shapeKey = meshDesc.shapeKey; 367 shapeInfo.shapeKey = meshDesc.shapeKey;
357 } 368 }
358 else 369 else
359 { 370 {
360 if (TryGetHullByPtr(cShape, out hullDesc)) 371 if (TryGetHullByPtr(shapeInfo, out hullDesc))
361 { 372 {
362 shapeInfo.type = BSPhysicsShapeType.SHAPE_HULL; 373 shapeInfo.type = BSPhysicsShapeType.SHAPE_HULL;
363 shapeInfo.shapeKey = hullDesc.shapeKey; 374 shapeInfo.shapeKey = hullDesc.shapeKey;
364 } 375 }
365 else 376 else
366 { 377 {
367 if (BulletSimAPI.IsCompound2(cShape)) 378 if (PhysicsScene.PE.IsCompound(shapeInfo))
368 { 379 {
369 shapeInfo.type = BSPhysicsShapeType.SHAPE_COMPOUND; 380 shapeInfo.type = BSPhysicsShapeType.SHAPE_COMPOUND;
370 } 381 }
371 else 382 else
372 { 383 {
373 if (BulletSimAPI.IsNativeShape2(cShape)) 384 if (PhysicsScene.PE.IsNativeShape(shapeInfo))
374 { 385 {
375 shapeInfo.isNativeShape = true; 386 shapeInfo.isNativeShape = true;
376 shapeInfo.type = BSPhysicsShapeType.SHAPE_BOX; // (technically, type doesn't matter) 387 shapeInfo.type = BSPhysicsShapeType.SHAPE_BOX; // (technically, type doesn't matter)
@@ -379,7 +390,7 @@ public sealed class BSShapeCollection : IDisposable
379 } 390 }
380 } 391 }
381 392
382 DetailLog("{0},BSShapeCollection.DereferenceAnonCollisionShape,shape={1}", BSScene.DetailLogZero, shapeInfo); 393 if (DDetail) DetailLog("{0},BSShapeCollection.DereferenceAnonCollisionShape,shape={1}", BSScene.DetailLogZero, shapeInfo);
383 394
384 if (shapeInfo.type != BSPhysicsShapeType.SHAPE_UNKNOWN) 395 if (shapeInfo.type != BSPhysicsShapeType.SHAPE_UNKNOWN)
385 { 396 {
@@ -388,7 +399,7 @@ public sealed class BSShapeCollection : IDisposable
388 else 399 else
389 { 400 {
390 PhysicsScene.Logger.ErrorFormat("{0} Could not decypher shape type. Region={1}, addr={2}", 401 PhysicsScene.Logger.ErrorFormat("{0} Could not decypher shape type. Region={1}, addr={2}",
391 LogHeader, PhysicsScene.RegionName, cShape.ToString("X")); 402 LogHeader, PhysicsScene.RegionName, shapeInfo.AddrString);
392 } 403 }
393 } 404 }
394 405
@@ -408,19 +419,18 @@ public sealed class BSShapeCollection : IDisposable
408 if (!haveShape && prim.PreferredPhysicalShape == BSPhysicsShapeType.SHAPE_CAPSULE) 419 if (!haveShape && prim.PreferredPhysicalShape == BSPhysicsShapeType.SHAPE_CAPSULE)
409 { 420 {
410 // an avatar capsule is close to a native shape (it is not shared) 421 // an avatar capsule is close to a native shape (it is not shared)
411 ret = GetReferenceToNativeShape(prim, BSPhysicsShapeType.SHAPE_CAPSULE, 422 GetReferenceToNativeShape(prim, BSPhysicsShapeType.SHAPE_CAPSULE, FixedShapeKey.KEY_CAPSULE, shapeCallback);
412 FixedShapeKey.KEY_CAPSULE, shapeCallback); 423 if (DDetail) DetailLog("{0},BSShapeCollection.CreateGeom,avatarCapsule,shape={1}", prim.LocalID, prim.PhysShape);
413 DetailLog("{0},BSShapeCollection.CreateGeom,avatarCapsule,shape={1}", prim.LocalID, prim.PhysShape);
414 ret = true; 424 ret = true;
415 haveShape = true; 425 haveShape = true;
416 } 426 }
417 427
418 // Compound shapes are handled special as they are rebuilt from scratch. 428 // Compound shapes are handled special as they are rebuilt from scratch.
419 // This isn't too great a hardship since most of the child shapes will already been created. 429 // This isn't too great a hardship since most of the child shapes will have already been created.
420 if (!haveShape && prim.PreferredPhysicalShape == BSPhysicsShapeType.SHAPE_COMPOUND) 430 if (!haveShape && prim.PreferredPhysicalShape == BSPhysicsShapeType.SHAPE_COMPOUND)
421 { 431 {
422 ret = GetReferenceToCompoundShape(prim, shapeCallback); 432 ret = GetReferenceToCompoundShape(prim, shapeCallback);
423 DetailLog("{0},BSShapeCollection.CreateGeom,compoundShape,shape={1}", prim.LocalID, prim.PhysShape); 433 if (DDetail) DetailLog("{0},BSShapeCollection.CreateGeom,compoundShape,shape={1}", prim.LocalID, prim.PhysShape);
424 haveShape = true; 434 haveShape = true;
425 } 435 }
426 436
@@ -433,7 +443,7 @@ public sealed class BSShapeCollection : IDisposable
433 } 443 }
434 444
435 // Create a mesh/hull shape or a native shape if 'nativeShapePossible' is 'true'. 445 // Create a mesh/hull shape or a native shape if 'nativeShapePossible' is 'true'.
436 private bool CreateGeomNonSpecial(bool forceRebuild, BSPhysObject prim, ShapeDestructionCallback shapeCallback) 446 public bool CreateGeomNonSpecial(bool forceRebuild, BSPhysObject prim, ShapeDestructionCallback shapeCallback)
437 { 447 {
438 bool ret = false; 448 bool ret = false;
439 bool haveShape = false; 449 bool haveShape = false;
@@ -443,8 +453,9 @@ public sealed class BSShapeCollection : IDisposable
443 // If the prim attributes are simple, this could be a simple Bullet native shape 453 // If the prim attributes are simple, this could be a simple Bullet native shape
444 if (!haveShape 454 if (!haveShape
445 && pbs != null 455 && pbs != null
456 && !pbs.SculptEntry
446 && nativeShapePossible 457 && nativeShapePossible
447 && ((pbs.SculptEntry && !PhysicsScene.ShouldMeshSculptedPrim) 458 && ((pbs.SculptEntry && !BSParam.ShouldMeshSculptedPrim)
448 || (pbs.ProfileBegin == 0 && pbs.ProfileEnd == 0 459 || (pbs.ProfileBegin == 0 && pbs.ProfileEnd == 0
449 && pbs.ProfileHollow == 0 460 && pbs.ProfileHollow == 0
450 && pbs.PathTwist == 0 && pbs.PathTwistBegin == 0 461 && pbs.PathTwist == 0 && pbs.PathTwistBegin == 0
@@ -453,19 +464,27 @@ public sealed class BSShapeCollection : IDisposable
453 && pbs.PathScaleX == 100 && pbs.PathScaleY == 100 464 && pbs.PathScaleX == 100 && pbs.PathScaleY == 100
454 && pbs.PathShearX == 0 && pbs.PathShearY == 0) ) ) 465 && pbs.PathShearX == 0 && pbs.PathShearY == 0) ) )
455 { 466 {
467 // Get the scale of any existing shape so we can see if the new shape is same native type and same size.
468 OMV.Vector3 scaleOfExistingShape = OMV.Vector3.Zero;
469 if (prim.PhysShape.HasPhysicalShape)
470 scaleOfExistingShape = PhysicsScene.PE.GetLocalScaling(prim.PhysShape);
471
472 if (DDetail) DetailLog("{0},BSShapeCollection.CreateGeom,maybeNative,force={1},primScale={2},primSize={3},primShape={4}",
473 prim.LocalID, forceRebuild, prim.Scale, prim.Size, prim.PhysShape.type);
474
456 // It doesn't look like Bullet scales spheres so make sure the scales are all equal 475 // It doesn't look like Bullet scales spheres so make sure the scales are all equal
457 if ((pbs.ProfileShape == ProfileShape.HalfCircle && pbs.PathCurve == (byte)Extrusion.Curve1) 476 if ((pbs.ProfileShape == ProfileShape.HalfCircle && pbs.PathCurve == (byte)Extrusion.Curve1)
458 && pbs.Scale.X == pbs.Scale.Y && pbs.Scale.Y == pbs.Scale.Z) 477 && pbs.Scale.X == pbs.Scale.Y && pbs.Scale.Y == pbs.Scale.Z)
459 { 478 {
460 haveShape = true; 479 haveShape = true;
461 if (forceRebuild 480 if (forceRebuild
462 || prim.Scale != prim.Size 481 || prim.Scale != scaleOfExistingShape
463 || prim.PhysShape.type != BSPhysicsShapeType.SHAPE_SPHERE 482 || prim.PhysShape.type != BSPhysicsShapeType.SHAPE_SPHERE
464 ) 483 )
465 { 484 {
466 ret = GetReferenceToNativeShape(prim, BSPhysicsShapeType.SHAPE_SPHERE, 485 ret = GetReferenceToNativeShape(prim, BSPhysicsShapeType.SHAPE_SPHERE,
467 FixedShapeKey.KEY_SPHERE, shapeCallback); 486 FixedShapeKey.KEY_SPHERE, shapeCallback);
468 DetailLog("{0},BSShapeCollection.CreateGeom,sphere,force={1},shape={2}", 487 if (DDetail) DetailLog("{0},BSShapeCollection.CreateGeom,sphere,force={1},shape={2}",
469 prim.LocalID, forceRebuild, prim.PhysShape); 488 prim.LocalID, forceRebuild, prim.PhysShape);
470 } 489 }
471 } 490 }
@@ -473,13 +492,13 @@ public sealed class BSShapeCollection : IDisposable
473 { 492 {
474 haveShape = true; 493 haveShape = true;
475 if (forceRebuild 494 if (forceRebuild
476 || prim.Scale != prim.Size 495 || prim.Scale != scaleOfExistingShape
477 || prim.PhysShape.type != BSPhysicsShapeType.SHAPE_BOX 496 || prim.PhysShape.type != BSPhysicsShapeType.SHAPE_BOX
478 ) 497 )
479 { 498 {
480 ret = GetReferenceToNativeShape( prim, BSPhysicsShapeType.SHAPE_BOX, 499 ret = GetReferenceToNativeShape( prim, BSPhysicsShapeType.SHAPE_BOX,
481 FixedShapeKey.KEY_BOX, shapeCallback); 500 FixedShapeKey.KEY_BOX, shapeCallback);
482 DetailLog("{0},BSShapeCollection.CreateGeom,box,force={1},shape={2}", 501 if (DDetail) DetailLog("{0},BSShapeCollection.CreateGeom,box,force={1},shape={2}",
483 prim.LocalID, forceRebuild, prim.PhysShape); 502 prim.LocalID, forceRebuild, prim.PhysShape);
484 } 503 }
485 } 504 }
@@ -500,17 +519,17 @@ public sealed class BSShapeCollection : IDisposable
500 bool ret = false; 519 bool ret = false;
501 // Note that if it's a native shape, the check for physical/non-physical is not 520 // Note that if it's a native shape, the check for physical/non-physical is not
502 // made. Native shapes work in either case. 521 // made. Native shapes work in either case.
503 if (prim.IsPhysical && PhysicsScene.ShouldUseHullsForPhysicalObjects) 522 if (prim.IsPhysical && BSParam.ShouldUseHullsForPhysicalObjects)
504 { 523 {
505 // Update prim.BSShape to reference a hull of this shape. 524 // Update prim.BSShape to reference a hull of this shape.
506 ret = GetReferenceToHull(prim,shapeCallback); 525 ret = GetReferenceToHull(prim,shapeCallback);
507 DetailLog("{0},BSShapeCollection.CreateGeom,hull,shape={1},key={2}", 526 if (DDetail) DetailLog("{0},BSShapeCollection.CreateGeom,hull,shape={1},key={2}",
508 prim.LocalID, prim.PhysShape, prim.PhysShape.shapeKey.ToString("X")); 527 prim.LocalID, prim.PhysShape, prim.PhysShape.shapeKey.ToString("X"));
509 } 528 }
510 else 529 else
511 { 530 {
512 ret = GetReferenceToMesh(prim, shapeCallback); 531 ret = GetReferenceToMesh(prim, shapeCallback);
513 DetailLog("{0},BSShapeCollection.CreateGeom,mesh,shape={1},key={2}", 532 if (DDetail) DetailLog("{0},BSShapeCollection.CreateGeom,mesh,shape={1},key={2}",
514 prim.LocalID, prim.PhysShape, prim.PhysShape.shapeKey.ToString("X")); 533 prim.LocalID, prim.PhysShape, prim.PhysShape.shapeKey.ToString("X"));
515 } 534 }
516 return ret; 535 return ret;
@@ -528,9 +547,10 @@ public sealed class BSShapeCollection : IDisposable
528 BulletShape newShape = BuildPhysicalNativeShape(prim, shapeType, shapeKey); 547 BulletShape newShape = BuildPhysicalNativeShape(prim, shapeType, shapeKey);
529 548
530 // Don't need to do a 'ReferenceShape()' here because native shapes are not shared. 549 // Don't need to do a 'ReferenceShape()' here because native shapes are not shared.
531 DetailLog("{0},BSShapeCollection.AddNativeShapeToPrim,create,newshape={1},scale={2}", 550 if (DDetail) DetailLog("{0},BSShapeCollection.AddNativeShapeToPrim,create,newshape={1},scale={2}",
532 prim.LocalID, newShape, prim.Scale); 551 prim.LocalID, newShape, prim.Scale);
533 552
553 // native shapes are scaled by Bullet
534 prim.PhysShape = newShape; 554 prim.PhysShape = newShape;
535 return true; 555 return true;
536 } 556 }
@@ -550,20 +570,17 @@ public sealed class BSShapeCollection : IDisposable
550 570
551 if (shapeType == BSPhysicsShapeType.SHAPE_CAPSULE) 571 if (shapeType == BSPhysicsShapeType.SHAPE_CAPSULE)
552 { 572 {
553 // The proper scale has been calculated in the prim. 573
554 newShape = new BulletShape( 574 newShape = PhysicsScene.PE.BuildCapsuleShape(PhysicsScene.World, 1f, 1f, prim.Scale);
555 BulletSimAPI.BuildCapsuleShape2(PhysicsScene.World.ptr, 1f, 1f, prim.Scale) 575 if (DDetail) DetailLog("{0},BSShapeCollection.BuiletPhysicalNativeShape,capsule,scale={1}", prim.LocalID, prim.Scale);
556 , shapeType);
557 DetailLog("{0},BSShapeCollection.BuiletPhysicalNativeShape,capsule,scale={1}", prim.LocalID, prim.Scale);
558 } 576 }
559 else 577 else
560 { 578 {
561 // Native shapes are scaled in Bullet so set the scaling to the size 579 // Native shapes are scaled in Bullet so set the scaling to the size
562 prim.Scale = prim.Size; 580 newShape = PhysicsScene.PE.BuildNativeShape(PhysicsScene.World, nativeShapeData);
563 nativeShapeData.Scale = prim.Scale; 581
564 newShape = new BulletShape(BulletSimAPI.BuildNativeShape2(PhysicsScene.World.ptr, nativeShapeData), shapeType);
565 } 582 }
566 if (newShape.ptr == IntPtr.Zero) 583 if (!newShape.HasPhysicalShape)
567 { 584 {
568 PhysicsScene.Logger.ErrorFormat("{0} BuildPhysicalNativeShape failed. ID={1}, shape={2}", 585 PhysicsScene.Logger.ErrorFormat("{0} BuildPhysicalNativeShape failed. ID={1}, shape={2}",
569 LogHeader, prim.LocalID, shapeType); 586 LogHeader, prim.LocalID, shapeType);
@@ -580,7 +597,7 @@ public sealed class BSShapeCollection : IDisposable
580 // Called at taint-time! 597 // Called at taint-time!
581 private bool GetReferenceToMesh(BSPhysObject prim, ShapeDestructionCallback shapeCallback) 598 private bool GetReferenceToMesh(BSPhysObject prim, ShapeDestructionCallback shapeCallback)
582 { 599 {
583 BulletShape newShape = new BulletShape(IntPtr.Zero); 600 BulletShape newShape = new BulletShape();
584 601
585 float lod; 602 float lod;
586 System.UInt64 newMeshKey = ComputeShapeKey(prim.Size, prim.BaseShape, out lod); 603 System.UInt64 newMeshKey = ComputeShapeKey(prim.Size, prim.BaseShape, out lod);
@@ -589,7 +606,7 @@ public sealed class BSShapeCollection : IDisposable
589 if (newMeshKey == prim.PhysShape.shapeKey && prim.PhysShape.type == BSPhysicsShapeType.SHAPE_MESH) 606 if (newMeshKey == prim.PhysShape.shapeKey && prim.PhysShape.type == BSPhysicsShapeType.SHAPE_MESH)
590 return false; 607 return false;
591 608
592 DetailLog("{0},BSShapeCollection.GetReferenceToMesh,create,oldKey={1},newKey={2}", 609 if (DDetail) DetailLog("{0},BSShapeCollection.GetReferenceToMesh,create,oldKey={1},newKey={2}",
593 prim.LocalID, prim.PhysShape.shapeKey.ToString("X"), newMeshKey.ToString("X")); 610 prim.LocalID, prim.PhysShape.shapeKey.ToString("X"), newMeshKey.ToString("X"));
594 611
595 // Since we're recreating new, get rid of the reference to the previous shape 612 // Since we're recreating new, get rid of the reference to the previous shape
@@ -601,8 +618,6 @@ public sealed class BSShapeCollection : IDisposable
601 618
602 ReferenceShape(newShape); 619 ReferenceShape(newShape);
603 620
604 // meshes are already scaled by the meshmerizer
605 prim.Scale = new OMV.Vector3(1f, 1f, 1f);
606 prim.PhysShape = newShape; 621 prim.PhysShape = newShape;
607 622
608 return true; // 'true' means a new shape has been added to this prim 623 return true; // 'true' means a new shape has been added to this prim
@@ -610,18 +625,18 @@ public sealed class BSShapeCollection : IDisposable
610 625
611 private BulletShape CreatePhysicalMesh(string objName, System.UInt64 newMeshKey, PrimitiveBaseShape pbs, OMV.Vector3 size, float lod) 626 private BulletShape CreatePhysicalMesh(string objName, System.UInt64 newMeshKey, PrimitiveBaseShape pbs, OMV.Vector3 size, float lod)
612 { 627 {
628 BulletShape newShape = new BulletShape();
613 IMesh meshData = null; 629 IMesh meshData = null;
614 IntPtr meshPtr = IntPtr.Zero; 630
615 MeshDesc meshDesc; 631 MeshDesc meshDesc;
616 if (Meshes.TryGetValue(newMeshKey, out meshDesc)) 632 if (Meshes.TryGetValue(newMeshKey, out meshDesc))
617 { 633 {
618 // If the mesh has already been built just use it. 634 // If the mesh has already been built just use it.
619 meshPtr = meshDesc.ptr; 635 newShape = meshDesc.shape.Clone();
620 } 636 }
621 else 637 else
622 { 638 {
623 // Pass false for physicalness as this creates some sort of bounding box which we don't need 639 meshData = PhysicsScene.mesher.CreateMesh(objName, pbs, size, lod, true, false, false, false);
624 meshData = PhysicsScene.mesher.CreateMesh(objName, pbs, size, lod, false);
625 640
626 if (meshData != null) 641 if (meshData != null)
627 { 642 {
@@ -640,11 +655,10 @@ public sealed class BSShapeCollection : IDisposable
640 // m_log.DebugFormat("{0}: BSShapeCollection.CreatePhysicalMesh: calling CreateMesh. lid={1}, key={2}, indices={3}, vertices={4}", 655 // m_log.DebugFormat("{0}: BSShapeCollection.CreatePhysicalMesh: calling CreateMesh. lid={1}, key={2}, indices={3}, vertices={4}",
641 // LogHeader, prim.LocalID, newMeshKey, indices.Length, vertices.Count); 656 // LogHeader, prim.LocalID, newMeshKey, indices.Length, vertices.Count);
642 657
643 meshPtr = BulletSimAPI.CreateMeshShape2(PhysicsScene.World.ptr, 658 newShape = PhysicsScene.PE.CreateMeshShape(PhysicsScene.World,
644 indices.GetLength(0), indices, vertices.Count, verticesAsFloats); 659 indices.GetLength(0), indices, vertices.Count, verticesAsFloats);
645 } 660 }
646 } 661 }
647 BulletShape newShape = new BulletShape(meshPtr, BSPhysicsShapeType.SHAPE_MESH);
648 newShape.shapeKey = newMeshKey; 662 newShape.shapeKey = newMeshKey;
649 663
650 return newShape; 664 return newShape;
@@ -663,7 +677,7 @@ public sealed class BSShapeCollection : IDisposable
663 if (newHullKey == prim.PhysShape.shapeKey && prim.PhysShape.type == BSPhysicsShapeType.SHAPE_HULL) 677 if (newHullKey == prim.PhysShape.shapeKey && prim.PhysShape.type == BSPhysicsShapeType.SHAPE_HULL)
664 return false; 678 return false;
665 679
666 DetailLog("{0},BSShapeCollection.GetReferenceToHull,create,oldKey={1},newKey={2}", 680 if (DDetail) DetailLog("{0},BSShapeCollection.GetReferenceToHull,create,oldKey={1},newKey={2}",
667 prim.LocalID, prim.PhysShape.shapeKey.ToString("X"), newHullKey.ToString("X")); 681 prim.LocalID, prim.PhysShape.shapeKey.ToString("X"), newHullKey.ToString("X"));
668 682
669 // Remove usage of the previous shape. 683 // Remove usage of the previous shape.
@@ -674,8 +688,6 @@ public sealed class BSShapeCollection : IDisposable
674 688
675 ReferenceShape(newShape); 689 ReferenceShape(newShape);
676 690
677 // hulls are already scaled by the meshmerizer
678 prim.Scale = new OMV.Vector3(1f, 1f, 1f);
679 prim.PhysShape = newShape; 691 prim.PhysShape = newShape;
680 return true; // 'true' means a new shape has been added to this prim 692 return true; // 'true' means a new shape has been added to this prim
681 } 693 }
@@ -684,18 +696,20 @@ public sealed class BSShapeCollection : IDisposable
684 private BulletShape CreatePhysicalHull(string objName, System.UInt64 newHullKey, PrimitiveBaseShape pbs, OMV.Vector3 size, float lod) 696 private BulletShape CreatePhysicalHull(string objName, System.UInt64 newHullKey, PrimitiveBaseShape pbs, OMV.Vector3 size, float lod)
685 { 697 {
686 698
699 BulletShape newShape = new BulletShape();
687 IntPtr hullPtr = IntPtr.Zero; 700 IntPtr hullPtr = IntPtr.Zero;
701
688 HullDesc hullDesc; 702 HullDesc hullDesc;
689 if (Hulls.TryGetValue(newHullKey, out hullDesc)) 703 if (Hulls.TryGetValue(newHullKey, out hullDesc))
690 { 704 {
691 // If the hull shape already is created, just use it. 705 // If the hull shape already is created, just use it.
692 hullPtr = hullDesc.ptr; 706 newShape = hullDesc.shape.Clone();
693 } 707 }
694 else 708 else
695 { 709 {
696 // Build a new hull in the physical world 710 // Build a new hull in the physical world
697 // Pass false for physicalness as this creates some sort of bounding box which we don't need 711 // Pass true for physicalness as this creates some sort of bounding box which we don't need
698 IMesh meshData = PhysicsScene.mesher.CreateMesh(objName, pbs, size, lod, false); 712 IMesh meshData = PhysicsScene.mesher.CreateMesh(objName, pbs, size, lod, true, false, false, false);
699 if (meshData != null) 713 if (meshData != null)
700 { 714 {
701 715
@@ -777,14 +791,13 @@ public sealed class BSShapeCollection : IDisposable
777 } 791 }
778 } 792 }
779 // create the hull data structure in Bullet 793 // create the hull data structure in Bullet
780 hullPtr = BulletSimAPI.CreateHullShape2(PhysicsScene.World.ptr, hullCount, convHulls); 794 newShape = PhysicsScene.PE.CreateHullShape(PhysicsScene.World, hullCount, convHulls);
781 } 795 }
782 } 796 }
783 797
784 BulletShape newShape = new BulletShape(hullPtr, BSPhysicsShapeType.SHAPE_HULL);
785 newShape.shapeKey = newHullKey; 798 newShape.shapeKey = newHullKey;
786 799
787 return newShape; // 'true' means a new shape has been added to this prim 800 return newShape;
788 } 801 }
789 802
790 // Callback from convex hull creater with a newly created hull. 803 // Callback from convex hull creater with a newly created hull.
@@ -803,13 +816,13 @@ public sealed class BSShapeCollection : IDisposable
803 // Don't need to do this as the shape is freed when the new root shape is created below. 816 // Don't need to do this as the shape is freed when the new root shape is created below.
804 // DereferenceShape(prim.PhysShape, true, shapeCallback); 817 // DereferenceShape(prim.PhysShape, true, shapeCallback);
805 818
806 BulletShape cShape = new BulletShape( 819
807 BulletSimAPI.CreateCompoundShape2(PhysicsScene.World.ptr, false), BSPhysicsShapeType.SHAPE_COMPOUND); 820 BulletShape cShape = PhysicsScene.PE.CreateCompoundShape(PhysicsScene.World, false);
808 821
809 // Create the shape for the root prim and add it to the compound shape. Cannot be a native shape. 822 // Create the shape for the root prim and add it to the compound shape. Cannot be a native shape.
810 CreateGeomMeshOrHull(prim, shapeCallback); 823 CreateGeomMeshOrHull(prim, shapeCallback);
811 BulletSimAPI.AddChildShapeToCompoundShape2(cShape.ptr, prim.PhysShape.ptr, OMV.Vector3.Zero, OMV.Quaternion.Identity); 824 PhysicsScene.PE.AddChildShapeToCompoundShape(cShape, prim.PhysShape, OMV.Vector3.Zero, OMV.Quaternion.Identity);
812 DetailLog("{0},BSShapeCollection.GetReferenceToCompoundShape,addRootPrim,compShape={1},rootShape={2}", 825 if (DDetail) DetailLog("{0},BSShapeCollection.GetReferenceToCompoundShape,addRootPrim,compShape={1},rootShape={2}",
813 prim.LocalID, cShape, prim.PhysShape); 826 prim.LocalID, cShape, prim.PhysShape);
814 827
815 prim.PhysShape = cShape; 828 prim.PhysShape = cShape;
@@ -822,14 +835,14 @@ public sealed class BSShapeCollection : IDisposable
822 private System.UInt64 ComputeShapeKey(OMV.Vector3 size, PrimitiveBaseShape pbs, out float retLod) 835 private System.UInt64 ComputeShapeKey(OMV.Vector3 size, PrimitiveBaseShape pbs, out float retLod)
823 { 836 {
824 // level of detail based on size and type of the object 837 // level of detail based on size and type of the object
825 float lod = PhysicsScene.MeshLOD; 838 float lod = BSParam.MeshLOD;
826 if (pbs.SculptEntry) 839 if (pbs.SculptEntry)
827 lod = PhysicsScene.SculptLOD; 840 lod = BSParam.SculptLOD;
828 841
829 // Mega prims usually get more detail because one can interact with shape approximations at this size. 842 // Mega prims usually get more detail because one can interact with shape approximations at this size.
830 float maxAxis = Math.Max(size.X, Math.Max(size.Y, size.Z)); 843 float maxAxis = Math.Max(size.X, Math.Max(size.Y, size.Z));
831 if (maxAxis > PhysicsScene.MeshMegaPrimThreshold) 844 if (maxAxis > BSParam.MeshMegaPrimThreshold)
832 lod = PhysicsScene.MeshMegaPrimLOD; 845 lod = BSParam.MeshMegaPrimLOD;
833 846
834 retLod = lod; 847 retLod = lod;
835 return pbs.GetMeshKey(size, lod); 848 return pbs.GetMeshKey(size, lod);
@@ -851,7 +864,7 @@ public sealed class BSShapeCollection : IDisposable
851 private BulletShape VerifyMeshCreated(BulletShape newShape, BSPhysObject prim) 864 private BulletShape VerifyMeshCreated(BulletShape newShape, BSPhysObject prim)
852 { 865 {
853 // If the shape was successfully created, nothing more to do 866 // If the shape was successfully created, nothing more to do
854 if (newShape.ptr != IntPtr.Zero) 867 if (newShape.HasPhysicalShape)
855 return newShape; 868 return newShape;
856 869
857 // If this mesh has an underlying asset and we have not failed getting it before, fetch the asset 870 // If this mesh has an underlying asset and we have not failed getting it before, fetch the asset
@@ -893,7 +906,7 @@ public sealed class BSShapeCollection : IDisposable
893 } 906 }
894 } 907 }
895 908
896 // While we figure out the real problem, stick a simple native shape on the object. 909 // While we figure out the real problem, stick in a simple box for the object.
897 BulletShape fillinShape = 910 BulletShape fillinShape =
898 BuildPhysicalNativeShape(prim, BSPhysicsShapeType.SHAPE_BOX, FixedShapeKey.KEY_BOX); 911 BuildPhysicalNativeShape(prim, BSPhysicsShapeType.SHAPE_BOX, FixedShapeKey.KEY_BOX);
899 912
@@ -904,19 +917,19 @@ public sealed class BSShapeCollection : IDisposable
904 // Updates prim.BSBody with the information about the new body if one is created. 917 // Updates prim.BSBody with the information about the new body if one is created.
905 // Returns 'true' if an object was actually created. 918 // Returns 'true' if an object was actually created.
906 // Called at taint-time. 919 // Called at taint-time.
907 private bool CreateBody(bool forceRebuild, BSPhysObject prim, BulletSim sim, BulletShape shape, 920 private bool CreateBody(bool forceRebuild, BSPhysObject prim, BulletWorld sim, BulletShape shape,
908 BodyDestructionCallback bodyCallback) 921 BodyDestructionCallback bodyCallback)
909 { 922 {
910 bool ret = false; 923 bool ret = false;
911 924
912 // the mesh, hull or native shape must have already been created in Bullet 925 // the mesh, hull or native shape must have already been created in Bullet
913 bool mustRebuild = (prim.PhysBody.ptr == IntPtr.Zero); 926 bool mustRebuild = !prim.PhysBody.HasPhysicalBody;
914 927
915 // If there is an existing body, verify it's of an acceptable type. 928 // If there is an existing body, verify it's of an acceptable type.
916 // If not a solid object, body is a GhostObject. Otherwise a RigidBody. 929 // If not a solid object, body is a GhostObject. Otherwise a RigidBody.
917 if (!mustRebuild) 930 if (!mustRebuild)
918 { 931 {
919 CollisionObjectTypes bodyType = (CollisionObjectTypes)BulletSimAPI.GetBodyType2(prim.PhysBody.ptr); 932 CollisionObjectTypes bodyType = (CollisionObjectTypes)PhysicsScene.PE.GetBodyType(prim.PhysBody);
920 if (prim.IsSolid && bodyType != CollisionObjectTypes.CO_RIGID_BODY 933 if (prim.IsSolid && bodyType != CollisionObjectTypes.CO_RIGID_BODY
921 || !prim.IsSolid && bodyType != CollisionObjectTypes.CO_GHOST_OBJECT) 934 || !prim.IsSolid && bodyType != CollisionObjectTypes.CO_GHOST_OBJECT)
922 { 935 {
@@ -931,20 +944,16 @@ public sealed class BSShapeCollection : IDisposable
931 DereferenceBody(prim.PhysBody, true, bodyCallback); 944 DereferenceBody(prim.PhysBody, true, bodyCallback);
932 945
933 BulletBody aBody; 946 BulletBody aBody;
934 IntPtr bodyPtr = IntPtr.Zero;
935 if (prim.IsSolid) 947 if (prim.IsSolid)
936 { 948 {
937 bodyPtr = BulletSimAPI.CreateBodyFromShape2(sim.ptr, shape.ptr, 949 aBody = PhysicsScene.PE.CreateBodyFromShape(sim, shape, prim.LocalID, prim.RawPosition, prim.RawOrientation);
938 prim.LocalID, prim.RawPosition, prim.RawOrientation); 950 if (DDetail) DetailLog("{0},BSShapeCollection.CreateBody,mesh,body={1}", prim.LocalID, aBody);
939 DetailLog("{0},BSShapeCollection.CreateBody,mesh,ptr={1}", prim.LocalID, bodyPtr.ToString("X"));
940 } 951 }
941 else 952 else
942 { 953 {
943 bodyPtr = BulletSimAPI.CreateGhostFromShape2(sim.ptr, shape.ptr, 954 aBody = PhysicsScene.PE.CreateGhostFromShape(sim, shape, prim.LocalID, prim.RawPosition, prim.RawOrientation);
944 prim.LocalID, prim.RawPosition, prim.RawOrientation); 955 if (DDetail) DetailLog("{0},BSShapeCollection.CreateBody,ghost,body={1}", prim.LocalID, aBody);
945 DetailLog("{0},BSShapeCollection.CreateBody,ghost,ptr={1}", prim.LocalID, bodyPtr.ToString("X"));
946 } 956 }
947 aBody = new BulletBody(prim.LocalID, bodyPtr);
948 957
949 ReferenceBody(aBody, true); 958 ReferenceBody(aBody, true);
950 959
@@ -956,13 +965,13 @@ public sealed class BSShapeCollection : IDisposable
956 return ret; 965 return ret;
957 } 966 }
958 967
959 private bool TryGetMeshByPtr(IntPtr addr, out MeshDesc outDesc) 968 private bool TryGetMeshByPtr(BulletShape shape, out MeshDesc outDesc)
960 { 969 {
961 bool ret = false; 970 bool ret = false;
962 MeshDesc foundDesc = new MeshDesc(); 971 MeshDesc foundDesc = new MeshDesc();
963 foreach (MeshDesc md in Meshes.Values) 972 foreach (MeshDesc md in Meshes.Values)
964 { 973 {
965 if (md.ptr == addr) 974 if (md.shape.ReferenceSame(shape))
966 { 975 {
967 foundDesc = md; 976 foundDesc = md;
968 ret = true; 977 ret = true;
@@ -974,13 +983,13 @@ public sealed class BSShapeCollection : IDisposable
974 return ret; 983 return ret;
975 } 984 }
976 985
977 private bool TryGetHullByPtr(IntPtr addr, out HullDesc outDesc) 986 private bool TryGetHullByPtr(BulletShape shape, out HullDesc outDesc)
978 { 987 {
979 bool ret = false; 988 bool ret = false;
980 HullDesc foundDesc = new HullDesc(); 989 HullDesc foundDesc = new HullDesc();
981 foreach (HullDesc hd in Hulls.Values) 990 foreach (HullDesc hd in Hulls.Values)
982 { 991 {
983 if (hd.ptr == addr) 992 if (hd.shape.ReferenceSame(shape))
984 { 993 {
985 foundDesc = hd; 994 foundDesc = hd;
986 ret = true; 995 ret = true;
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSShapes.cs b/OpenSim/Region/Physics/BulletSPlugin/BSShapes.cs
index 96cd55e..ee18379 100755
--- a/OpenSim/Region/Physics/BulletSPlugin/BSShapes.cs
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSShapes.cs
@@ -27,24 +27,19 @@
27 27
28using System; 28using System;
29using System.Collections.Generic; 29using System.Collections.Generic;
30using System.Linq;
31using System.Text; 30using System.Text;
32 31
32using OMV = OpenMetaverse;
33
33namespace OpenSim.Region.Physics.BulletSPlugin 34namespace OpenSim.Region.Physics.BulletSPlugin
34{ 35{
35public abstract class BSShape 36public abstract class BSShape
36{ 37{
37 public IntPtr ptr { get; set; }
38 public BSPhysicsShapeType type { get; set; }
39 public System.UInt64 key { get; set; }
40 public int referenceCount { get; set; } 38 public int referenceCount { get; set; }
41 public DateTime lastReferenced { get; set; } 39 public DateTime lastReferenced { get; set; }
42 40
43 public BSShape() 41 public BSShape()
44 { 42 {
45 ptr = IntPtr.Zero;
46 type = BSPhysicsShapeType.SHAPE_UNKNOWN;
47 key = 0;
48 referenceCount = 0; 43 referenceCount = 0;
49 lastReferenced = DateTime.Now; 44 lastReferenced = DateTime.Now;
50 } 45 }
@@ -63,7 +58,7 @@ public abstract class BSShape
63 } 58 }
64 59
65 // Compound shapes are handled special as they are rebuilt from scratch. 60 // Compound shapes are handled special as they are rebuilt from scratch.
66 // This isn't too great a hardship since most of the child shapes will already been created. 61 // This isn't too great a hardship since most of the child shapes will have already been created.
67 if (ret == null && prim.PreferredPhysicalShape == BSPhysicsShapeType.SHAPE_COMPOUND) 62 if (ret == null && prim.PreferredPhysicalShape == BSPhysicsShapeType.SHAPE_COMPOUND)
68 { 63 {
69 // Getting a reference to a compound shape gets you the compound shape with the root prim shape added 64 // Getting a reference to a compound shape gets you the compound shape with the root prim shape added
@@ -71,6 +66,14 @@ public abstract class BSShape
71 physicsScene.DetailLog("{0},BSShapeCollection.CreateGeom,compoundShape,shape={1}", prim.LocalID, ret); 66 physicsScene.DetailLog("{0},BSShapeCollection.CreateGeom,compoundShape,shape={1}", prim.LocalID, ret);
72 } 67 }
73 68
69 // Avatars have their own unique shape
70 if (ret == null && prim.PreferredPhysicalShape == BSPhysicsShapeType.SHAPE_AVATAR)
71 {
72 // Getting a reference to a compound shape gets you the compound shape with the root prim shape added
73 ret = BSShapeAvatar.GetReference(prim);
74 physicsScene.DetailLog("{0},BSShapeCollection.CreateGeom,avatarShape,shape={1}", prim.LocalID, ret);
75 }
76
74 if (ret == null) 77 if (ret == null)
75 ret = GetShapeReferenceNonSpecial(physicsScene, forceRebuild, prim); 78 ret = GetShapeReferenceNonSpecial(physicsScene, forceRebuild, prim);
76 79
@@ -91,15 +94,17 @@ public abstract class BSShape
91 // All shapes have a static call to get a reference to the physical shape 94 // All shapes have a static call to get a reference to the physical shape
92 // protected abstract static BSShape GetReference(); 95 // protected abstract static BSShape GetReference();
93 96
97 // Returns a string for debugging that uniquily identifies the memory used by this instance
98 public virtual string AddrString
99 {
100 get { return "unknown"; }
101 }
102
94 public override string ToString() 103 public override string ToString()
95 { 104 {
96 StringBuilder buff = new StringBuilder(); 105 StringBuilder buff = new StringBuilder();
97 buff.Append("<p="); 106 buff.Append("<p=");
98 buff.Append(ptr.ToString("X")); 107 buff.Append(AddrString);
99 buff.Append(",s=");
100 buff.Append(type.ToString());
101 buff.Append(",k=");
102 buff.Append(key.ToString("X"));
103 buff.Append(",c="); 108 buff.Append(",c=");
104 buff.Append(referenceCount.ToString()); 109 buff.Append(referenceCount.ToString());
105 buff.Append(">"); 110 buff.Append(">");
@@ -126,7 +131,8 @@ public class BSShapeNative : BSShape
126 BSPhysicsShapeType shapeType, FixedShapeKey shapeKey) 131 BSPhysicsShapeType shapeType, FixedShapeKey shapeKey)
127 { 132 {
128 // Native shapes are not shared and are always built anew. 133 // Native shapes are not shared and are always built anew.
129 return new BSShapeNative(physicsScene, prim, shapeType, shapeKey); 134 //return new BSShapeNative(physicsScene, prim, shapeType, shapeKey);
135 return null;
130 } 136 }
131 137
132 private BSShapeNative(BSScene physicsScene, BSPhysObject prim, 138 private BSShapeNative(BSScene physicsScene, BSPhysObject prim,
@@ -141,14 +147,15 @@ public class BSShapeNative : BSShape
141 nativeShapeData.HullKey = (ulong)shapeKey; 147 nativeShapeData.HullKey = (ulong)shapeKey;
142 148
143 149
150 /*
144 if (shapeType == BSPhysicsShapeType.SHAPE_CAPSULE) 151 if (shapeType == BSPhysicsShapeType.SHAPE_CAPSULE)
145 { 152 {
146 ptr = BulletSimAPI.BuildCapsuleShape2(physicsScene.World.ptr, 1f, 1f, prim.Scale); 153 ptr = PhysicsScene.PE.BuildCapsuleShape(physicsScene.World, 1f, 1f, prim.Scale);
147 physicsScene.DetailLog("{0},BSShapeCollection.BuiletPhysicalNativeShape,capsule,scale={1}", prim.LocalID, prim.Scale); 154 physicsScene.DetailLog("{0},BSShapeCollection.BuiletPhysicalNativeShape,capsule,scale={1}", prim.LocalID, prim.Scale);
148 } 155 }
149 else 156 else
150 { 157 {
151 ptr = BulletSimAPI.BuildNativeShape2(physicsScene.World.ptr, nativeShapeData); 158 ptr = PhysicsScene.PE.BuildNativeShape(physicsScene.World, nativeShapeData);
152 } 159 }
153 if (ptr == IntPtr.Zero) 160 if (ptr == IntPtr.Zero)
154 { 161 {
@@ -157,15 +164,18 @@ public class BSShapeNative : BSShape
157 } 164 }
158 type = shapeType; 165 type = shapeType;
159 key = (UInt64)shapeKey; 166 key = (UInt64)shapeKey;
167 */
160 } 168 }
161 // Make this reference to the physical shape go away since native shapes are not shared. 169 // Make this reference to the physical shape go away since native shapes are not shared.
162 public override void Dereference(BSScene physicsScene) 170 public override void Dereference(BSScene physicsScene)
163 { 171 {
172 /*
164 // Native shapes are not tracked and are released immediately 173 // Native shapes are not tracked and are released immediately
165 physicsScene.DetailLog("{0},BSShapeCollection.DereferenceShape,deleteNativeShape,shape={1}", BSScene.DetailLogZero, this); 174 physicsScene.DetailLog("{0},BSShapeCollection.DereferenceShape,deleteNativeShape,shape={1}", BSScene.DetailLogZero, this);
166 BulletSimAPI.DeleteCollisionShape2(physicsScene.World.ptr, ptr); 175 PhysicsScene.PE.DeleteCollisionShape(physicsScene.World, this);
167 ptr = IntPtr.Zero; 176 ptr = IntPtr.Zero;
168 // Garbage collection will free up this instance. 177 // Garbage collection will free up this instance.
178 */
169 } 179 }
170} 180}
171 181
@@ -205,4 +215,143 @@ public class BSShapeCompound : BSShape
205 } 215 }
206 public override void Dereference(BSScene physicsScene) { } 216 public override void Dereference(BSScene physicsScene) { }
207} 217}
218
219public class BSShapeAvatar : BSShape
220{
221 private static string LogHeader = "[BULLETSIM SHAPE AVATAR]";
222 public BSShapeAvatar() : base()
223 {
224 }
225 public static BSShape GetReference(BSPhysObject prim)
226 {
227 return new BSShapeNull();
228 }
229 public override void Dereference(BSScene physicsScene) { }
230
231 // From the front:
232 // A---A
233 // / \
234 // B-------B
235 // / \ +Z
236 // C-----------C |
237 // \ / -Y --+-- +Y
238 // \ / |
239 // \ / -Z
240 // D-----D
241 // \ /
242 // E-E
243
244 // From the top A and E are just lines.
245 // B, C and D are hexagons:
246 //
247 // C1--C2 +X
248 // / \ |
249 // C0 C3 -Y --+-- +Y
250 // \ / |
251 // C5--C4 -X
252
253 // Zero goes directly through the middle so the offsets are from that middle axis
254 // and up and down from a middle horizon (A and E are the same distance from the zero).
255 // The height, width and depth is one. All scaling is done by the simulator.
256
257 // Z component -- how far the level is from the middle zero
258 private const float Aup = 0.5f;
259 private const float Bup = 0.4f;
260 private const float Cup = 0.3f;
261 private const float Dup = -0.4f;
262 private const float Eup = -0.5f;
263
264 // Y component -- distance from center to x0 and x3
265 private const float Awid = 0.25f;
266 private const float Bwid = 0.3f;
267 private const float Cwid = 0.5f;
268 private const float Dwid = 0.3f;
269 private const float Ewid = 0.2f;
270
271 // Y component -- distance from center to x1, x2, x4 and x5
272 private const float Afwid = 0.0f;
273 private const float Bfwid = 0.2f;
274 private const float Cfwid = 0.4f;
275 private const float Dfwid = 0.2f;
276 private const float Efwid = 0.0f;
277
278 // X component -- distance from zero to the front or back of a level
279 private const float Adep = 0f;
280 private const float Bdep = 0.3f;
281 private const float Cdep = 0.5f;
282 private const float Ddep = 0.2f;
283 private const float Edep = 0f;
284
285 private OMV.Vector3[] avatarVertices = {
286 new OMV.Vector3( 0.0f, -Awid, Aup), // A0
287 new OMV.Vector3( 0.0f, +Awid, Aup), // A3
288
289 new OMV.Vector3( 0.0f, -Bwid, Bup), // B0
290 new OMV.Vector3(+Bdep, -Bfwid, Bup), // B1
291 new OMV.Vector3(+Bdep, +Bfwid, Bup), // B2
292 new OMV.Vector3( 0.0f, +Bwid, Bup), // B3
293 new OMV.Vector3(-Bdep, +Bfwid, Bup), // B4
294 new OMV.Vector3(-Bdep, -Bfwid, Bup), // B5
295
296 new OMV.Vector3( 0.0f, -Cwid, Cup), // C0
297 new OMV.Vector3(+Cdep, -Cfwid, Cup), // C1
298 new OMV.Vector3(+Cdep, +Cfwid, Cup), // C2
299 new OMV.Vector3( 0.0f, +Cwid, Cup), // C3
300 new OMV.Vector3(-Cdep, +Cfwid, Cup), // C4
301 new OMV.Vector3(-Cdep, -Cfwid, Cup), // C5
302
303 new OMV.Vector3( 0.0f, -Dwid, Dup), // D0
304 new OMV.Vector3(+Ddep, -Dfwid, Dup), // D1
305 new OMV.Vector3(+Ddep, +Dfwid, Dup), // D2
306 new OMV.Vector3( 0.0f, +Dwid, Dup), // D3
307 new OMV.Vector3(-Ddep, +Dfwid, Dup), // D4
308 new OMV.Vector3(-Ddep, -Dfwid, Dup), // D5
309
310 new OMV.Vector3( 0.0f, -Ewid, Eup), // E0
311 new OMV.Vector3( 0.0f, +Ewid, Eup), // E3
312 };
313
314 // Offsets of the vertices in the vertices array
315 private enum Ind : int
316 {
317 A0, A3,
318 B0, B1, B2, B3, B4, B5,
319 C0, C1, C2, C3, C4, C5,
320 D0, D1, D2, D3, D4, D5,
321 E0, E3
322 }
323
324 // Comments specify trianges and quads in clockwise direction
325 private Ind[] avatarIndices = {
326 Ind.A0, Ind.B0, Ind.B1, // A0,B0,B1
327 Ind.A0, Ind.B1, Ind.B2, Ind.B2, Ind.A3, Ind.A0, // A0,B1,B2,A3
328 Ind.A3, Ind.B2, Ind.B3, // A3,B2,B3
329 Ind.A3, Ind.B3, Ind.B4, // A3,B3,B4
330 Ind.A3, Ind.B4, Ind.B5, Ind.B5, Ind.A0, Ind.A3, // A3,B4,B5,A0
331 Ind.A0, Ind.B5, Ind.B0, // A0,B5,B0
332
333 Ind.B0, Ind.C0, Ind.C1, Ind.C1, Ind.B1, Ind.B0, // B0,C0,C1,B1
334 Ind.B1, Ind.C1, Ind.C2, Ind.C2, Ind.B2, Ind.B1, // B1,C1,C2,B2
335 Ind.B2, Ind.C2, Ind.C3, Ind.C3, Ind.B3, Ind.B2, // B2,C2,C3,B3
336 Ind.B3, Ind.C3, Ind.C4, Ind.C4, Ind.B4, Ind.B3, // B3,C3,C4,B4
337 Ind.B4, Ind.C4, Ind.C5, Ind.C5, Ind.B5, Ind.B4, // B4,C4,C5,B5
338 Ind.B5, Ind.C5, Ind.C0, Ind.C0, Ind.B0, Ind.B5, // B5,C5,C0,B0
339
340 Ind.C0, Ind.D0, Ind.D1, Ind.D1, Ind.C1, Ind.C0, // C0,D0,D1,C1
341 Ind.C1, Ind.D1, Ind.D2, Ind.D2, Ind.C2, Ind.C1, // C1,D1,D2,C2
342 Ind.C2, Ind.D2, Ind.D3, Ind.D3, Ind.C3, Ind.C2, // C2,D2,D3,C3
343 Ind.C3, Ind.D3, Ind.D4, Ind.D4, Ind.C4, Ind.C3, // C3,D3,D4,C4
344 Ind.C4, Ind.D4, Ind.D5, Ind.D5, Ind.C5, Ind.C4, // C4,D4,D5,C5
345 Ind.C5, Ind.D5, Ind.D0, Ind.D0, Ind.C0, Ind.C5, // C5,D5,D0,C0
346
347 Ind.E0, Ind.D0, Ind.D1, // E0,D0,D1
348 Ind.E0, Ind.D1, Ind.D2, Ind.D2, Ind.E3, Ind.E0, // E0,D1,D2,E3
349 Ind.E3, Ind.D2, Ind.D3, // E3,D2,D3
350 Ind.E3, Ind.D3, Ind.D4, // E3,D3,D4
351 Ind.E3, Ind.D4, Ind.D5, Ind.D5, Ind.E0, Ind.E3, // E3,D4,D5,E0
352 Ind.E0, Ind.D5, Ind.D0, // E0,D5,D0
353
354 };
355
356}
208} 357}
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainHeightmap.cs b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainHeightmap.cs
index 3ca756c..e4fecc3 100755
--- a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainHeightmap.cs
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainHeightmap.cs
@@ -44,7 +44,7 @@ public sealed class BSTerrainHeightmap : BSTerrainPhys
44{ 44{
45 static string LogHeader = "[BULLETSIM TERRAIN HEIGHTMAP]"; 45 static string LogHeader = "[BULLETSIM TERRAIN HEIGHTMAP]";
46 46
47 BulletHeightMapInfo m_mapInfo = null; 47 BulletHMapInfo m_mapInfo = null;
48 48
49 // Constructor to build a default, flat heightmap terrain. 49 // Constructor to build a default, flat heightmap terrain.
50 public BSTerrainHeightmap(BSScene physicsScene, Vector3 regionBase, uint id, Vector3 regionSize) 50 public BSTerrainHeightmap(BSScene physicsScene, Vector3 regionBase, uint id, Vector3 regionSize)
@@ -58,7 +58,7 @@ public sealed class BSTerrainHeightmap : BSTerrainPhys
58 { 58 {
59 initialMap[ii] = BSTerrainManager.HEIGHT_INITIALIZATION; 59 initialMap[ii] = BSTerrainManager.HEIGHT_INITIALIZATION;
60 } 60 }
61 m_mapInfo = new BulletHeightMapInfo(id, initialMap, IntPtr.Zero); 61 m_mapInfo = new BulletHMapInfo(id, initialMap);
62 m_mapInfo.minCoords = minTerrainCoords; 62 m_mapInfo.minCoords = minTerrainCoords;
63 m_mapInfo.maxCoords = maxTerrainCoords; 63 m_mapInfo.maxCoords = maxTerrainCoords;
64 m_mapInfo.terrainRegionBase = TerrainBase; 64 m_mapInfo.terrainRegionBase = TerrainBase;
@@ -72,7 +72,7 @@ public sealed class BSTerrainHeightmap : BSTerrainPhys
72 Vector3 minCoords, Vector3 maxCoords) 72 Vector3 minCoords, Vector3 maxCoords)
73 : base(physicsScene, regionBase, id) 73 : base(physicsScene, regionBase, id)
74 { 74 {
75 m_mapInfo = new BulletHeightMapInfo(id, initialMap, IntPtr.Zero); 75 m_mapInfo = new BulletHMapInfo(id, initialMap);
76 m_mapInfo.minCoords = minCoords; 76 m_mapInfo.minCoords = minCoords;
77 m_mapInfo.maxCoords = maxCoords; 77 m_mapInfo.maxCoords = maxCoords;
78 m_mapInfo.minZ = minCoords.Z; 78 m_mapInfo.minZ = minCoords.Z;
@@ -91,13 +91,11 @@ public sealed class BSTerrainHeightmap : BSTerrainPhys
91 // Using the information in m_mapInfo, create the physical representation of the heightmap. 91 // Using the information in m_mapInfo, create the physical representation of the heightmap.
92 private void BuildHeightmapTerrain() 92 private void BuildHeightmapTerrain()
93 { 93 {
94 m_mapInfo.Ptr = BulletSimAPI.CreateHeightMapInfo2(PhysicsScene.World.ptr, m_mapInfo.ID,
95 m_mapInfo.minCoords, m_mapInfo.maxCoords,
96 m_mapInfo.heightMap, BSTerrainManager.TERRAIN_COLLISION_MARGIN);
97
98 // Create the terrain shape from the mapInfo 94 // Create the terrain shape from the mapInfo
99 m_mapInfo.terrainShape = new BulletShape(BulletSimAPI.CreateTerrainShape2(m_mapInfo.Ptr), 95 m_mapInfo.terrainShape = PhysicsScene.PE.CreateTerrainShape( m_mapInfo.ID,
100 BSPhysicsShapeType.SHAPE_TERRAIN); 96 new Vector3(m_mapInfo.sizeX, m_mapInfo.sizeY, 0), m_mapInfo.minZ, m_mapInfo.maxZ,
97 m_mapInfo.heightMap, 1f, BSParam.TerrainCollisionMargin);
98
101 99
102 // The terrain object initial position is at the center of the object 100 // The terrain object initial position is at the center of the object
103 Vector3 centerPos; 101 Vector3 centerPos;
@@ -105,28 +103,26 @@ public sealed class BSTerrainHeightmap : BSTerrainPhys
105 centerPos.Y = m_mapInfo.minCoords.Y + (m_mapInfo.sizeY / 2f); 103 centerPos.Y = m_mapInfo.minCoords.Y + (m_mapInfo.sizeY / 2f);
106 centerPos.Z = m_mapInfo.minZ + ((m_mapInfo.maxZ - m_mapInfo.minZ) / 2f); 104 centerPos.Z = m_mapInfo.minZ + ((m_mapInfo.maxZ - m_mapInfo.minZ) / 2f);
107 105
108 m_mapInfo.terrainBody = new BulletBody(m_mapInfo.ID, 106 m_mapInfo.terrainBody = PhysicsScene.PE.CreateBodyWithDefaultMotionState(m_mapInfo.terrainShape,
109 BulletSimAPI.CreateBodyWithDefaultMotionState2(m_mapInfo.terrainShape.ptr, 107 m_mapInfo.ID, centerPos, Quaternion.Identity);
110 m_mapInfo.ID, centerPos, Quaternion.Identity));
111 108
112 // Set current terrain attributes 109 // Set current terrain attributes
113 BulletSimAPI.SetFriction2(m_mapInfo.terrainBody.ptr, PhysicsScene.Params.terrainFriction); 110 PhysicsScene.PE.SetFriction(m_mapInfo.terrainBody, BSParam.TerrainFriction);
114 BulletSimAPI.SetHitFraction2(m_mapInfo.terrainBody.ptr, PhysicsScene.Params.terrainHitFraction); 111 PhysicsScene.PE.SetHitFraction(m_mapInfo.terrainBody, BSParam.TerrainHitFraction);
115 BulletSimAPI.SetRestitution2(m_mapInfo.terrainBody.ptr, PhysicsScene.Params.terrainRestitution); 112 PhysicsScene.PE.SetRestitution(m_mapInfo.terrainBody, BSParam.TerrainRestitution);
116 BulletSimAPI.SetCollisionFlags2(m_mapInfo.terrainBody.ptr, CollisionFlags.CF_STATIC_OBJECT); 113 PhysicsScene.PE.SetCollisionFlags(m_mapInfo.terrainBody, CollisionFlags.CF_STATIC_OBJECT);
117 114
118 // Return the new terrain to the world of physical objects 115 // Return the new terrain to the world of physical objects
119 BulletSimAPI.AddObjectToWorld2(PhysicsScene.World.ptr, m_mapInfo.terrainBody.ptr); 116 PhysicsScene.PE.AddObjectToWorld(PhysicsScene.World, m_mapInfo.terrainBody);
120 117
121 // redo its bounding box now that it is in the world 118 // redo its bounding box now that it is in the world
122 BulletSimAPI.UpdateSingleAabb2(PhysicsScene.World.ptr, m_mapInfo.terrainBody.ptr); 119 PhysicsScene.PE.UpdateSingleAabb(PhysicsScene.World, m_mapInfo.terrainBody);
123 120
124 BulletSimAPI.SetCollisionFilterMask2(m_mapInfo.terrainBody.ptr, 121 m_mapInfo.terrainBody.collisionType = CollisionType.Terrain;
125 (uint)CollisionFilterGroups.TerrainFilter, 122 m_mapInfo.terrainBody.ApplyCollisionMask(PhysicsScene);
126 (uint)CollisionFilterGroups.TerrainMask);
127 123
128 // Make it so the terrain will not move or be considered for movement. 124 // Make it so the terrain will not move or be considered for movement.
129 BulletSimAPI.ForceActivationState2(m_mapInfo.terrainBody.ptr, ActivationState.DISABLE_SIMULATION); 125 PhysicsScene.PE.ForceActivationState(m_mapInfo.terrainBody, ActivationState.DISABLE_SIMULATION);
130 126
131 return; 127 return;
132 } 128 }
@@ -136,19 +132,18 @@ public sealed class BSTerrainHeightmap : BSTerrainPhys
136 { 132 {
137 if (m_mapInfo != null) 133 if (m_mapInfo != null)
138 { 134 {
139 if (m_mapInfo.terrainBody.ptr != IntPtr.Zero) 135 if (m_mapInfo.terrainBody.HasPhysicalBody)
140 { 136 {
141 BulletSimAPI.RemoveObjectFromWorld2(PhysicsScene.World.ptr, m_mapInfo.terrainBody.ptr); 137 PhysicsScene.PE.RemoveObjectFromWorld(PhysicsScene.World, m_mapInfo.terrainBody);
142 // Frees both the body and the shape. 138 // Frees both the body and the shape.
143 BulletSimAPI.DestroyObject2(PhysicsScene.World.ptr, m_mapInfo.terrainBody.ptr); 139 PhysicsScene.PE.DestroyObject(PhysicsScene.World, m_mapInfo.terrainBody);
144 BulletSimAPI.ReleaseHeightMapInfo2(m_mapInfo.Ptr);
145 } 140 }
146 } 141 }
147 m_mapInfo = null; 142 m_mapInfo = null;
148 } 143 }
149 144
150 // The passed position is relative to the base of the region. 145 // The passed position is relative to the base of the region.
151 public override float GetHeightAtXYZ(Vector3 pos) 146 public override float GetTerrainHeightAtXYZ(Vector3 pos)
152 { 147 {
153 float ret = BSTerrainManager.HEIGHT_GETHEIGHT_RET; 148 float ret = BSTerrainManager.HEIGHT_GETHEIGHT_RET;
154 149
@@ -166,5 +161,11 @@ public sealed class BSTerrainHeightmap : BSTerrainPhys
166 } 161 }
167 return ret; 162 return ret;
168 } 163 }
164
165 // The passed position is relative to the base of the region.
166 public override float GetWaterLevelAtXYZ(Vector3 pos)
167 {
168 return PhysicsScene.SimpleWaterLevel;
169 }
169} 170}
170} 171}
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainManager.cs b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainManager.cs
index 23fcfd3..2e9db39 100755
--- a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainManager.cs
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainManager.cs
@@ -62,11 +62,12 @@ public abstract class BSTerrainPhys : IDisposable
62 ID = id; 62 ID = id;
63 } 63 }
64 public abstract void Dispose(); 64 public abstract void Dispose();
65 public abstract float GetHeightAtXYZ(Vector3 pos); 65 public abstract float GetTerrainHeightAtXYZ(Vector3 pos);
66 public abstract float GetWaterLevelAtXYZ(Vector3 pos);
66} 67}
67 68
68// ========================================================================================== 69// ==========================================================================================
69public sealed class BSTerrainManager 70public sealed class BSTerrainManager : IDisposable
70{ 71{
71 static string LogHeader = "[BULLETSIM TERRAIN MANAGER]"; 72 static string LogHeader = "[BULLETSIM TERRAIN MANAGER]";
72 73
@@ -75,13 +76,12 @@ public sealed class BSTerrainManager
75 public const float HEIGHT_INITIALIZATION = 24.987f; 76 public const float HEIGHT_INITIALIZATION = 24.987f;
76 public const float HEIGHT_INITIAL_LASTHEIGHT = 24.876f; 77 public const float HEIGHT_INITIAL_LASTHEIGHT = 24.876f;
77 public const float HEIGHT_GETHEIGHT_RET = 24.765f; 78 public const float HEIGHT_GETHEIGHT_RET = 24.765f;
79 public const float WATER_HEIGHT_GETHEIGHT_RET = 19.998f;
78 80
79 // If the min and max height are equal, we reduce the min by this 81 // If the min and max height are equal, we reduce the min by this
80 // amount to make sure that a bounding box is built for the terrain. 82 // amount to make sure that a bounding box is built for the terrain.
81 public const float HEIGHT_EQUAL_FUDGE = 0.2f; 83 public const float HEIGHT_EQUAL_FUDGE = 0.2f;
82 84
83 public const float TERRAIN_COLLISION_MARGIN = 0.0f;
84
85 // Until the whole simulator is changed to pass us the region size, we rely on constants. 85 // Until the whole simulator is changed to pass us the region size, we rely on constants.
86 public Vector3 DefaultRegionSize = new Vector3(Constants.RegionSize, Constants.RegionSize, Constants.RegionHeight); 86 public Vector3 DefaultRegionSize = new Vector3(Constants.RegionSize, Constants.RegionSize, Constants.RegionHeight);
87 87
@@ -122,25 +122,28 @@ public sealed class BSTerrainManager
122 MegaRegionParentPhysicsScene = null; 122 MegaRegionParentPhysicsScene = null;
123 } 123 }
124 124
125 public void Dispose()
126 {
127 ReleaseGroundPlaneAndTerrain();
128 }
129
125 // Create the initial instance of terrain and the underlying ground plane. 130 // Create the initial instance of terrain and the underlying ground plane.
126 // This is called from the initialization routine so we presume it is 131 // This is called from the initialization routine so we presume it is
127 // safe to call Bullet in real time. We hope no one is moving prims around yet. 132 // safe to call Bullet in real time. We hope no one is moving prims around yet.
128 public void CreateInitialGroundPlaneAndTerrain() 133 public void CreateInitialGroundPlaneAndTerrain()
129 { 134 {
130 // The ground plane is here to catch things that are trying to drop to negative infinity 135 // The ground plane is here to catch things that are trying to drop to negative infinity
131 BulletShape groundPlaneShape = new BulletShape( 136 BulletShape groundPlaneShape = PhysicsScene.PE.CreateGroundPlaneShape(BSScene.GROUNDPLANE_ID, 1f, BSParam.TerrainCollisionMargin);
132 BulletSimAPI.CreateGroundPlaneShape2(BSScene.GROUNDPLANE_ID, 1f, TERRAIN_COLLISION_MARGIN), 137 m_groundPlane = PhysicsScene.PE.CreateBodyWithDefaultMotionState(groundPlaneShape,
133 BSPhysicsShapeType.SHAPE_GROUNDPLANE); 138 BSScene.GROUNDPLANE_ID, Vector3.Zero, Quaternion.Identity);
134 m_groundPlane = new BulletBody(BSScene.GROUNDPLANE_ID, 139
135 BulletSimAPI.CreateBodyWithDefaultMotionState2(groundPlaneShape.ptr, BSScene.GROUNDPLANE_ID, 140 PhysicsScene.PE.AddObjectToWorld(PhysicsScene.World, m_groundPlane);
136 Vector3.Zero, Quaternion.Identity)); 141 PhysicsScene.PE.UpdateSingleAabb(PhysicsScene.World, m_groundPlane);
137 BulletSimAPI.AddObjectToWorld2(PhysicsScene.World.ptr, m_groundPlane.ptr);
138 BulletSimAPI.UpdateSingleAabb2(PhysicsScene.World.ptr, m_groundPlane.ptr);
139 // Ground plane does not move 142 // Ground plane does not move
140 BulletSimAPI.ForceActivationState2(m_groundPlane.ptr, ActivationState.DISABLE_SIMULATION); 143 PhysicsScene.PE.ForceActivationState(m_groundPlane, ActivationState.DISABLE_SIMULATION);
141 // Everything collides with the ground plane. 144 // Everything collides with the ground plane.
142 BulletSimAPI.SetCollisionFilterMask2(m_groundPlane.ptr, 145 m_groundPlane.collisionType = CollisionType.Groundplane;
143 (uint)CollisionFilterGroups.GroundPlaneFilter, (uint)CollisionFilterGroups.GroundPlaneMask); 146 m_groundPlane.ApplyCollisionMask(PhysicsScene);
144 147
145 // Build an initial terrain and put it in the world. This quickly gets replaced by the real region terrain. 148 // Build an initial terrain and put it in the world. This quickly gets replaced by the real region terrain.
146 BSTerrainPhys initialTerrain = new BSTerrainHeightmap(PhysicsScene, Vector3.Zero, BSScene.TERRAIN_ID, DefaultRegionSize); 149 BSTerrainPhys initialTerrain = new BSTerrainHeightmap(PhysicsScene, Vector3.Zero, BSScene.TERRAIN_ID, DefaultRegionSize);
@@ -150,13 +153,13 @@ public sealed class BSTerrainManager
150 // Release all the terrain structures we might have allocated 153 // Release all the terrain structures we might have allocated
151 public void ReleaseGroundPlaneAndTerrain() 154 public void ReleaseGroundPlaneAndTerrain()
152 { 155 {
153 if (m_groundPlane.ptr != IntPtr.Zero) 156 if (m_groundPlane.HasPhysicalBody)
154 { 157 {
155 if (BulletSimAPI.RemoveObjectFromWorld2(PhysicsScene.World.ptr, m_groundPlane.ptr)) 158 if (PhysicsScene.PE.RemoveObjectFromWorld(PhysicsScene.World, m_groundPlane))
156 { 159 {
157 BulletSimAPI.DestroyObject2(PhysicsScene.World.ptr, m_groundPlane.ptr); 160 PhysicsScene.PE.DestroyObject(PhysicsScene.World, m_groundPlane);
158 } 161 }
159 m_groundPlane.ptr = IntPtr.Zero; 162 m_groundPlane.Clear();
160 } 163 }
161 164
162 ReleaseTerrain(); 165 ReleaseTerrain();
@@ -165,17 +168,22 @@ public sealed class BSTerrainManager
165 // Release all the terrain we have allocated 168 // Release all the terrain we have allocated
166 public void ReleaseTerrain() 169 public void ReleaseTerrain()
167 { 170 {
168 foreach (KeyValuePair<Vector3, BSTerrainPhys> kvp in m_terrains) 171 lock (m_terrains)
169 { 172 {
170 kvp.Value.Dispose(); 173 foreach (KeyValuePair<Vector3, BSTerrainPhys> kvp in m_terrains)
174 {
175 kvp.Value.Dispose();
176 }
177 m_terrains.Clear();
171 } 178 }
172 m_terrains.Clear();
173 } 179 }
174 180
175 // The simulator wants to set a new heightmap for the terrain. 181 // The simulator wants to set a new heightmap for the terrain.
176 public void SetTerrain(float[] heightMap) { 182 public void SetTerrain(float[] heightMap) {
177 float[] localHeightMap = heightMap; 183 float[] localHeightMap = heightMap;
178 PhysicsScene.TaintedObject("TerrainManager.SetTerrain", delegate() 184 // If there are multiple requests for changes to the same terrain between ticks,
185 // only do that last one.
186 PhysicsScene.PostTaintObject("TerrainManager.SetTerrain-"+ m_worldOffset.ToString(), 0, delegate()
179 { 187 {
180 if (m_worldOffset != Vector3.Zero && MegaRegionParentPhysicsScene != null) 188 if (m_worldOffset != Vector3.Zero && MegaRegionParentPhysicsScene != null)
181 { 189 {
@@ -211,6 +219,7 @@ public sealed class BSTerrainManager
211 // terrain shape is created and added to the body. 219 // terrain shape is created and added to the body.
212 // This call is most often used to update the heightMap and parameters of the terrain. 220 // This call is most often used to update the heightMap and parameters of the terrain.
213 // (The above does suggest that some simplification/refactoring is in order.) 221 // (The above does suggest that some simplification/refactoring is in order.)
222 // Called during taint-time.
214 private void UpdateTerrain(uint id, float[] heightMap, 223 private void UpdateTerrain(uint id, float[] heightMap,
215 Vector3 minCoords, Vector3 maxCoords, bool inTaintTime) 224 Vector3 minCoords, Vector3 maxCoords, bool inTaintTime)
216 { 225 {
@@ -220,7 +229,7 @@ public sealed class BSTerrainManager
220 // Find high and low points of passed heightmap. 229 // Find high and low points of passed heightmap.
221 // The min and max passed in is usually the area objects can be in (maximum 230 // The min and max passed in is usually the area objects can be in (maximum
222 // object height, for instance). The terrain wants the bounding box for the 231 // object height, for instance). The terrain wants the bounding box for the
223 // terrain so we replace passed min and max Z with the actual terrain min/max Z. 232 // terrain so replace passed min and max Z with the actual terrain min/max Z.
224 float minZ = float.MaxValue; 233 float minZ = float.MaxValue;
225 float maxZ = float.MinValue; 234 float maxZ = float.MinValue;
226 foreach (float height in heightMap) 235 foreach (float height in heightMap)
@@ -238,15 +247,15 @@ public sealed class BSTerrainManager
238 247
239 Vector3 terrainRegionBase = new Vector3(minCoords.X, minCoords.Y, 0f); 248 Vector3 terrainRegionBase = new Vector3(minCoords.X, minCoords.Y, 0f);
240 249
241 BSTerrainPhys terrainPhys; 250 lock (m_terrains)
242 if (m_terrains.TryGetValue(terrainRegionBase, out terrainPhys))
243 { 251 {
244 // There is already a terrain in this spot. Free the old and build the new. 252 BSTerrainPhys terrainPhys;
245 DetailLog("{0},UpdateTerrain:UpdateExisting,call,id={1},base={2},minC={3},maxC={4}", 253 if (m_terrains.TryGetValue(terrainRegionBase, out terrainPhys))
246 BSScene.DetailLogZero, id, terrainRegionBase, minCoords, minCoords);
247
248 PhysicsScene.TaintedObject(inTaintTime, "BSScene.UpdateTerrain:UpdateExisting", delegate()
249 { 254 {
255 // There is already a terrain in this spot. Free the old and build the new.
256 DetailLog("{0},UpdateTerrain:UpdateExisting,call,id={1},base={2},minC={3},maxC={4}",
257 BSScene.DetailLogZero, id, terrainRegionBase, minCoords, minCoords);
258
250 // Remove old terrain from the collection 259 // Remove old terrain from the collection
251 m_terrains.Remove(terrainRegionBase); 260 m_terrains.Remove(terrainRegionBase);
252 // Release any physical memory it may be using. 261 // Release any physical memory it may be using.
@@ -271,35 +280,24 @@ public sealed class BSTerrainManager
271 // I hate doing this, but just bail 280 // I hate doing this, but just bail
272 return; 281 return;
273 } 282 }
274 }); 283 }
275 } 284 else
276 else 285 {
277 { 286 // We don't know about this terrain so either we are creating a new terrain or
278 // We don't know about this terrain so either we are creating a new terrain or 287 // our mega-prim child is giving us a new terrain to add to the phys world
279 // our mega-prim child is giving us a new terrain to add to the phys world
280
281 // if this is a child terrain, calculate a unique terrain id
282 uint newTerrainID = id;
283 if (newTerrainID >= BSScene.CHILDTERRAIN_ID)
284 newTerrainID = ++m_terrainCount;
285
286 float[] heightMapX = heightMap;
287 Vector3 minCoordsX = minCoords;
288 Vector3 maxCoordsX = maxCoords;
289 288
290 DetailLog("{0},UpdateTerrain:NewTerrain,call,id={1}, minC={2}, maxC={3}", 289 // if this is a child terrain, calculate a unique terrain id
291 BSScene.DetailLogZero, newTerrainID, minCoords, minCoords); 290 uint newTerrainID = id;
291 if (newTerrainID >= BSScene.CHILDTERRAIN_ID)
292 newTerrainID = ++m_terrainCount;
292 293
293 // Code that must happen at taint-time 294 DetailLog("{0},UpdateTerrain:NewTerrain,taint,newID={1},minCoord={2},maxCoord={3}",
294 PhysicsScene.TaintedObject(inTaintTime, "BSScene.UpdateTerrain:NewTerrain", delegate() 295 BSScene.DetailLogZero, newTerrainID, minCoords, minCoords);
295 {
296 DetailLog("{0},UpdateTerrain:NewTerrain,taint,baseX={1},baseY={2}",
297 BSScene.DetailLogZero, minCoordsX.X, minCoordsX.Y);
298 BSTerrainPhys newTerrainPhys = BuildPhysicalTerrain(terrainRegionBase, id, heightMap, minCoords, maxCoords); 296 BSTerrainPhys newTerrainPhys = BuildPhysicalTerrain(terrainRegionBase, id, heightMap, minCoords, maxCoords);
299 m_terrains.Add(terrainRegionBase, newTerrainPhys); 297 m_terrains.Add(terrainRegionBase, newTerrainPhys);
300 298
301 m_terrainModified = true; 299 m_terrainModified = true;
302 }); 300 }
303 } 301 }
304 } 302 }
305 303
@@ -308,9 +306,9 @@ public sealed class BSTerrainManager
308 { 306 {
309 PhysicsScene.Logger.DebugFormat("{0} Terrain for {1}/{2} created with {3}", 307 PhysicsScene.Logger.DebugFormat("{0} Terrain for {1}/{2} created with {3}",
310 LogHeader, PhysicsScene.RegionName, terrainRegionBase, 308 LogHeader, PhysicsScene.RegionName, terrainRegionBase,
311 (BSTerrainPhys.TerrainImplementation)PhysicsScene.Params.terrainImplementation); 309 (BSTerrainPhys.TerrainImplementation)BSParam.TerrainImplementation);
312 BSTerrainPhys newTerrainPhys = null; 310 BSTerrainPhys newTerrainPhys = null;
313 switch ((int)PhysicsScene.Params.terrainImplementation) 311 switch ((int)BSParam.TerrainImplementation)
314 { 312 {
315 case (int)BSTerrainPhys.TerrainImplementation.Heightmap: 313 case (int)BSTerrainPhys.TerrainImplementation.Heightmap:
316 newTerrainPhys = new BSTerrainHeightmap(PhysicsScene, terrainRegionBase, id, 314 newTerrainPhys = new BSTerrainHeightmap(PhysicsScene, terrainRegionBase, id,
@@ -323,14 +321,21 @@ public sealed class BSTerrainManager
323 default: 321 default:
324 PhysicsScene.Logger.ErrorFormat("{0} Bad terrain implementation specified. Type={1}/{2},Region={3}/{4}", 322 PhysicsScene.Logger.ErrorFormat("{0} Bad terrain implementation specified. Type={1}/{2},Region={3}/{4}",
325 LogHeader, 323 LogHeader,
326 (int)PhysicsScene.Params.terrainImplementation, 324 (int)BSParam.TerrainImplementation,
327 PhysicsScene.Params.terrainImplementation, 325 BSParam.TerrainImplementation,
328 PhysicsScene.RegionName, terrainRegionBase); 326 PhysicsScene.RegionName, terrainRegionBase);
329 break; 327 break;
330 } 328 }
331 return newTerrainPhys; 329 return newTerrainPhys;
332 } 330 }
333 331
332 // Return 'true' of this position is somewhere in known physical terrain space
333 public bool IsWithinKnownTerrain(Vector3 pos)
334 {
335 Vector3 terrainBaseXYZ;
336 BSTerrainPhys physTerrain;
337 return GetTerrainPhysicalAtXYZ(pos, out physTerrain, out terrainBaseXYZ);
338 }
334 339
335 // Given an X and Y, find the height of the terrain. 340 // Given an X and Y, find the height of the terrain.
336 // Since we could be handling multiple terrains for a mega-region, 341 // Since we could be handling multiple terrains for a mega-region,
@@ -341,40 +346,74 @@ public sealed class BSTerrainManager
341 private float lastHeightTX = 999999f; 346 private float lastHeightTX = 999999f;
342 private float lastHeightTY = 999999f; 347 private float lastHeightTY = 999999f;
343 private float lastHeight = HEIGHT_INITIAL_LASTHEIGHT; 348 private float lastHeight = HEIGHT_INITIAL_LASTHEIGHT;
344 public float GetTerrainHeightAtXYZ(Vector3 loc) 349 public float GetTerrainHeightAtXYZ(Vector3 pos)
345 { 350 {
346 float tX = loc.X; 351 float tX = pos.X;
347 float tY = loc.Y; 352 float tY = pos.Y;
348 // You'd be surprized at the number of times this routine is called 353 // You'd be surprized at the number of times this routine is called
349 // with the same parameters as last time. 354 // with the same parameters as last time.
350 if (!m_terrainModified && lastHeightTX == tX && lastHeightTY == tY) 355 if (!m_terrainModified && (lastHeightTX == tX) && (lastHeightTY == tY))
351 return lastHeight; 356 return lastHeight;
357 m_terrainModified = false;
352 358
353 lastHeightTX = tX; 359 lastHeightTX = tX;
354 lastHeightTY = tY; 360 lastHeightTY = tY;
355 float ret = HEIGHT_GETHEIGHT_RET; 361 float ret = HEIGHT_GETHEIGHT_RET;
356 362
357 int offsetX = ((int)(tX / (int)DefaultRegionSize.X)) * (int)DefaultRegionSize.X; 363 Vector3 terrainBaseXYZ;
358 int offsetY = ((int)(tY / (int)DefaultRegionSize.Y)) * (int)DefaultRegionSize.Y;
359 Vector3 terrainBaseXYZ = new Vector3(offsetX, offsetY, 0f);
360
361 BSTerrainPhys physTerrain; 364 BSTerrainPhys physTerrain;
362 if (m_terrains.TryGetValue(terrainBaseXYZ, out physTerrain)) 365 if (GetTerrainPhysicalAtXYZ(pos, out physTerrain, out terrainBaseXYZ))
363 { 366 {
364 ret = physTerrain.GetHeightAtXYZ(loc - terrainBaseXYZ); 367 ret = physTerrain.GetTerrainHeightAtXYZ(pos - terrainBaseXYZ);
365 DetailLog("{0},BSTerrainManager.GetTerrainHeightAtXYZ,loc={1},base={2},height={3}",
366 BSScene.DetailLogZero, loc, terrainBaseXYZ, ret);
367 } 368 }
368 else 369 else
369 { 370 {
370 PhysicsScene.Logger.ErrorFormat("{0} GetTerrainHeightAtXY: terrain not found: region={1}, x={2}, y={3}", 371 PhysicsScene.Logger.ErrorFormat("{0} GetTerrainHeightAtXY: terrain not found: region={1}, x={2}, y={3}",
371 LogHeader, PhysicsScene.RegionName, tX, tY); 372 LogHeader, PhysicsScene.RegionName, tX, tY);
373 DetailLog("{0},BSTerrainManager.GetTerrainHeightAtXYZ,terrainNotFound,pos={1},base={2}",
374 BSScene.DetailLogZero, pos, terrainBaseXYZ);
372 } 375 }
373 m_terrainModified = false; 376
374 lastHeight = ret; 377 lastHeight = ret;
375 return ret; 378 return ret;
376 } 379 }
377 380
381 public float GetWaterLevelAtXYZ(Vector3 pos)
382 {
383 float ret = WATER_HEIGHT_GETHEIGHT_RET;
384
385 Vector3 terrainBaseXYZ;
386 BSTerrainPhys physTerrain;
387 if (GetTerrainPhysicalAtXYZ(pos, out physTerrain, out terrainBaseXYZ))
388 {
389 ret = physTerrain.GetWaterLevelAtXYZ(pos);
390 }
391 else
392 {
393 PhysicsScene.Logger.ErrorFormat("{0} GetWaterHeightAtXY: terrain not found: pos={1}, terrainBase={2}, height={3}",
394 LogHeader, PhysicsScene.RegionName, pos, terrainBaseXYZ, ret);
395 }
396 return ret;
397 }
398
399 // Given an address, return 'true' of there is a description of that terrain and output
400 // the descriptor class and the 'base' fo the addresses therein.
401 private bool GetTerrainPhysicalAtXYZ(Vector3 pos, out BSTerrainPhys outPhysTerrain, out Vector3 outTerrainBase)
402 {
403 int offsetX = ((int)(pos.X / (int)DefaultRegionSize.X)) * (int)DefaultRegionSize.X;
404 int offsetY = ((int)(pos.Y / (int)DefaultRegionSize.Y)) * (int)DefaultRegionSize.Y;
405 Vector3 terrainBaseXYZ = new Vector3(offsetX, offsetY, 0f);
406
407 BSTerrainPhys physTerrain = null;
408 lock (m_terrains)
409 {
410 m_terrains.TryGetValue(terrainBaseXYZ, out physTerrain);
411 }
412 outTerrainBase = terrainBaseXYZ;
413 outPhysTerrain = physTerrain;
414 return (physTerrain != null);
415 }
416
378 // Although no one seems to check this, I do support combining. 417 // Although no one seems to check this, I do support combining.
379 public bool SupportsCombining() 418 public bool SupportsCombining()
380 { 419 {
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs
index dca7150..8244f02 100755
--- a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs
@@ -88,11 +88,11 @@ public sealed class BSTerrainMesh : BSTerrainPhys
88 // Something is very messed up and a crash is in our future. 88 // Something is very messed up and a crash is in our future.
89 return; 89 return;
90 } 90 }
91 PhysicsScene.DetailLog("{0},BSTerrainMesh.create,meshed,indices={1},indSz={2},vertices={3},vertSz={4}",
92 ID, indicesCount, indices.Length, verticesCount, vertices.Length);
91 93
92 m_terrainShape = new BulletShape(BulletSimAPI.CreateMeshShape2(PhysicsScene.World.ptr, 94 m_terrainShape = PhysicsScene.PE.CreateMeshShape(PhysicsScene.World, indicesCount, indices, verticesCount, vertices);
93 indicesCount, indices, verticesCount, vertices), 95 if (!m_terrainShape.HasPhysicalShape)
94 BSPhysicsShapeType.SHAPE_MESH);
95 if (m_terrainShape.ptr == IntPtr.Zero)
96 { 96 {
97 // DISASTER!! 97 // DISASTER!!
98 PhysicsScene.DetailLog("{0},BSTerrainMesh.create,failedCreationOfShape", ID); 98 PhysicsScene.DetailLog("{0},BSTerrainMesh.create,failedCreationOfShape", ID);
@@ -104,8 +104,8 @@ public sealed class BSTerrainMesh : BSTerrainPhys
104 Vector3 pos = regionBase; 104 Vector3 pos = regionBase;
105 Quaternion rot = Quaternion.Identity; 105 Quaternion rot = Quaternion.Identity;
106 106
107 m_terrainBody = new BulletBody(id, BulletSimAPI.CreateBodyWithDefaultMotionState2( m_terrainShape.ptr, ID, pos, rot)); 107 m_terrainBody = PhysicsScene.PE.CreateBodyWithDefaultMotionState(m_terrainShape, ID, pos, rot);
108 if (m_terrainBody.ptr == IntPtr.Zero) 108 if (!m_terrainBody.HasPhysicalBody)
109 { 109 {
110 // DISASTER!! 110 // DISASTER!!
111 physicsScene.Logger.ErrorFormat("{0} Failed creation of terrain body! base={1}", LogHeader, TerrainBase); 111 physicsScene.Logger.ErrorFormat("{0} Failed creation of terrain body! base={1}", LogHeader, TerrainBase);
@@ -114,39 +114,40 @@ public sealed class BSTerrainMesh : BSTerrainPhys
114 } 114 }
115 115
116 // Set current terrain attributes 116 // Set current terrain attributes
117 BulletSimAPI.SetFriction2(m_terrainBody.ptr, PhysicsScene.Params.terrainFriction); 117 PhysicsScene.PE.SetFriction(m_terrainBody, BSParam.TerrainFriction);
118 BulletSimAPI.SetHitFraction2(m_terrainBody.ptr, PhysicsScene.Params.terrainHitFraction); 118 PhysicsScene.PE.SetHitFraction(m_terrainBody, BSParam.TerrainHitFraction);
119 BulletSimAPI.SetRestitution2(m_terrainBody.ptr, PhysicsScene.Params.terrainRestitution); 119 PhysicsScene.PE.SetRestitution(m_terrainBody, BSParam.TerrainRestitution);
120 BulletSimAPI.SetCollisionFlags2(m_terrainBody.ptr, CollisionFlags.CF_STATIC_OBJECT); 120 PhysicsScene.PE.SetCollisionFlags(m_terrainBody, CollisionFlags.CF_STATIC_OBJECT);
121 121
122 // Static objects are not very massive. 122 // Static objects are not very massive.
123 BulletSimAPI.SetMassProps2(m_terrainBody.ptr, 0f, Vector3.Zero); 123 PhysicsScene.PE.SetMassProps(m_terrainBody, 0f, Vector3.Zero);
124 124
125 // Return the new terrain to the world of physical objects 125 // Put the new terrain to the world of physical objects
126 BulletSimAPI.AddObjectToWorld2(PhysicsScene.World.ptr, m_terrainBody.ptr); 126 PhysicsScene.PE.AddObjectToWorld(PhysicsScene.World, m_terrainBody);
127 127
128 // redo its bounding box now that it is in the world 128 // Redo its bounding box now that it is in the world
129 BulletSimAPI.UpdateSingleAabb2(PhysicsScene.World.ptr, m_terrainBody.ptr); 129 PhysicsScene.PE.UpdateSingleAabb(PhysicsScene.World, m_terrainBody);
130 130
131 BulletSimAPI.SetCollisionFilterMask2(m_terrainBody.ptr, 131 m_terrainBody.collisionType = CollisionType.Terrain;
132 (uint)CollisionFilterGroups.TerrainFilter, 132 m_terrainBody.ApplyCollisionMask(PhysicsScene);
133 (uint)CollisionFilterGroups.TerrainMask);
134 133
135 // Make it so the terrain will not move or be considered for movement. 134 // Make it so the terrain will not move or be considered for movement.
136 BulletSimAPI.ForceActivationState2(m_terrainBody.ptr, ActivationState.DISABLE_SIMULATION); 135 PhysicsScene.PE.ForceActivationState(m_terrainBody, ActivationState.DISABLE_SIMULATION);
137 } 136 }
138 137
139 public override void Dispose() 138 public override void Dispose()
140 { 139 {
141 if (m_terrainBody.ptr != IntPtr.Zero) 140 if (m_terrainBody.HasPhysicalBody)
142 { 141 {
143 BulletSimAPI.RemoveObjectFromWorld2(PhysicsScene.World.ptr, m_terrainBody.ptr); 142 PhysicsScene.PE.RemoveObjectFromWorld(PhysicsScene.World, m_terrainBody);
144 // Frees both the body and the shape. 143 // Frees both the body and the shape.
145 BulletSimAPI.DestroyObject2(PhysicsScene.World.ptr, m_terrainBody.ptr); 144 PhysicsScene.PE.DestroyObject(PhysicsScene.World, m_terrainBody);
145 m_terrainBody.Clear();
146 m_terrainShape.Clear();
146 } 147 }
147 } 148 }
148 149
149 public override float GetHeightAtXYZ(Vector3 pos) 150 public override float GetTerrainHeightAtXYZ(Vector3 pos)
150 { 151 {
151 // For the moment use the saved heightmap to get the terrain height. 152 // For the moment use the saved heightmap to get the terrain height.
152 // TODO: raycast downward to find the true terrain below the position. 153 // TODO: raycast downward to find the true terrain below the position.
@@ -167,6 +168,12 @@ public sealed class BSTerrainMesh : BSTerrainPhys
167 return ret; 168 return ret;
168 } 169 }
169 170
171 // The passed position is relative to the base of the region.
172 public override float GetWaterLevelAtXYZ(Vector3 pos)
173 {
174 return PhysicsScene.SimpleWaterLevel;
175 }
176
170 // Convert the passed heightmap to mesh information suitable for CreateMeshShape2(). 177 // Convert the passed heightmap to mesh information suitable for CreateMeshShape2().
171 // Return 'true' if successfully created. 178 // Return 'true' if successfully created.
172 public static bool ConvertHeightmapToMesh( 179 public static bool ConvertHeightmapToMesh(
@@ -188,6 +195,11 @@ public sealed class BSTerrainMesh : BSTerrainPhys
188 // Simple mesh creation which assumes magnification == 1. 195 // Simple mesh creation which assumes magnification == 1.
189 // TODO: do a more general solution that scales, adds new vertices and smoothes the result. 196 // TODO: do a more general solution that scales, adds new vertices and smoothes the result.
190 197
198 // Create an array of vertices that is sizeX+1 by sizeY+1 (note the loop
199 // from zero to <= sizeX). The triangle indices are then generated as two triangles
200 // per heightmap point. There are sizeX by sizeY of these squares. The extra row and
201 // column of vertices are used to complete the triangles of the last row and column
202 // of the heightmap.
191 try 203 try
192 { 204 {
193 // One vertice per heightmap value plus the vertices off the top and bottom edge. 205 // One vertice per heightmap value plus the vertices off the top and bottom edge.
@@ -200,16 +212,18 @@ public sealed class BSTerrainMesh : BSTerrainPhys
200 float magY = (float)sizeY / extentY; 212 float magY = (float)sizeY / extentY;
201 physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,totVert={1},totInd={2},extentBase={3},magX={4},magY={5}", 213 physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,totVert={1},totInd={2},extentBase={3},magX={4},magY={5}",
202 BSScene.DetailLogZero, totalVertices, totalIndices, extentBase, magX, magY); 214 BSScene.DetailLogZero, totalVertices, totalIndices, extentBase, magX, magY);
215 float minHeight = float.MaxValue;
203 // Note that sizeX+1 vertices are created since there is land between this and the next region. 216 // Note that sizeX+1 vertices are created since there is land between this and the next region.
204 for (int yy = 0; yy <= sizeY; yy++) 217 for (int yy = 0; yy <= sizeY; yy++)
205 { 218 {
206 for (int xx = 0; xx <= sizeX; xx++) // Hint: the "<=" means we got through sizeX + 1 times 219 for (int xx = 0; xx <= sizeX; xx++) // Hint: the "<=" means we go around sizeX + 1 times
207 { 220 {
208 int offset = yy * sizeX + xx; 221 int offset = yy * sizeX + xx;
209 // Extend the height from the height from the last row or column 222 // Extend the height with the height from the last row or column
210 if (yy == sizeY) offset -= sizeX; 223 if (yy == sizeY) offset -= sizeX;
211 if (xx == sizeX) offset -= 1; 224 if (xx == sizeX) offset -= 1;
212 float height = heightMap[offset]; 225 float height = heightMap[offset];
226 minHeight = Math.Min(minHeight, height);
213 vertices[verticesCount + 0] = (float)xx * magX + extentBase.X; 227 vertices[verticesCount + 0] = (float)xx * magX + extentBase.X;
214 vertices[verticesCount + 1] = (float)yy * magY + extentBase.Y; 228 vertices[verticesCount + 1] = (float)yy * magY + extentBase.Y;
215 vertices[verticesCount + 2] = height + extentBase.Z; 229 vertices[verticesCount + 2] = height + extentBase.Z;
@@ -217,14 +231,12 @@ public sealed class BSTerrainMesh : BSTerrainPhys
217 } 231 }
218 } 232 }
219 verticesCount = verticesCount / 3; 233 verticesCount = verticesCount / 3;
220 physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,completeVerts,verCount={1}",
221 BSScene.DetailLogZero, verticesCount);
222 234
223 for (int yy = 0; yy < sizeY; yy++) 235 for (int yy = 0; yy < sizeY; yy++)
224 { 236 {
225 for (int xx = 0; xx < sizeX; xx++) 237 for (int xx = 0; xx < sizeX; xx++)
226 { 238 {
227 int offset = yy * sizeX + xx; 239 int offset = yy * (sizeX + 1) + xx;
228 // Each vertices is presumed to be the upper left corner of a box of two triangles 240 // Each vertices is presumed to be the upper left corner of a box of two triangles
229 indices[indicesCount + 0] = offset; 241 indices[indicesCount + 0] = offset;
230 indices[indicesCount + 1] = offset + 1; 242 indices[indicesCount + 1] = offset + 1;
@@ -235,8 +247,7 @@ public sealed class BSTerrainMesh : BSTerrainPhys
235 indicesCount += 6; 247 indicesCount += 6;
236 } 248 }
237 } 249 }
238 physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,completeIndices,indCount={1}", // DEEBUG DEBUG DEBUG 250
239 LogHeader, indicesCount); // DEBUG
240 ret = true; 251 ret = true;
241 } 252 }
242 catch (Exception e) 253 catch (Exception e)
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BulletSimAPI.cs b/OpenSim/Region/Physics/BulletSPlugin/BulletSimAPI.cs
deleted file mode 100644
index e60a760..0000000
--- a/OpenSim/Region/Physics/BulletSPlugin/BulletSimAPI.cs
+++ /dev/null
@@ -1,1015 +0,0 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyrightD
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27using System;
28using System.Runtime.InteropServices;
29using System.Security;
30using System.Text;
31using OpenMetaverse;
32
33namespace OpenSim.Region.Physics.BulletSPlugin {
34
35// Classes to allow some type checking for the API
36// These hold pointers to allocated objects in the unmanaged space.
37
38// The physics engine controller class created at initialization
39public struct BulletSim
40{
41 public BulletSim(uint worldId, BSScene bss, IntPtr xx)
42 {
43 ptr = xx;
44 worldID = worldId;
45 physicsScene = bss;
46 }
47 public IntPtr ptr;
48 public uint worldID;
49 // The scene is only in here so very low level routines have a handle to print debug/error messages
50 public BSScene physicsScene;
51}
52
53// An allocated Bullet btRigidBody
54public struct BulletBody
55{
56 public BulletBody(uint id, IntPtr xx)
57 {
58 ID = id;
59 ptr = xx;
60 collisionFilter = 0;
61 collisionMask = 0;
62 }
63 public IntPtr ptr;
64 public uint ID;
65 public CollisionFilterGroups collisionFilter;
66 public CollisionFilterGroups collisionMask;
67 public override string ToString()
68 {
69 StringBuilder buff = new StringBuilder();
70 buff.Append("<id=");
71 buff.Append(ID.ToString());
72 buff.Append(",p=");
73 buff.Append(ptr.ToString("X"));
74 if (collisionFilter != 0 || collisionMask != 0)
75 {
76 buff.Append(",f=");
77 buff.Append(collisionFilter.ToString("X"));
78 buff.Append(",m=");
79 buff.Append(collisionMask.ToString("X"));
80 }
81 buff.Append(">");
82 return buff.ToString();
83 }
84}
85
86public struct BulletShape
87{
88 public BulletShape(IntPtr xx)
89 {
90 ptr = xx;
91 type=BSPhysicsShapeType.SHAPE_UNKNOWN;
92 shapeKey = (System.UInt64)FixedShapeKey.KEY_NONE;
93 isNativeShape = false;
94 }
95 public BulletShape(IntPtr xx, BSPhysicsShapeType typ)
96 {
97 ptr = xx;
98 type = typ;
99 shapeKey = 0;
100 isNativeShape = false;
101 }
102 public IntPtr ptr;
103 public BSPhysicsShapeType type;
104 public System.UInt64 shapeKey;
105 public bool isNativeShape;
106 public override string ToString()
107 {
108 StringBuilder buff = new StringBuilder();
109 buff.Append("<p=");
110 buff.Append(ptr.ToString("X"));
111 buff.Append(",s=");
112 buff.Append(type.ToString());
113 buff.Append(",k=");
114 buff.Append(shapeKey.ToString("X"));
115 buff.Append(",n=");
116 buff.Append(isNativeShape.ToString());
117 buff.Append(">");
118 return buff.ToString();
119 }
120}
121
122 // Constraint type values as defined by Bullet
123public enum ConstraintType : int
124{
125 POINT2POINT_CONSTRAINT_TYPE = 3,
126 HINGE_CONSTRAINT_TYPE,
127 CONETWIST_CONSTRAINT_TYPE,
128 D6_CONSTRAINT_TYPE,
129 SLIDER_CONSTRAINT_TYPE,
130 CONTACT_CONSTRAINT_TYPE,
131 D6_SPRING_CONSTRAINT_TYPE,
132 MAX_CONSTRAINT_TYPE
133}
134
135// An allocated Bullet btConstraint
136public struct BulletConstraint
137{
138 public BulletConstraint(IntPtr xx)
139 {
140 ptr = xx;
141 }
142 public IntPtr ptr;
143}
144
145// An allocated HeightMapThing which holds various heightmap info.
146// Made a class rather than a struct so there would be only one
147// instance of this and C# will pass around pointers rather
148// than making copies.
149public class BulletHeightMapInfo
150{
151 public BulletHeightMapInfo(uint id, float[] hm, IntPtr xx) {
152 ID = id;
153 Ptr = xx;
154 heightMap = hm;
155 terrainRegionBase = Vector3.Zero;
156 minCoords = new Vector3(100f, 100f, 25f);
157 maxCoords = new Vector3(101f, 101f, 26f);
158 minZ = maxZ = 0f;
159 sizeX = sizeY = 256f;
160 }
161 public uint ID;
162 public IntPtr Ptr;
163 public float[] heightMap;
164 public Vector3 terrainRegionBase;
165 public Vector3 minCoords;
166 public Vector3 maxCoords;
167 public float sizeX, sizeY;
168 public float minZ, maxZ;
169 public BulletShape terrainShape;
170 public BulletBody terrainBody;
171}
172
173// ===============================================================================
174[StructLayout(LayoutKind.Sequential)]
175public struct ConvexHull
176{
177 Vector3 Offset;
178 int VertexCount;
179 Vector3[] Vertices;
180}
181public enum BSPhysicsShapeType
182{
183 SHAPE_UNKNOWN = 0,
184 SHAPE_CAPSULE = 1,
185 SHAPE_BOX = 2,
186 SHAPE_CONE = 3,
187 SHAPE_CYLINDER = 4,
188 SHAPE_SPHERE = 5,
189 SHAPE_MESH = 6,
190 SHAPE_HULL = 7,
191 // following defined by BulletSim
192 SHAPE_GROUNDPLANE = 20,
193 SHAPE_TERRAIN = 21,
194 SHAPE_COMPOUND = 22,
195 SHAPE_HEIGHTMAP = 23,
196};
197
198// The native shapes have predefined shape hash keys
199public enum FixedShapeKey : ulong
200{
201 KEY_NONE = 0,
202 KEY_BOX = 1,
203 KEY_SPHERE = 2,
204 KEY_CONE = 3,
205 KEY_CYLINDER = 4,
206 KEY_CAPSULE = 5,
207}
208
209[StructLayout(LayoutKind.Sequential)]
210public struct ShapeData
211{
212 public uint ID;
213 public BSPhysicsShapeType Type;
214 public Vector3 Position;
215 public Quaternion Rotation;
216 public Vector3 Velocity;
217 public Vector3 Scale;
218 public float Mass;
219 public float Buoyancy;
220 public System.UInt64 HullKey;
221 public System.UInt64 MeshKey;
222 public float Friction;
223 public float Restitution;
224 public float Collidable; // true of things bump into this
225 public float Static; // true if a static object. Otherwise gravity, etc.
226 public float Solid; // true if object cannot be passed through
227 public Vector3 Size;
228
229 // note that bools are passed as floats since bool size changes by language and architecture
230 public const float numericTrue = 1f;
231 public const float numericFalse = 0f;
232}
233[StructLayout(LayoutKind.Sequential)]
234public struct SweepHit
235{
236 public uint ID;
237 public float Fraction;
238 public Vector3 Normal;
239 public Vector3 Point;
240}
241[StructLayout(LayoutKind.Sequential)]
242public struct RaycastHit
243{
244 public uint ID;
245 public float Fraction;
246 public Vector3 Normal;
247}
248[StructLayout(LayoutKind.Sequential)]
249public struct CollisionDesc
250{
251 public uint aID;
252 public uint bID;
253 public Vector3 point;
254 public Vector3 normal;
255}
256[StructLayout(LayoutKind.Sequential)]
257public struct EntityProperties
258{
259 public uint ID;
260 public Vector3 Position;
261 public Quaternion Rotation;
262 public Vector3 Velocity;
263 public Vector3 Acceleration;
264 public Vector3 RotationalVelocity;
265}
266
267// Format of this structure must match the definition in the C++ code
268[StructLayout(LayoutKind.Sequential)]
269public struct ConfigurationParameters
270{
271 public float defaultFriction;
272 public float defaultDensity;
273 public float defaultRestitution;
274 public float collisionMargin;
275 public float gravity;
276
277 public float linearDamping;
278 public float angularDamping;
279 public float deactivationTime;
280 public float linearSleepingThreshold;
281 public float angularSleepingThreshold;
282 public float ccdMotionThreshold;
283 public float ccdSweptSphereRadius;
284 public float contactProcessingThreshold;
285
286 public float terrainImplementation;
287 public float terrainFriction;
288 public float terrainHitFraction;
289 public float terrainRestitution;
290 public float avatarFriction;
291 public float avatarStandingFriction;
292 public float avatarDensity;
293 public float avatarRestitution;
294 public float avatarCapsuleWidth;
295 public float avatarCapsuleDepth;
296 public float avatarCapsuleHeight;
297 public float avatarContactProcessingThreshold;
298
299 public float maxPersistantManifoldPoolSize;
300 public float maxCollisionAlgorithmPoolSize;
301 public float shouldDisableContactPoolDynamicAllocation;
302 public float shouldForceUpdateAllAabbs;
303 public float shouldRandomizeSolverOrder;
304 public float shouldSplitSimulationIslands;
305 public float shouldEnableFrictionCaching;
306 public float numberOfSolverIterations;
307
308 public float linksetImplementation;
309 public float linkConstraintUseFrameOffset;
310 public float linkConstraintEnableTransMotor;
311 public float linkConstraintTransMotorMaxVel;
312 public float linkConstraintTransMotorMaxForce;
313 public float linkConstraintERP;
314 public float linkConstraintCFM;
315 public float linkConstraintSolverIterations;
316
317 public float physicsLoggingFrames;
318
319 public const float numericTrue = 1f;
320 public const float numericFalse = 0f;
321}
322
323
324// The states a bullet collision object can have
325public enum ActivationState : uint
326{
327 ACTIVE_TAG = 1,
328 ISLAND_SLEEPING,
329 WANTS_DEACTIVATION,
330 DISABLE_DEACTIVATION,
331 DISABLE_SIMULATION,
332}
333
334public enum CollisionObjectTypes : int
335{
336 CO_COLLISION_OBJECT = 1 << 0,
337 CO_RIGID_BODY = 1 << 1,
338 CO_GHOST_OBJECT = 1 << 2,
339 CO_SOFT_BODY = 1 << 3,
340 CO_HF_FLUID = 1 << 4,
341 CO_USER_TYPE = 1 << 5,
342}
343
344// Values used by Bullet and BulletSim to control object properties.
345// Bullet's "CollisionFlags" has more to do with operations on the
346// object (if collisions happen, if gravity effects it, ...).
347public enum CollisionFlags : uint
348{
349 CF_STATIC_OBJECT = 1 << 0,
350 CF_KINEMATIC_OBJECT = 1 << 1,
351 CF_NO_CONTACT_RESPONSE = 1 << 2,
352 CF_CUSTOM_MATERIAL_CALLBACK = 1 << 3,
353 CF_CHARACTER_OBJECT = 1 << 4,
354 CF_DISABLE_VISUALIZE_OBJECT = 1 << 5,
355 CF_DISABLE_SPU_COLLISION_PROCESS = 1 << 6,
356 // Following used by BulletSim to control collisions
357 BS_SUBSCRIBE_COLLISION_EVENTS = 1 << 10,
358 BS_FLOATS_ON_WATER = 1 << 11,
359 BS_NONE = 0,
360 BS_ALL = 0xFFFFFFFF,
361
362 // These are the collision flags switched depending on physical state.
363 // The other flags are used for other things and should not be fooled with.
364 BS_ACTIVE = CF_STATIC_OBJECT
365 | CF_KINEMATIC_OBJECT
366 | CF_NO_CONTACT_RESPONSE
367};
368
369// Values for collisions groups and masks
370public enum CollisionFilterGroups : uint
371{
372 // Don't use the bit definitions!! Define the use in a
373 // filter/mask definition below. This way collision interactions
374 // are more easily debugged.
375 BNoneFilter = 0,
376 BDefaultFilter = 1 << 0,
377 BStaticFilter = 1 << 1,
378 BKinematicFilter = 1 << 2,
379 BDebrisFilter = 1 << 3,
380 BSensorTrigger = 1 << 4,
381 BCharacterFilter = 1 << 5,
382 BAllFilter = 0xFFFFFFFF,
383 // Filter groups defined by BulletSim
384 BGroundPlaneFilter = 1 << 10,
385 BTerrainFilter = 1 << 11,
386 BRaycastFilter = 1 << 12,
387 BSolidFilter = 1 << 13,
388 BLinksetFilter = 1 << 14,
389
390 // The collsion filters and masked are defined in one place -- don't want them scattered
391 AvatarFilter = BCharacterFilter,
392 AvatarMask = BAllFilter,
393 ObjectFilter = BSolidFilter,
394 ObjectMask = BAllFilter,
395 StaticObjectFilter = BStaticFilter,
396 StaticObjectMask = BAllFilter & ~BStaticFilter, // static objects don't collide with each other
397 LinksetFilter = BLinksetFilter,
398 LinksetMask = BAllFilter & ~BLinksetFilter, // linkset objects don't collide with each other
399 VolumeDetectFilter = BSensorTrigger,
400 VolumeDetectMask = ~BSensorTrigger,
401 TerrainFilter = BTerrainFilter,
402 TerrainMask = BAllFilter & ~BStaticFilter, // static objects on the ground don't collide
403 GroundPlaneFilter = BGroundPlaneFilter,
404 GroundPlaneMask = BAllFilter
405
406};
407
408// CFM controls the 'hardness' of the constraint. 0=fixed, 0..1=violatable. Default=0
409// ERP controls amount of correction per tick. Usable range=0.1..0.8. Default=0.2.
410public enum ConstraintParams : int
411{
412 BT_CONSTRAINT_ERP = 1, // this one is not used in Bullet as of 20120730
413 BT_CONSTRAINT_STOP_ERP,
414 BT_CONSTRAINT_CFM,
415 BT_CONSTRAINT_STOP_CFM,
416};
417public enum ConstraintParamAxis : int
418{
419 AXIS_LINEAR_X = 0,
420 AXIS_LINEAR_Y,
421 AXIS_LINEAR_Z,
422 AXIS_ANGULAR_X,
423 AXIS_ANGULAR_Y,
424 AXIS_ANGULAR_Z,
425 AXIS_LINEAR_ALL = 20, // these last three added by BulletSim so we don't have to do zillions of calls
426 AXIS_ANGULAR_ALL,
427 AXIS_ALL
428};
429
430// ===============================================================================
431static class BulletSimAPI {
432
433// Link back to the managed code for outputting log messages
434[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
435public delegate void DebugLogCallback([MarshalAs(UnmanagedType.LPStr)]string msg);
436
437// ===============================================================================
438// Initialization and simulation
439[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
440public static extern IntPtr Initialize2(Vector3 maxPosition, IntPtr parms,
441 int maxCollisions, IntPtr collisionArray,
442 int maxUpdates, IntPtr updateArray,
443 DebugLogCallback logRoutine);
444
445[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
446public static extern bool UpdateParameter2(IntPtr world, uint localID, String parm, float value);
447
448[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
449public static extern void SetHeightMap2(IntPtr world, float[] heightmap);
450
451[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
452public static extern void Shutdown2(IntPtr sim);
453
454[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
455public static extern int PhysicsStep2(IntPtr world, float timeStep, int maxSubSteps, float fixedTimeStep,
456 out int updatedEntityCount,
457 out IntPtr updatedEntitiesPtr,
458 out int collidersCount,
459 out IntPtr collidersPtr);
460
461[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
462public static extern bool PushUpdate2(IntPtr obj);
463
464// =====================================================================================
465// Mesh, hull, shape and body creation helper routines
466[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
467public static extern IntPtr CreateMeshShape2(IntPtr world,
468 int indicesCount, [MarshalAs(UnmanagedType.LPArray)] int[] indices,
469 int verticesCount, [MarshalAs(UnmanagedType.LPArray)] float[] vertices );
470
471[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
472public static extern IntPtr CreateHullShape2(IntPtr world,
473 int hullCount, [MarshalAs(UnmanagedType.LPArray)] float[] hulls);
474
475[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
476public static extern IntPtr BuildHullShapeFromMesh2(IntPtr world, IntPtr meshShape);
477
478[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
479public static extern IntPtr BuildNativeShape2(IntPtr world, ShapeData shapeData);
480
481[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
482public static extern bool IsNativeShape2(IntPtr shape);
483
484[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
485public static extern IntPtr BuildCapsuleShape2(IntPtr world, float radius, float height, Vector3 scale);
486
487[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
488public static extern IntPtr CreateCompoundShape2(IntPtr sim, bool enableDynamicAabbTree);
489
490[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
491public static extern int GetNumberOfCompoundChildren2(IntPtr cShape);
492
493[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
494public static extern void AddChildShapeToCompoundShape2(IntPtr cShape, IntPtr addShape, Vector3 pos, Quaternion rot);
495
496[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
497public static extern IntPtr GetChildShapeFromCompoundShapeIndex2(IntPtr cShape, int indx);
498
499[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
500public static extern IntPtr RemoveChildShapeFromCompoundShapeIndex2(IntPtr cShape, int indx);
501
502[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
503public static extern void RemoveChildShapeFromCompoundShape2(IntPtr cShape, IntPtr removeShape);
504
505[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
506public static extern void RecalculateCompoundShapeLocalAabb2(IntPtr cShape);
507
508[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
509public static extern IntPtr DuplicateCollisionShape2(IntPtr sim, IntPtr srcShape, uint id);
510
511[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
512public static extern IntPtr CreateBodyFromShapeAndInfo2(IntPtr sim, IntPtr shape, uint id, IntPtr constructionInfo);
513
514[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
515public static extern bool DeleteCollisionShape2(IntPtr world, IntPtr shape);
516
517[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
518public static extern int GetBodyType2(IntPtr obj);
519
520[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
521public static extern IntPtr CreateBodyFromShape2(IntPtr sim, IntPtr shape, uint id, Vector3 pos, Quaternion rot);
522
523[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
524public static extern IntPtr CreateBodyWithDefaultMotionState2(IntPtr shape, uint id, Vector3 pos, Quaternion rot);
525
526[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
527public static extern IntPtr CreateGhostFromShape2(IntPtr sim, IntPtr shape, uint id, Vector3 pos, Quaternion rot);
528
529[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
530public static extern IntPtr AllocateBodyInfo2(IntPtr obj);
531
532[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
533public static extern void ReleaseBodyInfo2(IntPtr obj);
534
535[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
536public static extern void DestroyObject2(IntPtr sim, IntPtr obj);
537
538// =====================================================================================
539// Terrain creation and helper routines
540[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
541public static extern IntPtr CreateHeightMapInfo2(IntPtr sim, uint id, Vector3 minCoords, Vector3 maxCoords,
542 [MarshalAs(UnmanagedType.LPArray)] float[] heightMap, float collisionMargin);
543
544[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
545public static extern IntPtr FillHeightMapInfo2(IntPtr sim, IntPtr mapInfo, uint id, Vector3 minCoords, Vector3 maxCoords,
546 [MarshalAs(UnmanagedType.LPArray)] float[] heightMap, float collisionMargin);
547
548[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
549public static extern bool ReleaseHeightMapInfo2(IntPtr heightMapInfo);
550
551[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
552public static extern IntPtr CreateGroundPlaneShape2(uint id, float height, float collisionMargin);
553
554[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
555public static extern IntPtr CreateTerrainShape2(IntPtr mapInfo);
556
557// =====================================================================================
558// Constraint creation and helper routines
559[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
560public static extern IntPtr Create6DofConstraint2(IntPtr world, IntPtr obj1, IntPtr obj2,
561 Vector3 frame1loc, Quaternion frame1rot,
562 Vector3 frame2loc, Quaternion frame2rot,
563 bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies);
564
565[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
566public static extern IntPtr Create6DofConstraintToPoint2(IntPtr world, IntPtr obj1, IntPtr obj2,
567 Vector3 joinPoint,
568 bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies);
569
570[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
571public static extern IntPtr CreateHingeConstraint2(IntPtr world, IntPtr obj1, IntPtr obj2,
572 Vector3 pivotinA, Vector3 pivotinB,
573 Vector3 axisInA, Vector3 axisInB,
574 bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies);
575
576[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
577public static extern void SetConstraintEnable2(IntPtr constrain, float numericTrueFalse);
578
579[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
580public static extern void SetConstraintNumSolverIterations2(IntPtr constrain, float iterations);
581
582[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
583public static extern bool SetFrames2(IntPtr constrain,
584 Vector3 frameA, Quaternion frameArot, Vector3 frameB, Quaternion frameBrot);
585
586[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
587public static extern bool SetLinearLimits2(IntPtr constrain, Vector3 low, Vector3 hi);
588
589[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
590public static extern bool SetAngularLimits2(IntPtr constrain, Vector3 low, Vector3 hi);
591
592[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
593public static extern bool UseFrameOffset2(IntPtr constrain, float enable);
594
595[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
596public static extern bool TranslationalLimitMotor2(IntPtr constrain, float enable, float targetVel, float maxMotorForce);
597
598[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
599public static extern bool SetBreakingImpulseThreshold2(IntPtr constrain, float threshold);
600
601[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
602public static extern bool CalculateTransforms2(IntPtr constrain);
603
604[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
605public static extern bool SetConstraintParam2(IntPtr constrain, ConstraintParams paramIndex, float value, ConstraintParamAxis axis);
606
607[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
608public static extern bool DestroyConstraint2(IntPtr world, IntPtr constrain);
609
610// =====================================================================================
611// btCollisionWorld entries
612[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
613public static extern void UpdateSingleAabb2(IntPtr world, IntPtr obj);
614
615[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
616public static extern void UpdateAabbs2(IntPtr world);
617
618[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
619public static extern bool GetForceUpdateAllAabbs2(IntPtr world);
620
621[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
622public static extern void SetForceUpdateAllAabbs2(IntPtr world, bool force);
623
624// =====================================================================================
625// btDynamicsWorld entries
626[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
627public static extern bool AddObjectToWorld2(IntPtr world, IntPtr obj);
628
629[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
630public static extern bool RemoveObjectFromWorld2(IntPtr world, IntPtr obj);
631
632[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
633public static extern bool AddConstraintToWorld2(IntPtr world, IntPtr constrain, bool disableCollisionsBetweenLinkedObjects);
634
635[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
636public static extern bool RemoveConstraintFromWorld2(IntPtr world, IntPtr constrain);
637// =====================================================================================
638// btCollisionObject entries
639[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
640public static extern Vector3 GetAnisotripicFriction2(IntPtr constrain);
641
642[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
643public static extern Vector3 SetAnisotripicFriction2(IntPtr constrain, Vector3 frict);
644
645[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
646public static extern bool HasAnisotripicFriction2(IntPtr constrain);
647
648[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
649public static extern void SetContactProcessingThreshold2(IntPtr obj, float val);
650
651[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
652public static extern float GetContactProcessingThreshold2(IntPtr obj);
653
654[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
655public static extern bool IsStaticObject2(IntPtr obj);
656
657[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
658public static extern bool IsKinematicObject2(IntPtr obj);
659
660[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
661public static extern bool IsStaticOrKinematicObject2(IntPtr obj);
662
663[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
664public static extern bool HasContactResponse2(IntPtr obj);
665
666[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
667public static extern void SetCollisionShape2(IntPtr sim, IntPtr obj, IntPtr shape);
668
669[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
670public static extern IntPtr GetCollisionShape2(IntPtr obj);
671
672[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
673public static extern int GetActivationState2(IntPtr obj);
674
675[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
676public static extern void SetActivationState2(IntPtr obj, int state);
677
678[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
679public static extern void SetDeactivationTime2(IntPtr obj, float dtime);
680
681[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
682public static extern float GetDeactivationTime2(IntPtr obj);
683
684[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
685public static extern void ForceActivationState2(IntPtr obj, ActivationState state);
686
687[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
688public static extern void Activate2(IntPtr obj, bool forceActivation);
689
690[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
691public static extern bool IsActive2(IntPtr obj);
692
693[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
694public static extern void SetRestitution2(IntPtr obj, float val);
695
696[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
697public static extern float GetRestitution2(IntPtr obj);
698
699[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
700public static extern void SetFriction2(IntPtr obj, float val);
701
702[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
703public static extern float GetFriction2(IntPtr obj);
704
705 /* Haven't defined the type 'Transform'
706[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
707public static extern Transform GetWorldTransform2(IntPtr obj);
708
709[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
710public static extern void setWorldTransform2(IntPtr obj, Transform trans);
711 */
712
713[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
714public static extern Vector3 GetPosition2(IntPtr obj);
715
716[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
717public static extern Quaternion GetOrientation2(IntPtr obj);
718
719[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
720public static extern void SetTranslation2(IntPtr obj, Vector3 position, Quaternion rotation);
721
722[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
723public static extern IntPtr GetBroadphaseHandle2(IntPtr obj);
724
725[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
726public static extern void SetBroadphaseHandle2(IntPtr obj, IntPtr handle);
727
728 /*
729[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
730public static extern Transform GetInterpolationWorldTransform2(IntPtr obj);
731
732[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
733public static extern void SetInterpolationWorldTransform2(IntPtr obj, Transform trans);
734 */
735
736[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
737public static extern void SetInterpolationLinearVelocity2(IntPtr obj, Vector3 vel);
738
739[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
740public static extern void SetInterpolationAngularVelocity2(IntPtr obj, Vector3 vel);
741
742[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
743public static extern void SetInterpolationVelocity2(IntPtr obj, Vector3 linearVel, Vector3 angularVel);
744
745[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
746public static extern float GetHitFraction2(IntPtr obj);
747
748[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
749public static extern void SetHitFraction2(IntPtr obj, float val);
750
751[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
752public static extern CollisionFlags GetCollisionFlags2(IntPtr obj);
753
754[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
755public static extern CollisionFlags SetCollisionFlags2(IntPtr obj, CollisionFlags flags);
756
757[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
758public static extern CollisionFlags AddToCollisionFlags2(IntPtr obj, CollisionFlags flags);
759
760[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
761public static extern CollisionFlags RemoveFromCollisionFlags2(IntPtr obj, CollisionFlags flags);
762
763[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
764public static extern float GetCcdMotionThreshold2(IntPtr obj);
765
766[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
767public static extern void SetCcdMotionThreshold2(IntPtr obj, float val);
768
769[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
770public static extern float GetCcdSweptSphereRadius2(IntPtr obj);
771
772[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
773public static extern void SetCcdSweptSphereRadius2(IntPtr obj, float val);
774
775[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
776public static extern IntPtr GetUserPointer2(IntPtr obj);
777
778[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
779public static extern void SetUserPointer2(IntPtr obj, IntPtr val);
780
781// =====================================================================================
782// btRigidBody entries
783[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
784public static extern void ApplyGravity2(IntPtr obj);
785
786[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
787public static extern void SetGravity2(IntPtr obj, Vector3 val);
788
789[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
790public static extern Vector3 GetGravity2(IntPtr obj);
791
792[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
793public static extern void SetDamping2(IntPtr obj, float lin_damping, float ang_damping);
794
795[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
796public static extern void SetLinearDamping2(IntPtr obj, float lin_damping);
797
798[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
799public static extern void SetAngularDamping2(IntPtr obj, float ang_damping);
800
801[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
802public static extern float GetLinearDamping2(IntPtr obj);
803
804[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
805public static extern float GetAngularDamping2(IntPtr obj);
806
807[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
808public static extern float GetLinearSleepingThreshold2(IntPtr obj);
809
810[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
811public static extern float GetAngularSleepingThreshold2(IntPtr obj);
812
813[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
814public static extern void ApplyDamping2(IntPtr obj, float timeStep);
815
816[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
817public static extern void SetMassProps2(IntPtr obj, float mass, Vector3 inertia);
818
819[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
820public static extern Vector3 GetLinearFactor2(IntPtr obj);
821
822[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
823public static extern void SetLinearFactor2(IntPtr obj, Vector3 factor);
824
825 /*
826[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
827public static extern void SetCenterOfMassTransform2(IntPtr obj, Transform trans);
828 */
829
830[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
831public static extern void SetCenterOfMassByPosRot2(IntPtr obj, Vector3 pos, Quaternion rot);
832
833// Add a force to the object as if its mass is one.
834[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
835public static extern void ApplyCentralForce2(IntPtr obj, Vector3 force);
836
837// Set the force being applied to the object as if its mass is one.
838[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
839public static extern void SetObjectForce2(IntPtr obj, Vector3 force);
840
841[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
842public static extern Vector3 GetTotalForce2(IntPtr obj);
843
844[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
845public static extern Vector3 GetTotalTorque2(IntPtr obj);
846
847[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
848public static extern Vector3 GetInvInertiaDiagLocal2(IntPtr obj);
849
850[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
851public static extern void SetInvInertiaDiagLocal2(IntPtr obj, Vector3 inert);
852
853[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
854public static extern void SetSleepingThresholds2(IntPtr obj, float lin_threshold, float ang_threshold);
855
856[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
857public static extern void ApplyTorque2(IntPtr obj, Vector3 torque);
858
859// Apply force at the given point. Will add torque to the object.
860[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
861public static extern void ApplyForce2(IntPtr obj, Vector3 force, Vector3 pos);
862
863// Apply impulse to the object. Same as "ApplycentralForce" but force scaled by object's mass.
864[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
865public static extern void ApplyCentralImpulse2(IntPtr obj, Vector3 imp);
866
867// Apply impulse to the object's torque. Force is scaled by object's mass.
868[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
869public static extern void ApplyTorqueImpulse2(IntPtr obj, Vector3 imp);
870
871// Apply impulse at the point given. For is scaled by object's mass and effects both linear and angular forces.
872[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
873public static extern void ApplyImpulse2(IntPtr obj, Vector3 imp, Vector3 pos);
874
875[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
876public static extern void ClearForces2(IntPtr obj);
877
878[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
879public static extern void ClearAllForces2(IntPtr obj);
880
881[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
882public static extern void UpdateInertiaTensor2(IntPtr obj);
883
884[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
885public static extern Vector3 GetCenterOfMassPosition2(IntPtr obj);
886
887 /*
888[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
889public static extern Transform GetCenterOfMassTransform2(IntPtr obj);
890 */
891
892[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
893public static extern Vector3 GetLinearVelocity2(IntPtr obj);
894
895[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
896public static extern Vector3 GetAngularVelocity2(IntPtr obj);
897
898[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
899public static extern void SetLinearVelocity2(IntPtr obj, Vector3 val);
900
901[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
902public static extern void SetAngularVelocity2(IntPtr obj, Vector3 angularVelocity);
903
904[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
905public static extern Vector3 GetVelocityInLocalPoint2(IntPtr obj, Vector3 pos);
906
907[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
908public static extern void Translate2(IntPtr obj, Vector3 trans);
909
910[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
911public static extern void UpdateDeactivation2(IntPtr obj, float timeStep);
912
913[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
914public static extern bool WantsSleeping2(IntPtr obj);
915
916[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
917public static extern void SetAngularFactor2(IntPtr obj, float factor);
918
919[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
920public static extern void SetAngularFactorV2(IntPtr obj, Vector3 factor);
921
922[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
923public static extern Vector3 GetAngularFactor2(IntPtr obj);
924
925[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
926public static extern bool IsInWorld2(IntPtr obj);
927
928[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
929public static extern void AddConstraintRef2(IntPtr obj, IntPtr constrain);
930
931[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
932public static extern void RemoveConstraintRef2(IntPtr obj, IntPtr constrain);
933
934[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
935public static extern IntPtr GetConstraintRef2(IntPtr obj, int index);
936
937[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
938public static extern int GetNumConstraintRefs2(IntPtr obj);
939
940[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
941public static extern void SetCollisionFilterMask2(IntPtr body, uint filter, uint mask);
942
943// =====================================================================================
944// btCollisionShape entries
945
946[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
947public static extern float GetAngularMotionDisc2(IntPtr shape);
948
949[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
950public static extern float GetContactBreakingThreshold2(IntPtr shape, float defaultFactor);
951
952[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
953public static extern bool IsPolyhedral2(IntPtr shape);
954
955[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
956public static extern bool IsConvex2d2(IntPtr shape);
957
958[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
959public static extern bool IsConvex2(IntPtr shape);
960
961[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
962public static extern bool IsNonMoving2(IntPtr shape);
963
964[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
965public static extern bool IsConcave2(IntPtr shape);
966
967[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
968public static extern bool IsCompound2(IntPtr shape);
969
970[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
971public static extern bool IsSoftBody2(IntPtr shape);
972
973[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
974public static extern bool IsInfinite2(IntPtr shape);
975
976[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
977public static extern void SetLocalScaling2(IntPtr shape, Vector3 scale);
978
979[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
980public static extern Vector3 GetLocalScaling2(IntPtr shape);
981
982[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
983public static extern Vector3 CalculateLocalInertia2(IntPtr shape, float mass);
984
985[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
986public static extern int GetShapeType2(IntPtr shape);
987
988[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
989public static extern void SetMargin2(IntPtr shape, float val);
990
991[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
992public static extern float GetMargin2(IntPtr shape);
993
994// =====================================================================================
995// Debugging
996[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
997public static extern void DumpRigidBody2(IntPtr sim, IntPtr collisionObject);
998
999[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1000public static extern void DumpCollisionShape2(IntPtr sim, IntPtr collisionShape);
1001
1002[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1003public static extern void DumpConstraint2(IntPtr sim, IntPtr constrain);
1004
1005[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1006public static extern void DumpAllInfo2(IntPtr sim);
1007
1008[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1009public static extern void DumpMapInfo2(IntPtr sim, IntPtr manInfo);
1010
1011[DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
1012public static extern void DumpPhysicsStatistics2(IntPtr sim);
1013
1014}
1015}
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BulletSimData.cs b/OpenSim/Region/Physics/BulletSPlugin/BulletSimData.cs
new file mode 100755
index 0000000..c7a2f7e
--- /dev/null
+++ b/OpenSim/Region/Physics/BulletSPlugin/BulletSimData.cs
@@ -0,0 +1,265 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyrightD
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27using System;
28using System.Collections.Generic;
29using System.Text;
30using OMV = OpenMetaverse;
31
32namespace OpenSim.Region.Physics.BulletSPlugin
33{
34// Classes to allow some type checking for the API
35// These hold pointers to allocated objects in the unmanaged space.
36// These classes are subclassed by the various physical implementations of
37// objects. In particular, there is a version for physical instances in
38// unmanaged memory ("unman") and one for in managed memory ("XNA").
39
40// Currently, the instances of these classes are a reference to a
41// physical representation and this has no releationship to other
42// instances. Someday, refarb the usage of these classes so each instance
43// refers to a particular physical instance and this class controls reference
44// counts and such. This should be done along with adding BSShapes.
45
46public class BulletWorld
47{
48 public BulletWorld(uint worldId, BSScene bss)
49 {
50 worldID = worldId;
51 physicsScene = bss;
52 }
53 public uint worldID;
54 // The scene is only in here so very low level routines have a handle to print debug/error messages
55 public BSScene physicsScene;
56}
57
58// An allocated Bullet btRigidBody
59public class BulletBody
60{
61 public BulletBody(uint id)
62 {
63 ID = id;
64 collisionType = CollisionType.Static;
65 }
66 public uint ID;
67 public CollisionType collisionType;
68
69 public virtual void Clear() { }
70 public virtual bool HasPhysicalBody { get { return false; } }
71
72 // Apply the specificed collision mask into the physical world
73 public virtual bool ApplyCollisionMask(BSScene physicsScene)
74 {
75 // Should assert the body has been added to the physical world.
76 // (The collision masks are stored in the collision proxy cache which only exists for
77 // a collision body that is in the world.)
78 return physicsScene.PE.SetCollisionGroupMask(this,
79 BulletSimData.CollisionTypeMasks[collisionType].group,
80 BulletSimData.CollisionTypeMasks[collisionType].mask);
81 }
82
83 // Used for log messages for a unique display of the memory/object allocated to this instance
84 public virtual string AddrString
85 {
86 get { return "unknown"; }
87 }
88
89 public override string ToString()
90 {
91 StringBuilder buff = new StringBuilder();
92 buff.Append("<id=");
93 buff.Append(ID.ToString());
94 buff.Append(",p=");
95 buff.Append(AddrString);
96 buff.Append(",c=");
97 buff.Append(collisionType);
98 buff.Append(">");
99 return buff.ToString();
100 }
101}
102
103public class BulletShape
104{
105 public BulletShape()
106 {
107 type = BSPhysicsShapeType.SHAPE_UNKNOWN;
108 shapeKey = (System.UInt64)FixedShapeKey.KEY_NONE;
109 isNativeShape = false;
110 }
111 public BSPhysicsShapeType type;
112 public System.UInt64 shapeKey;
113 public bool isNativeShape;
114
115 public virtual void Clear() { }
116 public virtual bool HasPhysicalShape { get { return false; } }
117
118 // Make another reference to this physical object.
119 public virtual BulletShape Clone() { return new BulletShape(); }
120
121 // Return 'true' if this and other refer to the same physical object
122 public virtual bool ReferenceSame(BulletShape xx) { return false; }
123
124 // Used for log messages for a unique display of the memory/object allocated to this instance
125 public virtual string AddrString
126 {
127 get { return "unknown"; }
128 }
129
130 public override string ToString()
131 {
132 StringBuilder buff = new StringBuilder();
133 buff.Append("<p=");
134 buff.Append(AddrString);
135 buff.Append(",s=");
136 buff.Append(type.ToString());
137 buff.Append(",k=");
138 buff.Append(shapeKey.ToString("X"));
139 buff.Append(",n=");
140 buff.Append(isNativeShape.ToString());
141 buff.Append(">");
142 return buff.ToString();
143 }
144}
145
146// An allocated Bullet btConstraint
147public class BulletConstraint
148{
149 public BulletConstraint()
150 {
151 }
152 public virtual void Clear() { }
153 public virtual bool HasPhysicalConstraint { get { return false; } }
154
155 // Used for log messages for a unique display of the memory/object allocated to this instance
156 public virtual string AddrString
157 {
158 get { return "unknown"; }
159 }
160}
161
162// An allocated HeightMapThing which holds various heightmap info.
163// Made a class rather than a struct so there would be only one
164// instance of this and C# will pass around pointers rather
165// than making copies.
166public class BulletHMapInfo
167{
168 public BulletHMapInfo(uint id, float[] hm) {
169 ID = id;
170 heightMap = hm;
171 terrainRegionBase = OMV.Vector3.Zero;
172 minCoords = new OMV.Vector3(100f, 100f, 25f);
173 maxCoords = new OMV.Vector3(101f, 101f, 26f);
174 minZ = maxZ = 0f;
175 sizeX = sizeY = 256f;
176 }
177 public uint ID;
178 public float[] heightMap;
179 public OMV.Vector3 terrainRegionBase;
180 public OMV.Vector3 minCoords;
181 public OMV.Vector3 maxCoords;
182 public float sizeX, sizeY;
183 public float minZ, maxZ;
184 public BulletShape terrainShape;
185 public BulletBody terrainBody;
186}
187
188// The general class of collsion object.
189public enum CollisionType
190{
191 Avatar,
192 Groundplane,
193 Terrain,
194 Static,
195 Dynamic,
196 VolumeDetect,
197 // Linkset, // A linkset should be either Static or Dynamic
198 LinksetChild,
199 Unknown
200};
201
202// Hold specification of group and mask collision flags for a CollisionType
203public struct CollisionTypeFilterGroup
204{
205 public CollisionTypeFilterGroup(CollisionType t, uint g, uint m)
206 {
207 type = t;
208 group = g;
209 mask = m;
210 }
211 public CollisionType type;
212 public uint group;
213 public uint mask;
214};
215
216public static class BulletSimData
217{
218
219// Map of collisionTypes to flags for collision groups and masks.
220// As mentioned above, don't use the CollisionFilterGroups definitions directly in the code
221// but, instead, use references to this dictionary. Finding and debugging
222// collision flag problems will be made easier.
223public static Dictionary<CollisionType, CollisionTypeFilterGroup> CollisionTypeMasks
224 = new Dictionary<CollisionType, CollisionTypeFilterGroup>()
225{
226 { CollisionType.Avatar,
227 new CollisionTypeFilterGroup(CollisionType.Avatar,
228 (uint)CollisionFilterGroups.BCharacterGroup,
229 (uint)CollisionFilterGroups.BAllGroup)
230 },
231 { CollisionType.Groundplane,
232 new CollisionTypeFilterGroup(CollisionType.Groundplane,
233 (uint)CollisionFilterGroups.BGroundPlaneGroup,
234 (uint)CollisionFilterGroups.BAllGroup)
235 },
236 { CollisionType.Terrain,
237 new CollisionTypeFilterGroup(CollisionType.Terrain,
238 (uint)CollisionFilterGroups.BTerrainGroup,
239 (uint)(CollisionFilterGroups.BAllGroup & ~CollisionFilterGroups.BStaticGroup))
240 },
241 { CollisionType.Static,
242 new CollisionTypeFilterGroup(CollisionType.Static,
243 (uint)CollisionFilterGroups.BStaticGroup,
244 (uint)(CollisionFilterGroups.BCharacterGroup | CollisionFilterGroups.BSolidGroup))
245 },
246 { CollisionType.Dynamic,
247 new CollisionTypeFilterGroup(CollisionType.Dynamic,
248 (uint)CollisionFilterGroups.BSolidGroup,
249 (uint)(CollisionFilterGroups.BAllGroup))
250 },
251 { CollisionType.VolumeDetect,
252 new CollisionTypeFilterGroup(CollisionType.VolumeDetect,
253 (uint)CollisionFilterGroups.BSensorTrigger,
254 (uint)(~CollisionFilterGroups.BSensorTrigger))
255 },
256 { CollisionType.LinksetChild,
257 new CollisionTypeFilterGroup(CollisionType.LinksetChild,
258 (uint)CollisionFilterGroups.BLinksetChildGroup,
259 (uint)(CollisionFilterGroups.BNoneGroup))
260 // (uint)(CollisionFilterGroups.BCharacterGroup | CollisionFilterGroups.BSolidGroup))
261 },
262};
263
264}
265}
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BulletSimTODO.txt b/OpenSim/Region/Physics/BulletSPlugin/BulletSimTODO.txt
new file mode 100755
index 0000000..59cbab9
--- /dev/null
+++ b/OpenSim/Region/Physics/BulletSPlugin/BulletSimTODO.txt
@@ -0,0 +1,290 @@
1CURRENT PRIORITIES
2=================================================
3Nebadon vehicles turning funny in arena
4limitMotorUp calibration (more down?)
5Vehicle angular vertical attraction
6Vehicle angular deflection
7 Preferred orientation angular correction fix
8vehicle angular banking
9Avatars walking up stairs (HALF DONE)
10 Radius of the capsule affects ability to climb edges.
11Vehicle movement on terrain smoothness
12Surfboard go wonky when turning
13 Angular motor direction is global coordinates rather than local coordinates?
14Boats float low in the water (DONE)
15Avatar movement
16 flying into a wall doesn't stop avatar who keeps appearing to move through the obstacle (DONE)
17 walking up stairs is not calibrated correctly (stairs out of Kepler cabin)
18 avatar capsule rotation completed (NOT DONE - Bullet's capsule shape is not the solution)
19Enable vehicle border crossings (at least as poorly as ODE)
20 Terrain skirts
21 Avatar created in previous region and not new region when crossing border
22 Vehicle recreated in new sim at small Z value (offset from root value?) (DONE)
23Vehicle script tuning/debugging
24 Avanti speed script
25 Weapon shooter script
26Add material densities to the material types
27
28CRASHES
29=================================================
3020121129.1411: editting/moving phys object across region boundries causes crash
31 getPos-> btRigidBody::upcast -> getBodyType -> BOOM
3220121128.1600: mesh object not rezzing (no physics mesh).
33 Causes many errors. Doesn't stop after first error with box shape.
34 Eventually crashes when deleting the object.
3520121206.1434: rez Sam-pan into OSGrid BulletSim11 region
36 Immediate simulator crash. Mono does not output any stacktrace and
37 log just stops after reporting taint-time linking of the linkset.
38
39VEHICLES TODO LIST:
40=================================================
41Border crossing with linked vehicle causes crash
42Vehicles (Move smoothly)
43Some vehicles should not be able to turn if no speed or off ground.
44Cannot edit/move a vehicle being ridden: it jumps back to the origional position.
45Neb car jiggling left and right
46 Happens on terrain and any other mesh object. Flat cubes are much smoother.
47 This has been reduced but not eliminated.
48Implement referenceFrame for all the motion routines.
49For limitMotorUp, use raycast down to find if vehicle is in the air.
50Angular motion around Z moves the vehicle in world Z and not vehicle Z in ODE.
51 Verify that angular motion specified around Z moves in the vehicle coordinates.
52Verify llGetVel() is returning a smooth and good value for vehicle movement.
53llGetVel() should return the root's velocity if requested in a child prim.
54Implement function efficiency for lineaar and angular motion.
55After getting off a vehicle, the root prim is phantom (can be walked through)
56 Need to force a position update for the root prim after compound shape destruction
57Linkset explosion after three "rides" on Nebadon lite vehicle (LinksetConstraint)
58Remove vehicle angular velocity zeroing in BSPrim.UpdateProperties().
59 A kludge that isn't fixing the real problem of Bullet adding extra motion.
60Incorporate inter-relationship of angular corrections. For instance, angularDeflection
61 and angularMotorUp will compute same X or Y correction. When added together
62 creates over-correction and over-shoot and wabbling.
63
64GENERAL TODO LIST:
65=================================================
66Implement an avatar mesh shape. The Bullet capsule is way too limited.
67 Consider just hand creating a vertex/index array in a new BSShapeAvatar.
68Revisit CollisionMargin. Builders notice the 0.04 spacing between prims.
69Duplicating a physical prim causes old prim to jump away
70 Dup a phys prim and the original become unselected and thus interacts w/ selected prim.
71Scenes with hundred of thousands of static objects take a lot of physics CPU time.
72BSPrim.Force should set a continious force on the prim. The force should be
73 applied each tick. Some limits?
74Gun sending shooter flying.
75Collision margin (gap between physical objects lying on each other)
76Boundry checking (crashes related to crossing boundry)
77 Add check for border edge position for avatars and objects.
78 Verify the events are created for border crossings.
79Avatar rotation (check out changes to ScenePresence for physical rotation)
80Avatar running (what does phys engine need to do?)
81Small physical objects do not interact correctly
82 Create chain of .5x.5x.1 torui and make all but top physical so to hang.
83 The chain will fall apart and pairs will dance around on ground
84 Chains of 1x1x.2 will stay connected but will dance.
85 Chains above 2x2x.4 are more stable and get stablier as torui get larger.
86Add PID motor for avatar movement (slow to stop, ...)
87setForce should set a constant force. Different than AddImpulse.
88Implement raycast.
89Implement ShapeCollection.Dispose()
90Implement water as a plain so raycasting and collisions can happen with same.
91Add collision penetration return
92 Add field passed back by BulletSim.dll and fill with info in ManifoldConstact.GetDistance()
93Add osGetPhysicsEngineName() so scripters can tell whether BulletSim or ODE
94 Also osGetPhysicsEngineVerion() maybe.
95Linkset.Position and Linkset.Orientation requre rewrite to properly return
96 child position. LinksetConstraint acts like it's at taint time!!
97Implement LockAngularMotion -- implements llSetStatus(ROTATE_AXIS_*, T/F)
98Should the different PID factors have non-equal contributions for different
99 values of Efficiency?
100Selecting and deselecting physical objects causes CPU processing time to jump
101 http://www.youtube.com/watch?v=Hjg57fWg8yI&hd=1
102 put thousand physical objects, select and deselect same. CPU time will be large.
103Re-implement buoyancy as a separate force on the object rather than diddling gravity.
104 Register a pre-step event to add the force.
105More efficient memory usage when passing hull information from BSPrim to BulletSim
106Avatar movement motor check for zero or small movement. Somehow suppress small movements
107 when avatar has stopped and is just standing. Simple test for near zero has
108 the problem of preventing starting up (increase from zero) especially when falling.
109Physical and phantom will drop through the terrain
110
111
112LINKSETS
113======================================================
114Offset the center of the linkset to be the geometric center of all the prims
115 Not quite the same as the center-of-gravity
116Linksets should allow collisions to individual children
117 Add LocalID to children shapes in LinksetCompound and create events for individuals
118LinksetCompound: when one of the children changes orientation (like tires
119 turning on a vehicle, the whole compound object is rebuilt. Optimize this
120 so orientation/position of individual children can change without a rebuild.
121Verify/think through scripts in children of linksets. What do they reference
122 and return when getting position, velocity, ...
123Confirm constraint linksets still work after making all the changes for compound linksets.
124Use PostTaint callback to do rebuilds for constraint linksets to reduce rebuilding
125Add 'changed' flag or similar to reduce the number of times a linkset is rebuilt.
126 For compound linksets, add ability to remove or reposition individual child shapes.
127Speed up creation of large physical linksets
128 For instance, sitting in Neb's car (130 prims) takes several seconds to become physical.
129 REALLY bad for very large physical linksets (freezes the sim for many seconds).
130Eliminate collisions between objects in a linkset. (LinksetConstraint)
131 Have UserPointer point to struct with localID and linksetID?
132 Objects in original linkset still collide with each other?
133
134MORE
135======================================================
136Use the HACD convex hull routine in Bullet rather than the C# version.
137Do we need to do convex hulls all the time? Can complex meshes be left meshes?
138 There is some problem with meshes and collisions
139 Hulls are not as detailed as meshes. Hulled vehicles insides are different shape.
140Debounce avatar contact so legs don't keep folding up when standing.
141Implement LSL physics controls. Like STATUS_ROTATE_X.
142Add border extensions to terrain to help region crossings and objects leaving region.
143Use a different capsule shape for avatar when sitting
144 LL uses a pyrimidal shape scaled by the avatar's bounding box
145 http://wiki.secondlife.com/wiki/File:Avmeshforms.png
146Performance test with lots of avatars. Can BulletSim support a thousand?
147Optimize collisions in C++: only send up to the object subscribed to collisions.
148 Use collision subscription and remove the collsion(A,B) and collision(B,A)
149Check whether SimMotionState needs large if statement (see TODO).
150Implement 'top colliders' info.
151Avatar jump
152Performance measurement and changes to make quicker.
153Implement detailed physics stats (GetStats()).
154Measure performance improvement from hulls
155Test not using ghost objects for volume detect implementation.
156Performance of closures and delegates for taint processing
157 Are there faster ways?
158 Is any slowdown introduced by the existing implementation significant?
159Is there are more efficient method of implementing pre and post step actions?
160 See http://www.codeproject.com/Articles/29922/Weak-Events-in-C
161Physics Arena central pyramid: why is one side permiable?
162In SL, perfect spheres don't seem to have rolling friction. Add special case.
163Enforce physical parameter min/max:
164 Gravity: [-1, 28]
165 Friction: [0, 255]
166 Density: [1, 22587]
167 Restitution [0, 1]
168 http://wiki.secondlife.com/wiki/Physics_Material_Settings_test
169Avatar attachments have no mass? http://forums-archive.secondlife.com/54/f0/31796/1.html
170
171INTERNAL IMPROVEMENT/CLEANUP
172=================================================
173Can the 'inTaintTime' flag be cleaned up and used? For instance, a call to
174 BSScene.TaintedObject() could immediately execute the callback if already in taint time.
175Create the physical wrapper classes (BulletBody, BulletShape) by methods on
176 BSAPITemplate and make their actual implementation Bullet engine specific.
177 For the short term, just call the existing functions in ShapeCollection.
178Consider moving prim/character body and shape destruction in destroy()
179 to postTimeTime rather than protecting all the potential sets that
180 might have been queued up.
181Remove unused fields from ShapeData (not used in API2)
182Remove unused fields from pinned memory shared parameter block
183 Create parameter variables in BSScene to replace same.
184Breakout code for mesh/hull/compound/native into separate BSShape* classes
185 Standardize access to building and reference code.
186 The skeleton classes are in the sources but are not complete or linked in.
187Make BSBody and BSShape real classes to centralize creation/changin/destruction
188 Convert state and parameter calls from BulletSimAPI direct calls to
189 calls on BSBody and BSShape
190Generalize Dynamics and PID with standardized motors.
191Generalize Linkset and vehicles into PropertyManagers
192 Methods for Refresh, RemoveBodyDependencies, RestoreBodyDependencies
193 Potentially add events for shape destruction, etc.
194Better mechanism for resetting linkset set and vehicle parameters when body rebuilt.
195 BSPrim.CreateGeomAndObject is kludgy with the callbacks, etc.
196Implement linkset by setting position of children when root updated. (LinksetManual)
197 Linkset implementation using manual prim movement.
198LinkablePrim class? Would that simplify/centralize the linkset logic?
199BSScene.UpdateParameterSet() is broken. How to set params on objects?
200Add floating motor for BS_FLOATS_ON_WATER so prim and avatar will
201 bob at the water level. BSPrim.PositionSanityCheck()
202Should taints check for existance or activeness of target?
203 When destroying linksets/etc, taints can be generated for objects that are
204 actually gone when the taint happens. Crashes don't happen because the taint closure
205 keeps the object from being freed, but that is just an accident.
206 Possibly have an 'active' flag that is checked by the taint processor?
207Parameters for physics logging should be moved from BSScene to BSParam (at least boolean ones)
208Can some of the physical wrapper classes (BulletBody, BulletWorld, BulletShape) be 'sealed'?
209There are TOO MANY interfaces from BulletSim core to Bullet itself
210 Think of something to eliminate one or more of the layers
211
212THREADING
213=================================================
214Do taint action immediately if not actually executing Bullet.
215 Add lock around Bullet execution and just do taint actions if simulation is not happening.
216
217DONE DONE DONE DONE
218=================================================
219Cleanup code in BSDynamics by using motors. (Resolution: started)
220Consider implementing terrain with a mesh rather than heightmap. (Resolution: done)
221 Would have better and adjustable resolution.
222Build terrain mesh so heighmap is height of the center of the square meter.
223 Resolution: NOT DONE: SL and ODE define meter square as being at one corner with one diagional.
224Terrain as mesh. (Resolution: done)
225How are static linksets seen by the physics engine?
226 Resolution: they are not linked in physics. When moved, all the children are repositioned.
227Convert BSCharacter to use all API2 (Resolution: done)
228Avatar pushing difficult (too heavy?)
229Use asset service passed to BulletSim to get sculptie bodies, etc. (Resolution: done)
230Remove old code in DLL (all non-API2 stuff). (Resolution: done)
231Measurements of mega-physical prim performance (with graph) (Resolution: done, email)
232Debug Bullet internal stats output (why is timing all wrong?)
233 Resolution: Bullet stats logging only works with a single instance of Bullet (one region).
234Implement meshes or just verify that they work. (Resolution: they do!)
235Do prim hash codes work for sculpties and meshes? (Resolution: yes)
236Linkset implementation using compound shapes. (Resolution: implemented LinksetCompound)
237 Compound shapes will need the LocalID in the shapes and collision
238 processing to get it from there.
239Light cycle not banking (Resolution: It doesn't. Banking is roll adding yaw.)
240Package Bullet source mods for Bullet internal stats output
241 (Resolution: move code into WorldData.h rather than relying on patches)
242Single prim vehicles don't seem to properly vehiclize.
243 (Resolution: mass was not getting set properly for single prim linksets)
244Add material type linkage and input all the material property definitions.
245 Skeleton classes and table are in the sources but are not filled or used.
246 (Resolution:
247Neb vehicle taking > 25ms of physics time!!
248 (Resolution: compound linksets were being rebuild WAY too often)
249Avatar height off after unsitting (floats off ground)
250 Editting appearance then moving restores.
251 Must not be initializing height when recreating capsule after unsit.
252 (Resolution: confusion of scale vs size for native objects removed)
253Light cycle falling over when driving (Resolution: implemented angularMotorUp)
254Should vehicle angular/linear movement friction happen after all the components
255 or does it only apply to the basic movement?
256 (Resolution: friction added before returning newly computed motor value.
257 What is expected by some vehicles (turning up friction to moderate speed))
258Tune terrain/object friction to be closer to SL.
259 (Resolution: added material type with friction and resolution)
260Smooth avatar movement with motor (DONE)
261 Should motor update be all at taint-time? (Yes, DONE)
262 Fix avatar slowly sliding when standing (zero motion when stopped) (DONE)
263 (Resolution: added BSVMotor for avatar starting and stopping)
264llApplyImpulse()
265 Compare mass/movement in OS and SL. Calibrate actions. (DONE)
266 (Resolution: tested on SL and OS. AddForce scales the force for timestep)
267llSetBuoyancy() (DONE)
268 (Resolution: Bullet resets object gravity when added to world. Moved set gravity)
269Avatar density is WAY off. Compare and calibrate with what's in SL. (DONE)
270 (Resolution: set default density to 3.5 (from 60) which is closer to SL)
271Redo BulletSimAPI to allow native C# implementation of Bullet option (DONE)
272 (Resolution: added BSAPITemplate and then interfaces for C++ Bullet and C# BulletXNA
273Meshes rendering as bounding boxes (DONE)
274 (Resolution: Added test for mesh/sculpties in native shapes so it didn't think it was a box)
275llMoveToTarget (Resolution: added simple motor to update the position.)
276Angular motor direction is global coordinates rather than local coordinates (DONE)
277Add vehicle collisions so IsColliding is properly reported. (DONE)
278 Needed for banking, limitMotorUp, movementLimiting, ...
279 (Resolution: added CollisionFlags.BS_VEHICLE_COLLISION and code to use it)
280VehicleAddForce is not scaled by the simulation step but it is only
281 applied for one step. Should it be scaled? (DONE)
282 (Resolution: use force for timed things, Impulse for immediate, non-timed things)
283Complete implemention of preStepActions (DONE)
284 Replace vehicle step call with prestep event.
285 Is there a need for postStepActions? postStepTaints?
286Disable activity of passive linkset children. (DONE)
287 Since the linkset is a compound object, the old prims are left lying
288 around and need to be phantomized so they don't collide, ...
289Remove HeightmapInfo from terrain specification (DONE)
290 Since C++ code does not need terrain height, this structure et al are not needed. \ No newline at end of file