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