diff options
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSActorLockAxis.cs')
-rwxr-xr-x | OpenSim/Region/Physics/BulletSPlugin/BSActorLockAxis.cs | 175 |
1 files changed, 175 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..7219617 --- /dev/null +++ b/OpenSim/Region/Physics/BulletSPlugin/BSActorLockAxis.cs | |||
@@ -0,0 +1,175 @@ | |||
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 = true; | ||
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 | false /* useLinearReferenceFrameB */, true /* disableCollisionsBetweenLinkedBodies */); | ||
122 | LockAxisConstraint = axisConstrainer; | ||
123 | m_physicsScene.Constraints.AddConstraint(LockAxisConstraint); | ||
124 | |||
125 | // The constraint is tied to the world and oriented to the prim. | ||
126 | |||
127 | // Free to move linearly in the region | ||
128 | OMV.Vector3 linearLow = OMV.Vector3.Zero; | ||
129 | OMV.Vector3 linearHigh = m_physicsScene.TerrainManager.DefaultRegionSize; | ||
130 | axisConstrainer.SetLinearLimits(linearLow, linearHigh); | ||
131 | |||
132 | // Angular with some axis locked | ||
133 | float fPI = (float)Math.PI; | ||
134 | OMV.Vector3 angularLow = new OMV.Vector3(-fPI, -fPI, -fPI); | ||
135 | OMV.Vector3 angularHigh = new OMV.Vector3(fPI, fPI, fPI); | ||
136 | if (m_controllingPrim.LockedAxis.X != 1f) | ||
137 | { | ||
138 | angularLow.X = 0f; | ||
139 | angularHigh.X = 0f; | ||
140 | } | ||
141 | if (m_controllingPrim.LockedAxis.Y != 1f) | ||
142 | { | ||
143 | angularLow.Y = 0f; | ||
144 | angularHigh.Y = 0f; | ||
145 | } | ||
146 | if (m_controllingPrim.LockedAxis.Z != 1f) | ||
147 | { | ||
148 | angularLow.Z = 0f; | ||
149 | angularHigh.Z = 0f; | ||
150 | } | ||
151 | if (!axisConstrainer.SetAngularLimits(angularLow, angularHigh)) | ||
152 | { | ||
153 | m_physicsScene.DetailLog("{0},BSActorLockAxis.AddAxisLockConstraint,failedSetAngularLimits", m_controllingPrim.LocalID); | ||
154 | } | ||
155 | |||
156 | m_physicsScene.DetailLog("{0},BSActorLockAxis.AddAxisLockConstraint,create,linLow={1},linHi={2},angLow={3},angHi={4}", | ||
157 | m_controllingPrim.LocalID, linearLow, linearHigh, angularLow, angularHigh); | ||
158 | |||
159 | // Constants from one of the posts mentioned above and used in Bullet's ConstraintDemo. | ||
160 | axisConstrainer.TranslationalLimitMotor(true /* enable */, 5.0f, 0.1f); | ||
161 | |||
162 | axisConstrainer.RecomputeConstraintVariables(m_controllingPrim.RawMass); | ||
163 | } | ||
164 | |||
165 | private void RemoveAxisLockConstraint() | ||
166 | { | ||
167 | if (LockAxisConstraint != null) | ||
168 | { | ||
169 | m_physicsScene.Constraints.RemoveAndDestroyConstraint(LockAxisConstraint); | ||
170 | LockAxisConstraint = null; | ||
171 | m_physicsScene.DetailLog("{0},BSActorLockAxis.RemoveAxisLockConstraint,destroyingConstraint", m_controllingPrim.LocalID); | ||
172 | } | ||
173 | } | ||
174 | } | ||
175 | } | ||