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