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