aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Physics/BulletSPlugin/BSActorAvatarMove.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSActorAvatarMove.cs287
1 files changed, 287 insertions, 0 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSActorAvatarMove.cs b/OpenSim/Region/Physics/BulletSPlugin/BSActorAvatarMove.cs
new file mode 100755
index 0000000..634a898
--- /dev/null
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSActorAvatarMove.cs
@@ -0,0 +1,287 @@
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
28using System;
29using System.Collections.Generic;
30using System.Linq;
31using System.Text;
32
33using OpenSim.Region.Physics.Manager;
34
35using OMV = OpenMetaverse;
36
37namespace OpenSim.Region.Physics.BulletSPlugin
38{
39public class BSActorAvatarMove : BSActor
40{
41 BSVMotor m_velocityMotor;
42
43 public BSActorAvatarMove(BSScene physicsScene, BSPhysObject pObj, string actorName)
44 : base(physicsScene, pObj, actorName)
45 {
46 m_velocityMotor = null;
47 m_physicsScene.DetailLog("{0},BSActorAvatarMove,constructor", m_controllingPrim.LocalID);
48 }
49
50 // BSActor.isActive
51 public override bool isActive
52 {
53 get { return Enabled && m_controllingPrim.IsPhysicallyActive; }
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},BSActorAvatarMove,refresh", m_controllingPrim.LocalID);
69
70 // If not active any more, get rid of me (shouldn't ever happen, but just to be safe)
71 if (m_controllingPrim.RawForce == OMV.Vector3.Zero)
72 {
73 m_physicsScene.DetailLog("{0},BSActorAvatarMove,refresh,notAvatarMove,removing={1}", m_controllingPrim.LocalID, ActorName);
74 m_controllingPrim.PhysicalActors.RemoveAndRelease(ActorName);
75 return;
76 }
77
78 // If the object is physically active, add the hoverer prestep action
79 if (isActive)
80 {
81 ActivateAvatarMove();
82 }
83 else
84 {
85 DeactivateAvatarMove();
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.RemoveBodyDependencies()
93 public override void RemoveBodyDependencies()
94 {
95 // Nothing to do for the hoverer since it is all software at pre-step action time.
96 }
97
98 public void SetVelocityAndTarget(OMV.Vector3 vel, OMV.Vector3 targ, bool inTaintTime)
99 {
100 m_physicsScene.TaintedObject(inTaintTime, "BSActorAvatarMove.setVelocityAndTarget", delegate()
101 {
102 m_velocityMotor.Reset();
103 m_velocityMotor.SetTarget(targ);
104 m_velocityMotor.SetCurrent(vel);
105 m_velocityMotor.Enabled = true;
106 });
107 }
108
109 // If a hover motor has not been created, create one and start the hovering.
110 private void ActivateAvatarMove()
111 {
112 if (m_velocityMotor == null)
113 {
114 // Infinite decay and timescale values so motor only changes current to target values.
115 m_velocityMotor = new BSVMotor("BSCharacter.Velocity",
116 0.2f, // time scale
117 BSMotor.Infinite, // decay time scale
118 BSMotor.InfiniteVector, // friction timescale
119 1f // efficiency
120 );
121 // _velocityMotor.PhysicsScene = PhysicsScene; // DEBUG DEBUG so motor will output detail log messages.
122
123 m_physicsScene.BeforeStep += Mover;
124 }
125 }
126
127 private void DeactivateAvatarMove()
128 {
129 if (m_velocityMotor != null)
130 {
131 m_physicsScene.BeforeStep -= Mover;
132 m_velocityMotor = null;
133 }
134 }
135
136 // Called just before the simulation step. Update the vertical position for hoverness.
137 private void Mover(float timeStep)
138 {
139 // Don't do movement while the object is selected.
140 if (!isActive)
141 return;
142
143 // TODO: Decide if the step parameters should be changed depending on the avatar's
144 // state (flying, colliding, ...). There is code in ODE to do this.
145
146 // COMMENTARY: when the user is making the avatar walk, except for falling, the velocity
147 // specified for the avatar is the one that should be used. For falling, if the avatar
148 // is not flying and is not colliding then it is presumed to be falling and the Z
149 // component is not fooled with (thus allowing gravity to do its thing).
150 // When the avatar is standing, though, the user has specified a velocity of zero and
151 // the avatar should be standing. But if the avatar is pushed by something in the world
152 // (raising elevator platform, moving vehicle, ...) the avatar should be allowed to
153 // move. Thus, the velocity cannot be forced to zero. The problem is that small velocity
154 // errors can creap in and the avatar will slowly float off in some direction.
155 // So, the problem is that, when an avatar is standing, we cannot tell creaping error
156 // from real pushing.
157 // The code below uses whether the collider is static or moving to decide whether to zero motion.
158
159 m_velocityMotor.Step(timeStep);
160 m_controllingPrim.IsStationary = false;
161
162 // If we're not supposed to be moving, make sure things are zero.
163 if (m_velocityMotor.ErrorIsZero() && m_velocityMotor.TargetValue == OMV.Vector3.Zero)
164 {
165 // The avatar shouldn't be moving
166 m_velocityMotor.Zero();
167
168 if (m_controllingPrim.IsColliding)
169 {
170 // If we are colliding with a stationary object, presume we're standing and don't move around
171 if (!m_controllingPrim.ColliderIsMoving)
172 {
173 m_physicsScene.DetailLog("{0},BSCharacter.MoveMotor,collidingWithStationary,zeroingMotion", m_controllingPrim.LocalID);
174 m_controllingPrim.IsStationary = true;
175 m_controllingPrim.ZeroMotion(true /* inTaintTime */);
176 }
177
178 // Standing has more friction on the ground
179 if (m_controllingPrim.Friction != BSParam.AvatarStandingFriction)
180 {
181 m_controllingPrim.Friction = BSParam.AvatarStandingFriction;
182 m_physicsScene.PE.SetFriction(m_controllingPrim.PhysBody, m_controllingPrim.Friction);
183 }
184 }
185 else
186 {
187 if (m_controllingPrim.Flying)
188 {
189 // Flying and not collising and velocity nearly zero.
190 m_controllingPrim.ZeroMotion(true /* inTaintTime */);
191 }
192 }
193
194 m_physicsScene.DetailLog("{0},BSCharacter.MoveMotor,taint,stopping,target={1},colliding={2}",
195 m_controllingPrim.LocalID, m_velocityMotor.TargetValue, m_controllingPrim.IsColliding);
196 }
197 else
198 {
199 // Supposed to be moving.
200 OMV.Vector3 stepVelocity = m_velocityMotor.CurrentValue;
201
202 if (m_controllingPrim.Friction != BSParam.AvatarFriction)
203 {
204 // Probably starting up walking. Set friction to moving friction.
205 m_controllingPrim.Friction = BSParam.AvatarFriction;
206 m_physicsScene.PE.SetFriction(m_controllingPrim.PhysBody, m_controllingPrim.Friction);
207 }
208
209 // If falling, we keep the world's downward vector no matter what the other axis specify.
210 // The check for RawVelocity.Z < 0 makes jumping work (temporary upward force).
211 if (!m_controllingPrim.Flying && !m_controllingPrim.IsColliding)
212 {
213 if (m_controllingPrim.RawVelocity.Z < 0)
214 stepVelocity.Z = m_controllingPrim.RawVelocity.Z;
215 // DetailLog("{0},BSCharacter.MoveMotor,taint,overrideStepZWithWorldZ,stepVel={1}", LocalID, stepVelocity);
216 }
217
218 // 'stepVelocity' is now the speed we'd like the avatar to move in. Turn that into an instantanous force.
219 OMV.Vector3 moveForce = (stepVelocity - m_controllingPrim.RawVelocity) * m_controllingPrim.Mass;
220
221 // Should we check for move force being small and forcing velocity to zero?
222
223 // Add special movement force to allow avatars to walk up stepped surfaces.
224 moveForce += WalkUpStairs();
225
226 m_physicsScene.DetailLog("{0},BSCharacter.MoveMotor,move,stepVel={1},vel={2},mass={3},moveForce={4}",
227 m_controllingPrim.LocalID, stepVelocity, m_controllingPrim.RawVelocity, m_controllingPrim.Mass, moveForce);
228 m_physicsScene.PE.ApplyCentralImpulse(m_controllingPrim.PhysBody, moveForce);
229 }
230 }
231
232 // Decide if the character is colliding with a low object and compute a force to pop the
233 // avatar up so it can walk up and over the low objects.
234 private OMV.Vector3 WalkUpStairs()
235 {
236 OMV.Vector3 ret = OMV.Vector3.Zero;
237
238 // This test is done if moving forward, not flying and is colliding with something.
239 // DetailLog("{0},BSCharacter.WalkUpStairs,IsColliding={1},flying={2},targSpeed={3},collisions={4}",
240 // LocalID, IsColliding, Flying, TargetSpeed, CollisionsLastTick.Count);
241 if (m_controllingPrim.IsColliding && !m_controllingPrim.Flying && m_controllingPrim.TargetVelocitySpeed > 0.1f /* && ForwardSpeed < 0.1f */)
242 {
243 // The range near the character's feet where we will consider stairs
244 float nearFeetHeightMin = m_controllingPrim.RawPosition.Z - (m_controllingPrim.Size.Z / 2f) + 0.05f;
245 float nearFeetHeightMax = nearFeetHeightMin + BSParam.AvatarStepHeight;
246
247 // Look for a collision point that is near the character's feet and is oriented the same as the charactor is
248 foreach (KeyValuePair<uint, ContactPoint> kvp in m_controllingPrim.CollisionsLastTick.m_objCollisionList)
249 {
250 // Don't care about collisions with the terrain
251 if (kvp.Key > m_physicsScene.TerrainManager.HighestTerrainID)
252 {
253 OMV.Vector3 touchPosition = kvp.Value.Position;
254 // DetailLog("{0},BSCharacter.WalkUpStairs,min={1},max={2},touch={3}",
255 // LocalID, nearFeetHeightMin, nearFeetHeightMax, touchPosition);
256 if (touchPosition.Z >= nearFeetHeightMin && touchPosition.Z <= nearFeetHeightMax)
257 {
258 // This contact is within the 'near the feet' range.
259 // The normal should be our contact point to the object so it is pointing away
260 // thus the difference between our facing orientation and the normal should be small.
261 OMV.Vector3 directionFacing = OMV.Vector3.UnitX * m_controllingPrim.RawOrientation;
262 OMV.Vector3 touchNormal = OMV.Vector3.Normalize(kvp.Value.SurfaceNormal);
263 float diff = Math.Abs(OMV.Vector3.Distance(directionFacing, touchNormal));
264 if (diff < BSParam.AvatarStepApproachFactor)
265 {
266 // Found the stairs contact point. Push up a little to raise the character.
267 float upForce = (touchPosition.Z - nearFeetHeightMin) * m_controllingPrim.Mass * BSParam.AvatarStepForceFactor;
268 ret = new OMV.Vector3(0f, 0f, upForce);
269
270 // Also move the avatar up for the new height
271 OMV.Vector3 displacement = new OMV.Vector3(0f, 0f, BSParam.AvatarStepHeight / 2f);
272 m_controllingPrim.ForcePosition = m_controllingPrim.RawPosition + displacement;
273 }
274 m_physicsScene.DetailLog("{0},BSCharacter.WalkUpStairs,touchPos={1},nearFeetMin={2},faceDir={3},norm={4},diff={5},ret={6}",
275 m_controllingPrim.LocalID, touchPosition, nearFeetHeightMin, directionFacing, touchNormal, diff, ret);
276 }
277 }
278 }
279 }
280
281 return ret;
282 }
283
284}
285}
286
287