diff options
Diffstat (limited to '')
-rwxr-xr-x | OpenSim/Region/Physics/BulletSPlugin/BSLinksetConstraints.cs | 327 |
1 files changed, 327 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..c855fda --- /dev/null +++ b/OpenSim/Region/Physics/BulletSPlugin/BSLinksetConstraints.cs | |||
@@ -0,0 +1,327 @@ | |||
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 | */ | ||
27 | using System; | ||
28 | using System.Collections.Generic; | ||
29 | using System.Text; | ||
30 | |||
31 | using OMV = OpenMetaverse; | ||
32 | |||
33 | namespace OpenSim.Region.Physics.BulletSPlugin | ||
34 | { | ||
35 | public sealed 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 | // This is queued in the 'post taint' queue so the | ||
47 | // refresh will happen once after all the other taints are applied. | ||
48 | public override void Refresh(BSPhysObject requestor) | ||
49 | { | ||
50 | // Queue to happen after all the other taint processing | ||
51 | PhysicsScene.PostTaintObject("BSLinksetContraints.Refresh", requestor.LocalID, delegate() | ||
52 | { | ||
53 | if (HasAnyChildren && IsRoot(requestor)) | ||
54 | RecomputeLinksetConstraints(); | ||
55 | }); | ||
56 | } | ||
57 | |||
58 | // The object is going dynamic (physical). Do any setup necessary | ||
59 | // for a dynamic linkset. | ||
60 | // Only the state of the passed object can be modified. The rest of the linkset | ||
61 | // has not yet been fully constructed. | ||
62 | // Return 'true' if any properties updated on the passed object. | ||
63 | // Called at taint-time! | ||
64 | public override bool MakeDynamic(BSPhysObject child) | ||
65 | { | ||
66 | // What is done for each object in BSPrim is what we want. | ||
67 | return false; | ||
68 | } | ||
69 | |||
70 | // The object is going static (non-physical). Do any setup necessary for a static linkset. | ||
71 | // Return 'true' if any properties updated on the passed object. | ||
72 | // This doesn't normally happen -- OpenSim removes the objects from the physical | ||
73 | // world if it is a static linkset. | ||
74 | // Called at taint-time! | ||
75 | public override bool MakeStatic(BSPhysObject child) | ||
76 | { | ||
77 | // What is done for each object in BSPrim is what we want. | ||
78 | return false; | ||
79 | } | ||
80 | |||
81 | // Called at taint-time!! | ||
82 | public override void UpdateProperties(BSPhysObject updated) | ||
83 | { | ||
84 | // Nothing to do for constraints on property updates | ||
85 | } | ||
86 | |||
87 | // The children of the linkset are moved around by the constraints. | ||
88 | // Just grab the current values of wherever it is right now. | ||
89 | public override OMV.Vector3 Position(BSPhysObject member) | ||
90 | { | ||
91 | return BulletSimAPI.GetPosition2(member.PhysBody.ptr); | ||
92 | } | ||
93 | |||
94 | public override OMV.Quaternion Orientation(BSPhysObject member) | ||
95 | { | ||
96 | return BulletSimAPI.GetOrientation2(member.PhysBody.ptr); | ||
97 | } | ||
98 | |||
99 | // Routine called when rebuilding the body of some member of the linkset. | ||
100 | // Destroy all the constraints have have been made to root and set | ||
101 | // up to rebuild the constraints before the next simulation step. | ||
102 | // Returns 'true' of something was actually removed and would need restoring | ||
103 | // Called at taint-time!! | ||
104 | public override bool RemoveBodyDependencies(BSPrim child) | ||
105 | { | ||
106 | bool ret = false; | ||
107 | |||
108 | DetailLog("{0},BSLinksetConstraint.RemoveBodyDependencies,removeChildrenForRoot,rID={1},rBody={2}", | ||
109 | child.LocalID, LinksetRoot.LocalID, LinksetRoot.PhysBody.ptr.ToString("X")); | ||
110 | |||
111 | lock (m_linksetActivityLock) | ||
112 | { | ||
113 | // Just undo all the constraints for this linkset. Rebuild at the end of the step. | ||
114 | ret = PhysicallyUnlinkAllChildrenFromRoot(LinksetRoot); | ||
115 | // Cause the constraints, et al to be rebuilt before the next simulation step. | ||
116 | Refresh(LinksetRoot); | ||
117 | } | ||
118 | return ret; | ||
119 | } | ||
120 | |||
121 | // Companion to RemoveBodyDependencies(). If RemoveBodyDependencies() returns 'true', | ||
122 | // this routine will restore the removed constraints. | ||
123 | // Called at taint-time!! | ||
124 | public override void RestoreBodyDependencies(BSPrim child) | ||
125 | { | ||
126 | // The Refresh operation queued by RemoveBodyDependencies() will build any missing constraints. | ||
127 | } | ||
128 | |||
129 | // ================================================================ | ||
130 | |||
131 | // Add a new child to the linkset. | ||
132 | // Called while LinkActivity is locked. | ||
133 | protected override void AddChildToLinkset(BSPhysObject child) | ||
134 | { | ||
135 | if (!HasChild(child)) | ||
136 | { | ||
137 | m_children.Add(child); | ||
138 | |||
139 | DetailLog("{0},BSLinksetConstraints.AddChildToLinkset,call,child={1}", LinksetRoot.LocalID, child.LocalID); | ||
140 | |||
141 | // Cause constraints and assorted properties to be recomputed before the next simulation step. | ||
142 | Refresh(LinksetRoot); | ||
143 | } | ||
144 | return; | ||
145 | } | ||
146 | |||
147 | // Remove the specified child from the linkset. | ||
148 | // Safe to call even if the child is not really in my linkset. | ||
149 | protected override void RemoveChildFromLinkset(BSPhysObject child) | ||
150 | { | ||
151 | if (m_children.Remove(child)) | ||
152 | { | ||
153 | BSPhysObject rootx = LinksetRoot; // capture the root and body as of now | ||
154 | BSPhysObject childx = child; | ||
155 | |||
156 | DetailLog("{0},BSLinksetConstraints.RemoveChildFromLinkset,call,rID={1},rBody={2},cID={3},cBody={4}", | ||
157 | childx.LocalID, | ||
158 | rootx.LocalID, rootx.PhysBody.ptr.ToString("X"), | ||
159 | childx.LocalID, childx.PhysBody.ptr.ToString("X")); | ||
160 | |||
161 | PhysicsScene.TaintedObject("BSLinksetConstraints.RemoveChildFromLinkset", delegate() | ||
162 | { | ||
163 | PhysicallyUnlinkAChildFromRoot(rootx, childx); | ||
164 | }); | ||
165 | // See that the linkset parameters are recomputed at the end of the taint time. | ||
166 | Refresh(LinksetRoot); | ||
167 | } | ||
168 | else | ||
169 | { | ||
170 | // Non-fatal occurance. | ||
171 | // PhysicsScene.Logger.ErrorFormat("{0}: Asked to remove child from linkset that was not in linkset", LogHeader); | ||
172 | } | ||
173 | return; | ||
174 | } | ||
175 | |||
176 | // Create a constraint between me (root of linkset) and the passed prim (the child). | ||
177 | // Called at taint time! | ||
178 | private void PhysicallyLinkAChildToRoot(BSPhysObject rootPrim, BSPhysObject childPrim) | ||
179 | { | ||
180 | // Don't build the constraint when asked. Put it off until just before the simulation step. | ||
181 | Refresh(rootPrim); | ||
182 | } | ||
183 | |||
184 | private BSConstraint BuildConstraint(BSPhysObject rootPrim, BSPhysObject childPrim) | ||
185 | { | ||
186 | // Zero motion for children so they don't interpolate | ||
187 | childPrim.ZeroMotion(true); | ||
188 | |||
189 | // Relative position normalized to the root prim | ||
190 | // Essentually a vector pointing from center of rootPrim to center of childPrim | ||
191 | OMV.Vector3 childRelativePosition = childPrim.Position - rootPrim.Position; | ||
192 | |||
193 | // real world coordinate of midpoint between the two objects | ||
194 | OMV.Vector3 midPoint = rootPrim.Position + (childRelativePosition / 2); | ||
195 | |||
196 | DetailLog("{0},BSLinksetConstraint.BuildConstraint,taint,root={1},rBody={2},child={3},cBody={4},rLoc={5},cLoc={6},midLoc={7}", | ||
197 | rootPrim.LocalID, | ||
198 | rootPrim.LocalID, rootPrim.PhysBody.ptr.ToString("X"), | ||
199 | childPrim.LocalID, childPrim.PhysBody.ptr.ToString("X"), | ||
200 | rootPrim.Position, childPrim.Position, midPoint); | ||
201 | |||
202 | // create a constraint that allows no freedom of movement between the two objects | ||
203 | // http://bulletphysics.org/Bullet/phpBB3/viewtopic.php?t=4818 | ||
204 | |||
205 | BSConstraint6Dof constrain = new BSConstraint6Dof( | ||
206 | PhysicsScene.World, rootPrim.PhysBody, childPrim.PhysBody, midPoint, true, true ); | ||
207 | // PhysicsScene.World, childPrim.BSBody, rootPrim.BSBody, midPoint, true, true ); | ||
208 | |||
209 | /* NOTE: below is an attempt to build constraint with full frame computation, etc. | ||
210 | * Using the midpoint is easier since it lets the Bullet code manipulate the transforms | ||
211 | * of the objects. | ||
212 | * Code left for future programmers. | ||
213 | // ================================================================================== | ||
214 | // relative position normalized to the root prim | ||
215 | OMV.Quaternion invThisOrientation = OMV.Quaternion.Inverse(rootPrim.Orientation); | ||
216 | OMV.Vector3 childRelativePosition = (childPrim.Position - rootPrim.Position) * invThisOrientation; | ||
217 | |||
218 | // relative rotation of the child to the parent | ||
219 | OMV.Quaternion childRelativeRotation = invThisOrientation * childPrim.Orientation; | ||
220 | OMV.Quaternion inverseChildRelativeRotation = OMV.Quaternion.Inverse(childRelativeRotation); | ||
221 | |||
222 | DetailLog("{0},BSLinksetConstraint.PhysicallyLinkAChildToRoot,taint,root={1},child={2}", rootPrim.LocalID, rootPrim.LocalID, childPrim.LocalID); | ||
223 | BS6DofConstraint constrain = new BS6DofConstraint( | ||
224 | PhysicsScene.World, rootPrim.Body, childPrim.Body, | ||
225 | OMV.Vector3.Zero, | ||
226 | OMV.Quaternion.Inverse(rootPrim.Orientation), | ||
227 | OMV.Vector3.Zero, | ||
228 | OMV.Quaternion.Inverse(childPrim.Orientation), | ||
229 | true, | ||
230 | true | ||
231 | ); | ||
232 | // ================================================================================== | ||
233 | */ | ||
234 | |||
235 | PhysicsScene.Constraints.AddConstraint(constrain); | ||
236 | |||
237 | // zero linear and angular limits makes the objects unable to move in relation to each other | ||
238 | constrain.SetLinearLimits(OMV.Vector3.Zero, OMV.Vector3.Zero); | ||
239 | constrain.SetAngularLimits(OMV.Vector3.Zero, OMV.Vector3.Zero); | ||
240 | |||
241 | // tweek the constraint to increase stability | ||
242 | constrain.UseFrameOffset(PhysicsScene.BoolNumeric(PhysicsScene.Params.linkConstraintUseFrameOffset)); | ||
243 | constrain.TranslationalLimitMotor(PhysicsScene.BoolNumeric(PhysicsScene.Params.linkConstraintEnableTransMotor), | ||
244 | PhysicsScene.Params.linkConstraintTransMotorMaxVel, | ||
245 | PhysicsScene.Params.linkConstraintTransMotorMaxForce); | ||
246 | constrain.SetCFMAndERP(PhysicsScene.Params.linkConstraintCFM, PhysicsScene.Params.linkConstraintERP); | ||
247 | if (PhysicsScene.Params.linkConstraintSolverIterations != 0f) | ||
248 | { | ||
249 | constrain.SetSolverIterations(PhysicsScene.Params.linkConstraintSolverIterations); | ||
250 | } | ||
251 | return constrain; | ||
252 | } | ||
253 | |||
254 | // Remove linkage between the linkset root and a particular child | ||
255 | // The root and child bodies are passed in because we need to remove the constraint between | ||
256 | // the bodies that were present at unlink time. | ||
257 | // Called at taint time! | ||
258 | private bool PhysicallyUnlinkAChildFromRoot(BSPhysObject rootPrim, BSPhysObject childPrim) | ||
259 | { | ||
260 | bool ret = false; | ||
261 | DetailLog("{0},BSLinksetConstraint.PhysicallyUnlinkAChildFromRoot,taint,root={1},rBody={2},child={3},cBody={4}", | ||
262 | rootPrim.LocalID, | ||
263 | rootPrim.LocalID, rootPrim.PhysBody.ptr.ToString("X"), | ||
264 | childPrim.LocalID, childPrim.PhysBody.ptr.ToString("X")); | ||
265 | |||
266 | // Find the constraint for this link and get rid of it from the overall collection and from my list | ||
267 | if (PhysicsScene.Constraints.RemoveAndDestroyConstraint(rootPrim.PhysBody, childPrim.PhysBody)) | ||
268 | { | ||
269 | // Make the child refresh its location | ||
270 | BulletSimAPI.PushUpdate2(childPrim.PhysBody.ptr); | ||
271 | ret = true; | ||
272 | } | ||
273 | |||
274 | return ret; | ||
275 | } | ||
276 | |||
277 | // Remove linkage between myself and any possible children I might have. | ||
278 | // Returns 'true' of any constraints were destroyed. | ||
279 | // Called at taint time! | ||
280 | private bool PhysicallyUnlinkAllChildrenFromRoot(BSPhysObject rootPrim) | ||
281 | { | ||
282 | DetailLog("{0},BSLinksetConstraint.PhysicallyUnlinkAllChildren,taint", rootPrim.LocalID); | ||
283 | |||
284 | return PhysicsScene.Constraints.RemoveAndDestroyConstraint(rootPrim.PhysBody); | ||
285 | } | ||
286 | |||
287 | // Call each of the constraints that make up this linkset and recompute the | ||
288 | // various transforms and variables. Create constraints of not created yet. | ||
289 | // Called before the simulation step to make sure the constraint based linkset | ||
290 | // is all initialized. | ||
291 | // Called at taint time!! | ||
292 | private void RecomputeLinksetConstraints() | ||
293 | { | ||
294 | float linksetMass = LinksetMass; | ||
295 | LinksetRoot.UpdatePhysicalMassProperties(linksetMass); | ||
296 | |||
297 | // DEBUG: see of inter-linkset collisions are causing problems | ||
298 | // BulletSimAPI.SetCollisionFilterMask2(LinksetRoot.BSBody.ptr, | ||
299 | // (uint)CollisionFilterGroups.LinksetFilter, (uint)CollisionFilterGroups.LinksetMask); | ||
300 | DetailLog("{0},BSLinksetConstraint.RecomputeLinksetConstraints,set,rBody={1},linksetMass={2}", | ||
301 | LinksetRoot.LocalID, LinksetRoot.PhysBody.ptr.ToString("X"), linksetMass); | ||
302 | |||
303 | foreach (BSPhysObject child in m_children) | ||
304 | { | ||
305 | // A child in the linkset physically shows the mass of the whole linkset. | ||
306 | // This allows Bullet to apply enough force on the child to move the whole linkset. | ||
307 | // (Also do the mass stuff before recomputing the constraint so mass is not zero.) | ||
308 | child.UpdatePhysicalMassProperties(linksetMass); | ||
309 | |||
310 | BSConstraint constrain; | ||
311 | if (!PhysicsScene.Constraints.TryGetConstraint(LinksetRoot.PhysBody, child.PhysBody, out constrain)) | ||
312 | { | ||
313 | // If constraint doesn't exist yet, create it. | ||
314 | constrain = BuildConstraint(LinksetRoot, child); | ||
315 | } | ||
316 | constrain.RecomputeConstraintVariables(linksetMass); | ||
317 | |||
318 | // DEBUG: see of inter-linkset collisions are causing problems | ||
319 | // BulletSimAPI.SetCollisionFilterMask2(child.BSBody.ptr, | ||
320 | // (uint)CollisionFilterGroups.LinksetFilter, (uint)CollisionFilterGroups.LinksetMask); | ||
321 | |||
322 | // BulletSimAPI.DumpConstraint2(PhysicsScene.World.ptr, constrain.Constraint.ptr); // DEBUG DEBUG | ||
323 | } | ||
324 | |||
325 | } | ||
326 | } | ||
327 | } | ||