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