aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Physics/BulletSPlugin/BSCharacter.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSCharacter.cs')
-rw-r--r--OpenSim/Region/Physics/BulletSPlugin/BSCharacter.cs478
1 files changed, 338 insertions, 140 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSCharacter.cs b/OpenSim/Region/Physics/BulletSPlugin/BSCharacter.cs
index 4c195e1..478aeab 100644
--- a/OpenSim/Region/Physics/BulletSPlugin/BSCharacter.cs
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSCharacter.cs
@@ -58,17 +58,16 @@ public sealed class BSCharacter : BSPhysObject
58 private bool _flying; 58 private bool _flying;
59 private bool _setAlwaysRun; 59 private bool _setAlwaysRun;
60 private bool _throttleUpdates; 60 private bool _throttleUpdates;
61 private bool _isColliding;
62 private bool _collidingObj;
63 private bool _floatOnWater; 61 private bool _floatOnWater;
64 private OMV.Vector3 _rotationalVelocity; 62 private OMV.Vector3 _rotationalVelocity;
65 private bool _kinematic; 63 private bool _kinematic;
66 private float _buoyancy; 64 private float _buoyancy;
67 65
68 // The friction and velocity of the avatar is modified depending on whether walking or not. 66 // The friction and velocity of the avatar is modified depending on whether walking or not.
69 private OMV.Vector3 _appliedVelocity; // the last velocity applied to the avatar
70 private float _currentFriction; // the friction currently being used (changed by setVelocity). 67 private float _currentFriction; // the friction currently being used (changed by setVelocity).
71 68
69 private BSVMotor _velocityMotor;
70
72 private OMV.Vector3 _PIDTarget; 71 private OMV.Vector3 _PIDTarget;
73 private bool _usePID; 72 private bool _usePID;
74 private float _PIDTau; 73 private float _PIDTau;
@@ -83,34 +82,36 @@ public sealed class BSCharacter : BSPhysObject
83 _physicsActorType = (int)ActorTypes.Agent; 82 _physicsActorType = (int)ActorTypes.Agent;
84 _position = pos; 83 _position = pos;
85 84
86 // Old versions of ScenePresence passed only the height. If width and/or depth are zero,
87 // replace with the default values.
88 _size = size;
89 if (_size.X == 0f) _size.X = PhysicsScene.Params.avatarCapsuleDepth;
90 if (_size.Y == 0f) _size.Y = PhysicsScene.Params.avatarCapsuleWidth;
91
92 _flying = isFlying; 85 _flying = isFlying;
93 _orientation = OMV.Quaternion.Identity; 86 _orientation = OMV.Quaternion.Identity;
94 _velocity = OMV.Vector3.Zero; 87 _velocity = OMV.Vector3.Zero;
95 _appliedVelocity = OMV.Vector3.Zero;
96 _buoyancy = ComputeBuoyancyFromFlying(isFlying); 88 _buoyancy = ComputeBuoyancyFromFlying(isFlying);
97 _currentFriction = PhysicsScene.Params.avatarStandingFriction; 89 _currentFriction = BSParam.AvatarStandingFriction;
98 _avatarDensity = PhysicsScene.Params.avatarDensity; 90 _avatarDensity = BSParam.AvatarDensity;
91
92 // Old versions of ScenePresence passed only the height. If width and/or depth are zero,
93 // replace with the default values.
94 _size = size;
95 if (_size.X == 0f) _size.X = BSParam.AvatarCapsuleDepth;
96 if (_size.Y == 0f) _size.Y = BSParam.AvatarCapsuleWidth;
99 97
100 // The dimensions of the avatar capsule are kept in the scale. 98 // The dimensions of the physical capsule are kept in the scale.
101 // Physics creates a unit capsule which is scaled by the physics engine. 99 // Physics creates a unit capsule which is scaled by the physics engine.
102 ComputeAvatarScale(_size); 100 Scale = ComputeAvatarScale(_size);
103 // set _avatarVolume and _mass based on capsule size, _density and Scale 101 // set _avatarVolume and _mass based on capsule size, _density and Scale
104 ComputeAvatarVolumeAndMass(); 102 ComputeAvatarVolumeAndMass();
103
104 SetupMovementMotor();
105
105 DetailLog("{0},BSCharacter.create,call,size={1},scale={2},density={3},volume={4},mass={5}", 106 DetailLog("{0},BSCharacter.create,call,size={1},scale={2},density={3},volume={4},mass={5}",
106 LocalID, _size, Scale, _avatarDensity, _avatarVolume, RawMass); 107 LocalID, _size, Scale, _avatarDensity, _avatarVolume, RawMass);
107 108
108 // do actual create at taint time 109 // do actual creation in taint time
109 PhysicsScene.TaintedObject("BSCharacter.create", delegate() 110 PhysicsScene.TaintedObject("BSCharacter.create", delegate()
110 { 111 {
111 DetailLog("{0},BSCharacter.create,taint", LocalID); 112 DetailLog("{0},BSCharacter.create,taint", LocalID);
112 // New body and shape into PhysBody and PhysShape 113 // New body and shape into PhysBody and PhysShape
113 PhysicsScene.Shapes.GetBodyAndShape(true, PhysicsScene.World, this, null, null); 114 PhysicsScene.Shapes.GetBodyAndShape(true, PhysicsScene.World, this);
114 115
115 SetPhysicalProperties(); 116 SetPhysicalProperties();
116 }); 117 });
@@ -120,54 +121,155 @@ public sealed class BSCharacter : BSPhysObject
120 // called when this character is being destroyed and the resources should be released 121 // called when this character is being destroyed and the resources should be released
121 public override void Destroy() 122 public override void Destroy()
122 { 123 {
124 base.Destroy();
125
123 DetailLog("{0},BSCharacter.Destroy", LocalID); 126 DetailLog("{0},BSCharacter.Destroy", LocalID);
124 PhysicsScene.TaintedObject("BSCharacter.destroy", delegate() 127 PhysicsScene.TaintedObject("BSCharacter.destroy", delegate()
125 { 128 {
126 PhysicsScene.Shapes.DereferenceBody(PhysBody, true, null); 129 PhysicsScene.Shapes.DereferenceBody(PhysBody, true /* inTaintTime */, null /* bodyCallback */);
127 PhysicsScene.Shapes.DereferenceShape(PhysShape, true, null); 130 PhysBody.Clear();
131 PhysicsScene.Shapes.DereferenceShape(PhysShape, true /* inTaintTime */, null /* bodyCallback */);
132 PhysShape.Clear();
128 }); 133 });
129 } 134 }
130 135
131 private void SetPhysicalProperties() 136 private void SetPhysicalProperties()
132 { 137 {
133 BulletSimAPI.RemoveObjectFromWorld2(PhysicsScene.World.ptr, PhysBody.ptr); 138 PhysicsScene.PE.RemoveObjectFromWorld(PhysicsScene.World, PhysBody);
134 139
135 ZeroMotion(true); 140 ZeroMotion(true);
136 ForcePosition = _position; 141 ForcePosition = _position;
142
137 // Set the velocity and compute the proper friction 143 // Set the velocity and compute the proper friction
144 _velocityMotor.Reset();
145 _velocityMotor.SetTarget(_velocity);
146 _velocityMotor.SetCurrent(_velocity);
138 ForceVelocity = _velocity; 147 ForceVelocity = _velocity;
139 148
140 // This will enable or disable the flying buoyancy of the avatar. 149 // This will enable or disable the flying buoyancy of the avatar.
141 // Needs to be reset especially when an avatar is recreated after crossing a region boundry. 150 // Needs to be reset especially when an avatar is recreated after crossing a region boundry.
142 Flying = _flying; 151 Flying = _flying;
143 152
144 BulletSimAPI.SetRestitution2(PhysBody.ptr, PhysicsScene.Params.avatarRestitution); 153 PhysicsScene.PE.SetRestitution(PhysBody, BSParam.AvatarRestitution);
145 BulletSimAPI.SetMargin2(PhysShape.ptr, PhysicsScene.Params.collisionMargin); 154 PhysicsScene.PE.SetMargin(PhysShape, PhysicsScene.Params.collisionMargin);
146 BulletSimAPI.SetLocalScaling2(PhysShape.ptr, Scale); 155 PhysicsScene.PE.SetLocalScaling(PhysShape, Scale);
147 BulletSimAPI.SetContactProcessingThreshold2(PhysBody.ptr, PhysicsScene.Params.contactProcessingThreshold); 156 PhysicsScene.PE.SetContactProcessingThreshold(PhysBody, BSParam.ContactProcessingThreshold);
148 if (PhysicsScene.Params.ccdMotionThreshold > 0f) 157 if (BSParam.CcdMotionThreshold > 0f)
149 { 158 {
150 BulletSimAPI.SetCcdMotionThreshold2(PhysBody.ptr, PhysicsScene.Params.ccdMotionThreshold); 159 PhysicsScene.PE.SetCcdMotionThreshold(PhysBody, BSParam.CcdMotionThreshold);
151 BulletSimAPI.SetCcdSweptSphereRadius2(PhysBody.ptr, PhysicsScene.Params.ccdSweptSphereRadius); 160 PhysicsScene.PE.SetCcdSweptSphereRadius(PhysBody, BSParam.CcdSweptSphereRadius);
152 } 161 }
153 162
154 UpdatePhysicalMassProperties(RawMass); 163 UpdatePhysicalMassProperties(RawMass, false);
155 164
156 // Make so capsule does not fall over 165 // Make so capsule does not fall over
157 BulletSimAPI.SetAngularFactorV2(PhysBody.ptr, OMV.Vector3.Zero); 166 PhysicsScene.PE.SetAngularFactorV(PhysBody, OMV.Vector3.Zero);
158 167
159 BulletSimAPI.AddToCollisionFlags2(PhysBody.ptr, CollisionFlags.CF_CHARACTER_OBJECT); 168 PhysicsScene.PE.AddToCollisionFlags(PhysBody, CollisionFlags.CF_CHARACTER_OBJECT);
160 169
161 BulletSimAPI.AddObjectToWorld2(PhysicsScene.World.ptr, PhysBody.ptr); 170 PhysicsScene.PE.AddObjectToWorld(PhysicsScene.World, PhysBody);
162 171
163 // BulletSimAPI.ForceActivationState2(BSBody.ptr, ActivationState.ACTIVE_TAG); 172 // PhysicsScene.PE.ForceActivationState(PhysBody, ActivationState.ACTIVE_TAG);
164 BulletSimAPI.ForceActivationState2(PhysBody.ptr, ActivationState.DISABLE_DEACTIVATION); 173 PhysicsScene.PE.ForceActivationState(PhysBody, ActivationState.DISABLE_DEACTIVATION);
165 BulletSimAPI.UpdateSingleAabb2(PhysicsScene.World.ptr, PhysBody.ptr); 174 PhysicsScene.PE.UpdateSingleAabb(PhysicsScene.World, PhysBody);
166 175
167 // Do this after the object has been added to the world 176 // Do this after the object has been added to the world
168 BulletSimAPI.SetCollisionFilterMask2(PhysBody.ptr, 177 PhysBody.collisionType = CollisionType.Avatar;
169 (uint)CollisionFilterGroups.AvatarFilter, 178 PhysBody.ApplyCollisionMask(PhysicsScene);
170 (uint)CollisionFilterGroups.AvatarMask); 179 }
180
181 // The avatar's movement is controlled by this motor that speeds up and slows down
182 // the avatar seeking to reach the motor's target speed.
183 // This motor runs as a prestep action for the avatar so it will keep the avatar
184 // standing as well as moving. Destruction of the avatar will destroy the pre-step action.
185 private void SetupMovementMotor()
186 {
187 // Infinite decay and timescale values so motor only changes current to target values.
188 _velocityMotor = new BSVMotor("BSCharacter.Velocity",
189 0.2f, // time scale
190 BSMotor.Infinite, // decay time scale
191 BSMotor.InfiniteVector, // friction timescale
192 1f // efficiency
193 );
194 // _velocityMotor.PhysicsScene = PhysicsScene; // DEBUG DEBUG so motor will output detail log messages.
195
196 RegisterPreStepAction("BSCharactor.Movement", LocalID, delegate(float timeStep)
197 {
198 // TODO: Decide if the step parameters should be changed depending on the avatar's
199 // state (flying, colliding, ...). There is code in ODE to do this.
200
201 OMV.Vector3 stepVelocity = _velocityMotor.Step(timeStep);
202
203 // If falling, we keep the world's downward vector no matter what the other axis specify.
204 if (!Flying && !IsColliding)
205 {
206 stepVelocity.Z = _velocity.Z;
207 // DetailLog("{0},BSCharacter.MoveMotor,taint,overrideStepZWithWorldZ,stepVel={1}", LocalID, stepVelocity);
208 }
209
210 // 'stepVelocity' is now the speed we'd like the avatar to move in. Turn that into an instantanous force.
211 OMV.Vector3 moveForce = (stepVelocity - _velocity) * Mass;
212
213 // Should we check for move force being small and forcing velocity to zero?
214
215 // Add special movement force to allow avatars to walk up stepped surfaces.
216 moveForce += WalkUpStairs();
217
218 // DetailLog("{0},BSCharacter.MoveMotor,move,stepVel={1},vel={2},mass={3},moveForce={4}", LocalID, stepVelocity, _velocity, Mass, moveForce);
219 PhysicsScene.PE.ApplyCentralImpulse(PhysBody, moveForce);
220 });
221 }
222
223 // Decide of the character is colliding with a low object and compute a force to pop the
224 // avatar up so it has a chance of walking up and over the low object.
225 private OMV.Vector3 WalkUpStairs()
226 {
227 OMV.Vector3 ret = OMV.Vector3.Zero;
228
229 // This test is done if moving forward, not flying and is colliding with something.
230 // DetailLog("{0},BSCharacter.WalkUpStairs,IsColliding={1},flying={2},targSpeed={3},collisions={4}",
231 // LocalID, IsColliding, Flying, TargetSpeed, CollisionsLastTick.Count);
232 if (IsColliding && !Flying && TargetSpeed > 0.1f /* && ForwardSpeed < 0.1f */)
233 {
234 // The range near the character's feet where we will consider stairs
235 float nearFeetHeightMin = RawPosition.Z - (Size.Z / 2f) + 0.05f;
236 float nearFeetHeightMax = nearFeetHeightMin + BSParam.AvatarStepHeight;
237
238 // Look for a collision point that is near the character's feet and is oriented the same as the charactor is
239 foreach (KeyValuePair<uint, ContactPoint> kvp in CollisionsLastTick.m_objCollisionList)
240 {
241 // Don't care about collisions with the terrain
242 if (kvp.Key > PhysicsScene.TerrainManager.HighestTerrainID)
243 {
244 OMV.Vector3 touchPosition = kvp.Value.Position;
245 // DetailLog("{0},BSCharacter.WalkUpStairs,min={1},max={2},touch={3}",
246 // LocalID, nearFeetHeightMin, nearFeetHeightMax, touchPosition);
247 if (touchPosition.Z >= nearFeetHeightMin && touchPosition.Z <= nearFeetHeightMax)
248 {
249 // This contact is within the 'near the feet' range.
250 // The normal should be our contact point to the object so it is pointing away
251 // thus the difference between our facing orientation and the normal should be small.
252 OMV.Vector3 directionFacing = OMV.Vector3.UnitX * RawOrientation;
253 OMV.Vector3 touchNormal = OMV.Vector3.Normalize(kvp.Value.SurfaceNormal);
254 float diff = Math.Abs(OMV.Vector3.Distance(directionFacing, touchNormal));
255 if (diff < BSParam.AvatarStepApproachFactor)
256 {
257 // Found the stairs contact point. Push up a little to raise the character.
258 float upForce = (touchPosition.Z - nearFeetHeightMin) * Mass * BSParam.AvatarStepForceFactor;
259 ret = new OMV.Vector3(0f, 0f, upForce);
260
261 // Also move the avatar up for the new height
262 OMV.Vector3 displacement = new OMV.Vector3(0f, 0f, BSParam.AvatarStepHeight / 2f);
263 ForcePosition = RawPosition + displacement;
264 }
265 DetailLog("{0},BSCharacter.WalkUpStairs,touchPos={1},nearFeetMin={2},faceDir={3},norm={4},diff={5},ret={6}",
266 LocalID, touchPosition, nearFeetHeightMin, directionFacing, touchNormal, diff, ret);
267 }
268 }
269 }
270 }
271
272 return ret;
171 } 273 }
172 274
173 public override void RequestPhysicsterseUpdate() 275 public override void RequestPhysicsterseUpdate()
@@ -185,24 +287,31 @@ public sealed class BSCharacter : BSPhysObject
185 } 287 }
186 288
187 set { 289 set {
188 // When an avatar's size is set, only the height is changed.
189 _size = value; 290 _size = value;
190 ComputeAvatarScale(_size); 291 // Old versions of ScenePresence passed only the height. If width and/or depth are zero,
292 // replace with the default values.
293 if (_size.X == 0f) _size.X = BSParam.AvatarCapsuleDepth;
294 if (_size.Y == 0f) _size.Y = BSParam.AvatarCapsuleWidth;
295
296 Scale = ComputeAvatarScale(_size);
191 ComputeAvatarVolumeAndMass(); 297 ComputeAvatarVolumeAndMass();
192 DetailLog("{0},BSCharacter.setSize,call,size={1},scale={2},density={3},volume={4},mass={5}", 298 DetailLog("{0},BSCharacter.setSize,call,size={1},scale={2},density={3},volume={4},mass={5}",
193 LocalID, _size, Scale, _avatarDensity, _avatarVolume, RawMass); 299 LocalID, _size, Scale, _avatarDensity, _avatarVolume, RawMass);
194 300
195 PhysicsScene.TaintedObject("BSCharacter.setSize", delegate() 301 PhysicsScene.TaintedObject("BSCharacter.setSize", delegate()
196 { 302 {
197 BulletSimAPI.SetLocalScaling2(PhysShape.ptr, Scale); 303 if (PhysBody.HasPhysicalBody && PhysShape.HasPhysicalShape)
198 UpdatePhysicalMassProperties(RawMass); 304 {
305 PhysicsScene.PE.SetLocalScaling(PhysShape, Scale);
306 UpdatePhysicalMassProperties(RawMass, true);
307 // Make sure this change appears as a property update event
308 PhysicsScene.PE.PushUpdate(PhysBody);
309 }
199 }); 310 });
200 311
201 } 312 }
202 } 313 }
203 314
204 public override OMV.Vector3 Scale { get; set; }
205
206 public override PrimitiveBaseShape Shape 315 public override PrimitiveBaseShape Shape
207 { 316 {
208 set { BaseShape = value; } 317 set { BaseShape = value; }
@@ -219,6 +328,10 @@ public sealed class BSCharacter : BSPhysObject
219 public override bool Selected { 328 public override bool Selected {
220 set { _selected = value; } 329 set { _selected = value; }
221 } 330 }
331 public override bool IsSelected
332 {
333 get { return _selected; }
334 }
222 public override void CrossingFailure() { return; } 335 public override void CrossingFailure() { return; }
223 public override void link(PhysicsActor obj) { return; } 336 public override void link(PhysicsActor obj) { return; }
224 public override void delink() { return; } 337 public override void delink() { return; }
@@ -236,7 +349,8 @@ public sealed class BSCharacter : BSPhysObject
236 // Zero some other properties directly into the physics engine 349 // Zero some other properties directly into the physics engine
237 PhysicsScene.TaintedObject(inTaintTime, "BSCharacter.ZeroMotion", delegate() 350 PhysicsScene.TaintedObject(inTaintTime, "BSCharacter.ZeroMotion", delegate()
238 { 351 {
239 BulletSimAPI.ClearAllForces2(PhysBody.ptr); 352 if (PhysBody.HasPhysicalBody)
353 PhysicsScene.PE.ClearAllForces(PhysBody);
240 }); 354 });
241 } 355 }
242 public override void ZeroAngularMotion(bool inTaintTime) 356 public override void ZeroAngularMotion(bool inTaintTime)
@@ -245,10 +359,13 @@ public sealed class BSCharacter : BSPhysObject
245 359
246 PhysicsScene.TaintedObject(inTaintTime, "BSCharacter.ZeroMotion", delegate() 360 PhysicsScene.TaintedObject(inTaintTime, "BSCharacter.ZeroMotion", delegate()
247 { 361 {
248 BulletSimAPI.SetInterpolationAngularVelocity2(PhysBody.ptr, OMV.Vector3.Zero); 362 if (PhysBody.HasPhysicalBody)
249 BulletSimAPI.SetAngularVelocity2(PhysBody.ptr, OMV.Vector3.Zero); 363 {
250 // The next also get rid of applied linear force but the linear velocity is untouched. 364 PhysicsScene.PE.SetInterpolationAngularVelocity(PhysBody, OMV.Vector3.Zero);
251 BulletSimAPI.ClearForces2(PhysBody.ptr); 365 PhysicsScene.PE.SetAngularVelocity(PhysBody, OMV.Vector3.Zero);
366 // The next also get rid of applied linear force but the linear velocity is untouched.
367 PhysicsScene.PE.ClearForces(PhysBody);
368 }
252 }); 369 });
253 } 370 }
254 371
@@ -263,29 +380,31 @@ public sealed class BSCharacter : BSPhysObject
263 public override OMV.Vector3 Position { 380 public override OMV.Vector3 Position {
264 get { 381 get {
265 // Don't refetch the position because this function is called a zillion times 382 // Don't refetch the position because this function is called a zillion times
266 // _position = BulletSimAPI.GetObjectPosition2(Scene.World.ptr, LocalID); 383 // _position = PhysicsScene.PE.GetObjectPosition(Scene.World, LocalID);
267 return _position; 384 return _position;
268 } 385 }
269 set { 386 set {
270 _position = value; 387 _position = value;
271 PositionSanityCheck();
272 388
273 PhysicsScene.TaintedObject("BSCharacter.setPosition", delegate() 389 PhysicsScene.TaintedObject("BSCharacter.setPosition", delegate()
274 { 390 {
275 DetailLog("{0},BSCharacter.SetPosition,taint,pos={1},orient={2}", LocalID, _position, _orientation); 391 DetailLog("{0},BSCharacter.SetPosition,taint,pos={1},orient={2}", LocalID, _position, _orientation);
276 BulletSimAPI.SetTranslation2(PhysBody.ptr, _position, _orientation); 392 ForcePosition = _position;
277 }); 393 });
278 } 394 }
279 } 395 }
280 public override OMV.Vector3 ForcePosition { 396 public override OMV.Vector3 ForcePosition {
281 get { 397 get {
282 _position = BulletSimAPI.GetPosition2(PhysBody.ptr); 398 _position = PhysicsScene.PE.GetPosition(PhysBody);
283 return _position; 399 return _position;
284 } 400 }
285 set { 401 set {
286 _position = value; 402 _position = value;
287 PositionSanityCheck(); 403 if (PhysBody.HasPhysicalBody)
288 BulletSimAPI.SetTranslation2(PhysBody.ptr, _position, _orientation); 404 {
405 PositionSanityCheck();
406 PhysicsScene.PE.SetTranslation(PhysBody, _position, _orientation);
407 }
289 } 408 }
290 } 409 }
291 410
@@ -297,8 +416,17 @@ public sealed class BSCharacter : BSPhysObject
297 { 416 {
298 bool ret = false; 417 bool ret = false;
299 418
419 // TODO: check for out of bounds
420 if (!PhysicsScene.TerrainManager.IsWithinKnownTerrain(RawPosition))
421 {
422 // The character is out of the known/simulated area.
423 // Upper levels of code will handle the transition to other areas so, for
424 // the time, we just ignore the position.
425 return ret;
426 }
427
300 // If below the ground, move the avatar up 428 // If below the ground, move the avatar up
301 float terrainHeight = PhysicsScene.TerrainManager.GetTerrainHeightAtXYZ(_position); 429 float terrainHeight = PhysicsScene.TerrainManager.GetTerrainHeightAtXYZ(RawPosition);
302 if (Position.Z < terrainHeight) 430 if (Position.Z < terrainHeight)
303 { 431 {
304 DetailLog("{0},BSCharacter.PositionAdjustUnderGround,call,pos={1},terrain={2}", LocalID, _position, terrainHeight); 432 DetailLog("{0},BSCharacter.PositionAdjustUnderGround,call,pos={1},terrain={2}", LocalID, _position, terrainHeight);
@@ -307,7 +435,7 @@ public sealed class BSCharacter : BSPhysObject
307 } 435 }
308 if ((CurrentCollisionFlags & CollisionFlags.BS_FLOATS_ON_WATER) != 0) 436 if ((CurrentCollisionFlags & CollisionFlags.BS_FLOATS_ON_WATER) != 0)
309 { 437 {
310 float waterHeight = PhysicsScene.GetWaterLevelAtXYZ(_position); 438 float waterHeight = PhysicsScene.TerrainManager.GetWaterLevelAtXYZ(_position);
311 if (Position.Z < waterHeight) 439 if (Position.Z < waterHeight)
312 { 440 {
313 _position.Z = waterHeight; 441 _position.Z = waterHeight;
@@ -315,7 +443,6 @@ public sealed class BSCharacter : BSPhysObject
315 } 443 }
316 } 444 }
317 445
318 // TODO: check for out of bounds
319 return ret; 446 return ret;
320 } 447 }
321 448
@@ -332,7 +459,8 @@ public sealed class BSCharacter : BSPhysObject
332 PhysicsScene.TaintedObject(inTaintTime, "BSCharacter.PositionSanityCheck", delegate() 459 PhysicsScene.TaintedObject(inTaintTime, "BSCharacter.PositionSanityCheck", delegate()
333 { 460 {
334 DetailLog("{0},BSCharacter.PositionSanityCheck,taint,pos={1},orient={2}", LocalID, _position, _orientation); 461 DetailLog("{0},BSCharacter.PositionSanityCheck,taint,pos={1},orient={2}", LocalID, _position, _orientation);
335 BulletSimAPI.SetTranslation2(PhysBody.ptr, _position, _orientation); 462 if (PhysBody.HasPhysicalBody)
463 PhysicsScene.PE.SetTranslation(PhysBody, _position, _orientation);
336 }); 464 });
337 ret = true; 465 ret = true;
338 } 466 }
@@ -345,10 +473,10 @@ public sealed class BSCharacter : BSPhysObject
345 public override float RawMass { 473 public override float RawMass {
346 get {return _mass; } 474 get {return _mass; }
347 } 475 }
348 public override void UpdatePhysicalMassProperties(float physMass) 476 public override void UpdatePhysicalMassProperties(float physMass, bool inWorld)
349 { 477 {
350 OMV.Vector3 localInertia = BulletSimAPI.CalculateLocalInertia2(PhysShape.ptr, physMass); 478 OMV.Vector3 localInertia = PhysicsScene.PE.CalculateLocalInertia(PhysShape, physMass);
351 BulletSimAPI.SetMassProps2(PhysBody.ptr, physMass, localInertia); 479 PhysicsScene.PE.SetMassProps(PhysBody, physMass, localInertia);
352 } 480 }
353 481
354 public override OMV.Vector3 Force { 482 public override OMV.Vector3 Force {
@@ -359,7 +487,8 @@ public sealed class BSCharacter : BSPhysObject
359 PhysicsScene.TaintedObject("BSCharacter.SetForce", delegate() 487 PhysicsScene.TaintedObject("BSCharacter.SetForce", delegate()
360 { 488 {
361 DetailLog("{0},BSCharacter.setForce,taint,force={1}", LocalID, _force); 489 DetailLog("{0},BSCharacter.setForce,taint,force={1}", LocalID, _force);
362 BulletSimAPI.SetObjectForce2(PhysBody.ptr, _force); 490 if (PhysBody.HasPhysicalBody)
491 PhysicsScene.PE.SetObjectForce(PhysBody, _force);
363 }); 492 });
364 } 493 }
365 } 494 }
@@ -376,6 +505,36 @@ public sealed class BSCharacter : BSPhysObject
376 505
377 public override OMV.Vector3 GeometricCenter { get { return OMV.Vector3.Zero; } } 506 public override OMV.Vector3 GeometricCenter { get { return OMV.Vector3.Zero; } }
378 public override OMV.Vector3 CenterOfMass { get { return OMV.Vector3.Zero; } } 507 public override OMV.Vector3 CenterOfMass { get { return OMV.Vector3.Zero; } }
508
509 // Sets the target in the motor. This starts the changing of the avatar's velocity.
510 public override OMV.Vector3 TargetVelocity
511 {
512 get
513 {
514 return _velocityMotor.TargetValue;
515 }
516 set
517 {
518 DetailLog("{0},BSCharacter.setTargetVelocity,call,vel={1}", LocalID, value);
519 OMV.Vector3 targetVel = value;
520 if (_setAlwaysRun)
521 targetVel *= BSParam.AvatarAlwaysRunFactor;
522
523 PhysicsScene.TaintedObject("BSCharacter.setTargetVelocity", delegate()
524 {
525 _velocityMotor.Reset();
526 _velocityMotor.SetTarget(targetVel);
527 _velocityMotor.SetCurrent(_velocity);
528 _velocityMotor.Enabled = true;
529 });
530 }
531 }
532 public override OMV.Vector3 RawVelocity
533 {
534 get { return _velocity; }
535 set { _velocity = value; }
536 }
537 // Directly setting velocity means this is what the user really wants now.
379 public override OMV.Vector3 Velocity { 538 public override OMV.Vector3 Velocity {
380 get { return _velocity; } 539 get { return _velocity; }
381 set { 540 set {
@@ -383,6 +542,12 @@ public sealed class BSCharacter : BSPhysObject
383 // m_log.DebugFormat("{0}: set velocity = {1}", LogHeader, _velocity); 542 // m_log.DebugFormat("{0}: set velocity = {1}", LogHeader, _velocity);
384 PhysicsScene.TaintedObject("BSCharacter.setVelocity", delegate() 543 PhysicsScene.TaintedObject("BSCharacter.setVelocity", delegate()
385 { 544 {
545 _velocityMotor.Reset();
546 _velocityMotor.SetCurrent(_velocity);
547 _velocityMotor.SetTarget(_velocity);
548 // Even though the motor is initialized, it's not used and the velocity goes straight into the avatar.
549 _velocityMotor.Enabled = false;
550
386 DetailLog("{0},BSCharacter.setVelocity,taint,vel={1}", LocalID, _velocity); 551 DetailLog("{0},BSCharacter.setVelocity,taint,vel={1}", LocalID, _velocity);
387 ForceVelocity = _velocity; 552 ForceVelocity = _velocity;
388 }); 553 });
@@ -391,30 +556,32 @@ public sealed class BSCharacter : BSPhysObject
391 public override OMV.Vector3 ForceVelocity { 556 public override OMV.Vector3 ForceVelocity {
392 get { return _velocity; } 557 get { return _velocity; }
393 set { 558 set {
559 PhysicsScene.AssertInTaintTime("BSCharacter.ForceVelocity");
560
561 _velocity = value;
394 // Depending on whether the avatar is moving or not, change the friction 562 // Depending on whether the avatar is moving or not, change the friction
395 // to keep the avatar from slipping around 563 // to keep the avatar from slipping around
396 if (_velocity.Length() == 0) 564 if (_velocity.Length() == 0)
397 { 565 {
398 if (_currentFriction != PhysicsScene.Params.avatarStandingFriction) 566 if (_currentFriction != BSParam.AvatarStandingFriction)
399 { 567 {
400 _currentFriction = PhysicsScene.Params.avatarStandingFriction; 568 _currentFriction = BSParam.AvatarStandingFriction;
401 BulletSimAPI.SetFriction2(PhysBody.ptr, _currentFriction); 569 if (PhysBody.HasPhysicalBody)
570 PhysicsScene.PE.SetFriction(PhysBody, _currentFriction);
402 } 571 }
403 } 572 }
404 else 573 else
405 { 574 {
406 if (_currentFriction != PhysicsScene.Params.avatarFriction) 575 if (_currentFriction != BSParam.AvatarFriction)
407 { 576 {
408 _currentFriction = PhysicsScene.Params.avatarFriction; 577 _currentFriction = BSParam.AvatarFriction;
409 BulletSimAPI.SetFriction2(PhysBody.ptr, _currentFriction); 578 if (PhysBody.HasPhysicalBody)
579 PhysicsScene.PE.SetFriction(PhysBody, _currentFriction);
410 } 580 }
411 } 581 }
412 _velocity = value;
413 // Remember the set velocity so we can suppress the reduction by friction, ...
414 _appliedVelocity = value;
415 582
416 BulletSimAPI.SetLinearVelocity2(PhysBody.ptr, _velocity); 583 PhysicsScene.PE.SetLinearVelocity(PhysBody, _velocity);
417 BulletSimAPI.Activate2(PhysBody.ptr, true); 584 PhysicsScene.PE.Activate(PhysBody, true);
418 } 585 }
419 } 586 }
420 public override OMV.Vector3 Torque { 587 public override OMV.Vector3 Torque {
@@ -439,13 +606,16 @@ public sealed class BSCharacter : BSPhysObject
439 public override OMV.Quaternion Orientation { 606 public override OMV.Quaternion Orientation {
440 get { return _orientation; } 607 get { return _orientation; }
441 set { 608 set {
442 _orientation = value; 609 // Orientation is set zillions of times when an avatar is walking. It's like
443 // m_log.DebugFormat("{0}: set orientation to {1}", LogHeader, _orientation); 610 // the viewer doesn't trust us.
444 PhysicsScene.TaintedObject("BSCharacter.setOrientation", delegate() 611 if (_orientation != value)
445 { 612 {
446 // _position = BulletSimAPI.GetPosition2(BSBody.ptr); 613 _orientation = value;
447 BulletSimAPI.SetTranslation2(PhysBody.ptr, _position, _orientation); 614 PhysicsScene.TaintedObject("BSCharacter.setOrientation", delegate()
448 }); 615 {
616 ForceOrientation = _orientation;
617 });
618 }
449 } 619 }
450 } 620 }
451 // Go directly to Bullet to get/set the value. 621 // Go directly to Bullet to get/set the value.
@@ -453,13 +623,17 @@ public sealed class BSCharacter : BSPhysObject
453 { 623 {
454 get 624 get
455 { 625 {
456 _orientation = BulletSimAPI.GetOrientation2(PhysBody.ptr); 626 _orientation = PhysicsScene.PE.GetOrientation(PhysBody);
457 return _orientation; 627 return _orientation;
458 } 628 }
459 set 629 set
460 { 630 {
461 _orientation = value; 631 _orientation = value;
462 BulletSimAPI.SetTranslation2(PhysBody.ptr, _position, _orientation); 632 if (PhysBody.HasPhysicalBody)
633 {
634 // _position = PhysicsScene.PE.GetPosition(BSBody);
635 PhysicsScene.PE.SetTranslation(PhysBody, _position, _orientation);
636 }
463 } 637 }
464 } 638 }
465 public override int PhysicsActorType { 639 public override int PhysicsActorType {
@@ -478,10 +652,14 @@ public sealed class BSCharacter : BSPhysObject
478 public override bool IsStatic { 652 public override bool IsStatic {
479 get { return false; } 653 get { return false; }
480 } 654 }
655 public override bool IsPhysicallyActive {
656 get { return true; }
657 }
481 public override bool Flying { 658 public override bool Flying {
482 get { return _flying; } 659 get { return _flying; }
483 set { 660 set {
484 _flying = value; 661 _flying = value;
662
485 // simulate flying by changing the effect of gravity 663 // simulate flying by changing the effect of gravity
486 Buoyancy = ComputeBuoyancyFromFlying(_flying); 664 Buoyancy = ComputeBuoyancyFromFlying(_flying);
487 } 665 }
@@ -500,27 +678,18 @@ public sealed class BSCharacter : BSPhysObject
500 get { return _throttleUpdates; } 678 get { return _throttleUpdates; }
501 set { _throttleUpdates = value; } 679 set { _throttleUpdates = value; }
502 } 680 }
503 public override bool IsColliding {
504 get { return (CollidingStep == PhysicsScene.SimulationStep); }
505 set { _isColliding = value; }
506 }
507 public override bool CollidingGround {
508 get { return (CollidingGroundStep == PhysicsScene.SimulationStep); }
509 set { CollidingGround = value; }
510 }
511 public override bool CollidingObj {
512 get { return _collidingObj; }
513 set { _collidingObj = value; }
514 }
515 public override bool FloatOnWater { 681 public override bool FloatOnWater {
516 set { 682 set {
517 _floatOnWater = value; 683 _floatOnWater = value;
518 PhysicsScene.TaintedObject("BSCharacter.setFloatOnWater", delegate() 684 PhysicsScene.TaintedObject("BSCharacter.setFloatOnWater", delegate()
519 { 685 {
520 if (_floatOnWater) 686 if (PhysBody.HasPhysicalBody)
521 CurrentCollisionFlags = BulletSimAPI.AddToCollisionFlags2(PhysBody.ptr, CollisionFlags.BS_FLOATS_ON_WATER); 687 {
522 else 688 if (_floatOnWater)
523 CurrentCollisionFlags = BulletSimAPI.RemoveFromCollisionFlags2(PhysBody.ptr, CollisionFlags.BS_FLOATS_ON_WATER); 689 CurrentCollisionFlags = PhysicsScene.PE.AddToCollisionFlags(PhysBody, CollisionFlags.BS_FLOATS_ON_WATER);
690 else
691 CurrentCollisionFlags = PhysicsScene.PE.RemoveFromCollisionFlags(PhysBody, CollisionFlags.BS_FLOATS_ON_WATER);
692 }
524 }); 693 });
525 } 694 }
526 } 695 }
@@ -549,11 +718,15 @@ public sealed class BSCharacter : BSPhysObject
549 } 718 }
550 public override float ForceBuoyancy { 719 public override float ForceBuoyancy {
551 get { return _buoyancy; } 720 get { return _buoyancy; }
552 set { _buoyancy = value; 721 set {
722 PhysicsScene.AssertInTaintTime("BSCharacter.ForceBuoyancy");
723
724 _buoyancy = value;
553 DetailLog("{0},BSCharacter.setForceBuoyancy,taint,buoy={1}", LocalID, _buoyancy); 725 DetailLog("{0},BSCharacter.setForceBuoyancy,taint,buoy={1}", LocalID, _buoyancy);
554 // Buoyancy is faked by changing the gravity applied to the object 726 // Buoyancy is faked by changing the gravity applied to the object
555 float grav = PhysicsScene.Params.gravity * (1f - _buoyancy); 727 float grav = PhysicsScene.Params.gravity * (1f - _buoyancy);
556 BulletSimAPI.SetGravity2(PhysBody.ptr, new OMV.Vector3(0f, 0f, grav)); 728 if (PhysBody.HasPhysicalBody)
729 PhysicsScene.PE.SetGravity(PhysBody, new OMV.Vector3(0f, 0f, grav));
557 } 730 }
558 } 731 }
559 732
@@ -589,24 +762,40 @@ public sealed class BSCharacter : BSPhysObject
589 public override float APIDStrength { set { return; } } 762 public override float APIDStrength { set { return; } }
590 public override float APIDDamping { set { return; } } 763 public override float APIDDamping { set { return; } }
591 764
592 public override void AddForce(OMV.Vector3 force, bool pushforce) { 765 public override void AddForce(OMV.Vector3 force, bool pushforce)
766 {
767 // Since this force is being applied in only one step, make this a force per second.
768 OMV.Vector3 addForce = force / PhysicsScene.LastTimeStep;
769 AddForce(addForce, pushforce, false);
770 }
771 private void AddForce(OMV.Vector3 force, bool pushforce, bool inTaintTime) {
593 if (force.IsFinite()) 772 if (force.IsFinite())
594 { 773 {
595 _force.X += force.X; 774 float magnitude = force.Length();
596 _force.Y += force.Y; 775 if (magnitude > BSParam.MaxAddForceMagnitude)
597 _force.Z += force.Z; 776 {
598 // m_log.DebugFormat("{0}: AddForce. adding={1}, newForce={2}", LogHeader, force, _force); 777 // Force has a limit
599 PhysicsScene.TaintedObject("BSCharacter.AddForce", delegate() 778 force = force / magnitude * BSParam.MaxAddForceMagnitude;
779 }
780
781 OMV.Vector3 addForce = force;
782 // DetailLog("{0},BSCharacter.addForce,call,force={1}", LocalID, addForce);
783
784 PhysicsScene.TaintedObject(inTaintTime, "BSCharacter.AddForce", delegate()
600 { 785 {
601 DetailLog("{0},BSCharacter.setAddForce,taint,addedForce={1}", LocalID, _force); 786 // Bullet adds this central force to the total force for this tick
602 BulletSimAPI.SetObjectForce2(PhysBody.ptr, _force); 787 // DetailLog("{0},BSCharacter.addForce,taint,force={1}", LocalID, addForce);
788 if (PhysBody.HasPhysicalBody)
789 {
790 PhysicsScene.PE.ApplyCentralForce(PhysBody, addForce);
791 }
603 }); 792 });
604 } 793 }
605 else 794 else
606 { 795 {
607 m_log.ErrorFormat("{0}: Got a NaN force applied to a Character", LogHeader); 796 m_log.WarnFormat("{0}: Got a NaN force applied to a character. LocalID={1}", LogHeader, LocalID);
797 return;
608 } 798 }
609 //m_lastUpdateSent = false;
610 } 799 }
611 800
612 public override void AddAngularForce(OMV.Vector3 force, bool pushforce) { 801 public override void AddAngularForce(OMV.Vector3 force, bool pushforce) {
@@ -614,24 +803,31 @@ public sealed class BSCharacter : BSPhysObject
614 public override void SetMomentum(OMV.Vector3 momentum) { 803 public override void SetMomentum(OMV.Vector3 momentum) {
615 } 804 }
616 805
617 private void ComputeAvatarScale(OMV.Vector3 size) 806 private OMV.Vector3 ComputeAvatarScale(OMV.Vector3 size)
618 { 807 {
619 // The 'size' given by the simulator is the mid-point of the avatar 808 OMV.Vector3 newScale;
620 // and X and Y are unspecified. 809
621 810 // Bullet's capsule total height is the "passed height + radius * 2";
622 OMV.Vector3 newScale = size; 811 // The base capsule is 1 diameter and 2 height (passed radius=0.5, passed height = 1)
623 // newScale.X = PhysicsScene.Params.avatarCapsuleWidth; 812 // The number we pass in for 'scaling' is the multiplier to get that base
624 // newScale.Y = PhysicsScene.Params.avatarCapsuleDepth; 813 // shape to be the size desired.
625 814 // So, when creating the scale for the avatar height, we take the passed height
626 // From the total height, remove the capsule half spheres that are at each end 815 // (size.Z) and remove the caps.
627 // The 1.15f came from ODE. Not sure what this factors in. 816 // Another oddity of the Bullet capsule implementation is that it presumes the Y
628 // newScale.Z = (size.Z * 1.15f) - (newScale.X + newScale.Y); 817 // dimension is the radius of the capsule. Even though some of the code allows
818 // for a asymmetrical capsule, other parts of the code presume it is cylindrical.
819
820 // Scale is multiplier of radius with one of "0.5"
821 newScale.X = size.X / 2f;
822 newScale.Y = size.Y / 2f;
629 823
630 // The total scale height is the central cylindar plus the caps on the two ends. 824 // The total scale height is the central cylindar plus the caps on the two ends.
631 newScale.Z = size.Z + (Math.Min(size.X, size.Y) * 2f); 825 newScale.Z = (size.Z + (Math.Min(size.X, size.Y) * 2)) / 2f;
826 // If smaller than the endcaps, just fake like we're almost that small
827 if (newScale.Z < 0)
828 newScale.Z = 0.1f;
632 829
633 // Convert diameters to radii and height to half height -- the way Bullet expects it. 830 return newScale;
634 Scale = newScale / 2f;
635 } 831 }
636 832
637 // set _avatarVolume and _mass based on capsule size, _density and Scale 833 // set _avatarVolume and _mass based on capsule size, _density and Scale
@@ -639,14 +835,14 @@ public sealed class BSCharacter : BSPhysObject
639 { 835 {
640 _avatarVolume = (float)( 836 _avatarVolume = (float)(
641 Math.PI 837 Math.PI
642 * Scale.X 838 * Size.X / 2f
643 * Scale.Y // the area of capsule cylinder 839 * Size.Y / 2f // the area of capsule cylinder
644 * Scale.Z // times height of capsule cylinder 840 * Size.Z // times height of capsule cylinder
645 + 1.33333333f 841 + 1.33333333f
646 * Math.PI 842 * Math.PI
647 * Scale.X 843 * Size.X / 2f
648 * Math.Min(Scale.X, Scale.Y) 844 * Math.Min(Size.X, Size.Y) / 2
649 * Scale.Y // plus the volume of the capsule end caps 845 * Size.Y / 2f // plus the volume of the capsule end caps
650 ); 846 );
651 _mass = _avatarDensity * _avatarVolume; 847 _mass = _avatarDensity * _avatarVolume;
652 } 848 }
@@ -657,27 +853,29 @@ public sealed class BSCharacter : BSPhysObject
657 { 853 {
658 _position = entprop.Position; 854 _position = entprop.Position;
659 _orientation = entprop.Rotation; 855 _orientation = entprop.Rotation;
660 _velocity = entprop.Velocity; 856
857 // Smooth velocity. OpenSimulator is very sensitive to changes in velocity of the avatar
858 // and will send agent updates to the clients if velocity changes by more than
859 // 0.001m/s. Bullet introduces a lot of jitter in the velocity which causes many
860 // extra updates.
861 if (!entprop.Velocity.ApproxEquals(_velocity, 0.1f))
862 _velocity = entprop.Velocity;
863
661 _acceleration = entprop.Acceleration; 864 _acceleration = entprop.Acceleration;
662 _rotationalVelocity = entprop.RotationalVelocity; 865 _rotationalVelocity = entprop.RotationalVelocity;
866
663 // Do some sanity checking for the avatar. Make sure it's above ground and inbounds. 867 // Do some sanity checking for the avatar. Make sure it's above ground and inbounds.
664 PositionSanityCheck(true); 868 if (PositionSanityCheck(true))
869 {
870 entprop.Position = _position;
871 }
665 872
666 // remember the current and last set values 873 // remember the current and last set values
667 LastEntityProperties = CurrentEntityProperties; 874 LastEntityProperties = CurrentEntityProperties;
668 CurrentEntityProperties = entprop; 875 CurrentEntityProperties = entprop;
669 876
670 if (entprop.Velocity != LastEntityProperties.Velocity)
671 {
672 // Changes in the velocity are suppressed in avatars.
673 // That's just the way they are defined.
674 OMV.Vector3 avVel = new OMV.Vector3(_appliedVelocity.X, _appliedVelocity.Y, entprop.Velocity.Z);
675 _velocity = avVel;
676 BulletSimAPI.SetLinearVelocity2(PhysBody.ptr, avVel);
677 }
678
679 // Tell the linkset about value changes 877 // Tell the linkset about value changes
680 Linkset.UpdateProperties(this); 878 Linkset.UpdateProperties(this, true);
681 879
682 // Avatars don't report their changes the usual way. Changes are checked for in the heartbeat loop. 880 // Avatars don't report their changes the usual way. Changes are checked for in the heartbeat loop.
683 // base.RequestPhysicsterseUpdate(); 881 // base.RequestPhysicsterseUpdate();