aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Physics/BulletSPlugin/BSActorLockAxis.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSActorLockAxis.cs')
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSActorLockAxis.cs168
1 files changed, 168 insertions, 0 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSActorLockAxis.cs b/OpenSim/Region/Physics/BulletSPlugin/BSActorLockAxis.cs
new file mode 100755
index 0000000..b4af126
--- /dev/null
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSActorLockAxis.cs
@@ -0,0 +1,168 @@
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
28using System;
29using System.Collections.Generic;
30using System.Linq;
31using System.Text;
32
33using OMV = OpenMetaverse;
34
35namespace OpenSim.Region.Physics.BulletSPlugin
36{
37public class BSActorLockAxis : BSActor
38{
39 bool TryExperimentalLockAxisCode = false;
40 BSConstraint LockAxisConstraint = null;
41
42 public BSActorLockAxis(BSScene physicsScene, BSPhysObject pObj, string actorName)
43 : base(physicsScene, pObj,actorName)
44 {
45 LockAxisConstraint = null;
46 }
47
48 // BSActor.isActive
49 public override bool isActive
50 {
51 get { return Enabled && Prim.IsPhysicallyActive; }
52 }
53
54 // Release any connections and resources used by the actor.
55 // BSActor.Release()
56 public override void Release()
57 {
58 RemoveAxisLockConstraint();
59 }
60
61 // Called when physical parameters (properties set in Bullet) need to be re-applied.
62 // Called at taint-time.
63 // BSActor.Refresh()
64 public override void Refresh()
65 {
66 // If all the axis are free, we don't need to exist
67 if (Prim.LockedAxis == Prim.LockedAxisFree)
68 {
69 Prim.PhysicalActors.RemoveAndRelease(ActorName);
70 return;
71 }
72 // If the object is physically active, add the axis locking constraint
73 if (Enabled
74 && Prim.IsPhysicallyActive
75 && TryExperimentalLockAxisCode
76 && Prim.LockedAxis != Prim.LockedAxisFree)
77 {
78 if (LockAxisConstraint != null)
79 AddAxisLockConstraint();
80 }
81 else
82 {
83 RemoveAxisLockConstraint();
84 }
85 }
86
87 // The object's physical representation is being rebuilt so pick up any physical dependencies (constraints, ...).
88 // Register a prestep action to restore physical requirements before the next simulation step.
89 // Called at taint-time.
90 // BSActor.RemoveBodyDependencies()
91 public override void RemoveBodyDependencies()
92 {
93 if (LockAxisConstraint != null)
94 {
95 // If a constraint is set up, remove it from the physical scene
96 RemoveAxisLockConstraint();
97 // Schedule a call before the next simulation step to restore the constraint.
98 PhysicsScene.PostTaintObject(Prim.LockedAxisActorName, Prim.LocalID, delegate()
99 {
100 Refresh();
101 });
102 }
103 }
104
105 private void AddAxisLockConstraint()
106 {
107 // Lock that axis by creating a 6DOF constraint that has one end in the world and
108 // the other in the object.
109 // http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?p=20817
110 // http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?p=26380
111
112 // Remove any existing axis constraint (just to be sure)
113 RemoveAxisLockConstraint();
114
115 BSConstraint6Dof axisConstrainer = new BSConstraint6Dof(PhysicsScene.World, Prim.PhysBody,
116 OMV.Vector3.Zero, OMV.Quaternion.Inverse(Prim.RawOrientation),
117 true /* useLinearReferenceFrameB */, true /* disableCollisionsBetweenLinkedBodies */);
118 LockAxisConstraint = axisConstrainer;
119 PhysicsScene.Constraints.AddConstraint(LockAxisConstraint);
120
121 // The constraint is tied to the world and oriented to the prim.
122
123 // Free to move linearly
124 OMV.Vector3 linearLow = OMV.Vector3.Zero;
125 OMV.Vector3 linearHigh = PhysicsScene.TerrainManager.DefaultRegionSize;
126 axisConstrainer.SetLinearLimits(linearLow, linearHigh);
127
128 // Angular with some axis locked
129 float f2PI = (float)Math.PI * 2f;
130 OMV.Vector3 angularLow = new OMV.Vector3(-f2PI, -f2PI, -f2PI);
131 OMV.Vector3 angularHigh = new OMV.Vector3(f2PI, f2PI, f2PI);
132 if (Prim.LockedAxis.X != 1f)
133 {
134 angularLow.X = 0f;
135 angularHigh.X = 0f;
136 }
137 if (Prim.LockedAxis.Y != 1f)
138 {
139 angularLow.Y = 0f;
140 angularHigh.Y = 0f;
141 }
142 if (Prim.LockedAxis.Z != 1f)
143 {
144 angularLow.Z = 0f;
145 angularHigh.Z = 0f;
146 }
147 axisConstrainer.SetAngularLimits(angularLow, angularHigh);
148
149 PhysicsScene.DetailLog("{0},BSPrim.LockAngularMotion,create,linLow={1},linHi={2},angLow={3},angHi={4}",
150 Prim.LocalID, linearLow, linearHigh, angularLow, angularHigh);
151
152 // Constants from one of the posts mentioned above and used in Bullet's ConstraintDemo.
153 axisConstrainer.TranslationalLimitMotor(true /* enable */, 5.0f, 0.1f);
154
155 axisConstrainer.RecomputeConstraintVariables(Prim.RawMass);
156 }
157
158 private void RemoveAxisLockConstraint()
159 {
160 if (LockAxisConstraint != null)
161 {
162 PhysicsScene.Constraints.RemoveAndDestroyConstraint(LockAxisConstraint);
163 LockAxisConstraint = null;
164 PhysicsScene.DetailLog("{0},BSPrim.CleanUpLockAxisPhysicals,destroyingConstraint", Prim.LocalID);
165 }
166 }
167}
168}