aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Physics/BulletSPlugin/BSLinksetCompound.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSLinksetCompound.cs')
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSLinksetCompound.cs283
1 files changed, 202 insertions, 81 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSLinksetCompound.cs b/OpenSim/Region/Physics/BulletSPlugin/BSLinksetCompound.cs
index b9c2cf9..2a7b72c 100755
--- a/OpenSim/Region/Physics/BulletSPlugin/BSLinksetCompound.cs
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSLinksetCompound.cs
@@ -28,22 +28,54 @@ 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 OMV.Vector3 OffsetPos;
44 public OMV.Quaternion OffsetRot;
45 public BSLinksetCompoundInfo(OMV.Vector3 p, OMV.Quaternion r)
46 {
47 OffsetPos = p;
48 OffsetRot = r;
49 }
50 public override void Clear()
51 {
52 OffsetPos = OMV.Vector3.Zero;
53 OffsetRot = OMV.Quaternion.Identity;
54 }
55 public override string ToString()
56 {
57 StringBuilder buff = new StringBuilder();
58 buff.Append("<p=");
59 buff.Append(OffsetPos.ToString());
60 buff.Append(",r=");
61 buff.Append(OffsetRot.ToString());
62 buff.Append(">");
63 return buff.ToString();
64 }
65};
66
35public sealed class BSLinksetCompound : BSLinkset 67public sealed class BSLinksetCompound : BSLinkset
36{ 68{
37 private static string LogHeader = "[BULLETSIM LINKSET COMPOUND]"; 69 private static string LogHeader = "[BULLETSIM LINKSET COMPOUND]";
38 70
39 public BSLinksetCompound(BSScene scene, BSPhysObject parent) 71 public BSLinksetCompound(BSScene scene, BSPhysObject parent) : base(scene, parent)
40 { 72 {
41 base.Initialize(scene, parent);
42 } 73 }
43 74
44 // For compound implimented linksets, if there are children, use compound shape for the root. 75 // For compound implimented linksets, if there are children, use compound shape for the root.
45 public override BSPhysicsShapeType PreferredPhysicalShape(BSPhysObject requestor) 76 public override BSPhysicsShapeType PreferredPhysicalShape(BSPhysObject requestor)
46 { 77 {
78 // Returning 'unknown' means we don't have a preference.
47 BSPhysicsShapeType ret = BSPhysicsShapeType.SHAPE_UNKNOWN; 79 BSPhysicsShapeType ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
48 if (IsRoot(requestor) && HasAnyChildren) 80 if (IsRoot(requestor) && HasAnyChildren)
49 { 81 {
@@ -55,23 +87,27 @@ public sealed class BSLinksetCompound : BSLinkset
55 87
56 // When physical properties are changed the linkset needs to recalculate 88 // When physical properties are changed the linkset needs to recalculate
57 // its internal properties. 89 // 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) 90 public override void Refresh(BSPhysObject requestor)
61 { 91 {
62 // External request for Refresh (from BSPrim) is not necessary 92 // Something changed so do the rebuilding thing
63 // InternalRefresh(requestor); 93 // ScheduleRebuild();
64 } 94 }
65 95
66 private void InternalRefresh(BSPhysObject requestor) 96 // Schedule a refresh to happen after all the other taint processing.
97 private void ScheduleRebuild(BSPhysObject requestor)
67 { 98 {
68 DetailLog("{0},BSLinksetCompound.Refresh,schedulingRefresh,requestor={1}", LinksetRoot.LocalID, requestor.LocalID); 99 DetailLog("{0},BSLinksetCompound.Refresh,schedulingRefresh,rebuilding={1}",
69 // Queue to happen after all the other taint processing 100 requestor.LocalID, Rebuilding);
70 PhysicsScene.PostTaintObject("BSLinksetCompound.Refresh", requestor.LocalID, delegate() 101 // When rebuilding, it is possible to set properties that would normally require a rebuild.
102 // If already rebuilding, don't request another rebuild.
103 if (!Rebuilding)
71 { 104 {
72 if (IsRoot(requestor) && HasAnyChildren) 105 PhysicsScene.PostTaintObject("BSLinksetCompound.Refresh", LinksetRoot.LocalID, delegate()
73 RecomputeLinksetCompound(); 106 {
74 }); 107 if (HasAnyChildren)
108 RecomputeLinksetCompound();
109 });
110 }
75 } 111 }
76 112
77 // The object is going dynamic (physical). Do any setup necessary 113 // The object is going dynamic (physical). Do any setup necessary
@@ -84,12 +120,23 @@ public sealed class BSLinksetCompound : BSLinkset
84 { 120 {
85 bool ret = false; 121 bool ret = false;
86 DetailLog("{0},BSLinksetCompound.MakeDynamic,call,IsRoot={1}", child.LocalID, IsRoot(child)); 122 DetailLog("{0},BSLinksetCompound.MakeDynamic,call,IsRoot={1}", child.LocalID, IsRoot(child));
87 if (!IsRoot(child)) 123 if (IsRoot(child))
124 {
125 // The root is going dynamic. Make sure mass is properly set.
126 m_mass = ComputeLinksetMass();
127 ScheduleRebuild(LinksetRoot);
128 }
129 else
88 { 130 {
89 // Physical children are removed from the world as the shape ofthe root compound 131 // The origional prims are removed from the world as the shape of the root compound
90 // shape takes over. 132 // shape takes over.
91 BulletSimAPI.AddToCollisionFlags2(child.PhysBody.ptr, CollisionFlags.CF_NO_CONTACT_RESPONSE); 133 BulletSimAPI.AddToCollisionFlags2(child.PhysBody.ptr, CollisionFlags.CF_NO_CONTACT_RESPONSE);
92 BulletSimAPI.ForceActivationState2(child.PhysBody.ptr, ActivationState.DISABLE_SIMULATION); 134 BulletSimAPI.ForceActivationState2(child.PhysBody.ptr, ActivationState.DISABLE_SIMULATION);
135 // We don't want collisions from the old linkset children.
136 BulletSimAPI.RemoveFromCollisionFlags2(child.PhysBody.ptr, CollisionFlags.BS_SUBSCRIBE_COLLISION_EVENTS);
137
138 child.PhysBody.collisionType = CollisionType.LinksetChild;
139
93 ret = true; 140 ret = true;
94 } 141 }
95 return ret; 142 return ret;
@@ -104,33 +151,39 @@ public sealed class BSLinksetCompound : BSLinkset
104 { 151 {
105 bool ret = false; 152 bool ret = false;
106 DetailLog("{0},BSLinksetCompound.MakeStatic,call,IsRoot={1}", child.LocalID, IsRoot(child)); 153 DetailLog("{0},BSLinksetCompound.MakeStatic,call,IsRoot={1}", child.LocalID, IsRoot(child));
107 if (!IsRoot(child)) 154 if (IsRoot(child))
155 {
156 ScheduleRebuild(LinksetRoot);
157 }
158 else
108 { 159 {
109 // The non-physical children can come back to life. 160 // The non-physical children can come back to life.
110 BulletSimAPI.RemoveFromCollisionFlags2(child.PhysBody.ptr, CollisionFlags.CF_NO_CONTACT_RESPONSE); 161 BulletSimAPI.RemoveFromCollisionFlags2(child.PhysBody.ptr, CollisionFlags.CF_NO_CONTACT_RESPONSE);
111 // Don't force activation so setting of DISABLE_SIMULATION can stay. 162
163 child.PhysBody.collisionType = CollisionType.LinksetChild;
164
165 // Don't force activation so setting of DISABLE_SIMULATION can stay if used.
112 BulletSimAPI.Activate2(child.PhysBody.ptr, false); 166 BulletSimAPI.Activate2(child.PhysBody.ptr, false);
113 ret = true; 167 ret = true;
114 } 168 }
115 return ret; 169 return ret;
116 } 170 }
117 171
118 // Called at taint-time!! 172 public override void UpdateProperties(BSPhysObject updated, bool physicalUpdate)
119 public override void UpdateProperties(BSPhysObject updated)
120 { 173 {
121 // Nothing to do for constraints on property updates 174 // The user moving a child around requires the rebuilding of the linkset compound shape
122 } 175 // One problem is this happens when a border is crossed -- the simulator implementation
123 176 // is to store the position into the group which causes the move of the object
124 // The children move around in relationship to the root. 177 // but it also means all the child positions get updated.
125 // Just grab the current values of wherever it is right now. 178 // What would cause an unnecessary rebuild so we make sure the linkset is in a
126 public override OMV.Vector3 Position(BSPhysObject member) 179 // region before bothering to do a rebuild.
127 { 180 if (!IsRoot(updated)
128 return BulletSimAPI.GetPosition2(member.PhysBody.ptr); 181 && !physicalUpdate
129 } 182 && PhysicsScene.TerrainManager.IsWithinKnownTerrain(LinksetRoot.RawPosition))
130 183 {
131 public override OMV.Quaternion Orientation(BSPhysObject member) 184 updated.LinksetInfo = null;
132 { 185 ScheduleRebuild(updated);
133 return BulletSimAPI.GetOrientation2(member.PhysBody.ptr); 186 }
134 } 187 }
135 188
136 // Routine called when rebuilding the body of some member of the linkset. 189 // Routine called when rebuilding the body of some member of the linkset.
@@ -146,20 +199,58 @@ public sealed class BSLinksetCompound : BSLinkset
146 199
147 if (!IsRoot(child)) 200 if (!IsRoot(child))
148 { 201 {
149 // Cause the current shape to be freed and the new one to be built. 202 // Because it is a convenient time, recompute child world position and rotation based on
150 InternalRefresh(LinksetRoot); 203 // its position in the linkset.
151 ret = true; 204 RecomputeChildWorldPosition(child, true);
152 } 205 }
153 206
207 // Cannot schedule a refresh/rebuild here because this routine is called when
208 // the linkset is being rebuilt.
209 // InternalRefresh(LinksetRoot);
210
154 return ret; 211 return ret;
155 } 212 }
156 213
157 // Companion to RemoveBodyDependencies(). If RemoveBodyDependencies() returns 'true', 214 // Companion to RemoveBodyDependencies(). If RemoveBodyDependencies() returns 'true',
158 // this routine will restore the removed constraints. 215 // this routine will restore the removed constraints.
159 // Called at taint-time!! 216 // Called at taint-time!!
160 public override void RestoreBodyDependencies(BSPrim child) 217 public override void RestoreBodyDependencies(BSPrim child)
161 { 218 {
162 // The Refresh operation queued by RemoveBodyDependencies() will build any missing constraints. 219 }
220
221 // When the linkset is built, the child shape is added to the compound shape relative to the
222 // root shape. The linkset then moves around but this does not move the actual child
223 // prim. The child prim's location must be recomputed based on the location of the root shape.
224 private void RecomputeChildWorldPosition(BSPhysObject child, bool inTaintTime)
225 {
226 BSLinksetCompoundInfo lci = child.LinksetInfo as BSLinksetCompoundInfo;
227 if (lci != null)
228 {
229 if (inTaintTime)
230 {
231 OMV.Vector3 oldPos = child.RawPosition;
232 child.ForcePosition = LinksetRoot.RawPosition + lci.OffsetPos;
233 child.ForceOrientation = LinksetRoot.RawOrientation * lci.OffsetRot;
234 DetailLog("{0},BSLinksetCompound.RecomputeChildWorldPosition,oldPos={1},lci={2},newPos={3}",
235 child.LocalID, oldPos, lci, child.RawPosition);
236 }
237 else
238 {
239 // TaintedObject is not used here so the raw position is set now and not at taint-time.
240 child.Position = LinksetRoot.RawPosition + lci.OffsetPos;
241 child.Orientation = LinksetRoot.RawOrientation * lci.OffsetRot;
242 }
243 }
244 else
245 {
246 // This happens when children have been added to the linkset but the linkset
247 // has not been constructed yet. So like, at taint time, adding children to a linkset
248 // and then changing properties of the children (makePhysical, for instance)
249 // but the post-print action of actually rebuilding the linkset has not yet happened.
250 // PhysicsScene.Logger.WarnFormat("{0} Restoring linkset child position failed because of no relative position computed. ID={1}",
251 // LogHeader, child.LocalID);
252 DetailLog("{0},BSLinksetCompound.recomputeChildWorldPosition,noRelativePositonInfo", child.LocalID);
253 }
163 } 254 }
164 255
165 // ================================================================ 256 // ================================================================
@@ -174,14 +265,14 @@ public sealed class BSLinksetCompound : BSLinkset
174 265
175 DetailLog("{0},BSLinksetCompound.AddChildToLinkset,call,child={1}", LinksetRoot.LocalID, child.LocalID); 266 DetailLog("{0},BSLinksetCompound.AddChildToLinkset,call,child={1}", LinksetRoot.LocalID, child.LocalID);
176 267
177 // Cause constraints and assorted properties to be recomputed before the next simulation step. 268 // Rebuild the compound shape with the new child shape included
178 InternalRefresh(LinksetRoot); 269 ScheduleRebuild(child);
179 } 270 }
180 return; 271 return;
181 } 272 }
182 273
183 // Remove the specified child from the linkset. 274 // Remove the specified child from the linkset.
184 // Safe to call even if the child is not really in my linkset. 275 // Safe to call even if the child is not really in the linkset.
185 protected override void RemoveChildFromLinkset(BSPhysObject child) 276 protected override void RemoveChildFromLinkset(BSPhysObject child)
186 { 277 {
187 if (m_children.Remove(child)) 278 if (m_children.Remove(child))
@@ -192,6 +283,7 @@ public sealed class BSLinksetCompound : BSLinkset
192 child.LocalID, child.PhysBody.ptr.ToString("X")); 283 child.LocalID, child.PhysBody.ptr.ToString("X"));
193 284
194 // Cause the child's body to be rebuilt and thus restored to normal operation 285 // Cause the child's body to be rebuilt and thus restored to normal operation
286 RecomputeChildWorldPosition(child, false);
195 child.ForceBodyShapeRebuild(false); 287 child.ForceBodyShapeRebuild(false);
196 288
197 if (!HasAnyChildren) 289 if (!HasAnyChildren)
@@ -201,8 +293,8 @@ public sealed class BSLinksetCompound : BSLinkset
201 } 293 }
202 else 294 else
203 { 295 {
204 // Schedule a rebuild of the linkset before the next simulation tick. 296 // Rebuild the compound shape with the child removed
205 InternalRefresh(LinksetRoot); 297 ScheduleRebuild(child);
206 } 298 }
207 } 299 }
208 return; 300 return;
@@ -215,54 +307,83 @@ public sealed class BSLinksetCompound : BSLinkset
215 // Called at taint time!! 307 // Called at taint time!!
216 private void RecomputeLinksetCompound() 308 private void RecomputeLinksetCompound()
217 { 309 {
218 // Cause the root shape to be rebuilt as a compound object with just the root in it 310 try
219 LinksetRoot.ForceBodyShapeRebuild(true); 311 {
312 // Suppress rebuilding while rebuilding
313 Rebuilding = true;
220 314
221 DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,start,rBody={1},rShape={2},numChildren={3}", 315 // Cause the root shape to be rebuilt as a compound object with just the root in it
222 LinksetRoot.LocalID, LinksetRoot.PhysBody, LinksetRoot.PhysShape, NumberOfChildren); 316 LinksetRoot.ForceBodyShapeRebuild(true);
223 317
224 // Add a shape for each of the other children in the linkset 318 DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,start,rBody={1},rShape={2},numChildren={3}",
225 ForEachMember(delegate(BSPhysObject cPrim) 319 LinksetRoot.LocalID, LinksetRoot.PhysBody, LinksetRoot.PhysShape, NumberOfChildren);
226 { 320
227 if (!IsRoot(cPrim)) 321 // Add a shape for each of the other children in the linkset
322 ForEachMember(delegate(BSPhysObject cPrim)
228 { 323 {
229 // Each child position and rotation is given relative to the root. 324 if (!IsRoot(cPrim))
230 OMV.Quaternion invRootOrientation = OMV.Quaternion.Inverse(LinksetRoot.RawOrientation); 325 {
231 OMV.Vector3 displacementPos = (cPrim.RawPosition - LinksetRoot.RawPosition) * invRootOrientation; 326 // Compute the displacement of the child from the root of the linkset.
232 OMV.Quaternion displacementRot = cPrim.RawOrientation * invRootOrientation; 327 // This info is saved in the child prim so the relationship does not
328 // change over time and the new child position can be computed
329 // when the linkset is being disassembled (the linkset may have moved).
330 BSLinksetCompoundInfo lci = cPrim.LinksetInfo as BSLinksetCompoundInfo;
331 if (lci == null)
332 {
333 // Each child position and rotation is given relative to the root.
334 OMV.Quaternion invRootOrientation = OMV.Quaternion.Inverse(LinksetRoot.RawOrientation);
335 OMV.Vector3 displacementPos = (cPrim.RawPosition - LinksetRoot.RawPosition) * invRootOrientation;
336 OMV.Quaternion displacementRot = cPrim.RawOrientation * invRootOrientation;
337
338 // Save relative position for recomputing child's world position after moving linkset.
339 lci = new BSLinksetCompoundInfo(displacementPos, displacementRot);
340 cPrim.LinksetInfo = lci;
341 DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,creatingRelPos,lci={1}", cPrim.LocalID, lci);
342 }
233 343
234 DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,addMemberToShape,mID={1},mShape={2},dispPos={3},dispRot={4}", 344 DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,addMemberToShape,mID={1},mShape={2},dispPos={3},dispRot={4}",
235 LinksetRoot.LocalID, cPrim.LocalID, cPrim.PhysShape, displacementPos, displacementRot); 345 LinksetRoot.LocalID, cPrim.LocalID, cPrim.PhysShape, lci.OffsetPos, lci.OffsetRot);
236 346
237 if (cPrim.PhysShape.isNativeShape) 347 if (cPrim.PhysShape.isNativeShape)
238 { 348 {
239 // Native shapes are not shared so we need to create a new one. 349 // A native shape is turning into a hull collision shape because native
240 // A mesh or hull is created because scale is not available on a native shape. 350 // shapes are not shared so we have to hullify it so it will be tracked
241 // (TODO: Bullet does have a btScaledCollisionShape. Can that be used?) 351 // and freed at the correct time. This also solves the scaling problem
242 BulletShape saveShape = cPrim.PhysShape; 352 // (native shapes scaled but hull/meshes are assumed to not be).
243 cPrim.PhysShape.ptr = IntPtr.Zero; // Don't let the create free the child's shape 353 // TODO: decide of the native shape can just be used in the compound shape.
244 PhysicsScene.Shapes.CreateGeomMeshOrHull(cPrim, null); 354 // Use call to CreateGeomNonSpecial().
245 BulletShape newShape = cPrim.PhysShape; 355 BulletShape saveShape = cPrim.PhysShape;
246 cPrim.PhysShape = saveShape; 356 cPrim.PhysShape.Clear(); // Don't let the create free the child's shape
247 BulletSimAPI.AddChildShapeToCompoundShape2(LinksetRoot.PhysShape.ptr, newShape.ptr, displacementPos, displacementRot); 357 // PhysicsScene.Shapes.CreateGeomNonSpecial(true, cPrim, null);
248 } 358 PhysicsScene.Shapes.CreateGeomMeshOrHull(cPrim, null);
249 else 359 BulletShape newShape = cPrim.PhysShape;
250 { 360 cPrim.PhysShape = saveShape;
251 // For the shared shapes (meshes and hulls), just use the shape in the child. 361 BulletSimAPI.AddChildShapeToCompoundShape2(LinksetRoot.PhysShape.ptr, newShape.ptr, lci.OffsetPos, lci.OffsetRot);
252 if (PhysicsScene.Shapes.ReferenceShape(cPrim.PhysShape)) 362 }
363 else
253 { 364 {
254 PhysicsScene.Logger.ErrorFormat("{0} Rebuilt sharable shape when building linkset! Region={1}, primID={2}, shape={3}", 365 // For the shared shapes (meshes and hulls), just use the shape in the child.
255 LogHeader, PhysicsScene.RegionName, cPrim.LocalID, cPrim.PhysShape); 366 // The reference count added here will be decremented when the compound shape
367 // is destroyed in BSShapeCollection (the child shapes are looped over and dereferenced).
368 if (PhysicsScene.Shapes.ReferenceShape(cPrim.PhysShape))
369 {
370 PhysicsScene.Logger.ErrorFormat("{0} Rebuilt sharable shape when building linkset! Region={1}, primID={2}, shape={3}",
371 LogHeader, PhysicsScene.RegionName, cPrim.LocalID, cPrim.PhysShape);
372 }
373 BulletSimAPI.AddChildShapeToCompoundShape2(LinksetRoot.PhysShape.ptr, cPrim.PhysShape.ptr, lci.OffsetPos, lci.OffsetRot);
256 } 374 }
257 BulletSimAPI.AddChildShapeToCompoundShape2(LinksetRoot.PhysShape.ptr, cPrim.PhysShape.ptr, displacementPos, displacementRot);
258 } 375 }
259 } 376 return false; // 'false' says to move onto the next child in the list
260 return false; // 'false' says to move onto the next child in the list 377 });
261 });
262 378
263 // With all of the linkset packed into the root prim, it has the mass of everyone. 379 // With all of the linkset packed into the root prim, it has the mass of everyone.
264 float linksetMass = LinksetMass; 380 float linksetMass = LinksetMass;
265 LinksetRoot.UpdatePhysicalMassProperties(linksetMass); 381 LinksetRoot.UpdatePhysicalMassProperties(linksetMass);
382 }
383 finally
384 {
385 Rebuilding = false;
386 }
266 387
267 BulletSimAPI.RecalculateCompoundShapeLocalAabb2(LinksetRoot.PhysShape.ptr); 388 BulletSimAPI.RecalculateCompoundShapeLocalAabb2(LinksetRoot.PhysShape.ptr);
268 389