aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Physics/BulletSPlugin/BSLinksetConstraints.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSLinksetConstraints.cs')
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSLinksetConstraints.cs396
1 files changed, 396 insertions, 0 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSLinksetConstraints.cs b/OpenSim/Region/Physics/BulletSPlugin/BSLinksetConstraints.cs
new file mode 100755
index 0000000..ee53d92
--- /dev/null
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSLinksetConstraints.cs
@@ -0,0 +1,396 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyrightD
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27using System;
28using System.Collections.Generic;
29using System.Text;
30
31using OMV = OpenMetaverse;
32
33namespace OpenSim.Region.Physics.BulletSPlugin
34{
35public class BSLinksetConstraints : BSLinkset
36{
37 // private static string LogHeader = "[BULLETSIM LINKSET CONSTRAINTS]";
38
39 public BSLinksetConstraints(BSScene scene, BSPhysObject parent)
40 {
41 base.Initialize(scene, parent);
42 }
43
44 // When physical properties are changed the linkset needs to recalculate
45 // its internal properties.
46 // May be called at runtime or taint-time (just pass the appropriate flag).
47 public override void Refresh(BSPhysObject requestor, bool inTaintTime)
48 {
49 // If there are no children, not physical or not root, I am not the one that recomputes the constraints
50 // (For the moment, static linksets do create constraints so remove the test for physical.)
51 if (!HasAnyChildren || /*!requestor.IsPhysical ||*/ !IsRoot(requestor))
52 return;
53
54 BSScene.TaintCallback refreshOperation = delegate()
55 {
56 RecomputeLinksetConstraintVariables();
57 DetailLog("{0},BSLinkset.Refresh,complete,rBody={1}",
58 LinksetRoot.LocalID, LinksetRoot.BSBody.ptr.ToString("X"));
59 };
60 if (inTaintTime)
61 refreshOperation();
62 else
63 PhysicsScene.TaintedObject("BSLinkSet.Refresh", refreshOperation);
64 }
65
66 // The object is going dynamic (physical). Do any setup necessary
67 // for a dynamic linkset.
68 // Only the state of the passed object can be modified. The rest of the linkset
69 // has not yet been fully constructed.
70 // Return 'true' if any properties updated on the passed object.
71 // Called at taint-time!
72 public override bool MakeDynamic(BSPhysObject child)
73 {
74 // What is done for each object in BSPrim is what we want.
75 return false;
76 }
77
78 // The object is going static (non-physical). Do any setup necessary
79 // for a static linkset.
80 // Return 'true' if any properties updated on the passed object.
81 // Called at taint-time!
82 public override bool MakeStatic(BSPhysObject child)
83 {
84 // What is done for each object in BSPrim is what we want.
85 return false;
86 }
87
88 // If the software is handling the movement of all the objects in a linkset
89 // (like if one doesn't use constraints for static linksets), this is called
90 // when an update for the root of the linkset is received.
91 // Called at taint-time!!
92 public override void UpdateProperties(BSPhysObject physObject)
93 {
94 // The root local properties have been updated. Apply to the children if appropriate.
95 if (IsRoot(physObject) && HasAnyChildren)
96 {
97 if (!physObject.IsPhysical)
98 {
99 // TODO: implement software linkset update for static object linksets
100 }
101 }
102 }
103
104 // Routine used when rebuilding the body of the root of the linkset
105 // Destroy all the constraints have have been made to root.
106 // This is called when the root body is changing.
107 // Returns 'true' of something eas actually removed and would need restoring
108 // Called at taint-time!!
109 public override bool RemoveBodyDependencies(BSPrim child)
110 {
111 bool ret = false;
112
113 lock (m_linksetActivityLock)
114 {
115 if (IsRoot(child))
116 {
117 // If the one with the dependency is root, must undo all children
118 DetailLog("{0},BSLinkset.RemoveBodyDependencies,removeChildrenForRoot,rID={1},rBody={2}",
119 child.LocalID, LinksetRoot.LocalID, LinksetRoot.BSBody.ptr.ToString("X"));
120
121 ret = PhysicallyUnlinkAllChildrenFromRoot(LinksetRoot);
122 }
123 else
124 {
125 DetailLog("{0},BSLinkset.RemoveBodyDependencies,removeSingleChild,rID={1},rBody={2},cID={3},cBody={4}",
126 child.LocalID,
127 LinksetRoot.LocalID, LinksetRoot.BSBody.ptr.ToString("X"),
128 child.LocalID, child.BSBody.ptr.ToString("X"));
129 // ret = PhysicallyUnlinkAChildFromRoot(LinksetRoot, child);
130 // Despite the function name, this removes any link to the specified object.
131 ret = PhysicallyUnlinkAllChildrenFromRoot(child);
132 }
133 }
134 return ret;
135 }
136
137 // Companion to RemoveBodyDependencies(). If RemoveBodyDependencies() returns 'true',
138 // this routine will restore the removed constraints.
139 // Called at taint-time!!
140 public override void RestoreBodyDependencies(BSPrim child)
141 {
142 lock (m_linksetActivityLock)
143 {
144 if (IsRoot(child))
145 {
146 DetailLog("{0},BSLinkset.RestoreBodyDependencies,restoreChildrenForRoot,rID={1},numChild={2}",
147 child.LocalID, LinksetRoot.LocalID, m_taintChildren.Count);
148 foreach (BSPhysObject bpo in m_taintChildren)
149 {
150 PhysicallyLinkAChildToRoot(LinksetRoot, bpo);
151 }
152 }
153 else
154 {
155 DetailLog("{0},BSLinkset.RestoreBodyDependencies,restoreSingleChild,rID={1},rBody={2},cID={3},cBody={4}",
156 LinksetRoot.LocalID,
157 LinksetRoot.LocalID, LinksetRoot.BSBody.ptr.ToString("X"),
158 child.LocalID, child.BSBody.ptr.ToString("X"));
159 PhysicallyLinkAChildToRoot(LinksetRoot, child);
160 }
161 }
162 }
163
164 // ================================================================
165 // Below this point is internal magic
166
167 // I am the root of a linkset and a new child is being added
168 // Called while LinkActivity is locked.
169 protected override void AddChildToLinkset(BSPhysObject child)
170 {
171 if (!HasChild(child))
172 {
173 m_children.Add(child);
174
175 BSPhysObject rootx = LinksetRoot; // capture the root as of now
176 BSPhysObject childx = child;
177
178 DetailLog("{0},AddChildToLinkset,call,child={1}", LinksetRoot.LocalID, child.LocalID);
179
180 PhysicsScene.TaintedObject("AddChildToLinkset", delegate()
181 {
182 DetailLog("{0},AddChildToLinkset,taint,rID={1},rBody={2},cID={3},cBody={4}",
183 rootx.LocalID,
184 rootx.LocalID, rootx.BSBody.ptr.ToString("X"),
185 childx.LocalID, childx.BSBody.ptr.ToString("X"));
186 // Since this is taint-time, the body and shape could have changed for the child
187 rootx.ForcePosition = rootx.Position; // DEBUG
188 childx.ForcePosition = childx.Position; // DEBUG
189 PhysicallyLinkAChildToRoot(rootx, childx);
190 m_taintChildren.Add(child);
191 });
192 }
193 return;
194 }
195
196 // Forcefully removing a child from a linkset.
197 // This is not being called by the child so we have to make sure the child doesn't think
198 // it's still connected to the linkset.
199 // Normal OpenSimulator operation will never do this because other SceneObjectPart information
200 // also has to be updated (like pointer to prim's parent).
201 protected override void RemoveChildFromOtherLinkset(BSPhysObject pchild)
202 {
203 pchild.Linkset = BSLinkset.Factory(PhysicsScene, pchild);
204 RemoveChildFromLinkset(pchild);
205 }
206
207 // I am the root of a linkset and one of my children is being removed.
208 // Safe to call even if the child is not really in my linkset.
209 protected override void RemoveChildFromLinkset(BSPhysObject child)
210 {
211 if (m_children.Remove(child))
212 {
213 BSPhysObject rootx = LinksetRoot; // capture the root and body as of now
214 BSPhysObject childx = child;
215
216 DetailLog("{0},RemoveChildFromLinkset,call,rID={1},rBody={2},cID={3},cBody={4}",
217 childx.LocalID,
218 rootx.LocalID, rootx.BSBody.ptr.ToString("X"),
219 childx.LocalID, childx.BSBody.ptr.ToString("X"));
220
221 PhysicsScene.TaintedObject("RemoveChildFromLinkset", delegate()
222 {
223 m_taintChildren.Remove(child);
224 PhysicallyUnlinkAChildFromRoot(rootx, childx);
225 RecomputeLinksetConstraintVariables();
226 });
227
228 }
229 else
230 {
231 // This will happen if we remove the root of the linkset first. Non-fatal occurance.
232 // PhysicsScene.Logger.ErrorFormat("{0}: Asked to remove child from linkset that was not in linkset", LogHeader);
233 }
234 return;
235 }
236
237 // Create a constraint between me (root of linkset) and the passed prim (the child).
238 // Called at taint time!
239 private void PhysicallyLinkAChildToRoot(BSPhysObject rootPrim, BSPhysObject childPrim)
240 {
241 // Zero motion for children so they don't interpolate
242 childPrim.ZeroMotion();
243
244 // Relative position normalized to the root prim
245 // Essentually a vector pointing from center of rootPrim to center of childPrim
246 OMV.Vector3 childRelativePosition = childPrim.Position - rootPrim.Position;
247
248 // real world coordinate of midpoint between the two objects
249 OMV.Vector3 midPoint = rootPrim.Position + (childRelativePosition / 2);
250
251 DetailLog("{0},BSLinkset.PhysicallyLinkAChildToRoot,taint,root={1},rBody={2},child={3},cBody={4},rLoc={5},cLoc={6},midLoc={7}",
252 rootPrim.LocalID,
253 rootPrim.LocalID, rootPrim.BSBody.ptr.ToString("X"),
254 childPrim.LocalID, childPrim.BSBody.ptr.ToString("X"),
255 rootPrim.Position, childPrim.Position, midPoint);
256
257 // create a constraint that allows no freedom of movement between the two objects
258 // http://bulletphysics.org/Bullet/phpBB3/viewtopic.php?t=4818
259
260 BS6DofConstraint constrain = new BS6DofConstraint(
261 PhysicsScene.World, rootPrim.BSBody, childPrim.BSBody, midPoint, true, true );
262
263 /* NOTE: below is an attempt to build constraint with full frame computation, etc.
264 * Using the midpoint is easier since it lets the Bullet code manipulate the transforms
265 * of the objects.
266 * Code left as a warning to future programmers.
267 // ==================================================================================
268 // relative position normalized to the root prim
269 OMV.Quaternion invThisOrientation = OMV.Quaternion.Inverse(rootPrim.Orientation);
270 OMV.Vector3 childRelativePosition = (childPrim.Position - rootPrim.Position) * invThisOrientation;
271
272 // relative rotation of the child to the parent
273 OMV.Quaternion childRelativeRotation = invThisOrientation * childPrim.Orientation;
274 OMV.Quaternion inverseChildRelativeRotation = OMV.Quaternion.Inverse(childRelativeRotation);
275
276 // create a constraint that allows no freedom of movement between the two objects
277 // http://bulletphysics.org/Bullet/phpBB3/viewtopic.php?t=4818
278 DetailLog("{0},BSLinkset.PhysicallyLinkAChildToRoot,taint,root={1},child={2}", rootPrim.LocalID, rootPrim.LocalID, childPrim.LocalID);
279 BS6DofConstraint constrain = new BS6DofConstraint(
280 PhysicsScene.World, rootPrim.Body, childPrim.Body,
281 OMV.Vector3.Zero,
282 OMV.Quaternion.Inverse(rootPrim.Orientation),
283 OMV.Vector3.Zero,
284 OMV.Quaternion.Inverse(childPrim.Orientation),
285 // A point half way between the parent and child
286 // childRelativePosition/2,
287 // childRelativeRotation,
288 // childRelativePosition/2,
289 // inverseChildRelativeRotation,
290 true,
291 true
292 );
293 // ==================================================================================
294 */
295
296 PhysicsScene.Constraints.AddConstraint(constrain);
297
298 // zero linear and angular limits makes the objects unable to move in relation to each other
299 constrain.SetLinearLimits(OMV.Vector3.Zero, OMV.Vector3.Zero);
300 constrain.SetAngularLimits(OMV.Vector3.Zero, OMV.Vector3.Zero);
301
302 // tweek the constraint to increase stability
303 constrain.UseFrameOffset(PhysicsScene.BoolNumeric(PhysicsScene.Params.linkConstraintUseFrameOffset));
304 constrain.TranslationalLimitMotor(PhysicsScene.BoolNumeric(PhysicsScene.Params.linkConstraintEnableTransMotor),
305 PhysicsScene.Params.linkConstraintTransMotorMaxVel,
306 PhysicsScene.Params.linkConstraintTransMotorMaxForce);
307 constrain.SetCFMAndERP(PhysicsScene.Params.linkConstraintCFM, PhysicsScene.Params.linkConstraintERP);
308 if (PhysicsScene.Params.linkConstraintSolverIterations != 0f)
309 {
310 constrain.SetSolverIterations(PhysicsScene.Params.linkConstraintSolverIterations);
311 }
312 }
313
314 // Remove linkage between myself and a particular child
315 // The root and child bodies are passed in because we need to remove the constraint between
316 // the bodies that were at unlink time.
317 // Called at taint time!
318 private bool PhysicallyUnlinkAChildFromRoot(BSPhysObject rootPrim, BSPhysObject childPrim)
319 {
320 bool ret = false;
321 DetailLog("{0},BSLinkset.PhysicallyUnlinkAChildFromRoot,taint,root={1},rBody={2},child={3},cBody={4}",
322 rootPrim.LocalID,
323 rootPrim.LocalID, rootPrim.BSBody.ptr.ToString("X"),
324 childPrim.LocalID, childPrim.BSBody.ptr.ToString("X"));
325
326 // Find the constraint for this link and get rid of it from the overall collection and from my list
327 if (PhysicsScene.Constraints.RemoveAndDestroyConstraint(rootPrim.BSBody, childPrim.BSBody))
328 {
329 // Make the child refresh its location
330 BulletSimAPI.PushUpdate2(childPrim.BSBody.ptr);
331 ret = true;
332 }
333
334 return ret;
335 }
336
337 // Remove linkage between myself and any possible children I might have.
338 // Called at taint time!
339 private bool PhysicallyUnlinkAllChildrenFromRoot(BSPhysObject rootPrim)
340 {
341 DetailLog("{0},BSLinkset.PhysicallyUnlinkAllChildren,taint", rootPrim.LocalID);
342 bool ret = false;
343
344 if (PhysicsScene.Constraints.RemoveAndDestroyConstraint(rootPrim.BSBody))
345 {
346 ret = true;
347 }
348 return ret;
349 }
350
351 // Call each of the constraints that make up this linkset and recompute the
352 // various transforms and variables. Used when objects are added or removed
353 // from a linkset to make sure the constraints know about the new mass and
354 // geometry.
355 // Must only be called at taint time!!
356 private void RecomputeLinksetConstraintVariables()
357 {
358 float linksetMass = LinksetMass;
359 foreach (BSPhysObject child in m_taintChildren)
360 {
361 BSConstraint constrain;
362 if (PhysicsScene.Constraints.TryGetConstraint(LinksetRoot.BSBody, child.BSBody, out constrain))
363 {
364 // DetailLog("{0},BSLinkset.RecomputeLinksetConstraintVariables,taint,child={1},mass={2},A={3},B={4}",
365 // LinksetRoot.LocalID, child.LocalID, linksetMass, constrain.Body1.ID, constrain.Body2.ID);
366 constrain.RecomputeConstraintVariables(linksetMass);
367 }
368 else
369 {
370 // Non-fatal error that happens when children are being added to the linkset but
371 // their constraints have not been created yet.
372 break;
373 }
374 }
375
376 // If the whole linkset is not here, doesn't make sense to recompute linkset wide values
377 if (m_children.Count == m_taintChildren.Count)
378 {
379 // If this is a multiple object linkset, set everybody's center of mass to the set's center of mass
380 OMV.Vector3 centerOfMass = ComputeLinksetCenterOfMass();
381 BulletSimAPI.SetCenterOfMassByPosRot2(LinksetRoot.BSBody.ptr,
382 centerOfMass, OMV.Quaternion.Identity);
383 DetailLog("{0},BSLinkset.RecomputeLinksetConstraintVariables,setCenterOfMass,COM={1},rBody={2}",
384 LinksetRoot.LocalID, centerOfMass, LinksetRoot.BSBody.ptr.ToString("X"));
385 foreach (BSPhysObject child in m_taintChildren)
386 {
387 BulletSimAPI.SetCenterOfMassByPosRot2(child.BSBody.ptr,
388 centerOfMass, OMV.Quaternion.Identity);
389 }
390
391 // BulletSimAPI.DumpAllInfo2(PhysicsScene.World.ptr); // DEBUG DEBUG DEBUG
392 }
393 return;
394 }
395}
396}