diff options
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSActorLockAxis.cs')
-rwxr-xr-x | OpenSim/Region/Physics/BulletSPlugin/BSActorLockAxis.cs | 187 |
1 files changed, 187 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..8b0fdeb --- /dev/null +++ b/OpenSim/Region/Physics/BulletSPlugin/BSActorLockAxis.cs | |||
@@ -0,0 +1,187 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.Linq; | ||
31 | using System.Text; | ||
32 | |||
33 | using OMV = OpenMetaverse; | ||
34 | |||
35 | namespace OpenSim.Region.Physics.BulletSPlugin | ||
36 | { | ||
37 | public class BSActorLockAxis : BSActor | ||
38 | { | ||
39 | BSConstraint LockAxisConstraint = null; | ||
40 | |||
41 | public BSActorLockAxis(BSScene physicsScene, BSPhysObject pObj, string actorName) | ||
42 | : base(physicsScene, pObj, actorName) | ||
43 | { | ||
44 | m_physicsScene.DetailLog("{0},BSActorLockAxis,constructor", m_controllingPrim.LocalID); | ||
45 | LockAxisConstraint = null; | ||
46 | } | ||
47 | |||
48 | // BSActor.isActive | ||
49 | public override bool isActive | ||
50 | { | ||
51 | get { return Enabled && m_controllingPrim.IsPhysicallyActive; } | ||
52 | } | ||
53 | |||
54 | // Release any connections and resources used by the actor. | ||
55 | // BSActor.Dispose() | ||
56 | public override void Dispose() | ||
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 | m_physicsScene.DetailLog("{0},BSActorLockAxis,refresh,lockedAxis={1},enabled={2},pActive={3}", | ||
67 | m_controllingPrim.LocalID, m_controllingPrim.LockedAngularAxis, Enabled, m_controllingPrim.IsPhysicallyActive); | ||
68 | // If all the axis are free, we don't need to exist | ||
69 | if (m_controllingPrim.LockedAngularAxis == m_controllingPrim.LockedAxisFree) | ||
70 | { | ||
71 | Enabled = false; | ||
72 | } | ||
73 | |||
74 | // If the object is physically active, add the axis locking constraint | ||
75 | if (isActive) | ||
76 | { | ||
77 | AddAxisLockConstraint(); | ||
78 | } | ||
79 | else | ||
80 | { | ||
81 | RemoveAxisLockConstraint(); | ||
82 | } | ||
83 | } | ||
84 | |||
85 | // The object's physical representation is being rebuilt so pick up any physical dependencies (constraints, ...). | ||
86 | // Register a prestep action to restore physical requirements before the next simulation step. | ||
87 | // Called at taint-time. | ||
88 | // BSActor.RemoveDependencies() | ||
89 | public override void RemoveDependencies() | ||
90 | { | ||
91 | if (LockAxisConstraint != null) | ||
92 | { | ||
93 | // If a constraint is set up, remove it from the physical scene | ||
94 | RemoveAxisLockConstraint(); | ||
95 | // Schedule a call before the next simulation step to restore the constraint. | ||
96 | m_physicsScene.PostTaintObject("BSActorLockAxis:" + ActorName, m_controllingPrim.LocalID, delegate() | ||
97 | { | ||
98 | Refresh(); | ||
99 | }); | ||
100 | } | ||
101 | } | ||
102 | |||
103 | private void AddAxisLockConstraint() | ||
104 | { | ||
105 | if (LockAxisConstraint == null) | ||
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(m_physicsScene.World, m_controllingPrim.PhysBody, | ||
116 | OMV.Vector3.Zero, OMV.Quaternion.Identity, | ||
117 | false /* useLinearReferenceFrameB */, true /* disableCollisionsBetweenLinkedBodies */); | ||
118 | LockAxisConstraint = axisConstrainer; | ||
119 | m_physicsScene.Constraints.AddConstraint(LockAxisConstraint); | ||
120 | |||
121 | // The constraint is tied to the world and oriented to the prim. | ||
122 | |||
123 | // Free to move linearly in the region | ||
124 | OMV.Vector3 linearLow = OMV.Vector3.Zero; | ||
125 | OMV.Vector3 linearHigh = m_physicsScene.TerrainManager.DefaultRegionSize; | ||
126 | if (m_controllingPrim.LockedLinearAxis.X != BSPhysObject.FreeAxis) | ||
127 | { | ||
128 | linearLow.X = m_controllingPrim.RawPosition.X; | ||
129 | linearHigh.X = m_controllingPrim.RawPosition.X; | ||
130 | } | ||
131 | if (m_controllingPrim.LockedLinearAxis.Y != BSPhysObject.FreeAxis) | ||
132 | { | ||
133 | linearLow.Y = m_controllingPrim.RawPosition.Y; | ||
134 | linearHigh.Y = m_controllingPrim.RawPosition.Y; | ||
135 | } | ||
136 | if (m_controllingPrim.LockedLinearAxis.Z != BSPhysObject.FreeAxis) | ||
137 | { | ||
138 | linearLow.Z = m_controllingPrim.RawPosition.Z; | ||
139 | linearHigh.Z = m_controllingPrim.RawPosition.Z; | ||
140 | } | ||
141 | axisConstrainer.SetLinearLimits(linearLow, linearHigh); | ||
142 | |||
143 | // Angular with some axis locked | ||
144 | float fPI = (float)Math.PI; | ||
145 | OMV.Vector3 angularLow = new OMV.Vector3(-fPI, -fPI, -fPI); | ||
146 | OMV.Vector3 angularHigh = new OMV.Vector3(fPI, fPI, fPI); | ||
147 | if (m_controllingPrim.LockedAngularAxis.X != BSPhysObject.FreeAxis) | ||
148 | { | ||
149 | angularLow.X = 0f; | ||
150 | angularHigh.X = 0f; | ||
151 | } | ||
152 | if (m_controllingPrim.LockedAngularAxis.Y != BSPhysObject.FreeAxis) | ||
153 | { | ||
154 | angularLow.Y = 0f; | ||
155 | angularHigh.Y = 0f; | ||
156 | } | ||
157 | if (m_controllingPrim.LockedAngularAxis.Z != BSPhysObject.FreeAxis) | ||
158 | { | ||
159 | angularLow.Z = 0f; | ||
160 | angularHigh.Z = 0f; | ||
161 | } | ||
162 | if (!axisConstrainer.SetAngularLimits(angularLow, angularHigh)) | ||
163 | { | ||
164 | m_physicsScene.DetailLog("{0},BSActorLockAxis.AddAxisLockConstraint,failedSetAngularLimits", m_controllingPrim.LocalID); | ||
165 | } | ||
166 | |||
167 | m_physicsScene.DetailLog("{0},BSActorLockAxis.AddAxisLockConstraint,create,linLow={1},linHi={2},angLow={3},angHi={4}", | ||
168 | m_controllingPrim.LocalID, linearLow, linearHigh, angularLow, angularHigh); | ||
169 | |||
170 | // Constants from one of the posts mentioned above and used in Bullet's ConstraintDemo. | ||
171 | axisConstrainer.TranslationalLimitMotor(true /* enable */, 5.0f, 0.1f); | ||
172 | |||
173 | axisConstrainer.RecomputeConstraintVariables(m_controllingPrim.RawMass); | ||
174 | } | ||
175 | } | ||
176 | |||
177 | private void RemoveAxisLockConstraint() | ||
178 | { | ||
179 | if (LockAxisConstraint != null) | ||
180 | { | ||
181 | m_physicsScene.Constraints.RemoveAndDestroyConstraint(LockAxisConstraint); | ||
182 | LockAxisConstraint = null; | ||
183 | m_physicsScene.DetailLog("{0},BSActorLockAxis.RemoveAxisLockConstraint,destroyingConstraint", m_controllingPrim.LocalID); | ||
184 | } | ||
185 | } | ||
186 | } | ||
187 | } | ||