aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Physics/BulletSPlugin/BSActorAvatarMove.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSActorAvatarMove.cs')
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSActorAvatarMove.cs409
1 files changed, 409 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..68bc1b9
--- /dev/null
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSActorAvatarMove.cs
@@ -0,0 +1,409 @@
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 // Set to true if we think we're going up stairs.
44 // This state is remembered because collisions will turn on and off as we go up stairs.
45 int m_walkingUpStairs;
46 // The amount the step up is applying. Used to smooth stair walking.
47 float m_lastStepUp;
48
49 // Jumping happens over several frames. If use applies up force while colliding, start the
50 // jump and allow the jump to continue for this number of frames.
51 int m_jumpFrames = 0;
52 float m_jumpVelocity = 0f;
53
54 public BSActorAvatarMove(BSScene physicsScene, BSPhysObject pObj, string actorName)
55 : base(physicsScene, pObj, actorName)
56 {
57 m_velocityMotor = null;
58 m_walkingUpStairs = 0;
59 m_physicsScene.DetailLog("{0},BSActorAvatarMove,constructor", m_controllingPrim.LocalID);
60 }
61
62 // BSActor.isActive
63 public override bool isActive
64 {
65 get { return Enabled && m_controllingPrim.IsPhysicallyActive; }
66 }
67
68 // Release any connections and resources used by the actor.
69 // BSActor.Dispose()
70 public override void Dispose()
71 {
72 base.SetEnabled(false);
73 // Now that turned off, remove any state we have in the scene.
74 Refresh();
75 }
76
77 // Called when physical parameters (properties set in Bullet) need to be re-applied.
78 // Called at taint-time.
79 // BSActor.Refresh()
80 public override void Refresh()
81 {
82 m_physicsScene.DetailLog("{0},BSActorAvatarMove,refresh", m_controllingPrim.LocalID);
83
84 // If the object is physically active, add the hoverer prestep action
85 if (isActive)
86 {
87 ActivateAvatarMove();
88 }
89 else
90 {
91 DeactivateAvatarMove();
92 }
93 }
94
95 // The object's physical representation is being rebuilt so pick up any physical dependencies (constraints, ...).
96 // Register a prestep action to restore physical requirements before the next simulation step.
97 // Called at taint-time.
98 // BSActor.RemoveDependencies()
99 public override void RemoveDependencies()
100 {
101 // Nothing to do for the hoverer since it is all software at pre-step action time.
102 }
103
104 // Usually called when target velocity changes to set the current velocity and the target
105 // into the movement motor.
106 public void SetVelocityAndTarget(OMV.Vector3 vel, OMV.Vector3 targ, bool inTaintTime)
107 {
108 m_physicsScene.TaintedObject(inTaintTime, "BSActorAvatarMove.setVelocityAndTarget", delegate()
109 {
110 if (m_velocityMotor != null)
111 {
112 m_velocityMotor.Reset();
113 m_velocityMotor.SetTarget(targ);
114 m_velocityMotor.SetCurrent(vel);
115 m_velocityMotor.Enabled = true;
116 }
117 });
118 }
119
120 // If a hover motor has not been created, create one and start the hovering.
121 private void ActivateAvatarMove()
122 {
123 if (m_velocityMotor == null)
124 {
125 // Infinite decay and timescale values so motor only changes current to target values.
126 m_velocityMotor = new BSVMotor("BSCharacter.Velocity",
127 0.2f, // time scale
128 BSMotor.Infinite, // decay time scale
129 1f // efficiency
130 );
131 // _velocityMotor.PhysicsScene = PhysicsScene; // DEBUG DEBUG so motor will output detail log messages.
132 SetVelocityAndTarget(m_controllingPrim.RawVelocity, m_controllingPrim.TargetVelocity, true /* inTaintTime */);
133
134 m_physicsScene.BeforeStep += Mover;
135 m_controllingPrim.OnPreUpdateProperty += Process_OnPreUpdateProperty;
136
137 m_walkingUpStairs = 0;
138 }
139 }
140
141 private void DeactivateAvatarMove()
142 {
143 if (m_velocityMotor != null)
144 {
145 m_controllingPrim.OnPreUpdateProperty -= Process_OnPreUpdateProperty;
146 m_physicsScene.BeforeStep -= Mover;
147 m_velocityMotor = null;
148 }
149 }
150
151 // Called just before the simulation step. Update the vertical position for hoverness.
152 private void Mover(float timeStep)
153 {
154 // Don't do movement while the object is selected.
155 if (!isActive)
156 return;
157
158 // TODO: Decide if the step parameters should be changed depending on the avatar's
159 // state (flying, colliding, ...). There is code in ODE to do this.
160
161 // COMMENTARY: when the user is making the avatar walk, except for falling, the velocity
162 // specified for the avatar is the one that should be used. For falling, if the avatar
163 // is not flying and is not colliding then it is presumed to be falling and the Z
164 // component is not fooled with (thus allowing gravity to do its thing).
165 // When the avatar is standing, though, the user has specified a velocity of zero and
166 // the avatar should be standing. But if the avatar is pushed by something in the world
167 // (raising elevator platform, moving vehicle, ...) the avatar should be allowed to
168 // move. Thus, the velocity cannot be forced to zero. The problem is that small velocity
169 // errors can creap in and the avatar will slowly float off in some direction.
170 // So, the problem is that, when an avatar is standing, we cannot tell creaping error
171 // from real pushing.
172 // The code below uses whether the collider is static or moving to decide whether to zero motion.
173
174 m_velocityMotor.Step(timeStep);
175 m_controllingPrim.IsStationary = false;
176
177 // If we're not supposed to be moving, make sure things are zero.
178 if (m_velocityMotor.ErrorIsZero() && m_velocityMotor.TargetValue == OMV.Vector3.Zero)
179 {
180 // The avatar shouldn't be moving
181 m_velocityMotor.Zero();
182
183 if (m_controllingPrim.IsColliding)
184 {
185 // If we are colliding with a stationary object, presume we're standing and don't move around
186 if (!m_controllingPrim.ColliderIsMoving && !m_controllingPrim.ColliderIsVolumeDetect)
187 {
188 m_physicsScene.DetailLog("{0},BSCharacter.MoveMotor,collidingWithStationary,zeroingMotion", m_controllingPrim.LocalID);
189 m_controllingPrim.IsStationary = true;
190 m_controllingPrim.ZeroMotion(true /* inTaintTime */);
191 }
192
193 // Standing has more friction on the ground
194 if (m_controllingPrim.Friction != BSParam.AvatarStandingFriction)
195 {
196 m_controllingPrim.Friction = BSParam.AvatarStandingFriction;
197 m_physicsScene.PE.SetFriction(m_controllingPrim.PhysBody, m_controllingPrim.Friction);
198 }
199 }
200 else
201 {
202 if (m_controllingPrim.Flying)
203 {
204 // Flying and not colliding and velocity nearly zero.
205 m_controllingPrim.ZeroMotion(true /* inTaintTime */);
206 }
207 }
208
209 m_physicsScene.DetailLog("{0},BSCharacter.MoveMotor,taint,stopping,target={1},colliding={2}",
210 m_controllingPrim.LocalID, m_velocityMotor.TargetValue, m_controllingPrim.IsColliding);
211 }
212 else
213 {
214 // Supposed to be moving.
215 OMV.Vector3 stepVelocity = m_velocityMotor.CurrentValue;
216
217 if (m_controllingPrim.Friction != BSParam.AvatarFriction)
218 {
219 // Probably starting to walk. Set friction to moving friction.
220 m_controllingPrim.Friction = BSParam.AvatarFriction;
221 m_physicsScene.PE.SetFriction(m_controllingPrim.PhysBody, m_controllingPrim.Friction);
222 }
223
224 if (!m_controllingPrim.Flying && !m_controllingPrim.IsColliding)
225 {
226 stepVelocity.Z = m_controllingPrim.RawVelocity.Z;
227 }
228
229
230 // Colliding and not flying with an upward force. The avatar must be trying to jump.
231 if (!m_controllingPrim.Flying && m_controllingPrim.IsColliding && stepVelocity.Z > 0)
232 {
233 // We allow the upward force to happen for this many frames.
234 m_jumpFrames = BSParam.AvatarJumpFrames;
235 m_jumpVelocity = stepVelocity.Z;
236 }
237
238 // The case where the avatar is not colliding and is not flying is special.
239 // The avatar is either falling or jumping and the user can be applying force to the avatar
240 // (force in some direction or force up or down).
241 // If the avatar has negative Z velocity and is not colliding, presume we're falling and keep the velocity.
242 // If the user is trying to apply upward force but we're not colliding, assume the avatar
243 // is trying to jump and don't apply the upward force if not touching the ground any more.
244 if (!m_controllingPrim.Flying && !m_controllingPrim.IsColliding)
245 {
246 // If upward velocity is being applied, this must be a jump and only allow that to go on so long
247 if (m_jumpFrames > 0)
248 {
249 // Since not touching the ground, only apply upward force for so long.
250 m_jumpFrames--;
251 stepVelocity.Z = m_jumpVelocity;
252 }
253 else
254 {
255 // Since we're not affected by anything, whatever vertical motion the avatar has, continue that.
256 stepVelocity.Z = m_controllingPrim.RawVelocity.Z;
257 }
258 // DetailLog("{0},BSCharacter.MoveMotor,taint,overrideStepZWithWorldZ,stepVel={1}", LocalID, stepVelocity);
259 }
260
261 // 'stepVelocity' is now the speed we'd like the avatar to move in. Turn that into an instantanous force.
262 OMV.Vector3 moveForce = (stepVelocity - m_controllingPrim.RawVelocity) * m_controllingPrim.Mass;
263
264 // Add special movement force to allow avatars to walk up stepped surfaces.
265 moveForce += WalkUpStairs();
266
267 m_physicsScene.DetailLog("{0},BSCharacter.MoveMotor,move,stepVel={1},vel={2},mass={3},moveForce={4}",
268 m_controllingPrim.LocalID, stepVelocity, m_controllingPrim.RawVelocity, m_controllingPrim.Mass, moveForce);
269 m_physicsScene.PE.ApplyCentralImpulse(m_controllingPrim.PhysBody, moveForce);
270 }
271 }
272
273 // Called just as the property update is received from the physics engine.
274 // Do any mode necessary for avatar movement.
275 private void Process_OnPreUpdateProperty(ref EntityProperties entprop)
276 {
277 // Don't change position if standing on a stationary object.
278 if (m_controllingPrim.IsStationary)
279 {
280 entprop.Position = m_controllingPrim.RawPosition;
281 m_physicsScene.PE.SetTranslation(m_controllingPrim.PhysBody, entprop.Position, entprop.Rotation);
282 }
283
284 }
285
286 // Decide if the character is colliding with a low object and compute a force to pop the
287 // avatar up so it can walk up and over the low objects.
288 private OMV.Vector3 WalkUpStairs()
289 {
290 OMV.Vector3 ret = OMV.Vector3.Zero;
291
292 m_physicsScene.DetailLog("{0},BSCharacter.WalkUpStairs,IsColliding={1},flying={2},targSpeed={3},collisions={4},avHeight={5}",
293 m_controllingPrim.LocalID, m_controllingPrim.IsColliding, m_controllingPrim.Flying,
294 m_controllingPrim.TargetVelocitySpeed, m_controllingPrim.CollisionsLastTick.Count, m_controllingPrim.Size.Z);
295
296 // Check for stairs climbing if colliding, not flying and moving forward
297 if ( m_controllingPrim.IsColliding
298 && !m_controllingPrim.Flying
299 && m_controllingPrim.TargetVelocitySpeed > 0.1f )
300 {
301 // The range near the character's feet where we will consider stairs
302 // float nearFeetHeightMin = m_controllingPrim.RawPosition.Z - (m_controllingPrim.Size.Z / 2f) + 0.05f;
303 // Note: there is a problem with the computation of the capsule height. Thus RawPosition is off
304 // from the height. Revisit size and this computation when height is scaled properly.
305 float nearFeetHeightMin = m_controllingPrim.RawPosition.Z - (m_controllingPrim.Size.Z / 2f) - 0.05f;
306 float nearFeetHeightMax = nearFeetHeightMin + BSParam.AvatarStepHeight;
307
308 // Look for a collision point that is near the character's feet and is oriented the same as the charactor is.
309 // Find the highest 'good' collision.
310 OMV.Vector3 highestTouchPosition = OMV.Vector3.Zero;
311 foreach (KeyValuePair<uint, ContactPoint> kvp in m_controllingPrim.CollisionsLastTick.m_objCollisionList)
312 {
313 // Don't care about collisions with the terrain
314 if (kvp.Key > m_physicsScene.TerrainManager.HighestTerrainID)
315 {
316 BSPhysObject collisionObject;
317 if (m_physicsScene.PhysObjects.TryGetValue(kvp.Key, out collisionObject))
318 {
319 if (!collisionObject.IsVolumeDetect)
320 {
321 OMV.Vector3 touchPosition = kvp.Value.Position;
322 m_physicsScene.DetailLog("{0},BSCharacter.WalkUpStairs,min={1},max={2},touch={3}",
323 m_controllingPrim.LocalID, nearFeetHeightMin, nearFeetHeightMax, touchPosition);
324 if (touchPosition.Z >= nearFeetHeightMin && touchPosition.Z <= nearFeetHeightMax)
325 {
326 // This contact is within the 'near the feet' range.
327 // The normal should be our contact point to the object so it is pointing away
328 // thus the difference between our facing orientation and the normal should be small.
329 OMV.Vector3 directionFacing = OMV.Vector3.UnitX * m_controllingPrim.RawOrientation;
330 OMV.Vector3 touchNormal = OMV.Vector3.Normalize(kvp.Value.SurfaceNormal);
331 float diff = Math.Abs(OMV.Vector3.Distance(directionFacing, touchNormal));
332 if (diff < BSParam.AvatarStepApproachFactor)
333 {
334 if (highestTouchPosition.Z < touchPosition.Z)
335 highestTouchPosition = touchPosition;
336 }
337 }
338 }
339 }
340 }
341 }
342 m_walkingUpStairs = 0;
343 // If there is a good step sensing, move the avatar over the step.
344 if (highestTouchPosition != OMV.Vector3.Zero)
345 {
346 // Remember that we are going up stairs. This is needed because collisions
347 // will stop when we move up so this smoothes out that effect.
348 m_walkingUpStairs = BSParam.AvatarStepSmoothingSteps;
349
350 m_lastStepUp = highestTouchPosition.Z - nearFeetHeightMin;
351 ret = ComputeStairCorrection(m_lastStepUp);
352 m_physicsScene.DetailLog("{0},BSCharacter.WalkUpStairs,touchPos={1},nearFeetMin={2},ret={3}",
353 m_controllingPrim.LocalID, highestTouchPosition, nearFeetHeightMin, ret);
354 }
355 }
356 else
357 {
358 // If we used to be going up stairs but are not now, smooth the case where collision goes away while
359 // we are bouncing up the stairs.
360 if (m_walkingUpStairs > 0)
361 {
362 m_walkingUpStairs--;
363 ret = ComputeStairCorrection(m_lastStepUp);
364 }
365 }
366
367 return ret;
368 }
369
370 private OMV.Vector3 ComputeStairCorrection(float stepUp)
371 {
372 OMV.Vector3 ret = OMV.Vector3.Zero;
373 OMV.Vector3 displacement = OMV.Vector3.Zero;
374
375 if (stepUp > 0f)
376 {
377 // Found the stairs contact point. Push up a little to raise the character.
378 if (BSParam.AvatarStepForceFactor > 0f)
379 {
380 float upForce = stepUp * m_controllingPrim.Mass * BSParam.AvatarStepForceFactor;
381 ret = new OMV.Vector3(0f, 0f, upForce);
382 }
383
384 // Also move the avatar up for the new height
385 if (BSParam.AvatarStepUpCorrectionFactor > 0f)
386 {
387 // Move the avatar up related to the height of the collision
388 displacement = new OMV.Vector3(0f, 0f, stepUp * BSParam.AvatarStepUpCorrectionFactor);
389 m_controllingPrim.ForcePosition = m_controllingPrim.RawPosition + displacement;
390 }
391 else
392 {
393 if (BSParam.AvatarStepUpCorrectionFactor < 0f)
394 {
395 // Move the avatar up about the specified step height
396 displacement = new OMV.Vector3(0f, 0f, BSParam.AvatarStepHeight);
397 m_controllingPrim.ForcePosition = m_controllingPrim.RawPosition + displacement;
398 }
399 }
400 m_physicsScene.DetailLog("{0},BSCharacter.WalkUpStairs.ComputeStairCorrection,disp={1},force={2}",
401 m_controllingPrim.LocalID, displacement, ret);
402
403 }
404 return ret;
405 }
406}
407}
408
409