diff options
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSActorMoveToTarget.cs')
-rwxr-xr-x | OpenSim/Region/Physics/BulletSPlugin/BSActorMoveToTarget.cs | 157 |
1 files changed, 157 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..75ff24e --- /dev/null +++ b/OpenSim/Region/Physics/BulletSPlugin/BSActorMoveToTarget.cs | |||
@@ -0,0 +1,157 @@ | |||
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 | get { return Enabled; } | ||
54 | } | ||
55 | |||
56 | // Release any connections and resources used by the actor. | ||
57 | // BSActor.Dispose() | ||
58 | public override void Dispose() | ||
59 | { | ||
60 | Enabled = false; | ||
61 | } | ||
62 | |||
63 | // Called when physical parameters (properties set in Bullet) need to be re-applied. | ||
64 | // Called at taint-time. | ||
65 | // BSActor.Refresh() | ||
66 | public override void Refresh() | ||
67 | { | ||
68 | m_physicsScene.DetailLog("{0},BSActorMoveToTarget,refresh,enabled={1},active={2},target={3},tau={4}", | ||
69 | m_controllingPrim.LocalID, Enabled, m_controllingPrim.MoveToTargetActive, | ||
70 | m_controllingPrim.MoveToTargetTarget, m_controllingPrim.MoveToTargetTau ); | ||
71 | |||
72 | // If not active any more... | ||
73 | if (!m_controllingPrim.MoveToTargetActive) | ||
74 | { | ||
75 | Enabled = false; | ||
76 | } | ||
77 | |||
78 | if (isActive) | ||
79 | { | ||
80 | ActivateMoveToTarget(); | ||
81 | } | ||
82 | else | ||
83 | { | ||
84 | DeactivateMoveToTarget(); | ||
85 | } | ||
86 | } | ||
87 | |||
88 | // The object's physical representation is being rebuilt so pick up any physical dependencies (constraints, ...). | ||
89 | // Register a prestep action to restore physical requirements before the next simulation step. | ||
90 | // Called at taint-time. | ||
91 | // BSActor.RemoveDependencies() | ||
92 | public override void RemoveDependencies() | ||
93 | { | ||
94 | // Nothing to do for the moveToTarget since it is all software at pre-step action time. | ||
95 | } | ||
96 | |||
97 | // If a hover motor has not been created, create one and start the hovering. | ||
98 | private void ActivateMoveToTarget() | ||
99 | { | ||
100 | if (m_targetMotor == null) | ||
101 | { | ||
102 | // We're taking over after this. | ||
103 | m_controllingPrim.ZeroMotion(true); | ||
104 | |||
105 | m_targetMotor = new BSVMotor("BSActorMoveToTargget.Activate", | ||
106 | m_controllingPrim.MoveToTargetTau, // timeScale | ||
107 | BSMotor.Infinite, // decay time scale | ||
108 | 1f // efficiency | ||
109 | ); | ||
110 | m_targetMotor.PhysicsScene = m_physicsScene; // DEBUG DEBUG so motor will output detail log messages. | ||
111 | m_targetMotor.SetTarget(m_controllingPrim.MoveToTargetTarget); | ||
112 | m_targetMotor.SetCurrent(m_controllingPrim.RawPosition); | ||
113 | |||
114 | m_physicsScene.BeforeStep += Mover; | ||
115 | } | ||
116 | } | ||
117 | |||
118 | private void DeactivateMoveToTarget() | ||
119 | { | ||
120 | if (m_targetMotor != null) | ||
121 | { | ||
122 | m_physicsScene.BeforeStep -= Mover; | ||
123 | m_targetMotor = null; | ||
124 | } | ||
125 | } | ||
126 | |||
127 | // Called just before the simulation step. Update the vertical position for hoverness. | ||
128 | private void Mover(float timeStep) | ||
129 | { | ||
130 | // Don't do hovering while the object is selected. | ||
131 | if (!isActive) | ||
132 | return; | ||
133 | |||
134 | OMV.Vector3 origPosition = m_controllingPrim.RawPosition; // DEBUG DEBUG (for printout below) | ||
135 | |||
136 | // 'movePosition' is where we'd like the prim to be at this moment. | ||
137 | OMV.Vector3 movePosition = m_controllingPrim.RawPosition + m_targetMotor.Step(timeStep); | ||
138 | |||
139 | // If we are very close to our target, turn off the movement motor. | ||
140 | if (m_targetMotor.ErrorIsZero()) | ||
141 | { | ||
142 | m_physicsScene.DetailLog("{0},BSActorMoveToTarget.Mover,zeroMovement,movePos={1},pos={2},mass={3}", | ||
143 | m_controllingPrim.LocalID, movePosition, m_controllingPrim.RawPosition, m_controllingPrim.Mass); | ||
144 | m_controllingPrim.ForcePosition = m_targetMotor.TargetValue; | ||
145 | // Setting the position does not cause the physics engine to generate a property update. Force it. | ||
146 | m_physicsScene.PE.PushUpdate(m_controllingPrim.PhysBody); | ||
147 | } | ||
148 | else | ||
149 | { | ||
150 | m_controllingPrim.ForcePosition = movePosition; | ||
151 | // Setting the position does not cause the physics engine to generate a property update. Force it. | ||
152 | m_physicsScene.PE.PushUpdate(m_controllingPrim.PhysBody); | ||
153 | } | ||
154 | m_physicsScene.DetailLog("{0},BSActorMoveToTarget.Mover,move,fromPos={1},movePos={2}", m_controllingPrim.LocalID, origPosition, movePosition); | ||
155 | } | ||
156 | } | ||
157 | } | ||