diff options
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSActorAvatarMove.cs')
-rwxr-xr-x | OpenSim/Region/Physics/BulletSPlugin/BSActorAvatarMove.cs | 285 |
1 files changed, 285 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..8416740 --- /dev/null +++ b/OpenSim/Region/Physics/BulletSPlugin/BSActorAvatarMove.cs | |||
@@ -0,0 +1,285 @@ | |||
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 | public BSActorAvatarMove(BSScene physicsScene, BSPhysObject pObj, string actorName) | ||
44 | : base(physicsScene, pObj, actorName) | ||
45 | { | ||
46 | m_velocityMotor = null; | ||
47 | m_physicsScene.DetailLog("{0},BSActorAvatarMove,constructor", m_controllingPrim.LocalID); | ||
48 | } | ||
49 | |||
50 | // BSActor.isActive | ||
51 | public override bool isActive | ||
52 | { | ||
53 | get { return Enabled && m_controllingPrim.IsPhysicallyActive; } | ||
54 | } | ||
55 | |||
56 | // Release any connections and resources used by the actor. | ||
57 | // BSActor.Dispose() | ||
58 | public override void Dispose() | ||
59 | { | ||
60 | Enabled = false; | ||
61 | } | ||
62 | |||
63 | // Called when physical parameters (properties set in Bullet) need to be re-applied. | ||
64 | // Called at taint-time. | ||
65 | // BSActor.Refresh() | ||
66 | public override void Refresh() | ||
67 | { | ||
68 | m_physicsScene.DetailLog("{0},BSActorAvatarMove,refresh", m_controllingPrim.LocalID); | ||
69 | |||
70 | // If the object is physically active, add the hoverer prestep action | ||
71 | if (isActive) | ||
72 | { | ||
73 | ActivateAvatarMove(); | ||
74 | } | ||
75 | else | ||
76 | { | ||
77 | DeactivateAvatarMove(); | ||
78 | } | ||
79 | } | ||
80 | |||
81 | // The object's physical representation is being rebuilt so pick up any physical dependencies (constraints, ...). | ||
82 | // Register a prestep action to restore physical requirements before the next simulation step. | ||
83 | // Called at taint-time. | ||
84 | // BSActor.RemoveBodyDependencies() | ||
85 | public override void RemoveBodyDependencies() | ||
86 | { | ||
87 | // Nothing to do for the hoverer since it is all software at pre-step action time. | ||
88 | } | ||
89 | |||
90 | // Usually called when target velocity changes to set the current velocity and the target | ||
91 | // into the movement motor. | ||
92 | public void SetVelocityAndTarget(OMV.Vector3 vel, OMV.Vector3 targ, bool inTaintTime) | ||
93 | { | ||
94 | m_physicsScene.TaintedObject(inTaintTime, "BSActorAvatarMove.setVelocityAndTarget", delegate() | ||
95 | { | ||
96 | if (m_velocityMotor != null) | ||
97 | { | ||
98 | m_velocityMotor.Reset(); | ||
99 | m_velocityMotor.SetTarget(targ); | ||
100 | m_velocityMotor.SetCurrent(vel); | ||
101 | m_velocityMotor.Enabled = true; | ||
102 | } | ||
103 | }); | ||
104 | } | ||
105 | |||
106 | // If a hover motor has not been created, create one and start the hovering. | ||
107 | private void ActivateAvatarMove() | ||
108 | { | ||
109 | if (m_velocityMotor == null) | ||
110 | { | ||
111 | // Infinite decay and timescale values so motor only changes current to target values. | ||
112 | m_velocityMotor = new BSVMotor("BSCharacter.Velocity", | ||
113 | 0.2f, // time scale | ||
114 | BSMotor.Infinite, // decay time scale | ||
115 | BSMotor.InfiniteVector, // friction timescale | ||
116 | 1f // efficiency | ||
117 | ); | ||
118 | // _velocityMotor.PhysicsScene = PhysicsScene; // DEBUG DEBUG so motor will output detail log messages. | ||
119 | SetVelocityAndTarget(m_controllingPrim.RawVelocity, m_controllingPrim.TargetVelocity, true /* inTaintTime */); | ||
120 | |||
121 | m_physicsScene.BeforeStep += Mover; | ||
122 | } | ||
123 | } | ||
124 | |||
125 | private void DeactivateAvatarMove() | ||
126 | { | ||
127 | if (m_velocityMotor != null) | ||
128 | { | ||
129 | m_physicsScene.BeforeStep -= Mover; | ||
130 | m_velocityMotor = null; | ||
131 | } | ||
132 | } | ||
133 | |||
134 | // Called just before the simulation step. Update the vertical position for hoverness. | ||
135 | private void Mover(float timeStep) | ||
136 | { | ||
137 | // Don't do movement while the object is selected. | ||
138 | if (!isActive) | ||
139 | return; | ||
140 | |||
141 | // TODO: Decide if the step parameters should be changed depending on the avatar's | ||
142 | // state (flying, colliding, ...). There is code in ODE to do this. | ||
143 | |||
144 | // COMMENTARY: when the user is making the avatar walk, except for falling, the velocity | ||
145 | // specified for the avatar is the one that should be used. For falling, if the avatar | ||
146 | // is not flying and is not colliding then it is presumed to be falling and the Z | ||
147 | // component is not fooled with (thus allowing gravity to do its thing). | ||
148 | // When the avatar is standing, though, the user has specified a velocity of zero and | ||
149 | // the avatar should be standing. But if the avatar is pushed by something in the world | ||
150 | // (raising elevator platform, moving vehicle, ...) the avatar should be allowed to | ||
151 | // move. Thus, the velocity cannot be forced to zero. The problem is that small velocity | ||
152 | // errors can creap in and the avatar will slowly float off in some direction. | ||
153 | // So, the problem is that, when an avatar is standing, we cannot tell creaping error | ||
154 | // from real pushing. | ||
155 | // The code below uses whether the collider is static or moving to decide whether to zero motion. | ||
156 | |||
157 | m_velocityMotor.Step(timeStep); | ||
158 | m_controllingPrim.IsStationary = false; | ||
159 | |||
160 | // If we're not supposed to be moving, make sure things are zero. | ||
161 | if (m_velocityMotor.ErrorIsZero() && m_velocityMotor.TargetValue == OMV.Vector3.Zero) | ||
162 | { | ||
163 | // The avatar shouldn't be moving | ||
164 | m_velocityMotor.Zero(); | ||
165 | |||
166 | if (m_controllingPrim.IsColliding) | ||
167 | { | ||
168 | // If we are colliding with a stationary object, presume we're standing and don't move around | ||
169 | if (!m_controllingPrim.ColliderIsMoving) | ||
170 | { | ||
171 | m_physicsScene.DetailLog("{0},BSCharacter.MoveMotor,collidingWithStationary,zeroingMotion", m_controllingPrim.LocalID); | ||
172 | m_controllingPrim.IsStationary = true; | ||
173 | m_controllingPrim.ZeroMotion(true /* inTaintTime */); | ||
174 | } | ||
175 | |||
176 | // Standing has more friction on the ground | ||
177 | if (m_controllingPrim.Friction != BSParam.AvatarStandingFriction) | ||
178 | { | ||
179 | m_controllingPrim.Friction = BSParam.AvatarStandingFriction; | ||
180 | m_physicsScene.PE.SetFriction(m_controllingPrim.PhysBody, m_controllingPrim.Friction); | ||
181 | } | ||
182 | } | ||
183 | else | ||
184 | { | ||
185 | if (m_controllingPrim.Flying) | ||
186 | { | ||
187 | // Flying and not collising and velocity nearly zero. | ||
188 | m_controllingPrim.ZeroMotion(true /* inTaintTime */); | ||
189 | } | ||
190 | } | ||
191 | |||
192 | m_physicsScene.DetailLog("{0},BSCharacter.MoveMotor,taint,stopping,target={1},colliding={2}", | ||
193 | m_controllingPrim.LocalID, m_velocityMotor.TargetValue, m_controllingPrim.IsColliding); | ||
194 | } | ||
195 | else | ||
196 | { | ||
197 | // Supposed to be moving. | ||
198 | OMV.Vector3 stepVelocity = m_velocityMotor.CurrentValue; | ||
199 | |||
200 | if (m_controllingPrim.Friction != BSParam.AvatarFriction) | ||
201 | { | ||
202 | // Probably starting up walking. Set friction to moving friction. | ||
203 | m_controllingPrim.Friction = BSParam.AvatarFriction; | ||
204 | m_physicsScene.PE.SetFriction(m_controllingPrim.PhysBody, m_controllingPrim.Friction); | ||
205 | } | ||
206 | |||
207 | // If falling, we keep the world's downward vector no matter what the other axis specify. | ||
208 | // The check for RawVelocity.Z < 0 makes jumping work (temporary upward force). | ||
209 | if (!m_controllingPrim.Flying && !m_controllingPrim.IsColliding) | ||
210 | { | ||
211 | if (m_controllingPrim.RawVelocity.Z < 0) | ||
212 | stepVelocity.Z = m_controllingPrim.RawVelocity.Z; | ||
213 | // DetailLog("{0},BSCharacter.MoveMotor,taint,overrideStepZWithWorldZ,stepVel={1}", LocalID, stepVelocity); | ||
214 | } | ||
215 | |||
216 | // 'stepVelocity' is now the speed we'd like the avatar to move in. Turn that into an instantanous force. | ||
217 | OMV.Vector3 moveForce = (stepVelocity - m_controllingPrim.RawVelocity) * m_controllingPrim.Mass; | ||
218 | |||
219 | // Should we check for move force being small and forcing velocity to zero? | ||
220 | |||
221 | // Add special movement force to allow avatars to walk up stepped surfaces. | ||
222 | moveForce += WalkUpStairs(); | ||
223 | |||
224 | m_physicsScene.DetailLog("{0},BSCharacter.MoveMotor,move,stepVel={1},vel={2},mass={3},moveForce={4}", | ||
225 | m_controllingPrim.LocalID, stepVelocity, m_controllingPrim.RawVelocity, m_controllingPrim.Mass, moveForce); | ||
226 | m_physicsScene.PE.ApplyCentralImpulse(m_controllingPrim.PhysBody, moveForce); | ||
227 | } | ||
228 | } | ||
229 | |||
230 | // Decide if the character is colliding with a low object and compute a force to pop the | ||
231 | // avatar up so it can walk up and over the low objects. | ||
232 | private OMV.Vector3 WalkUpStairs() | ||
233 | { | ||
234 | OMV.Vector3 ret = OMV.Vector3.Zero; | ||
235 | |||
236 | // This test is done if moving forward, not flying and is colliding with something. | ||
237 | // DetailLog("{0},BSCharacter.WalkUpStairs,IsColliding={1},flying={2},targSpeed={3},collisions={4}", | ||
238 | // LocalID, IsColliding, Flying, TargetSpeed, CollisionsLastTick.Count); | ||
239 | if (m_controllingPrim.IsColliding && !m_controllingPrim.Flying && m_controllingPrim.TargetVelocitySpeed > 0.1f /* && ForwardSpeed < 0.1f */) | ||
240 | { | ||
241 | // The range near the character's feet where we will consider stairs | ||
242 | float nearFeetHeightMin = m_controllingPrim.RawPosition.Z - (m_controllingPrim.Size.Z / 2f) + 0.05f; | ||
243 | float nearFeetHeightMax = nearFeetHeightMin + BSParam.AvatarStepHeight; | ||
244 | |||
245 | // Look for a collision point that is near the character's feet and is oriented the same as the charactor is | ||
246 | foreach (KeyValuePair<uint, ContactPoint> kvp in m_controllingPrim.CollisionsLastTick.m_objCollisionList) | ||
247 | { | ||
248 | // Don't care about collisions with the terrain | ||
249 | if (kvp.Key > m_physicsScene.TerrainManager.HighestTerrainID) | ||
250 | { | ||
251 | OMV.Vector3 touchPosition = kvp.Value.Position; | ||
252 | // DetailLog("{0},BSCharacter.WalkUpStairs,min={1},max={2},touch={3}", | ||
253 | // LocalID, nearFeetHeightMin, nearFeetHeightMax, touchPosition); | ||
254 | if (touchPosition.Z >= nearFeetHeightMin && touchPosition.Z <= nearFeetHeightMax) | ||
255 | { | ||
256 | // This contact is within the 'near the feet' range. | ||
257 | // The normal should be our contact point to the object so it is pointing away | ||
258 | // thus the difference between our facing orientation and the normal should be small. | ||
259 | OMV.Vector3 directionFacing = OMV.Vector3.UnitX * m_controllingPrim.RawOrientation; | ||
260 | OMV.Vector3 touchNormal = OMV.Vector3.Normalize(kvp.Value.SurfaceNormal); | ||
261 | float diff = Math.Abs(OMV.Vector3.Distance(directionFacing, touchNormal)); | ||
262 | if (diff < BSParam.AvatarStepApproachFactor) | ||
263 | { | ||
264 | // Found the stairs contact point. Push up a little to raise the character. | ||
265 | float upForce = (touchPosition.Z - nearFeetHeightMin) * m_controllingPrim.Mass * BSParam.AvatarStepForceFactor; | ||
266 | ret = new OMV.Vector3(0f, 0f, upForce); | ||
267 | |||
268 | // Also move the avatar up for the new height | ||
269 | OMV.Vector3 displacement = new OMV.Vector3(0f, 0f, BSParam.AvatarStepHeight / 2f); | ||
270 | m_controllingPrim.ForcePosition = m_controllingPrim.RawPosition + displacement; | ||
271 | } | ||
272 | m_physicsScene.DetailLog("{0},BSCharacter.WalkUpStairs,touchPos={1},nearFeetMin={2},faceDir={3},norm={4},diff={5},ret={6}", | ||
273 | m_controllingPrim.LocalID, touchPosition, nearFeetHeightMin, directionFacing, touchNormal, diff, ret); | ||
274 | } | ||
275 | } | ||
276 | } | ||
277 | } | ||
278 | |||
279 | return ret; | ||
280 | } | ||
281 | |||
282 | } | ||
283 | } | ||
284 | |||
285 | |||