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