diff options
Diffstat (limited to '')
-rwxr-xr-x | OpenSim/Region/Physics/BulletSPlugin/BSLinkset.cs | 474 |
1 files changed, 112 insertions, 362 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSLinkset.cs b/OpenSim/Region/Physics/BulletSPlugin/BSLinkset.cs index 43b1262..0df4310 100755 --- a/OpenSim/Region/Physics/BulletSPlugin/BSLinkset.cs +++ b/OpenSim/Region/Physics/BulletSPlugin/BSLinkset.cs | |||
@@ -32,10 +32,39 @@ using OMV = OpenMetaverse; | |||
32 | 32 | ||
33 | namespace OpenSim.Region.Physics.BulletSPlugin | 33 | namespace OpenSim.Region.Physics.BulletSPlugin |
34 | { | 34 | { |
35 | public class BSLinkset | 35 | public abstract class BSLinkset |
36 | { | 36 | { |
37 | // private static string LogHeader = "[BULLETSIM LINKSET]"; | 37 | // private static string LogHeader = "[BULLETSIM LINKSET]"; |
38 | 38 | ||
39 | public enum LinksetImplementation | ||
40 | { | ||
41 | Constraint = 0, // linkset tied together with constraints | ||
42 | Compound = 1, // linkset tied together as a compound object | ||
43 | Manual = 2 // linkset tied together manually (code moves all the pieces) | ||
44 | } | ||
45 | // Create the correct type of linkset for this child | ||
46 | public static BSLinkset Factory(BSScene physScene, BSPhysObject parent) | ||
47 | { | ||
48 | BSLinkset ret = null; | ||
49 | |||
50 | switch ((int)physScene.Params.linksetImplementation) | ||
51 | { | ||
52 | case (int)LinksetImplementation.Constraint: | ||
53 | ret = new BSLinksetConstraints(physScene, parent); | ||
54 | break; | ||
55 | case (int)LinksetImplementation.Compound: | ||
56 | ret = new BSLinksetCompound(physScene, parent); | ||
57 | break; | ||
58 | case (int)LinksetImplementation.Manual: | ||
59 | // ret = new BSLinksetManual(physScene, parent); | ||
60 | break; | ||
61 | default: | ||
62 | ret = new BSLinksetCompound(physScene, parent); | ||
63 | break; | ||
64 | } | ||
65 | return ret; | ||
66 | } | ||
67 | |||
39 | public BSPhysObject LinksetRoot { get; protected set; } | 68 | public BSPhysObject LinksetRoot { get; protected set; } |
40 | 69 | ||
41 | public BSScene PhysicsScene { get; private set; } | 70 | public BSScene PhysicsScene { get; private set; } |
@@ -44,33 +73,39 @@ public class BSLinkset | |||
44 | public int LinksetID { get; private set; } | 73 | public int LinksetID { get; private set; } |
45 | 74 | ||
46 | // The children under the root in this linkset. | 75 | // The children under the root in this linkset. |
47 | // There are two lists of children: the current children at runtime | 76 | protected HashSet<BSPhysObject> m_children; |
48 | // and the children at taint-time. For instance, if you delink a | ||
49 | // child from the linkset, the child is removed from m_children | ||
50 | // but the constraint won't be removed until taint time. | ||
51 | // Two lists lets this track the 'current' children and | ||
52 | // the physical 'taint' children separately. | ||
53 | // After taint processing and before the simulation step, these | ||
54 | // two lists must be the same. | ||
55 | private HashSet<BSPhysObject> m_children; | ||
56 | private HashSet<BSPhysObject> m_taintChildren; | ||
57 | 77 | ||
58 | // We lock the diddling of linkset classes to prevent any badness. | 78 | // We lock the diddling of linkset classes to prevent any badness. |
59 | // This locks the modification of the instances of this class. Changes | 79 | // This locks the modification of the instances of this class. Changes |
60 | // to the physical representation is done via the tainting mechenism. | 80 | // to the physical representation is done via the tainting mechenism. |
61 | private object m_linksetActivityLock = new Object(); | 81 | protected object m_linksetActivityLock = new Object(); |
62 | 82 | ||
83 | // Some linksets have a preferred physical shape. | ||
84 | // Returns SHAPE_UNKNOWN if there is no preference. Causes the correct shape to be selected. | ||
85 | public virtual BSPhysicsShapeType PreferredPhysicalShape(BSPhysObject requestor) | ||
86 | { | ||
87 | return BSPhysicsShapeType.SHAPE_UNKNOWN; | ||
88 | } | ||
89 | |||
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 | |||
63 | // We keep the prim's mass in the linkset structure since it could be dependent on other prims | 97 | // We keep the prim's mass in the linkset structure since it could be dependent on other prims |
64 | private float m_mass; | 98 | protected float m_mass; |
65 | public float LinksetMass | 99 | public float LinksetMass |
66 | { | 100 | { |
67 | get | 101 | get |
68 | { | 102 | { |
69 | m_mass = ComputeLinksetMass(); | ||
70 | return m_mass; | 103 | return m_mass; |
71 | } | 104 | } |
72 | } | 105 | } |
73 | 106 | ||
107 | public virtual bool LinksetIsColliding { get { return false; } } | ||
108 | |||
74 | public OMV.Vector3 CenterOfMass | 109 | public OMV.Vector3 CenterOfMass |
75 | { | 110 | { |
76 | get { return ComputeLinksetCenterOfMass(); } | 111 | get { return ComputeLinksetCenterOfMass(); } |
@@ -81,7 +116,7 @@ public class BSLinkset | |||
81 | get { return ComputeLinksetGeometricCenter(); } | 116 | get { return ComputeLinksetGeometricCenter(); } |
82 | } | 117 | } |
83 | 118 | ||
84 | public BSLinkset(BSScene scene, BSPhysObject parent) | 119 | protected void Initialize(BSScene scene, BSPhysObject parent) |
85 | { | 120 | { |
86 | // A simple linkset of one (no children) | 121 | // A simple linkset of one (no children) |
87 | LinksetID = m_nextLinksetID++; | 122 | LinksetID = m_nextLinksetID++; |
@@ -91,8 +126,7 @@ public class BSLinkset | |||
91 | PhysicsScene = scene; | 126 | PhysicsScene = scene; |
92 | LinksetRoot = parent; | 127 | LinksetRoot = parent; |
93 | m_children = new HashSet<BSPhysObject>(); | 128 | m_children = new HashSet<BSPhysObject>(); |
94 | m_taintChildren = new HashSet<BSPhysObject>(); | 129 | m_mass = parent.RawMass; |
95 | m_mass = parent.MassRaw; | ||
96 | } | 130 | } |
97 | 131 | ||
98 | // Link to a linkset where the child knows the parent. | 132 | // Link to a linkset where the child knows the parent. |
@@ -106,6 +140,7 @@ public class BSLinkset | |||
106 | // Don't add the root to its own linkset | 140 | // Don't add the root to its own linkset |
107 | if (!IsRoot(child)) | 141 | if (!IsRoot(child)) |
108 | AddChildToLinkset(child); | 142 | AddChildToLinkset(child); |
143 | m_mass = ComputeLinksetMass(); | ||
109 | } | 144 | } |
110 | return this; | 145 | return this; |
111 | } | 146 | } |
@@ -123,12 +158,12 @@ public class BSLinkset | |||
123 | // Cannot remove the root from a linkset. | 158 | // Cannot remove the root from a linkset. |
124 | return this; | 159 | return this; |
125 | } | 160 | } |
126 | |||
127 | RemoveChildFromLinkset(child); | 161 | RemoveChildFromLinkset(child); |
162 | m_mass = ComputeLinksetMass(); | ||
128 | } | 163 | } |
129 | 164 | ||
130 | // The child is down to a linkset of just itself | 165 | // The child is down to a linkset of just itself |
131 | return new BSLinkset(PhysicsScene, child); | 166 | return BSLinkset.Factory(PhysicsScene, child); |
132 | } | 167 | } |
133 | 168 | ||
134 | // Return 'true' if the passed object is the root object of this linkset | 169 | // Return 'true' if the passed object is the root object of this linkset |
@@ -148,6 +183,8 @@ public class BSLinkset | |||
148 | bool ret = false; | 183 | bool ret = false; |
149 | lock (m_linksetActivityLock) | 184 | lock (m_linksetActivityLock) |
150 | { | 185 | { |
186 | ret = m_children.Contains(child); | ||
187 | /* Safer version but the above should work | ||
151 | foreach (BSPhysObject bp in m_children) | 188 | foreach (BSPhysObject bp in m_children) |
152 | { | 189 | { |
153 | if (child.LocalID == bp.LocalID) | 190 | if (child.LocalID == bp.LocalID) |
@@ -156,159 +193,102 @@ public class BSLinkset | |||
156 | break; | 193 | break; |
157 | } | 194 | } |
158 | } | 195 | } |
196 | */ | ||
159 | } | 197 | } |
160 | return ret; | 198 | return ret; |
161 | } | 199 | } |
162 | 200 | ||
163 | // When physical properties are changed the linkset needs to recalculate | 201 | // Perform an action on each member of the linkset including root prim. |
164 | // its internal properties. | 202 | // Depends on the action on whether this should be done at taint time. |
165 | // May be called at runtime or taint-time (just pass the appropriate flag). | 203 | public delegate bool ForEachMemberAction(BSPhysObject obj); |
166 | public void Refresh(BSPhysObject requestor, bool inTaintTime) | 204 | public virtual bool ForEachMember(ForEachMemberAction action) |
167 | { | 205 | { |
168 | // If there are no children, not physical or not root, I am not the one that recomputes the constraints | 206 | bool ret = false; |
169 | // (For the moment, static linksets do create constraints so remove the test for physical.) | 207 | lock (m_linksetActivityLock) |
170 | if (!HasAnyChildren || /*!requestor.IsPhysical ||*/ !IsRoot(requestor)) | 208 | { |
171 | return; | 209 | action(LinksetRoot); |
172 | 210 | foreach (BSPhysObject po in m_children) | |
173 | BSScene.TaintCallback refreshOperation = delegate() | ||
174 | { | 211 | { |
175 | RecomputeLinksetConstraintVariables(); | 212 | if (action(po)) |
176 | DetailLog("{0},BSLinkset.Refresh,complete,rBody={1}", | 213 | break; |
177 | LinksetRoot.LocalID, LinksetRoot.BSBody.ptr.ToString("X")); | 214 | } |
178 | }; | 215 | } |
179 | if (inTaintTime) | 216 | return ret; |
180 | refreshOperation(); | ||
181 | else | ||
182 | PhysicsScene.TaintedObject("BSLinkSet.Refresh", refreshOperation); | ||
183 | } | 217 | } |
184 | 218 | ||
219 | // I am the root of a linkset and a new child is being added | ||
220 | // Called while LinkActivity is locked. | ||
221 | protected abstract void AddChildToLinkset(BSPhysObject child); | ||
222 | |||
223 | // 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. | ||
225 | protected abstract void RemoveChildFromLinkset(BSPhysObject child); | ||
226 | |||
227 | // When physical properties are changed the linkset needs to recalculate | ||
228 | // its internal properties. | ||
229 | // May be called at runtime or taint-time. | ||
230 | public abstract void Refresh(BSPhysObject requestor); | ||
231 | |||
185 | // The object is going dynamic (physical). Do any setup necessary | 232 | // The object is going dynamic (physical). Do any setup necessary |
186 | // for a dynamic linkset. | 233 | // for a dynamic linkset. |
187 | // Only the state of the passed object can be modified. The rest of the linkset | 234 | // Only the state of the passed object can be modified. The rest of the linkset |
188 | // has not yet been fully constructed. | 235 | // has not yet been fully constructed. |
189 | // Return 'true' if any properties updated on the passed object. | 236 | // Return 'true' if any properties updated on the passed object. |
190 | // Called at taint-time! | 237 | // Called at taint-time! |
191 | public bool MakeDynamic(BSPhysObject child) | 238 | public abstract bool MakeDynamic(BSPhysObject child); |
192 | { | ||
193 | // What is done for each object in BSPrim is what we want. | ||
194 | return false; | ||
195 | } | ||
196 | 239 | ||
197 | // The object is going static (non-physical). Do any setup necessary | 240 | // The object is going static (non-physical). Do any setup necessary |
198 | // for a static linkset. | 241 | // for a static linkset. |
199 | // Return 'true' if any properties updated on the passed object. | 242 | // Return 'true' if any properties updated on the passed object. |
200 | // Called at taint-time! | 243 | // Called at taint-time! |
201 | public bool MakeStatic(BSPhysObject child) | 244 | public abstract bool MakeStatic(BSPhysObject child); |
202 | { | ||
203 | // What is done for each object in BSPrim is what we want. | ||
204 | return false; | ||
205 | } | ||
206 | 245 | ||
207 | // If the software is handling the movement of all the objects in a linkset | 246 | // Called when a parameter update comes from the physics engine for any object |
208 | // (like if one doesn't use constraints for static linksets), this is called | 247 | // of the linkset is received. |
209 | // when an update for the root of the linkset is received. | ||
210 | // Called at taint-time!! | 248 | // Called at taint-time!! |
211 | public void UpdateProperties(BSPhysObject physObject) | 249 | public abstract void UpdateProperties(BSPhysObject physObject); |
212 | { | ||
213 | // The root local properties have been updated. Apply to the children if appropriate. | ||
214 | if (IsRoot(physObject) && HasAnyChildren) | ||
215 | { | ||
216 | if (!physObject.IsPhysical) | ||
217 | { | ||
218 | // TODO: implement software linkset update for static object linksets | ||
219 | } | ||
220 | } | ||
221 | } | ||
222 | 250 | ||
223 | // Routine used when rebuilding the body of the root of the linkset | 251 | // Routine used when rebuilding the body of the root of the linkset |
224 | // Destroy all the constraints have have been made to root. | 252 | // Destroy all the constraints have have been made to root. |
225 | // This is called when the root body is changing. | 253 | // This is called when the root body is changing. |
226 | // Returns 'true' of something eas actually removed and would need restoring | 254 | // Returns 'true' of something was actually removed and would need restoring |
227 | // Called at taint-time!! | 255 | // Called at taint-time!! |
228 | public bool RemoveBodyDependencies(BSPrim child) | 256 | public abstract bool RemoveBodyDependencies(BSPrim child); |
229 | { | ||
230 | bool ret = false; | ||
231 | |||
232 | lock (m_linksetActivityLock) | ||
233 | { | ||
234 | if (IsRoot(child)) | ||
235 | { | ||
236 | // If the one with the dependency is root, must undo all children | ||
237 | DetailLog("{0},BSLinkset.RemoveBodyDependencies,removeChildrenForRoot,rID={1},rBody={2}", | ||
238 | child.LocalID, LinksetRoot.LocalID, LinksetRoot.BSBody.ptr.ToString("X")); | ||
239 | |||
240 | ret = PhysicallyUnlinkAllChildrenFromRoot(LinksetRoot); | ||
241 | } | ||
242 | else | ||
243 | { | ||
244 | DetailLog("{0},BSLinkset.RemoveBodyDependencies,removeSingleChild,rID={1},rBody={2},cID={3},cBody={4}", | ||
245 | child.LocalID, | ||
246 | LinksetRoot.LocalID, LinksetRoot.BSBody.ptr.ToString("X"), | ||
247 | child.LocalID, child.BSBody.ptr.ToString("X")); | ||
248 | // ret = PhysicallyUnlinkAChildFromRoot(LinksetRoot, child); | ||
249 | // Despite the function name, this removes any link to the specified object. | ||
250 | ret = PhysicallyUnlinkAllChildrenFromRoot(child); | ||
251 | } | ||
252 | } | ||
253 | return ret; | ||
254 | } | ||
255 | 257 | ||
256 | // Companion to RemoveBodyDependencies(). If RemoveBodyDependencies() returns 'true', | 258 | // Companion to RemoveBodyDependencies(). If RemoveBodyDependencies() returns 'true', |
257 | // this routine will restore the removed constraints. | 259 | // this routine will restore the removed constraints. |
258 | // Called at taint-time!! | 260 | // Called at taint-time!! |
259 | public void RestoreBodyDependencies(BSPrim child) | 261 | public abstract void RestoreBodyDependencies(BSPrim child); |
260 | { | ||
261 | lock (m_linksetActivityLock) | ||
262 | { | ||
263 | if (IsRoot(child)) | ||
264 | { | ||
265 | DetailLog("{0},BSLinkset.RestoreBodyDependencies,restoreChildrenForRoot,rID={1},numChild={2}", | ||
266 | child.LocalID, LinksetRoot.LocalID, m_taintChildren.Count); | ||
267 | foreach (BSPhysObject bpo in m_taintChildren) | ||
268 | { | ||
269 | PhysicallyLinkAChildToRoot(LinksetRoot, bpo); | ||
270 | } | ||
271 | } | ||
272 | else | ||
273 | { | ||
274 | DetailLog("{0},BSLinkset.RestoreBodyDependencies,restoreSingleChild,rID={1},rBody={2},cID={3},cBody={4}", | ||
275 | LinksetRoot.LocalID, | ||
276 | LinksetRoot.LocalID, LinksetRoot.BSBody.ptr.ToString("X"), | ||
277 | child.LocalID, child.BSBody.ptr.ToString("X")); | ||
278 | PhysicallyLinkAChildToRoot(LinksetRoot, child); | ||
279 | } | ||
280 | } | ||
281 | } | ||
282 | 262 | ||
283 | // ================================================================ | 263 | // ================================================================ |
284 | // Below this point is internal magic | 264 | protected virtual float ComputeLinksetMass() |
285 | |||
286 | private float ComputeLinksetMass() | ||
287 | { | 265 | { |
288 | float mass; | 266 | float mass = LinksetRoot.RawMass; |
289 | lock (m_linksetActivityLock) | 267 | if (HasAnyChildren) |
290 | { | 268 | { |
291 | mass = LinksetRoot.MassRaw; | 269 | lock (m_linksetActivityLock) |
292 | foreach (BSPhysObject bp in m_taintChildren) | ||
293 | { | 270 | { |
294 | mass += bp.MassRaw; | 271 | foreach (BSPhysObject bp in m_children) |
272 | { | ||
273 | mass += bp.RawMass; | ||
274 | } | ||
295 | } | 275 | } |
296 | } | 276 | } |
297 | return mass; | 277 | return mass; |
298 | } | 278 | } |
299 | 279 | ||
300 | private OMV.Vector3 ComputeLinksetCenterOfMass() | 280 | protected virtual OMV.Vector3 ComputeLinksetCenterOfMass() |
301 | { | 281 | { |
302 | OMV.Vector3 com; | 282 | OMV.Vector3 com; |
303 | lock (m_linksetActivityLock) | 283 | lock (m_linksetActivityLock) |
304 | { | 284 | { |
305 | com = LinksetRoot.Position * LinksetRoot.MassRaw; | 285 | com = LinksetRoot.Position * LinksetRoot.RawMass; |
306 | float totalMass = LinksetRoot.MassRaw; | 286 | float totalMass = LinksetRoot.RawMass; |
307 | 287 | ||
308 | foreach (BSPhysObject bp in m_taintChildren) | 288 | foreach (BSPhysObject bp in m_children) |
309 | { | 289 | { |
310 | com += bp.Position * bp.MassRaw; | 290 | com += bp.Position * bp.RawMass; |
311 | totalMass += bp.MassRaw; | 291 | totalMass += bp.RawMass; |
312 | } | 292 | } |
313 | if (totalMass != 0f) | 293 | if (totalMass != 0f) |
314 | com /= totalMass; | 294 | com /= totalMass; |
@@ -317,255 +297,25 @@ public class BSLinkset | |||
317 | return com; | 297 | return com; |
318 | } | 298 | } |
319 | 299 | ||
320 | private OMV.Vector3 ComputeLinksetGeometricCenter() | 300 | protected virtual OMV.Vector3 ComputeLinksetGeometricCenter() |
321 | { | 301 | { |
322 | OMV.Vector3 com; | 302 | OMV.Vector3 com; |
323 | lock (m_linksetActivityLock) | 303 | lock (m_linksetActivityLock) |
324 | { | 304 | { |
325 | com = LinksetRoot.Position; | 305 | com = LinksetRoot.Position; |
326 | 306 | ||
327 | foreach (BSPhysObject bp in m_taintChildren) | 307 | foreach (BSPhysObject bp in m_children) |
328 | { | 308 | { |
329 | com += bp.Position * bp.MassRaw; | 309 | com += bp.Position * bp.RawMass; |
330 | } | 310 | } |
331 | com /= (m_taintChildren.Count + 1); | 311 | com /= (m_children.Count + 1); |
332 | } | 312 | } |
333 | 313 | ||
334 | return com; | 314 | return com; |
335 | } | 315 | } |
336 | 316 | ||
337 | // I am the root of a linkset and a new child is being added | ||
338 | // Called while LinkActivity is locked. | ||
339 | private void AddChildToLinkset(BSPhysObject child) | ||
340 | { | ||
341 | if (!HasChild(child)) | ||
342 | { | ||
343 | m_children.Add(child); | ||
344 | |||
345 | BSPhysObject rootx = LinksetRoot; // capture the root as of now | ||
346 | BSPhysObject childx = child; | ||
347 | |||
348 | DetailLog("{0},AddChildToLinkset,call,child={1}", LinksetRoot.LocalID, child.LocalID); | ||
349 | |||
350 | PhysicsScene.TaintedObject("AddChildToLinkset", delegate() | ||
351 | { | ||
352 | DetailLog("{0},AddChildToLinkset,taint,rID={1},rBody={2},cID={3},cBody={4}", | ||
353 | rootx.LocalID, | ||
354 | rootx.LocalID, rootx.BSBody.ptr.ToString("X"), | ||
355 | childx.LocalID, childx.BSBody.ptr.ToString("X")); | ||
356 | // Since this is taint-time, the body and shape could have changed for the child | ||
357 | rootx.ForcePosition = rootx.Position; // DEBUG | ||
358 | childx.ForcePosition = childx.Position; // DEBUG | ||
359 | PhysicallyLinkAChildToRoot(rootx, childx); | ||
360 | m_taintChildren.Add(child); | ||
361 | }); | ||
362 | } | ||
363 | return; | ||
364 | } | ||
365 | |||
366 | // Forcefully removing a child from a linkset. | ||
367 | // This is not being called by the child so we have to make sure the child doesn't think | ||
368 | // it's still connected to the linkset. | ||
369 | // Normal OpenSimulator operation will never do this because other SceneObjectPart information | ||
370 | // also has to be updated (like pointer to prim's parent). | ||
371 | private void RemoveChildFromOtherLinkset(BSPhysObject pchild) | ||
372 | { | ||
373 | pchild.Linkset = new BSLinkset(PhysicsScene, pchild); | ||
374 | RemoveChildFromLinkset(pchild); | ||
375 | } | ||
376 | |||
377 | // I am the root of a linkset and one of my children is being removed. | ||
378 | // Safe to call even if the child is not really in my linkset. | ||
379 | private void RemoveChildFromLinkset(BSPhysObject child) | ||
380 | { | ||
381 | if (m_children.Remove(child)) | ||
382 | { | ||
383 | BSPhysObject rootx = LinksetRoot; // capture the root and body as of now | ||
384 | BSPhysObject childx = child; | ||
385 | |||
386 | DetailLog("{0},RemoveChildFromLinkset,call,rID={1},rBody={2},cID={3},cBody={4}", | ||
387 | childx.LocalID, | ||
388 | rootx.LocalID, rootx.BSBody.ptr.ToString("X"), | ||
389 | childx.LocalID, childx.BSBody.ptr.ToString("X")); | ||
390 | |||
391 | PhysicsScene.TaintedObject("RemoveChildFromLinkset", delegate() | ||
392 | { | ||
393 | m_taintChildren.Remove(child); | ||
394 | PhysicallyUnlinkAChildFromRoot(rootx, childx); | ||
395 | RecomputeLinksetConstraintVariables(); | ||
396 | }); | ||
397 | |||
398 | } | ||
399 | else | ||
400 | { | ||
401 | // This will happen if we remove the root of the linkset first. Non-fatal occurance. | ||
402 | // PhysicsScene.Logger.ErrorFormat("{0}: Asked to remove child from linkset that was not in linkset", LogHeader); | ||
403 | } | ||
404 | return; | ||
405 | } | ||
406 | |||
407 | // Create a constraint between me (root of linkset) and the passed prim (the child). | ||
408 | // Called at taint time! | ||
409 | private void PhysicallyLinkAChildToRoot(BSPhysObject rootPrim, BSPhysObject childPrim) | ||
410 | { | ||
411 | // Zero motion for children so they don't interpolate | ||
412 | childPrim.ZeroMotion(); | ||
413 | |||
414 | // Relative position normalized to the root prim | ||
415 | // Essentually a vector pointing from center of rootPrim to center of childPrim | ||
416 | OMV.Vector3 childRelativePosition = childPrim.Position - rootPrim.Position; | ||
417 | |||
418 | // real world coordinate of midpoint between the two objects | ||
419 | OMV.Vector3 midPoint = rootPrim.Position + (childRelativePosition / 2); | ||
420 | |||
421 | DetailLog("{0},BSLinkset.PhysicallyLinkAChildToRoot,taint,root={1},rBody={2},child={3},cBody={4},rLoc={5},cLoc={6},midLoc={7}", | ||
422 | rootPrim.LocalID, | ||
423 | rootPrim.LocalID, rootPrim.BSBody.ptr.ToString("X"), | ||
424 | childPrim.LocalID, childPrim.BSBody.ptr.ToString("X"), | ||
425 | rootPrim.Position, childPrim.Position, midPoint); | ||
426 | |||
427 | // create a constraint that allows no freedom of movement between the two objects | ||
428 | // http://bulletphysics.org/Bullet/phpBB3/viewtopic.php?t=4818 | ||
429 | |||
430 | BS6DofConstraint constrain = new BS6DofConstraint( | ||
431 | PhysicsScene.World, rootPrim.BSBody, childPrim.BSBody, midPoint, true, true ); | ||
432 | |||
433 | /* NOTE: below is an attempt to build constraint with full frame computation, etc. | ||
434 | * Using the midpoint is easier since it lets the Bullet code manipulate the transforms | ||
435 | * of the objects. | ||
436 | * Code left as a warning to future programmers. | ||
437 | // ================================================================================== | ||
438 | // relative position normalized to the root prim | ||
439 | OMV.Quaternion invThisOrientation = OMV.Quaternion.Inverse(rootPrim.Orientation); | ||
440 | OMV.Vector3 childRelativePosition = (childPrim.Position - rootPrim.Position) * invThisOrientation; | ||
441 | |||
442 | // relative rotation of the child to the parent | ||
443 | OMV.Quaternion childRelativeRotation = invThisOrientation * childPrim.Orientation; | ||
444 | OMV.Quaternion inverseChildRelativeRotation = OMV.Quaternion.Inverse(childRelativeRotation); | ||
445 | |||
446 | // create a constraint that allows no freedom of movement between the two objects | ||
447 | // http://bulletphysics.org/Bullet/phpBB3/viewtopic.php?t=4818 | ||
448 | DetailLog("{0},BSLinkset.PhysicallyLinkAChildToRoot,taint,root={1},child={2}", rootPrim.LocalID, rootPrim.LocalID, childPrim.LocalID); | ||
449 | BS6DofConstraint constrain = new BS6DofConstraint( | ||
450 | PhysicsScene.World, rootPrim.Body, childPrim.Body, | ||
451 | OMV.Vector3.Zero, | ||
452 | OMV.Quaternion.Inverse(rootPrim.Orientation), | ||
453 | OMV.Vector3.Zero, | ||
454 | OMV.Quaternion.Inverse(childPrim.Orientation), | ||
455 | // A point half way between the parent and child | ||
456 | // childRelativePosition/2, | ||
457 | // childRelativeRotation, | ||
458 | // childRelativePosition/2, | ||
459 | // inverseChildRelativeRotation, | ||
460 | true, | ||
461 | true | ||
462 | ); | ||
463 | // ================================================================================== | ||
464 | */ | ||
465 | |||
466 | PhysicsScene.Constraints.AddConstraint(constrain); | ||
467 | |||
468 | // zero linear and angular limits makes the objects unable to move in relation to each other | ||
469 | constrain.SetLinearLimits(OMV.Vector3.Zero, OMV.Vector3.Zero); | ||
470 | constrain.SetAngularLimits(OMV.Vector3.Zero, OMV.Vector3.Zero); | ||
471 | |||
472 | // tweek the constraint to increase stability | ||
473 | constrain.UseFrameOffset(PhysicsScene.BoolNumeric(PhysicsScene.Params.linkConstraintUseFrameOffset)); | ||
474 | constrain.TranslationalLimitMotor(PhysicsScene.BoolNumeric(PhysicsScene.Params.linkConstraintEnableTransMotor), | ||
475 | PhysicsScene.Params.linkConstraintTransMotorMaxVel, | ||
476 | PhysicsScene.Params.linkConstraintTransMotorMaxForce); | ||
477 | constrain.SetCFMAndERP(PhysicsScene.Params.linkConstraintCFM, PhysicsScene.Params.linkConstraintERP); | ||
478 | if (PhysicsScene.Params.linkConstraintSolverIterations != 0f) | ||
479 | { | ||
480 | constrain.SetSolverIterations(PhysicsScene.Params.linkConstraintSolverIterations); | ||
481 | } | ||
482 | } | ||
483 | |||
484 | // Remove linkage between myself and a particular child | ||
485 | // The root and child bodies are passed in because we need to remove the constraint between | ||
486 | // the bodies that were at unlink time. | ||
487 | // Called at taint time! | ||
488 | private bool PhysicallyUnlinkAChildFromRoot(BSPhysObject rootPrim, BSPhysObject childPrim) | ||
489 | { | ||
490 | bool ret = false; | ||
491 | DetailLog("{0},BSLinkset.PhysicallyUnlinkAChildFromRoot,taint,root={1},rBody={2},child={3},cBody={4}", | ||
492 | rootPrim.LocalID, | ||
493 | rootPrim.LocalID, rootPrim.BSBody.ptr.ToString("X"), | ||
494 | childPrim.LocalID, childPrim.BSBody.ptr.ToString("X")); | ||
495 | |||
496 | // Find the constraint for this link and get rid of it from the overall collection and from my list | ||
497 | if (PhysicsScene.Constraints.RemoveAndDestroyConstraint(rootPrim.BSBody, childPrim.BSBody)) | ||
498 | { | ||
499 | // Make the child refresh its location | ||
500 | BulletSimAPI.PushUpdate2(childPrim.BSBody.ptr); | ||
501 | ret = true; | ||
502 | } | ||
503 | |||
504 | return ret; | ||
505 | } | ||
506 | |||
507 | // Remove linkage between myself and any possible children I might have. | ||
508 | // Called at taint time! | ||
509 | private bool PhysicallyUnlinkAllChildrenFromRoot(BSPhysObject rootPrim) | ||
510 | { | ||
511 | DetailLog("{0},BSLinkset.PhysicallyUnlinkAllChildren,taint", rootPrim.LocalID); | ||
512 | bool ret = false; | ||
513 | |||
514 | if (PhysicsScene.Constraints.RemoveAndDestroyConstraint(rootPrim.BSBody)) | ||
515 | { | ||
516 | ret = true; | ||
517 | } | ||
518 | return ret; | ||
519 | } | ||
520 | |||
521 | // Call each of the constraints that make up this linkset and recompute the | ||
522 | // various transforms and variables. Used when objects are added or removed | ||
523 | // from a linkset to make sure the constraints know about the new mass and | ||
524 | // geometry. | ||
525 | // Must only be called at taint time!! | ||
526 | private void RecomputeLinksetConstraintVariables() | ||
527 | { | ||
528 | float linksetMass = LinksetMass; | ||
529 | foreach (BSPhysObject child in m_taintChildren) | ||
530 | { | ||
531 | BSConstraint constrain; | ||
532 | if (PhysicsScene.Constraints.TryGetConstraint(LinksetRoot.BSBody, child.BSBody, out constrain)) | ||
533 | { | ||
534 | // DetailLog("{0},BSLinkset.RecomputeLinksetConstraintVariables,taint,child={1},mass={2},A={3},B={4}", | ||
535 | // LinksetRoot.LocalID, child.LocalID, linksetMass, constrain.Body1.ID, constrain.Body2.ID); | ||
536 | constrain.RecomputeConstraintVariables(linksetMass); | ||
537 | } | ||
538 | else | ||
539 | { | ||
540 | // Non-fatal error that happens when children are being added to the linkset but | ||
541 | // their constraints have not been created yet. | ||
542 | break; | ||
543 | } | ||
544 | } | ||
545 | |||
546 | // If the whole linkset is not here, doesn't make sense to recompute linkset wide values | ||
547 | if (m_children.Count == m_taintChildren.Count) | ||
548 | { | ||
549 | // If this is a multiple object linkset, set everybody's center of mass to the set's center of mass | ||
550 | OMV.Vector3 centerOfMass = ComputeLinksetCenterOfMass(); | ||
551 | BulletSimAPI.SetCenterOfMassByPosRot2(LinksetRoot.BSBody.ptr, | ||
552 | centerOfMass, OMV.Quaternion.Identity); | ||
553 | DetailLog("{0},BSLinkset.RecomputeLinksetConstraintVariables,setCenterOfMass,COM={1},rBody={2}", | ||
554 | LinksetRoot.LocalID, centerOfMass, LinksetRoot.BSBody.ptr.ToString("X")); | ||
555 | foreach (BSPhysObject child in m_taintChildren) | ||
556 | { | ||
557 | BulletSimAPI.SetCenterOfMassByPosRot2(child.BSBody.ptr, | ||
558 | centerOfMass, OMV.Quaternion.Identity); | ||
559 | } | ||
560 | |||
561 | // BulletSimAPI.DumpAllInfo2(PhysicsScene.World.ptr); // DEBUG DEBUG DEBUG | ||
562 | } | ||
563 | return; | ||
564 | } | ||
565 | |||
566 | |||
567 | // Invoke the detailed logger and output something if it's enabled. | 317 | // Invoke the detailed logger and output something if it's enabled. |
568 | private void DetailLog(string msg, params Object[] args) | 318 | protected void DetailLog(string msg, params Object[] args) |
569 | { | 319 | { |
570 | if (PhysicsScene.PhysicsLogging.Enabled) | 320 | if (PhysicsScene.PhysicsLogging.Enabled) |
571 | PhysicsScene.DetailLog(msg, args); | 321 | PhysicsScene.DetailLog(msg, args); |