diff options
Diffstat (limited to '')
-rwxr-xr-x | OpenSim/Region/Physics/BulletSPlugin/BSLinksetCompound.cs | 409 |
1 files changed, 311 insertions, 98 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSLinksetCompound.cs b/OpenSim/Region/Physics/BulletSPlugin/BSLinksetCompound.cs index b9c2cf9..2c8dd23 100755 --- a/OpenSim/Region/Physics/BulletSPlugin/BSLinksetCompound.cs +++ b/OpenSim/Region/Physics/BulletSPlugin/BSLinksetCompound.cs | |||
@@ -28,22 +28,79 @@ using System; | |||
28 | using System.Collections.Generic; | 28 | using System.Collections.Generic; |
29 | using System.Text; | 29 | using System.Text; |
30 | 30 | ||
31 | using OpenSim.Framework; | ||
32 | |||
31 | using OMV = OpenMetaverse; | 33 | using OMV = OpenMetaverse; |
32 | 34 | ||
33 | namespace OpenSim.Region.Physics.BulletSPlugin | 35 | namespace 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. | ||
41 | sealed 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 | // 'centerDisplacement' is the distance from the root the the center-of-mass (Bullet 'zero' of the shape) | ||
55 | public BSLinksetCompoundInfo(int indx, BSPhysObject root, BSPhysObject child, OMV.Vector3 centerDisplacement) | ||
56 | { | ||
57 | // Each child position and rotation is given relative to the center-of-mass. | ||
58 | OMV.Quaternion invRootOrientation = OMV.Quaternion.Inverse(root.RawOrientation); | ||
59 | OMV.Vector3 displacementFromRoot = (child.RawPosition - root.RawPosition) * invRootOrientation; | ||
60 | OMV.Vector3 displacementFromCOM = displacementFromRoot - centerDisplacement; | ||
61 | OMV.Quaternion displacementRot = child.RawOrientation * invRootOrientation; | ||
62 | |||
63 | // Save relative position for recomputing child's world position after moving linkset. | ||
64 | Index = indx; | ||
65 | OffsetFromRoot = displacementFromRoot; | ||
66 | OffsetFromCenterOfMass = displacementFromCOM; | ||
67 | OffsetRot = displacementRot; | ||
68 | } | ||
69 | public override void Clear() | ||
70 | { | ||
71 | Index = 0; | ||
72 | OffsetFromRoot = OMV.Vector3.Zero; | ||
73 | OffsetFromCenterOfMass = OMV.Vector3.Zero; | ||
74 | OffsetRot = OMV.Quaternion.Identity; | ||
75 | } | ||
76 | public override string ToString() | ||
77 | { | ||
78 | StringBuilder buff = new StringBuilder(); | ||
79 | buff.Append("<i="); | ||
80 | buff.Append(Index.ToString()); | ||
81 | buff.Append(",p="); | ||
82 | buff.Append(OffsetFromRoot.ToString()); | ||
83 | buff.Append(",m="); | ||
84 | buff.Append(OffsetFromCenterOfMass.ToString()); | ||
85 | buff.Append(",r="); | ||
86 | buff.Append(OffsetRot.ToString()); | ||
87 | buff.Append(">"); | ||
88 | return buff.ToString(); | ||
89 | } | ||
90 | }; | ||
91 | |||
35 | public sealed class BSLinksetCompound : BSLinkset | 92 | public sealed class BSLinksetCompound : BSLinkset |
36 | { | 93 | { |
37 | private static string LogHeader = "[BULLETSIM LINKSET COMPOUND]"; | 94 | private static string LogHeader = "[BULLETSIM LINKSET COMPOUND]"; |
38 | 95 | ||
39 | public BSLinksetCompound(BSScene scene, BSPhysObject parent) | 96 | public BSLinksetCompound(BSScene scene, BSPhysObject parent) : base(scene, parent) |
40 | { | 97 | { |
41 | base.Initialize(scene, parent); | ||
42 | } | 98 | } |
43 | 99 | ||
44 | // For compound implimented linksets, if there are children, use compound shape for the root. | 100 | // For compound implimented linksets, if there are children, use compound shape for the root. |
45 | public override BSPhysicsShapeType PreferredPhysicalShape(BSPhysObject requestor) | 101 | public override BSPhysicsShapeType PreferredPhysicalShape(BSPhysObject requestor) |
46 | { | 102 | { |
103 | // Returning 'unknown' means we don't have a preference. | ||
47 | BSPhysicsShapeType ret = BSPhysicsShapeType.SHAPE_UNKNOWN; | 104 | BSPhysicsShapeType ret = BSPhysicsShapeType.SHAPE_UNKNOWN; |
48 | if (IsRoot(requestor) && HasAnyChildren) | 105 | if (IsRoot(requestor) && HasAnyChildren) |
49 | { | 106 | { |
@@ -55,27 +112,33 @@ public sealed class BSLinksetCompound : BSLinkset | |||
55 | 112 | ||
56 | // When physical properties are changed the linkset needs to recalculate | 113 | // When physical properties are changed the linkset needs to recalculate |
57 | // its internal properties. | 114 | // 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) | 115 | public override void Refresh(BSPhysObject requestor) |
61 | { | 116 | { |
62 | // External request for Refresh (from BSPrim) is not necessary | 117 | base.Refresh(requestor); |
63 | // InternalRefresh(requestor); | 118 | |
119 | // Something changed so do the rebuilding thing | ||
120 | // ScheduleRebuild(); | ||
64 | } | 121 | } |
65 | 122 | ||
66 | private void InternalRefresh(BSPhysObject requestor) | 123 | // Schedule a refresh to happen after all the other taint processing. |
124 | private void ScheduleRebuild(BSPhysObject requestor) | ||
67 | { | 125 | { |
68 | DetailLog("{0},BSLinksetCompound.Refresh,schedulingRefresh,requestor={1}", LinksetRoot.LocalID, requestor.LocalID); | 126 | DetailLog("{0},BSLinksetCompound.ScheduleRebuild,,rebuilding={1},hasChildren={2},actuallyScheduling={3}", |
69 | // Queue to happen after all the other taint processing | 127 | requestor.LocalID, Rebuilding, HasAnyChildren, (!Rebuilding && HasAnyChildren)); |
70 | PhysicsScene.PostTaintObject("BSLinksetCompound.Refresh", requestor.LocalID, delegate() | 128 | // When rebuilding, it is possible to set properties that would normally require a rebuild. |
129 | // If already rebuilding, don't request another rebuild. | ||
130 | // If a linkset with just a root prim (simple non-linked prim) don't bother rebuilding. | ||
131 | if (!Rebuilding && HasAnyChildren) | ||
71 | { | 132 | { |
72 | if (IsRoot(requestor) && HasAnyChildren) | 133 | PhysicsScene.PostTaintObject("BSLinksetCompound.ScheduleRebuild", LinksetRoot.LocalID, delegate() |
73 | RecomputeLinksetCompound(); | 134 | { |
74 | }); | 135 | if (HasAnyChildren) |
136 | RecomputeLinksetCompound(); | ||
137 | }); | ||
138 | } | ||
75 | } | 139 | } |
76 | 140 | ||
77 | // The object is going dynamic (physical). Do any setup necessary | 141 | // 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 | 142 | // Only the state of the passed object can be modified. The rest of the linkset |
80 | // has not yet been fully constructed. | 143 | // has not yet been fully constructed. |
81 | // Return 'true' if any properties updated on the passed object. | 144 | // Return 'true' if any properties updated on the passed object. |
@@ -84,12 +147,22 @@ public sealed class BSLinksetCompound : BSLinkset | |||
84 | { | 147 | { |
85 | bool ret = false; | 148 | bool ret = false; |
86 | DetailLog("{0},BSLinksetCompound.MakeDynamic,call,IsRoot={1}", child.LocalID, IsRoot(child)); | 149 | DetailLog("{0},BSLinksetCompound.MakeDynamic,call,IsRoot={1}", child.LocalID, IsRoot(child)); |
87 | if (!IsRoot(child)) | 150 | if (IsRoot(child)) |
151 | { | ||
152 | // The root is going dynamic. Rebuild the linkset so parts and mass get computed properly. | ||
153 | ScheduleRebuild(LinksetRoot); | ||
154 | } | ||
155 | else | ||
88 | { | 156 | { |
89 | // Physical children are removed from the world as the shape ofthe root compound | 157 | // The origional prims are removed from the world as the shape of the root compound |
90 | // shape takes over. | 158 | // shape takes over. |
91 | BulletSimAPI.AddToCollisionFlags2(child.PhysBody.ptr, CollisionFlags.CF_NO_CONTACT_RESPONSE); | 159 | PhysicsScene.PE.AddToCollisionFlags(child.PhysBody, CollisionFlags.CF_NO_CONTACT_RESPONSE); |
92 | BulletSimAPI.ForceActivationState2(child.PhysBody.ptr, ActivationState.DISABLE_SIMULATION); | 160 | PhysicsScene.PE.ForceActivationState(child.PhysBody, ActivationState.DISABLE_SIMULATION); |
161 | // We don't want collisions from the old linkset children. | ||
162 | PhysicsScene.PE.RemoveFromCollisionFlags(child.PhysBody, CollisionFlags.BS_SUBSCRIBE_COLLISION_EVENTS); | ||
163 | |||
164 | child.PhysBody.collisionType = CollisionType.LinksetChild; | ||
165 | |||
93 | ret = true; | 166 | ret = true; |
94 | } | 167 | } |
95 | return ret; | 168 | return ret; |
@@ -104,33 +177,92 @@ public sealed class BSLinksetCompound : BSLinkset | |||
104 | { | 177 | { |
105 | bool ret = false; | 178 | bool ret = false; |
106 | DetailLog("{0},BSLinksetCompound.MakeStatic,call,IsRoot={1}", child.LocalID, IsRoot(child)); | 179 | DetailLog("{0},BSLinksetCompound.MakeStatic,call,IsRoot={1}", child.LocalID, IsRoot(child)); |
107 | if (!IsRoot(child)) | 180 | if (IsRoot(child)) |
181 | { | ||
182 | ScheduleRebuild(LinksetRoot); | ||
183 | } | ||
184 | else | ||
108 | { | 185 | { |
109 | // The non-physical children can come back to life. | 186 | // The non-physical children can come back to life. |
110 | BulletSimAPI.RemoveFromCollisionFlags2(child.PhysBody.ptr, CollisionFlags.CF_NO_CONTACT_RESPONSE); | 187 | PhysicsScene.PE.RemoveFromCollisionFlags(child.PhysBody, CollisionFlags.CF_NO_CONTACT_RESPONSE); |
111 | // Don't force activation so setting of DISABLE_SIMULATION can stay. | 188 | |
112 | BulletSimAPI.Activate2(child.PhysBody.ptr, false); | 189 | child.PhysBody.collisionType = CollisionType.LinksetChild; |
190 | |||
191 | // Don't force activation so setting of DISABLE_SIMULATION can stay if used. | ||
192 | PhysicsScene.PE.Activate(child.PhysBody, false); | ||
113 | ret = true; | 193 | ret = true; |
114 | } | 194 | } |
115 | return ret; | 195 | return ret; |
116 | } | 196 | } |
117 | 197 | ||
118 | // Called at taint-time!! | 198 | // '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) | 199 | // Called at taint-time. |
120 | { | 200 | public override void UpdateProperties(UpdatedProperties whichUpdated, BSPhysObject updated) |
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 | { | 201 | { |
133 | return BulletSimAPI.GetOrientation2(member.PhysBody.ptr); | 202 | // The user moving a child around requires the rebuilding of the linkset compound shape |
203 | // One problem is this happens when a border is crossed -- the simulator implementation | ||
204 | // stores the position into the group which causes the move of the object | ||
205 | // but it also means all the child positions get updated. | ||
206 | // What would cause an unnecessary rebuild so we make sure the linkset is in a | ||
207 | // region before bothering to do a rebuild. | ||
208 | if (!IsRoot(updated) && PhysicsScene.TerrainManager.IsWithinKnownTerrain(LinksetRoot.RawPosition)) | ||
209 | { | ||
210 | // If a child of the linkset is updating only the position or rotation, that can be done | ||
211 | // without rebuilding the linkset. | ||
212 | // If a handle for the child can be fetch, we update the child here. If a rebuild was | ||
213 | // scheduled by someone else, the rebuild will just replace this setting. | ||
214 | |||
215 | bool updatedChild = false; | ||
216 | // Anything other than updating position or orientation usually means a physical update | ||
217 | // and that is caused by us updating the object. | ||
218 | if ((whichUpdated & ~(UpdatedProperties.Position | UpdatedProperties.Orientation)) == 0) | ||
219 | { | ||
220 | // Gather the child info. It might not be there if the linkset is in transition. | ||
221 | BSLinksetCompoundInfo lsi = updated.LinksetInfo as BSLinksetCompoundInfo; | ||
222 | if (LinksetRoot.PhysShape.HasPhysicalShape && lsi != null) | ||
223 | { | ||
224 | if (PhysicsScene.PE.IsCompound(LinksetRoot.PhysShape)) | ||
225 | { | ||
226 | BulletShape linksetChildShape = PhysicsScene.PE.GetChildShapeFromCompoundShapeIndex(LinksetRoot.PhysShape, lsi.Index); | ||
227 | if (linksetChildShape.HasPhysicalShape) | ||
228 | { | ||
229 | // Compute the offset from the center-of-gravity | ||
230 | BSLinksetCompoundInfo newLsi = new BSLinksetCompoundInfo(lsi.Index, LinksetRoot, updated, LinksetRoot.PositionDisplacement); | ||
231 | PhysicsScene.PE.UpdateChildTransform(LinksetRoot.PhysShape, lsi.Index, | ||
232 | newLsi.OffsetFromCenterOfMass, | ||
233 | newLsi.OffsetRot, | ||
234 | true /* shouldRecalculateLocalAabb */); | ||
235 | DetailLog("{0},BSLinksetCompound.UpdateProperties,changeChildPosRot,whichUpdated={1}newLsi={2}", | ||
236 | updated.LocalID, whichUpdated, newLsi); | ||
237 | updated.LinksetInfo = newLsi; | ||
238 | updatedChild = true; | ||
239 | } | ||
240 | else // DEBUG DEBUG | ||
241 | { // DEBUG DEBUG | ||
242 | DetailLog("{0},BSLinksetCompound.UpdateProperties,couldNotUpdateChild,noChildShape,shape={1}", | ||
243 | updated.LocalID, linksetChildShape); | ||
244 | } // DEBUG DEBUG | ||
245 | } | ||
246 | else // DEBUG DEBUG | ||
247 | { // DEBUG DEBUG | ||
248 | DetailLog("{0},BSLinksetCompound.UpdateProperties,couldNotUpdateChild,notCompound", updated.LocalID); | ||
249 | } // DEBUG DEBUG | ||
250 | } | ||
251 | else // DEBUG DEBUG | ||
252 | { // DEBUG DEBUG | ||
253 | DetailLog("{0},BSLinksetCompound.UpdateProperties,couldNotUpdateChild,rootPhysShape={1},lsi={2}", | ||
254 | updated.LocalID, LinksetRoot.PhysShape, lsi == null ? "NULL" : lsi.ToString()); | ||
255 | } // DEBUG DEBUG | ||
256 | if (!updatedChild) | ||
257 | { | ||
258 | // If couldn't do the individual child, the linkset needs a rebuild to incorporate the new child info. | ||
259 | DetailLog("{0},BSLinksetCompound.UpdateProperties,couldNotUpdateChild.schedulingRebuild,whichUpdated={1}", | ||
260 | updated.LocalID, whichUpdated); | ||
261 | updated.LinksetInfo = null; // setting to 'null' causes relative position to be recomputed. | ||
262 | ScheduleRebuild(updated); | ||
263 | } | ||
264 | } | ||
265 | } | ||
134 | } | 266 | } |
135 | 267 | ||
136 | // Routine called when rebuilding the body of some member of the linkset. | 268 | // Routine called when rebuilding the body of some member of the linkset. |
@@ -142,24 +274,55 @@ public sealed class BSLinksetCompound : BSLinkset | |||
142 | bool ret = false; | 274 | bool ret = false; |
143 | 275 | ||
144 | DetailLog("{0},BSLinksetCompound.RemoveBodyDependencies,refreshIfChild,rID={1},rBody={2},isRoot={3}", | 276 | DetailLog("{0},BSLinksetCompound.RemoveBodyDependencies,refreshIfChild,rID={1},rBody={2},isRoot={3}", |
145 | child.LocalID, LinksetRoot.LocalID, LinksetRoot.PhysBody.ptr.ToString("X"), IsRoot(child)); | 277 | child.LocalID, LinksetRoot.LocalID, LinksetRoot.PhysBody.AddrString, IsRoot(child)); |
146 | 278 | ||
147 | if (!IsRoot(child)) | 279 | if (!IsRoot(child)) |
148 | { | 280 | { |
149 | // Cause the current shape to be freed and the new one to be built. | 281 | // Because it is a convenient time, recompute child world position and rotation based on |
150 | InternalRefresh(LinksetRoot); | 282 | // its position in the linkset. |
151 | ret = true; | 283 | RecomputeChildWorldPosition(child, true); |
152 | } | 284 | } |
153 | 285 | ||
286 | // Cannot schedule a refresh/rebuild here because this routine is called when | ||
287 | // the linkset is being rebuilt. | ||
288 | // InternalRefresh(LinksetRoot); | ||
289 | |||
154 | return ret; | 290 | return ret; |
155 | } | 291 | } |
156 | 292 | ||
157 | // Companion to RemoveBodyDependencies(). If RemoveBodyDependencies() returns 'true', | 293 | // When the linkset is built, the child shape is added to the compound shape relative to the |
158 | // this routine will restore the removed constraints. | 294 | // root shape. The linkset then moves around but this does not move the actual child |
159 | // Called at taint-time!! | 295 | // prim. The child prim's location must be recomputed based on the location of the root shape. |
160 | public override void RestoreBodyDependencies(BSPrim child) | 296 | private void RecomputeChildWorldPosition(BSPhysObject child, bool inTaintTime) |
161 | { | 297 | { |
162 | // The Refresh operation queued by RemoveBodyDependencies() will build any missing constraints. | 298 | BSLinksetCompoundInfo lci = child.LinksetInfo as BSLinksetCompoundInfo; |
299 | if (lci != null) | ||
300 | { | ||
301 | if (inTaintTime) | ||
302 | { | ||
303 | OMV.Vector3 oldPos = child.RawPosition; | ||
304 | child.ForcePosition = LinksetRoot.RawPosition + lci.OffsetFromRoot; | ||
305 | child.ForceOrientation = LinksetRoot.RawOrientation * lci.OffsetRot; | ||
306 | DetailLog("{0},BSLinksetCompound.RecomputeChildWorldPosition,oldPos={1},lci={2},newPos={3}", | ||
307 | child.LocalID, oldPos, lci, child.RawPosition); | ||
308 | } | ||
309 | else | ||
310 | { | ||
311 | // TaintedObject is not used here so the raw position is set now and not at taint-time. | ||
312 | child.Position = LinksetRoot.RawPosition + lci.OffsetFromRoot; | ||
313 | child.Orientation = LinksetRoot.RawOrientation * lci.OffsetRot; | ||
314 | } | ||
315 | } | ||
316 | else | ||
317 | { | ||
318 | // This happens when children have been added to the linkset but the linkset | ||
319 | // has not been constructed yet. So like, at taint time, adding children to a linkset | ||
320 | // and then changing properties of the children (makePhysical, for instance) | ||
321 | // but the post-print action of actually rebuilding the linkset has not yet happened. | ||
322 | // PhysicsScene.Logger.WarnFormat("{0} Restoring linkset child position failed because of no relative position computed. ID={1}", | ||
323 | // LogHeader, child.LocalID); | ||
324 | DetailLog("{0},BSLinksetCompound.recomputeChildWorldPosition,noRelativePositonInfo", child.LocalID); | ||
325 | } | ||
163 | } | 326 | } |
164 | 327 | ||
165 | // ================================================================ | 328 | // ================================================================ |
@@ -174,24 +337,25 @@ public sealed class BSLinksetCompound : BSLinkset | |||
174 | 337 | ||
175 | DetailLog("{0},BSLinksetCompound.AddChildToLinkset,call,child={1}", LinksetRoot.LocalID, child.LocalID); | 338 | DetailLog("{0},BSLinksetCompound.AddChildToLinkset,call,child={1}", LinksetRoot.LocalID, child.LocalID); |
176 | 339 | ||
177 | // Cause constraints and assorted properties to be recomputed before the next simulation step. | 340 | // Rebuild the compound shape with the new child shape included |
178 | InternalRefresh(LinksetRoot); | 341 | ScheduleRebuild(child); |
179 | } | 342 | } |
180 | return; | 343 | return; |
181 | } | 344 | } |
182 | 345 | ||
183 | // Remove the specified child from the linkset. | 346 | // Remove the specified child from the linkset. |
184 | // Safe to call even if the child is not really in my linkset. | 347 | // Safe to call even if the child is not really in the linkset. |
185 | protected override void RemoveChildFromLinkset(BSPhysObject child) | 348 | protected override void RemoveChildFromLinkset(BSPhysObject child) |
186 | { | 349 | { |
187 | if (m_children.Remove(child)) | 350 | if (m_children.Remove(child)) |
188 | { | 351 | { |
189 | DetailLog("{0},BSLinksetCompound.RemoveChildFromLinkset,call,rID={1},rBody={2},cID={3},cBody={4}", | 352 | DetailLog("{0},BSLinksetCompound.RemoveChildFromLinkset,call,rID={1},rBody={2},cID={3},cBody={4}", |
190 | child.LocalID, | 353 | child.LocalID, |
191 | LinksetRoot.LocalID, LinksetRoot.PhysBody.ptr.ToString("X"), | 354 | LinksetRoot.LocalID, LinksetRoot.PhysBody.AddrString, |
192 | child.LocalID, child.PhysBody.ptr.ToString("X")); | 355 | child.LocalID, child.PhysBody.AddrString); |
193 | 356 | ||
194 | // Cause the child's body to be rebuilt and thus restored to normal operation | 357 | // Cause the child's body to be rebuilt and thus restored to normal operation |
358 | RecomputeChildWorldPosition(child, false); | ||
195 | child.ForceBodyShapeRebuild(false); | 359 | child.ForceBodyShapeRebuild(false); |
196 | 360 | ||
197 | if (!HasAnyChildren) | 361 | if (!HasAnyChildren) |
@@ -201,8 +365,8 @@ public sealed class BSLinksetCompound : BSLinkset | |||
201 | } | 365 | } |
202 | else | 366 | else |
203 | { | 367 | { |
204 | // Schedule a rebuild of the linkset before the next simulation tick. | 368 | // Rebuild the compound shape with the child removed |
205 | InternalRefresh(LinksetRoot); | 369 | ScheduleRebuild(LinksetRoot); |
206 | } | 370 | } |
207 | } | 371 | } |
208 | return; | 372 | return; |
@@ -213,63 +377,112 @@ public sealed class BSLinksetCompound : BSLinkset | |||
213 | // Constraint linksets are rebuilt every time. | 377 | // Constraint linksets are rebuilt every time. |
214 | // Note that this works for rebuilding just the root after a linkset is taken apart. | 378 | // Note that this works for rebuilding just the root after a linkset is taken apart. |
215 | // Called at taint time!! | 379 | // Called at taint time!! |
380 | private bool disableCOM = false; // disable until we get this debugged | ||
216 | private void RecomputeLinksetCompound() | 381 | private void RecomputeLinksetCompound() |
217 | { | 382 | { |
218 | // Cause the root shape to be rebuilt as a compound object with just the root in it | 383 | 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 | { | 384 | { |
227 | if (!IsRoot(cPrim)) | 385 | // Suppress rebuilding while rebuilding |
386 | Rebuilding = true; | ||
387 | |||
388 | // Cause the root shape to be rebuilt as a compound object with just the root in it | ||
389 | LinksetRoot.ForceBodyShapeRebuild(true); | ||
390 | |||
391 | // The center of mass for the linkset is the geometric center of the group. | ||
392 | // Compute a displacement for each component so it is relative to the center-of-mass. | ||
393 | // Bullet presumes an object's origin (relative <0,0,0>) is its center-of-mass | ||
394 | OMV.Vector3 centerOfMass; | ||
395 | OMV.Vector3 centerDisplacement = OMV.Vector3.Zero; | ||
396 | if (disableCOM) // DEBUG DEBUG | ||
397 | { // DEBUG DEBUG | ||
398 | centerOfMass = LinksetRoot.RawPosition; // DEBUG DEBUG | ||
399 | LinksetRoot.PositionDisplacement = OMV.Vector3.Zero; | ||
400 | } // DEBUG DEBUG | ||
401 | else | ||
228 | { | 402 | { |
229 | // Each child position and rotation is given relative to the root. | 403 | centerOfMass = ComputeLinksetCenterOfMass(); |
230 | OMV.Quaternion invRootOrientation = OMV.Quaternion.Inverse(LinksetRoot.RawOrientation); | 404 | // 'centerDisplacement' is the value to *add* to all the shape offsets |
231 | OMV.Vector3 displacementPos = (cPrim.RawPosition - LinksetRoot.RawPosition) * invRootOrientation; | 405 | centerDisplacement = LinksetRoot.RawPosition - centerOfMass; |
232 | OMV.Quaternion displacementRot = cPrim.RawOrientation * invRootOrientation; | 406 | |
407 | // Since we're displacing the center of the shape, we need to move the body in the world | ||
408 | LinksetRoot.PositionDisplacement = centerDisplacement; | ||
409 | |||
410 | // This causes the root prim position to be set properly based on the new PositionDisplacement | ||
411 | LinksetRoot.ForcePosition = LinksetRoot.RawPosition; | ||
412 | // Update the local transform for the root child shape so it is offset from the <0,0,0> which is COM | ||
413 | PhysicsScene.PE.UpdateChildTransform(LinksetRoot.PhysShape, 0, -centerDisplacement, OMV.Quaternion.Identity, false); | ||
414 | DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,COM,com={1},rootPos={2},centerDisp={3}", | ||
415 | LinksetRoot.LocalID, centerOfMass, LinksetRoot.RawPosition, centerDisplacement); | ||
416 | } | ||
233 | 417 | ||
234 | DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,addMemberToShape,mID={1},mShape={2},dispPos={3},dispRot={4}", | 418 | DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,start,rBody={1},rShape={2},numChildren={3}", |
235 | LinksetRoot.LocalID, cPrim.LocalID, cPrim.PhysShape, displacementPos, displacementRot); | 419 | LinksetRoot.LocalID, LinksetRoot.PhysBody, LinksetRoot.PhysShape, NumberOfChildren); |
236 | 420 | ||
237 | if (cPrim.PhysShape.isNativeShape) | 421 | // Add a shape for each of the other children in the linkset |
238 | { | 422 | int memberIndex = 1; |
239 | // Native shapes are not shared so we need to create a new one. | 423 | ForEachMember(delegate(BSPhysObject cPrim) |
240 | // A mesh or hull is created because scale is not available on a native shape. | 424 | { |
241 | // (TODO: Bullet does have a btScaledCollisionShape. Can that be used?) | 425 | if (!IsRoot(cPrim)) |
242 | BulletShape saveShape = cPrim.PhysShape; | ||
243 | cPrim.PhysShape.ptr = IntPtr.Zero; // Don't let the create free the child's shape | ||
244 | PhysicsScene.Shapes.CreateGeomMeshOrHull(cPrim, null); | ||
245 | BulletShape newShape = cPrim.PhysShape; | ||
246 | cPrim.PhysShape = saveShape; | ||
247 | BulletSimAPI.AddChildShapeToCompoundShape2(LinksetRoot.PhysShape.ptr, newShape.ptr, displacementPos, displacementRot); | ||
248 | } | ||
249 | else | ||
250 | { | 426 | { |
251 | // For the shared shapes (meshes and hulls), just use the shape in the child. | 427 | // Compute the displacement of the child from the root of the linkset. |
252 | if (PhysicsScene.Shapes.ReferenceShape(cPrim.PhysShape)) | 428 | // This info is saved in the child prim so the relationship does not |
429 | // change over time and the new child position can be computed | ||
430 | // when the linkset is being disassembled (the linkset may have moved). | ||
431 | BSLinksetCompoundInfo lci = cPrim.LinksetInfo as BSLinksetCompoundInfo; | ||
432 | if (lci == null) | ||
253 | { | 433 | { |
254 | PhysicsScene.Logger.ErrorFormat("{0} Rebuilt sharable shape when building linkset! Region={1}, primID={2}, shape={3}", | 434 | lci = new BSLinksetCompoundInfo(memberIndex, LinksetRoot, cPrim, centerDisplacement); |
255 | LogHeader, PhysicsScene.RegionName, cPrim.LocalID, cPrim.PhysShape); | 435 | cPrim.LinksetInfo = lci; |
436 | DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,creatingRelPos,lci={1}", cPrim.LocalID, lci); | ||
256 | } | 437 | } |
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 | 438 | ||
263 | // With all of the linkset packed into the root prim, it has the mass of everyone. | 439 | DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,addMemberToShape,mID={1},mShape={2},lci={3}", |
264 | float linksetMass = LinksetMass; | 440 | LinksetRoot.LocalID, cPrim.LocalID, cPrim.PhysShape, lci); |
265 | LinksetRoot.UpdatePhysicalMassProperties(linksetMass); | ||
266 | 441 | ||
267 | BulletSimAPI.RecalculateCompoundShapeLocalAabb2(LinksetRoot.PhysShape.ptr); | 442 | if (cPrim.PhysShape.isNativeShape) |
443 | { | ||
444 | // A native shape is turned into a hull collision shape because native | ||
445 | // shapes are not shared so we have to hullify it so it will be tracked | ||
446 | // and freed at the correct time. This also solves the scaling problem | ||
447 | // (native shapes scaled but hull/meshes are assumed to not be). | ||
448 | // TODO: decide of the native shape can just be used in the compound shape. | ||
449 | // Use call to CreateGeomNonSpecial(). | ||
450 | BulletShape saveShape = cPrim.PhysShape; | ||
451 | cPrim.PhysShape.Clear(); // Don't let the create free the child's shape | ||
452 | // PhysicsScene.Shapes.CreateGeomNonSpecial(true, cPrim, null); | ||
453 | PhysicsScene.Shapes.CreateGeomMeshOrHull(cPrim, null); | ||
454 | BulletShape newShape = cPrim.PhysShape; | ||
455 | cPrim.PhysShape = saveShape; | ||
456 | PhysicsScene.PE.AddChildShapeToCompoundShape(LinksetRoot.PhysShape, newShape, lci.OffsetFromCenterOfMass, lci.OffsetRot); | ||
457 | } | ||
458 | else | ||
459 | { | ||
460 | // For the shared shapes (meshes and hulls), just use the shape in the child. | ||
461 | // The reference count added here will be decremented when the compound shape | ||
462 | // is destroyed in BSShapeCollection (the child shapes are looped over and dereferenced). | ||
463 | if (PhysicsScene.Shapes.ReferenceShape(cPrim.PhysShape)) | ||
464 | { | ||
465 | PhysicsScene.Logger.ErrorFormat("{0} Rebuilt sharable shape when building linkset! Region={1}, primID={2}, shape={3}", | ||
466 | LogHeader, PhysicsScene.RegionName, cPrim.LocalID, cPrim.PhysShape); | ||
467 | } | ||
468 | PhysicsScene.PE.AddChildShapeToCompoundShape(LinksetRoot.PhysShape, cPrim.PhysShape, lci.OffsetFromCenterOfMass, lci.OffsetRot); | ||
469 | } | ||
470 | lci.Index = memberIndex; | ||
471 | memberIndex++; | ||
472 | } | ||
473 | return false; // 'false' says to move onto the next child in the list | ||
474 | }); | ||
268 | 475 | ||
269 | // DEBUG: see of inter-linkset collisions are causing problems for constraint linksets. | 476 | // With all of the linkset packed into the root prim, it has the mass of everyone. |
270 | // BulletSimAPI.SetCollisionFilterMask2(LinksetRoot.BSBody.ptr, | 477 | LinksetMass = ComputeLinksetMass(); |
271 | // (uint)CollisionFilterGroups.LinksetFilter, (uint)CollisionFilterGroups.LinksetMask); | 478 | LinksetRoot.UpdatePhysicalMassProperties(LinksetMass, true); |
479 | } | ||
480 | finally | ||
481 | { | ||
482 | Rebuilding = false; | ||
483 | } | ||
272 | 484 | ||
485 | PhysicsScene.PE.RecalculateCompoundShapeLocalAabb(LinksetRoot.PhysShape); | ||
273 | } | 486 | } |
274 | } | 487 | } |
275 | } \ No newline at end of file | 488 | } \ No newline at end of file |