diff options
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSActorMoveToTarget.cs')
-rwxr-xr-x | OpenSim/Region/Physics/BulletSPlugin/BSActorMoveToTarget.cs | 219 |
1 files changed, 219 insertions, 0 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSActorMoveToTarget.cs b/OpenSim/Region/Physics/BulletSPlugin/BSActorMoveToTarget.cs new file mode 100755 index 0000000..bdf4bc0 --- /dev/null +++ b/OpenSim/Region/Physics/BulletSPlugin/BSActorMoveToTarget.cs | |||
@@ -0,0 +1,219 @@ | |||
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 OpenSim.Region.Physics.Manager; | ||
34 | |||
35 | using OMV = OpenMetaverse; | ||
36 | |||
37 | namespace OpenSim.Region.Physics.BulletSPlugin | ||
38 | { | ||
39 | public class BSActorMoveToTarget : BSActor | ||
40 | { | ||
41 | private BSVMotor m_targetMotor; | ||
42 | |||
43 | public BSActorMoveToTarget(BSScene physicsScene, BSPhysObject pObj, string actorName) | ||
44 | : base(physicsScene, pObj, actorName) | ||
45 | { | ||
46 | m_targetMotor = null; | ||
47 | m_physicsScene.DetailLog("{0},BSActorMoveToTarget,constructor", m_controllingPrim.LocalID); | ||
48 | } | ||
49 | |||
50 | // BSActor.isActive | ||
51 | public override bool isActive | ||
52 | { | ||
53 | // MoveToTarget only works on physical prims | ||
54 | get { return Enabled && m_controllingPrim.IsPhysicallyActive; } | ||
55 | } | ||
56 | |||
57 | // Release any connections and resources used by the actor. | ||
58 | // BSActor.Dispose() | ||
59 | public override void Dispose() | ||
60 | { | ||
61 | Enabled = false; | ||
62 | } | ||
63 | |||
64 | // Called when physical parameters (properties set in Bullet) need to be re-applied. | ||
65 | // Called at taint-time. | ||
66 | // BSActor.Refresh() | ||
67 | public override void Refresh() | ||
68 | { | ||
69 | m_physicsScene.DetailLog("{0},BSActorMoveToTarget,refresh,enabled={1},active={2},target={3},tau={4}", | ||
70 | m_controllingPrim.LocalID, Enabled, m_controllingPrim.MoveToTargetActive, | ||
71 | m_controllingPrim.MoveToTargetTarget, m_controllingPrim.MoveToTargetTau ); | ||
72 | |||
73 | // If not active any more... | ||
74 | if (!m_controllingPrim.MoveToTargetActive) | ||
75 | { | ||
76 | Enabled = false; | ||
77 | } | ||
78 | |||
79 | if (isActive) | ||
80 | { | ||
81 | ActivateMoveToTarget(); | ||
82 | } | ||
83 | else | ||
84 | { | ||
85 | DeactivateMoveToTarget(); | ||
86 | } | ||
87 | } | ||
88 | |||
89 | // The object's physical representation is being rebuilt so pick up any physical dependencies (constraints, ...). | ||
90 | // Register a prestep action to restore physical requirements before the next simulation step. | ||
91 | // Called at taint-time. | ||
92 | // BSActor.RemoveDependencies() | ||
93 | public override void RemoveDependencies() | ||
94 | { | ||
95 | // Nothing to do for the moveToTarget since it is all software at pre-step action time. | ||
96 | } | ||
97 | |||
98 | // If a hover motor has not been created, create one and start the hovering. | ||
99 | private void ActivateMoveToTarget() | ||
100 | { | ||
101 | if (m_targetMotor == null) | ||
102 | { | ||
103 | // We're taking over after this. | ||
104 | m_controllingPrim.ZeroMotion(true); | ||
105 | |||
106 | /* Someday use the PID controller | ||
107 | m_targetMotor = new BSPIDVMotor("BSActorMoveToTarget-" + m_controllingPrim.LocalID.ToString()); | ||
108 | m_targetMotor.TimeScale = m_controllingPrim.MoveToTargetTau; | ||
109 | m_targetMotor.Efficiency = 1f; | ||
110 | */ | ||
111 | m_targetMotor = new BSVMotor("BSActorMoveToTarget-" + m_controllingPrim.LocalID.ToString(), | ||
112 | m_controllingPrim.MoveToTargetTau, // timeScale | ||
113 | BSMotor.Infinite, // decay time scale | ||
114 | 1f // efficiency | ||
115 | ); | ||
116 | m_targetMotor.PhysicsScene = m_physicsScene; // DEBUG DEBUG so motor will output detail log messages. | ||
117 | m_targetMotor.SetTarget(m_controllingPrim.MoveToTargetTarget); | ||
118 | m_targetMotor.SetCurrent(m_controllingPrim.RawPosition); | ||
119 | |||
120 | // m_physicsScene.BeforeStep += Mover; | ||
121 | m_physicsScene.BeforeStep += Mover2; | ||
122 | } | ||
123 | else | ||
124 | { | ||
125 | // If already allocated, make sure the target and other paramters are current | ||
126 | m_targetMotor.SetTarget(m_controllingPrim.MoveToTargetTarget); | ||
127 | m_targetMotor.SetCurrent(m_controllingPrim.RawPosition); | ||
128 | } | ||
129 | } | ||
130 | |||
131 | private void DeactivateMoveToTarget() | ||
132 | { | ||
133 | if (m_targetMotor != null) | ||
134 | { | ||
135 | // m_physicsScene.BeforeStep -= Mover; | ||
136 | m_physicsScene.BeforeStep -= Mover2; | ||
137 | m_targetMotor = null; | ||
138 | } | ||
139 | } | ||
140 | |||
141 | // Origional mover that set the objects position to move to the target. | ||
142 | // The problem was that gravity would keep trying to push the object down so | ||
143 | // the overall downward velocity would increase to infinity. | ||
144 | // Called just before the simulation step. | ||
145 | private void Mover(float timeStep) | ||
146 | { | ||
147 | // Don't do hovering while the object is selected. | ||
148 | if (!isActive) | ||
149 | return; | ||
150 | |||
151 | OMV.Vector3 origPosition = m_controllingPrim.RawPosition; // DEBUG DEBUG (for printout below) | ||
152 | |||
153 | // 'movePosition' is where we'd like the prim to be at this moment. | ||
154 | OMV.Vector3 movePosition = m_controllingPrim.RawPosition + m_targetMotor.Step(timeStep); | ||
155 | |||
156 | // If we are very close to our target, turn off the movement motor. | ||
157 | if (m_targetMotor.ErrorIsZero()) | ||
158 | { | ||
159 | m_physicsScene.DetailLog("{0},BSActorMoveToTarget.Mover,zeroMovement,movePos={1},pos={2},mass={3}", | ||
160 | m_controllingPrim.LocalID, movePosition, m_controllingPrim.RawPosition, m_controllingPrim.Mass); | ||
161 | m_controllingPrim.ForcePosition = m_targetMotor.TargetValue; | ||
162 | m_controllingPrim.ForceVelocity = OMV.Vector3.Zero; | ||
163 | // Setting the position does not cause the physics engine to generate a property update. Force it. | ||
164 | m_physicsScene.PE.PushUpdate(m_controllingPrim.PhysBody); | ||
165 | } | ||
166 | else | ||
167 | { | ||
168 | m_controllingPrim.ForcePosition = movePosition; | ||
169 | // Setting the position does not cause the physics engine to generate a property update. Force it. | ||
170 | m_physicsScene.PE.PushUpdate(m_controllingPrim.PhysBody); | ||
171 | } | ||
172 | m_physicsScene.DetailLog("{0},BSActorMoveToTarget.Mover,move,fromPos={1},movePos={2}", | ||
173 | m_controllingPrim.LocalID, origPosition, movePosition); | ||
174 | } | ||
175 | |||
176 | // Version of mover that applies forces to move the physical object to the target. | ||
177 | // Also overcomes gravity so the object doesn't just drop to the ground. | ||
178 | // Called just before the simulation step. | ||
179 | private void Mover2(float timeStep) | ||
180 | { | ||
181 | // Don't do hovering while the object is selected. | ||
182 | if (!isActive) | ||
183 | return; | ||
184 | |||
185 | OMV.Vector3 origPosition = m_controllingPrim.RawPosition; // DEBUG DEBUG (for printout below) | ||
186 | OMV.Vector3 addedForce = OMV.Vector3.Zero; | ||
187 | |||
188 | // CorrectionVector is the movement vector required this step | ||
189 | OMV.Vector3 correctionVector = m_targetMotor.Step(timeStep, m_controllingPrim.RawPosition); | ||
190 | |||
191 | // If we are very close to our target, turn off the movement motor. | ||
192 | if (m_targetMotor.ErrorIsZero()) | ||
193 | { | ||
194 | m_physicsScene.DetailLog("{0},BSActorMoveToTarget.Mover3,zeroMovement,pos={1},mass={2}", | ||
195 | m_controllingPrim.LocalID, m_controllingPrim.RawPosition, m_controllingPrim.Mass); | ||
196 | m_controllingPrim.ForcePosition = m_targetMotor.TargetValue; | ||
197 | m_controllingPrim.ForceVelocity = OMV.Vector3.Zero; | ||
198 | // Setting the position does not cause the physics engine to generate a property update. Force it. | ||
199 | m_physicsScene.PE.PushUpdate(m_controllingPrim.PhysBody); | ||
200 | } | ||
201 | else | ||
202 | { | ||
203 | // First force to move us there -- the motor return a timestep scaled value. | ||
204 | addedForce = correctionVector / timeStep; | ||
205 | // Remove the existing velocity (only the moveToTarget force counts) | ||
206 | addedForce -= m_controllingPrim.RawVelocity; | ||
207 | // Overcome gravity. | ||
208 | addedForce -= m_controllingPrim.Gravity; | ||
209 | |||
210 | // Add enough force to overcome the mass of the object | ||
211 | addedForce *= m_controllingPrim.Mass; | ||
212 | |||
213 | m_controllingPrim.AddForce(addedForce, false /* pushForce */, true /* inTaintTime */); | ||
214 | } | ||
215 | m_physicsScene.DetailLog("{0},BSActorMoveToTarget.Mover3,move,fromPos={1},addedForce={2}", | ||
216 | m_controllingPrim.LocalID, origPosition, addedForce); | ||
217 | } | ||
218 | } | ||
219 | } | ||