diff options
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSLinksetCompound.cs')
-rwxr-xr-x | OpenSim/Region/Physics/BulletSPlugin/BSLinksetCompound.cs | 370 |
1 files changed, 142 insertions, 228 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSLinksetCompound.cs b/OpenSim/Region/Physics/BulletSPlugin/BSLinksetCompound.cs index e05562a..085d195 100755 --- a/OpenSim/Region/Physics/BulletSPlugin/BSLinksetCompound.cs +++ b/OpenSim/Region/Physics/BulletSPlugin/BSLinksetCompound.cs | |||
@@ -35,81 +35,56 @@ using OMV = OpenMetaverse; | |||
35 | namespace OpenSim.Region.Physics.BulletSPlugin | 35 | namespace OpenSim.Region.Physics.BulletSPlugin |
36 | { | 36 | { |
37 | 37 | ||
38 | // When a child is linked, the relationship position of the child to the parent | 38 | public sealed class BSLinksetCompound : BSLinkset |
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 | { | 39 | { |
43 | public int Index; | 40 | private static string LogHeader = "[BULLETSIM LINKSET COMPOUND]"; |
44 | public OMV.Vector3 OffsetFromRoot; | 41 | |
45 | public OMV.Vector3 OffsetFromCenterOfMass; | 42 | public BSLinksetCompound(BSScene scene, BSPrimLinkable parent) |
46 | public OMV.Quaternion OffsetRot; | 43 | : base(scene, parent) |
47 | public BSLinksetCompoundInfo(int indx, OMV.Vector3 p, OMV.Quaternion r) | ||
48 | { | 44 | { |
49 | Index = indx; | 45 | LinksetImpl = LinksetImplementation.Compound; |
50 | OffsetFromRoot = p; | ||
51 | OffsetFromCenterOfMass = p; | ||
52 | OffsetRot = r; | ||
53 | } | 46 | } |
54 | // 'centerDisplacement' is the distance from the root the the center-of-mass (Bullet 'zero' of the shape) | 47 | |
55 | public BSLinksetCompoundInfo(int indx, BSPrimLinkable root, BSPrimLinkable child, OMV.Vector3 centerDisplacement) | 48 | // ================================================================ |
49 | // Changing the physical property of the linkset only needs to change the root | ||
50 | public override void SetPhysicalFriction(float friction) | ||
56 | { | 51 | { |
57 | // Each child position and rotation is given relative to the center-of-mass. | 52 | if (LinksetRoot.PhysBody.HasPhysicalBody) |
58 | OMV.Quaternion invRootOrientation = OMV.Quaternion.Inverse(root.RawOrientation); | 53 | m_physicsScene.PE.SetFriction(LinksetRoot.PhysBody, friction); |
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 | } | 54 | } |
69 | public override void Clear() | 55 | public override void SetPhysicalRestitution(float restitution) |
70 | { | 56 | { |
71 | Index = 0; | 57 | if (LinksetRoot.PhysBody.HasPhysicalBody) |
72 | OffsetFromRoot = OMV.Vector3.Zero; | 58 | m_physicsScene.PE.SetRestitution(LinksetRoot.PhysBody, restitution); |
73 | OffsetFromCenterOfMass = OMV.Vector3.Zero; | ||
74 | OffsetRot = OMV.Quaternion.Identity; | ||
75 | } | 59 | } |
76 | public override string ToString() | 60 | public override void SetPhysicalGravity(OMV.Vector3 gravity) |
77 | { | 61 | { |
78 | StringBuilder buff = new StringBuilder(); | 62 | if (LinksetRoot.PhysBody.HasPhysicalBody) |
79 | buff.Append("<i="); | 63 | m_physicsScene.PE.SetGravity(LinksetRoot.PhysBody, gravity); |
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 | } | 64 | } |
90 | }; | 65 | public override void ComputeAndSetLocalInertia(OMV.Vector3 inertiaFactor, float linksetMass) |
91 | |||
92 | public sealed class BSLinksetCompound : BSLinkset | ||
93 | { | ||
94 | private static string LogHeader = "[BULLETSIM LINKSET COMPOUND]"; | ||
95 | |||
96 | public BSLinksetCompound(BSScene scene, BSPrimLinkable parent) | ||
97 | : base(scene, parent) | ||
98 | { | 66 | { |
67 | OMV.Vector3 inertia = m_physicsScene.PE.CalculateLocalInertia(LinksetRoot.PhysShape.physShapeInfo, linksetMass); | ||
68 | LinksetRoot.Inertia = inertia * inertiaFactor; | ||
69 | m_physicsScene.PE.SetMassProps(LinksetRoot.PhysBody, linksetMass, LinksetRoot.Inertia); | ||
70 | m_physicsScene.PE.UpdateInertiaTensor(LinksetRoot.PhysBody); | ||
99 | } | 71 | } |
100 | 72 | public override void SetPhysicalCollisionFlags(CollisionFlags collFlags) | |
101 | // For compound implimented linksets, if there are children, use compound shape for the root. | 73 | { |
102 | public override BSPhysicsShapeType PreferredPhysicalShape(BSPrimLinkable requestor) | 74 | if (LinksetRoot.PhysBody.HasPhysicalBody) |
103 | { | 75 | m_physicsScene.PE.SetCollisionFlags(LinksetRoot.PhysBody, collFlags); |
104 | // Returning 'unknown' means we don't have a preference. | 76 | } |
105 | BSPhysicsShapeType ret = BSPhysicsShapeType.SHAPE_UNKNOWN; | 77 | public override void AddToPhysicalCollisionFlags(CollisionFlags collFlags) |
106 | if (IsRoot(requestor) && HasAnyChildren) | 78 | { |
107 | { | 79 | if (LinksetRoot.PhysBody.HasPhysicalBody) |
108 | ret = BSPhysicsShapeType.SHAPE_COMPOUND; | 80 | m_physicsScene.PE.AddToCollisionFlags(LinksetRoot.PhysBody, collFlags); |
109 | } | 81 | } |
110 | // DetailLog("{0},BSLinksetCompound.PreferredPhysicalShape,call,shape={1}", LinksetRoot.LocalID, ret); | 82 | public override void RemoveFromPhysicalCollisionFlags(CollisionFlags collFlags) |
111 | return ret; | 83 | { |
84 | if (LinksetRoot.PhysBody.HasPhysicalBody) | ||
85 | m_physicsScene.PE.RemoveFromCollisionFlags(LinksetRoot.PhysBody, collFlags); | ||
112 | } | 86 | } |
87 | // ================================================================ | ||
113 | 88 | ||
114 | // When physical properties are changed the linkset needs to recalculate | 89 | // When physical properties are changed the linkset needs to recalculate |
115 | // its internal properties. | 90 | // its internal properties. |
@@ -124,14 +99,15 @@ public sealed class BSLinksetCompound : BSLinkset | |||
124 | // Schedule a refresh to happen after all the other taint processing. | 99 | // Schedule a refresh to happen after all the other taint processing. |
125 | private void ScheduleRebuild(BSPrimLinkable requestor) | 100 | private void ScheduleRebuild(BSPrimLinkable requestor) |
126 | { | 101 | { |
127 | DetailLog("{0},BSLinksetCompound.ScheduleRebuild,,rebuilding={1},hasChildren={2},actuallyScheduling={3}", | 102 | DetailLog("{0},BSLinksetCompound.ScheduleRebuild,,rebuilding={1},hasChildren={2},actuallyScheduling={3}", |
128 | requestor.LocalID, Rebuilding, HasAnyChildren, (!Rebuilding && HasAnyChildren)); | 103 | requestor.LocalID, Rebuilding, HasAnyChildren, (!Rebuilding && HasAnyChildren)); |
104 | |||
129 | // When rebuilding, it is possible to set properties that would normally require a rebuild. | 105 | // When rebuilding, it is possible to set properties that would normally require a rebuild. |
130 | // If already rebuilding, don't request another rebuild. | 106 | // If already rebuilding, don't request another rebuild. |
131 | // If a linkset with just a root prim (simple non-linked prim) don't bother rebuilding. | 107 | // If a linkset with just a root prim (simple non-linked prim) don't bother rebuilding. |
132 | if (!Rebuilding && HasAnyChildren) | 108 | if (!Rebuilding && HasAnyChildren) |
133 | { | 109 | { |
134 | PhysicsScene.PostTaintObject("BSLinksetCompound.ScheduleRebuild", LinksetRoot.LocalID, delegate() | 110 | m_physicsScene.PostTaintObject("BSLinksetCompound.ScheduleRebuild", LinksetRoot.LocalID, delegate() |
135 | { | 111 | { |
136 | if (HasAnyChildren) | 112 | if (HasAnyChildren) |
137 | RecomputeLinksetCompound(); | 113 | RecomputeLinksetCompound(); |
@@ -153,46 +129,23 @@ public sealed class BSLinksetCompound : BSLinkset | |||
153 | // The root is going dynamic. Rebuild the linkset so parts and mass get computed properly. | 129 | // The root is going dynamic. Rebuild the linkset so parts and mass get computed properly. |
154 | ScheduleRebuild(LinksetRoot); | 130 | ScheduleRebuild(LinksetRoot); |
155 | } | 131 | } |
156 | else | ||
157 | { | ||
158 | // The origional prims are removed from the world as the shape of the root compound | ||
159 | // shape takes over. | ||
160 | PhysicsScene.PE.AddToCollisionFlags(child.PhysBody, CollisionFlags.CF_NO_CONTACT_RESPONSE); | ||
161 | PhysicsScene.PE.ForceActivationState(child.PhysBody, ActivationState.DISABLE_SIMULATION); | ||
162 | // We don't want collisions from the old linkset children. | ||
163 | PhysicsScene.PE.RemoveFromCollisionFlags(child.PhysBody, CollisionFlags.BS_SUBSCRIBE_COLLISION_EVENTS); | ||
164 | |||
165 | child.PhysBody.collisionType = CollisionType.LinksetChild; | ||
166 | |||
167 | ret = true; | ||
168 | } | ||
169 | return ret; | 132 | return ret; |
170 | } | 133 | } |
171 | 134 | ||
172 | // The object is going static (non-physical). Do any setup necessary for a static linkset. | 135 | // The object is going static (non-physical). We do not do anything for static linksets. |
173 | // Return 'true' if any properties updated on the passed object. | 136 | // Return 'true' if any properties updated on the passed object. |
174 | // This doesn't normally happen -- OpenSim removes the objects from the physical | ||
175 | // world if it is a static linkset. | ||
176 | // Called at taint-time! | 137 | // Called at taint-time! |
177 | public override bool MakeStatic(BSPrimLinkable child) | 138 | public override bool MakeStatic(BSPrimLinkable child) |
178 | { | 139 | { |
179 | bool ret = false; | 140 | bool ret = false; |
141 | |||
180 | DetailLog("{0},BSLinksetCompound.MakeStatic,call,IsRoot={1}", child.LocalID, IsRoot(child)); | 142 | DetailLog("{0},BSLinksetCompound.MakeStatic,call,IsRoot={1}", child.LocalID, IsRoot(child)); |
143 | child.ClearDisplacement(); | ||
181 | if (IsRoot(child)) | 144 | if (IsRoot(child)) |
182 | { | 145 | { |
146 | // Schedule a rebuild to verify that the root shape is set to the real shape. | ||
183 | ScheduleRebuild(LinksetRoot); | 147 | ScheduleRebuild(LinksetRoot); |
184 | } | 148 | } |
185 | else | ||
186 | { | ||
187 | // The non-physical children can come back to life. | ||
188 | PhysicsScene.PE.RemoveFromCollisionFlags(child.PhysBody, CollisionFlags.CF_NO_CONTACT_RESPONSE); | ||
189 | |||
190 | child.PhysBody.collisionType = CollisionType.LinksetChild; | ||
191 | |||
192 | // Don't force activation so setting of DISABLE_SIMULATION can stay if used. | ||
193 | PhysicsScene.PE.Activate(child.PhysBody, false); | ||
194 | ret = true; | ||
195 | } | ||
196 | return ret; | 149 | return ret; |
197 | } | 150 | } |
198 | 151 | ||
@@ -200,13 +153,20 @@ public sealed class BSLinksetCompound : BSLinkset | |||
200 | // Called at taint-time. | 153 | // Called at taint-time. |
201 | public override void UpdateProperties(UpdatedProperties whichUpdated, BSPrimLinkable updated) | 154 | public override void UpdateProperties(UpdatedProperties whichUpdated, BSPrimLinkable updated) |
202 | { | 155 | { |
156 | if (!LinksetRoot.IsPhysicallyActive) | ||
157 | { | ||
158 | // No reason to do this physical stuff for static linksets. | ||
159 | DetailLog("{0},BSLinksetCompound.UpdateProperties,notPhysical", LinksetRoot.LocalID); | ||
160 | return; | ||
161 | } | ||
162 | |||
203 | // The user moving a child around requires the rebuilding of the linkset compound shape | 163 | // The user moving a child around requires the rebuilding of the linkset compound shape |
204 | // One problem is this happens when a border is crossed -- the simulator implementation | 164 | // One problem is this happens when a border is crossed -- the simulator implementation |
205 | // stores the position into the group which causes the move of the object | 165 | // stores the position into the group which causes the move of the object |
206 | // but it also means all the child positions get updated. | 166 | // but it also means all the child positions get updated. |
207 | // What would cause an unnecessary rebuild so we make sure the linkset is in a | 167 | // What would cause an unnecessary rebuild so we make sure the linkset is in a |
208 | // region before bothering to do a rebuild. | 168 | // region before bothering to do a rebuild. |
209 | if (!IsRoot(updated) && PhysicsScene.TerrainManager.IsWithinKnownTerrain(LinksetRoot.RawPosition)) | 169 | if (!IsRoot(updated) && m_physicsScene.TerrainManager.IsWithinKnownTerrain(LinksetRoot.RawPosition)) |
210 | { | 170 | { |
211 | // If a child of the linkset is updating only the position or rotation, that can be done | 171 | // If a child of the linkset is updating only the position or rotation, that can be done |
212 | // without rebuilding the linkset. | 172 | // without rebuilding the linkset. |
@@ -218,22 +178,22 @@ public sealed class BSLinksetCompound : BSLinkset | |||
218 | // and that is caused by us updating the object. | 178 | // and that is caused by us updating the object. |
219 | if ((whichUpdated & ~(UpdatedProperties.Position | UpdatedProperties.Orientation)) == 0) | 179 | if ((whichUpdated & ~(UpdatedProperties.Position | UpdatedProperties.Orientation)) == 0) |
220 | { | 180 | { |
221 | // Find the physical instance of the child | 181 | // Find the physical instance of the child |
222 | if (LinksetRoot.PhysShape.HasPhysicalShape && PhysicsScene.PE.IsCompound(LinksetRoot.PhysShape)) | 182 | if (LinksetRoot.PhysShape.HasPhysicalShape && m_physicsScene.PE.IsCompound(LinksetRoot.PhysShape.physShapeInfo)) |
223 | { | 183 | { |
224 | // It is possible that the linkset is still under construction and the child is not yet | 184 | // It is possible that the linkset is still under construction and the child is not yet |
225 | // inserted into the compound shape. A rebuild of the linkset in a pre-step action will | 185 | // inserted into the compound shape. A rebuild of the linkset in a pre-step action will |
226 | // build the whole thing with the new position or rotation. | 186 | // build the whole thing with the new position or rotation. |
227 | // The index must be checked because Bullet references the child array but does no validity | 187 | // The index must be checked because Bullet references the child array but does no validity |
228 | // checking of the child index passed. | 188 | // checking of the child index passed. |
229 | int numLinksetChildren = PhysicsScene.PE.GetNumberOfCompoundChildren(LinksetRoot.PhysShape); | 189 | int numLinksetChildren = m_physicsScene.PE.GetNumberOfCompoundChildren(LinksetRoot.PhysShape.physShapeInfo); |
230 | if (updated.LinksetChildIndex < numLinksetChildren) | 190 | if (updated.LinksetChildIndex < numLinksetChildren) |
231 | { | 191 | { |
232 | BulletShape linksetChildShape = PhysicsScene.PE.GetChildShapeFromCompoundShapeIndex(LinksetRoot.PhysShape, updated.LinksetChildIndex); | 192 | BulletShape linksetChildShape = m_physicsScene.PE.GetChildShapeFromCompoundShapeIndex(LinksetRoot.PhysShape.physShapeInfo, updated.LinksetChildIndex); |
233 | if (linksetChildShape.HasPhysicalShape) | 193 | if (linksetChildShape.HasPhysicalShape) |
234 | { | 194 | { |
235 | // Found the child shape within the compound shape | 195 | // Found the child shape within the compound shape |
236 | PhysicsScene.PE.UpdateChildTransform(LinksetRoot.PhysShape, updated.LinksetChildIndex, | 196 | m_physicsScene.PE.UpdateChildTransform(LinksetRoot.PhysShape.physShapeInfo, updated.LinksetChildIndex, |
237 | updated.RawPosition - LinksetRoot.RawPosition, | 197 | updated.RawPosition - LinksetRoot.RawPosition, |
238 | updated.RawOrientation * OMV.Quaternion.Inverse(LinksetRoot.RawOrientation), | 198 | updated.RawOrientation * OMV.Quaternion.Inverse(LinksetRoot.RawOrientation), |
239 | true /* shouldRecalculateLocalAabb */); | 199 | true /* shouldRecalculateLocalAabb */); |
@@ -267,7 +227,6 @@ public sealed class BSLinksetCompound : BSLinkset | |||
267 | // there will already be a rebuild scheduled. | 227 | // there will already be a rebuild scheduled. |
268 | DetailLog("{0},BSLinksetCompound.UpdateProperties,couldNotUpdateChild.schedulingRebuild,whichUpdated={1}", | 228 | DetailLog("{0},BSLinksetCompound.UpdateProperties,couldNotUpdateChild.schedulingRebuild,whichUpdated={1}", |
269 | updated.LocalID, whichUpdated); | 229 | updated.LocalID, whichUpdated); |
270 | updated.LinksetInfo = null; // setting to 'null' causes relative position to be recomputed. | ||
271 | ScheduleRebuild(updated); | 230 | ScheduleRebuild(updated); |
272 | } | 231 | } |
273 | } | 232 | } |
@@ -275,75 +234,22 @@ public sealed class BSLinksetCompound : BSLinkset | |||
275 | } | 234 | } |
276 | 235 | ||
277 | // Routine called when rebuilding the body of some member of the linkset. | 236 | // Routine called when rebuilding the body of some member of the linkset. |
278 | // Since we don't keep in world relationships, do nothing unless it's a child changing. | 237 | // If one of the bodies is being changed, the linkset needs rebuilding. |
238 | // For instance, a linkset is built and then a mesh asset is read in and the mesh is recreated. | ||
279 | // Returns 'true' of something was actually removed and would need restoring | 239 | // Returns 'true' of something was actually removed and would need restoring |
280 | // Called at taint-time!! | 240 | // Called at taint-time!! |
281 | public override bool RemoveBodyDependencies(BSPrimLinkable child) | 241 | public override bool RemoveDependencies(BSPrimLinkable child) |
282 | { | 242 | { |
283 | bool ret = false; | 243 | bool ret = false; |
284 | 244 | ||
285 | DetailLog("{0},BSLinksetCompound.RemoveBodyDependencies,refreshIfChild,rID={1},rBody={2},isRoot={3}", | 245 | DetailLog("{0},BSLinksetCompound.RemoveBodyDependencies,refreshIfChild,rID={1},rBody={2},isRoot={3}", |
286 | child.LocalID, LinksetRoot.LocalID, LinksetRoot.PhysBody, IsRoot(child)); | 246 | child.LocalID, LinksetRoot.LocalID, LinksetRoot.PhysBody, IsRoot(child)); |
287 | 247 | ||
288 | if (!IsRoot(child)) | 248 | ScheduleRebuild(child); |
289 | { | ||
290 | // Because it is a convenient time, recompute child world position and rotation based on | ||
291 | // its position in the linkset. | ||
292 | RecomputeChildWorldPosition(child, true /* inTaintTime */); | ||
293 | child.LinksetInfo = null; | ||
294 | } | ||
295 | |||
296 | // Cannot schedule a refresh/rebuild here because this routine is called when | ||
297 | // the linkset is being rebuilt. | ||
298 | // InternalRefresh(LinksetRoot); | ||
299 | 249 | ||
300 | return ret; | 250 | return ret; |
301 | } | 251 | } |
302 | 252 | ||
303 | // When the linkset is built, the child shape is added to the compound shape relative to the | ||
304 | // root shape. The linkset then moves around but this does not move the actual child | ||
305 | // prim. The child prim's location must be recomputed based on the location of the root shape. | ||
306 | private void RecomputeChildWorldPosition(BSPrimLinkable child, bool inTaintTime) | ||
307 | { | ||
308 | // For the moment (20130201), disable this computation (converting the child physical addr back to | ||
309 | // a region address) until we have a good handle on center-of-mass offsets and what the physics | ||
310 | // engine moving a child actually means. | ||
311 | // The simulator keeps track of where children should be as the linkset moves. Setting | ||
312 | // the pos/rot here does not effect that knowledge as there is no good way for the | ||
313 | // physics engine to send the simulator an update for a child. | ||
314 | |||
315 | /* | ||
316 | BSLinksetCompoundInfo lci = child.LinksetInfo as BSLinksetCompoundInfo; | ||
317 | if (lci != null) | ||
318 | { | ||
319 | if (inTaintTime) | ||
320 | { | ||
321 | OMV.Vector3 oldPos = child.RawPosition; | ||
322 | child.ForcePosition = LinksetRoot.RawPosition + lci.OffsetFromRoot; | ||
323 | child.ForceOrientation = LinksetRoot.RawOrientation * lci.OffsetRot; | ||
324 | DetailLog("{0},BSLinksetCompound.RecomputeChildWorldPosition,oldPos={1},lci={2},newPos={3}", | ||
325 | child.LocalID, oldPos, lci, child.RawPosition); | ||
326 | } | ||
327 | else | ||
328 | { | ||
329 | // TaintedObject is not used here so the raw position is set now and not at taint-time. | ||
330 | child.Position = LinksetRoot.RawPosition + lci.OffsetFromRoot; | ||
331 | child.Orientation = LinksetRoot.RawOrientation * lci.OffsetRot; | ||
332 | } | ||
333 | } | ||
334 | else | ||
335 | { | ||
336 | // This happens when children have been added to the linkset but the linkset | ||
337 | // has not been constructed yet. So like, at taint time, adding children to a linkset | ||
338 | // and then changing properties of the children (makePhysical, for instance) | ||
339 | // but the post-print action of actually rebuilding the linkset has not yet happened. | ||
340 | // PhysicsScene.Logger.WarnFormat("{0} Restoring linkset child position failed because of no relative position computed. ID={1}", | ||
341 | // LogHeader, child.LocalID); | ||
342 | DetailLog("{0},BSLinksetCompound.recomputeChildWorldPosition,noRelativePositonInfo", child.LocalID); | ||
343 | } | ||
344 | */ | ||
345 | } | ||
346 | |||
347 | // ================================================================ | 253 | // ================================================================ |
348 | 254 | ||
349 | // Add a new child to the linkset. | 255 | // Add a new child to the linkset. |
@@ -352,7 +258,7 @@ public sealed class BSLinksetCompound : BSLinkset | |||
352 | { | 258 | { |
353 | if (!HasChild(child)) | 259 | if (!HasChild(child)) |
354 | { | 260 | { |
355 | m_children.Add(child); | 261 | m_children.Add(child, new BSLinkInfo(child)); |
356 | 262 | ||
357 | DetailLog("{0},BSLinksetCompound.AddChildToLinkset,call,child={1}", LinksetRoot.LocalID, child.LocalID); | 263 | DetailLog("{0},BSLinksetCompound.AddChildToLinkset,call,child={1}", LinksetRoot.LocalID, child.LocalID); |
358 | 264 | ||
@@ -376,8 +282,6 @@ public sealed class BSLinksetCompound : BSLinkset | |||
376 | child.LocalID, child.PhysBody.AddrString); | 282 | child.LocalID, child.PhysBody.AddrString); |
377 | 283 | ||
378 | // Cause the child's body to be rebuilt and thus restored to normal operation | 284 | // Cause the child's body to be rebuilt and thus restored to normal operation |
379 | RecomputeChildWorldPosition(child, false); | ||
380 | child.LinksetInfo = null; | ||
381 | child.ForceBodyShapeRebuild(false); | 285 | child.ForceBodyShapeRebuild(false); |
382 | 286 | ||
383 | if (!HasAnyChildren) | 287 | if (!HasAnyChildren) |
@@ -399,108 +303,118 @@ public sealed class BSLinksetCompound : BSLinkset | |||
399 | // Constraint linksets are rebuilt every time. | 303 | // Constraint linksets are rebuilt every time. |
400 | // Note that this works for rebuilding just the root after a linkset is taken apart. | 304 | // Note that this works for rebuilding just the root after a linkset is taken apart. |
401 | // Called at taint time!! | 305 | // Called at taint time!! |
402 | private bool disableCOM = true; // DEBUG DEBUG: disable until we get this debugged | 306 | private bool UseBulletSimRootOffsetHack = false; // Attempt to have Bullet track the coords of root compound shape |
403 | private void RecomputeLinksetCompound() | 307 | private void RecomputeLinksetCompound() |
404 | { | 308 | { |
405 | try | 309 | try |
406 | { | 310 | { |
407 | // Suppress rebuilding while rebuilding. (We know rebuilding is on only one thread.) | ||
408 | Rebuilding = true; | 311 | Rebuilding = true; |
409 | 312 | ||
410 | // Cause the root shape to be rebuilt as a compound object with just the root in it | 313 | // No matter what is being done, force the root prim's PhysBody and PhysShape to get set |
411 | LinksetRoot.ForceBodyShapeRebuild(true /* inTaintTime */); | 314 | // to what they should be as if the root was not in a linkset. |
315 | // Not that bad since we only get into this routine if there are children in the linkset and | ||
316 | // something has been updated/changed. | ||
317 | // Have to do the rebuild before checking for physical because this might be a linkset | ||
318 | // being destructed and going non-physical. | ||
319 | LinksetRoot.ForceBodyShapeRebuild(true); | ||
412 | 320 | ||
413 | // The center of mass for the linkset is the geometric center of the group. | 321 | // There is no reason to build all this physical stuff for a non-physical linkset. |
414 | // Compute a displacement for each component so it is relative to the center-of-mass. | 322 | if (!LinksetRoot.IsPhysicallyActive) |
415 | // Bullet presumes an object's origin (relative <0,0,0>) is its center-of-mass | ||
416 | OMV.Vector3 centerOfMassW = LinksetRoot.RawPosition; | ||
417 | if (!disableCOM) // DEBUG DEBUG | ||
418 | { | 323 | { |
419 | // Compute a center-of-mass in world coordinates. | 324 | DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,notPhysical", LinksetRoot.LocalID); |
420 | centerOfMassW = ComputeLinksetCenterOfMass(); | 325 | return; // Note the 'finally' clause at the botton which will get executed. |
421 | } | 326 | } |
422 | 327 | ||
423 | OMV.Quaternion invRootOrientation = OMV.Quaternion.Inverse(LinksetRoot.RawOrientation); | 328 | // Get a new compound shape to build the linkset shape in. |
329 | BSShape linksetShape = BSShapeCompound.GetReference(m_physicsScene); | ||
424 | 330 | ||
425 | // 'centerDisplacement' is the value to subtract from children to give physical offset position | 331 | // Compute a displacement for each component so it is relative to the center-of-mass. |
426 | OMV.Vector3 centerDisplacement = (centerOfMassW - LinksetRoot.RawPosition) * invRootOrientation; | 332 | // Bullet presumes an object's origin (relative <0,0,0>) is its center-of-mass |
427 | LinksetRoot.SetEffectiveCenterOfMassW(centerDisplacement); | 333 | OMV.Vector3 centerOfMassW = ComputeLinksetCenterOfMass(); |
428 | |||
429 | // This causes the physical position of the root prim to be offset to accomodate for the displacements | ||
430 | LinksetRoot.ForcePosition = LinksetRoot.RawPosition; | ||
431 | 334 | ||
432 | // Update the local transform for the root child shape so it is offset from the <0,0,0> which is COM | 335 | OMV.Quaternion invRootOrientation = OMV.Quaternion.Normalize(OMV.Quaternion.Inverse(LinksetRoot.RawOrientation)); |
433 | PhysicsScene.PE.UpdateChildTransform(LinksetRoot.PhysShape, 0 /* childIndex */, | 336 | OMV.Vector3 origRootPosition = LinksetRoot.RawPosition; |
434 | -centerDisplacement, | ||
435 | OMV.Quaternion.Identity, // LinksetRoot.RawOrientation, | ||
436 | false /* shouldRecalculateLocalAabb (is done later after linkset built) */); | ||
437 | 337 | ||
438 | DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,COM,com={1},rootPos={2},centerDisp={3}", | 338 | // 'centerDisplacementV' is the vehicle relative distance from the simulator root position to the center-of-mass |
439 | LinksetRoot.LocalID, centerOfMassW, LinksetRoot.RawPosition, centerDisplacement); | 339 | OMV.Vector3 centerDisplacementV = (centerOfMassW - LinksetRoot.RawPosition) * invRootOrientation; |
340 | if (UseBulletSimRootOffsetHack || !BSParam.LinksetOffsetCenterOfMass) | ||
341 | { | ||
342 | // Zero everything if center-of-mass displacement is not being done. | ||
343 | centerDisplacementV = OMV.Vector3.Zero; | ||
344 | LinksetRoot.ClearDisplacement(); | ||
345 | } | ||
346 | else | ||
347 | { | ||
348 | // The actual center-of-mass could have been set by the user. | ||
349 | centerDisplacementV = LinksetRoot.SetEffectiveCenterOfMassDisplacement(centerDisplacementV); | ||
350 | } | ||
440 | 351 | ||
441 | DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,start,rBody={1},rShape={2},numChildren={3}", | 352 | DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,COM,rootPos={1},com={2},comDisp={3}", |
442 | LinksetRoot.LocalID, LinksetRoot.PhysBody, LinksetRoot.PhysShape, NumberOfChildren); | 353 | LinksetRoot.LocalID, origRootPosition, centerOfMassW, centerDisplacementV); |
443 | 354 | ||
444 | // Add a shape for each of the other children in the linkset | 355 | // Add the shapes of all the components of the linkset |
445 | int memberIndex = 1; | 356 | int memberIndex = 1; |
446 | ForEachMember(delegate(BSPrimLinkable cPrim) | 357 | ForEachMember((cPrim) => |
447 | { | 358 | { |
448 | if (IsRoot(cPrim)) | 359 | if (IsRoot(cPrim)) |
449 | { | 360 | { |
361 | // Root shape is always index zero. | ||
450 | cPrim.LinksetChildIndex = 0; | 362 | cPrim.LinksetChildIndex = 0; |
451 | } | 363 | } |
452 | else | 364 | else |
453 | { | 365 | { |
454 | cPrim.LinksetChildIndex = memberIndex; | 366 | cPrim.LinksetChildIndex = memberIndex; |
367 | memberIndex++; | ||
368 | } | ||
455 | 369 | ||
456 | if (cPrim.PhysShape.isNativeShape) | 370 | // Get a reference to the shape of the child for adding of that shape to the linkset compound shape |
457 | { | 371 | BSShape childShape = cPrim.PhysShape.GetReference(m_physicsScene, cPrim); |
458 | // A native shape is turned into a hull collision shape because native | ||
459 | // shapes are not shared so we have to hullify it so it will be tracked | ||
460 | // and freed at the correct time. This also solves the scaling problem | ||
461 | // (native shapes scale but hull/meshes are assumed to not be). | ||
462 | // TODO: decide of the native shape can just be used in the compound shape. | ||
463 | // Use call to CreateGeomNonSpecial(). | ||
464 | BulletShape saveShape = cPrim.PhysShape; | ||
465 | cPrim.PhysShape.Clear(); // Don't let the create free the child's shape | ||
466 | PhysicsScene.Shapes.CreateGeomMeshOrHull(cPrim, null); | ||
467 | BulletShape newShape = cPrim.PhysShape; | ||
468 | cPrim.PhysShape = saveShape; | ||
469 | |||
470 | OMV.Vector3 offsetPos = (cPrim.RawPosition - LinksetRoot.RawPosition) * invRootOrientation - centerDisplacement; | ||
471 | OMV.Quaternion offsetRot = cPrim.RawOrientation * invRootOrientation; | ||
472 | PhysicsScene.PE.AddChildShapeToCompoundShape(LinksetRoot.PhysShape, newShape, offsetPos, offsetRot); | ||
473 | DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,addNative,indx={1},rShape={2},cShape={3},offPos={4},offRot={5}", | ||
474 | LinksetRoot.LocalID, memberIndex, LinksetRoot.PhysShape, newShape, offsetPos, offsetRot); | ||
475 | } | ||
476 | else | ||
477 | { | ||
478 | // For the shared shapes (meshes and hulls), just use the shape in the child. | ||
479 | // The reference count added here will be decremented when the compound shape | ||
480 | // is destroyed in BSShapeCollection (the child shapes are looped over and dereferenced). | ||
481 | if (PhysicsScene.Shapes.ReferenceShape(cPrim.PhysShape)) | ||
482 | { | ||
483 | PhysicsScene.Logger.ErrorFormat("{0} Rebuilt sharable shape when building linkset! Region={1}, primID={2}, shape={3}", | ||
484 | LogHeader, PhysicsScene.RegionName, cPrim.LocalID, cPrim.PhysShape); | ||
485 | } | ||
486 | OMV.Vector3 offsetPos = (cPrim.RawPosition - LinksetRoot.RawPosition) * invRootOrientation - centerDisplacement; | ||
487 | OMV.Quaternion offsetRot = cPrim.RawOrientation * invRootOrientation; | ||
488 | PhysicsScene.PE.AddChildShapeToCompoundShape(LinksetRoot.PhysShape, cPrim.PhysShape, offsetPos, offsetRot); | ||
489 | DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,addNonNative,indx={1},rShape={2},cShape={3},offPos={4},offRot={5}", | ||
490 | LinksetRoot.LocalID, memberIndex, LinksetRoot.PhysShape, cPrim.PhysShape, offsetPos, offsetRot); | ||
491 | 372 | ||
492 | } | 373 | // Offset the child shape from the center-of-mass and rotate it to vehicle relative. |
493 | memberIndex++; | 374 | OMV.Vector3 offsetPos = (cPrim.RawPosition - origRootPosition) * invRootOrientation - centerDisplacementV; |
375 | OMV.Quaternion offsetRot = OMV.Quaternion.Normalize(cPrim.RawOrientation) * invRootOrientation; | ||
376 | |||
377 | // Add the child shape to the compound shape being built | ||
378 | m_physicsScene.PE.AddChildShapeToCompoundShape(linksetShape.physShapeInfo, childShape.physShapeInfo, offsetPos, offsetRot); | ||
379 | DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,addChild,indx={1},cShape={2},offPos={3},offRot={4}", | ||
380 | LinksetRoot.LocalID, cPrim.LinksetChildIndex, childShape, offsetPos, offsetRot); | ||
381 | |||
382 | // Since we are borrowing the shape of the child, disable the origional child body | ||
383 | if (!IsRoot(cPrim)) | ||
384 | { | ||
385 | m_physicsScene.PE.AddToCollisionFlags(cPrim.PhysBody, CollisionFlags.CF_NO_CONTACT_RESPONSE); | ||
386 | m_physicsScene.PE.ForceActivationState(cPrim.PhysBody, ActivationState.DISABLE_SIMULATION); | ||
387 | // We don't want collisions from the old linkset children. | ||
388 | m_physicsScene.PE.RemoveFromCollisionFlags(cPrim.PhysBody, CollisionFlags.BS_SUBSCRIBE_COLLISION_EVENTS); | ||
389 | cPrim.PhysBody.collisionType = CollisionType.LinksetChild; | ||
494 | } | 390 | } |
391 | |||
495 | return false; // 'false' says to move onto the next child in the list | 392 | return false; // 'false' says to move onto the next child in the list |
496 | }); | 393 | }); |
497 | 394 | ||
395 | // Replace the root shape with the built compound shape. | ||
396 | // Object removed and added to world to get collision cache rebuilt for new shape. | ||
397 | LinksetRoot.PhysShape.Dereference(m_physicsScene); | ||
398 | LinksetRoot.PhysShape = linksetShape; | ||
399 | m_physicsScene.PE.RemoveObjectFromWorld(m_physicsScene.World, LinksetRoot.PhysBody); | ||
400 | m_physicsScene.PE.SetCollisionShape(m_physicsScene.World, LinksetRoot.PhysBody, linksetShape.physShapeInfo); | ||
401 | m_physicsScene.PE.AddObjectToWorld(m_physicsScene.World, LinksetRoot.PhysBody); | ||
402 | DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,addBody,body={1},shape={2}", | ||
403 | LinksetRoot.LocalID, LinksetRoot.PhysBody, linksetShape); | ||
404 | |||
498 | // With all of the linkset packed into the root prim, it has the mass of everyone. | 405 | // With all of the linkset packed into the root prim, it has the mass of everyone. |
499 | LinksetMass = ComputeLinksetMass(); | 406 | LinksetMass = ComputeLinksetMass(); |
500 | LinksetRoot.UpdatePhysicalMassProperties(LinksetMass, true); | 407 | LinksetRoot.UpdatePhysicalMassProperties(LinksetMass, true); |
501 | 408 | ||
502 | // Enable the physical position updator to return the position and rotation of the root shape | 409 | if (UseBulletSimRootOffsetHack) |
503 | PhysicsScene.PE.AddToCollisionFlags(LinksetRoot.PhysBody, CollisionFlags.BS_RETURN_ROOT_COMPOUND_SHAPE); | 410 | { |
411 | // Enable the physical position updator to return the position and rotation of the root shape. | ||
412 | // This enables a feature in the C++ code to return the world coordinates of the first shape in the | ||
413 | // compound shape. This aleviates the need to offset the returned physical position by the | ||
414 | // center-of-mass offset. | ||
415 | // TODO: either debug this feature or remove it. | ||
416 | m_physicsScene.PE.AddToCollisionFlags(LinksetRoot.PhysBody, CollisionFlags.BS_RETURN_ROOT_COMPOUND_SHAPE); | ||
417 | } | ||
504 | } | 418 | } |
505 | finally | 419 | finally |
506 | { | 420 | { |
@@ -508,7 +422,7 @@ public sealed class BSLinksetCompound : BSLinkset | |||
508 | } | 422 | } |
509 | 423 | ||
510 | // See that the Aabb surrounds the new shape | 424 | // See that the Aabb surrounds the new shape |
511 | PhysicsScene.PE.RecalculateCompoundShapeLocalAabb(LinksetRoot.PhysShape); | 425 | m_physicsScene.PE.RecalculateCompoundShapeLocalAabb(LinksetRoot.PhysShape.physShapeInfo); |
512 | } | 426 | } |
513 | } | 427 | } |
514 | } \ No newline at end of file | 428 | } \ No newline at end of file |