diff options
Diffstat (limited to 'OpenSim/Region/Physics/ChOdePlugin/ODECharacter.cs')
-rw-r--r-- | OpenSim/Region/Physics/ChOdePlugin/ODECharacter.cs | 1421 |
1 files changed, 1421 insertions, 0 deletions
diff --git a/OpenSim/Region/Physics/ChOdePlugin/ODECharacter.cs b/OpenSim/Region/Physics/ChOdePlugin/ODECharacter.cs new file mode 100644 index 0000000..61c16b8 --- /dev/null +++ b/OpenSim/Region/Physics/ChOdePlugin/ODECharacter.cs | |||
@@ -0,0 +1,1421 @@ | |||
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 copyright | ||
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.Reflection; | ||
31 | using OpenMetaverse; | ||
32 | using Ode.NET; | ||
33 | using OpenSim.Framework; | ||
34 | using OpenSim.Region.Physics.Manager; | ||
35 | using log4net; | ||
36 | |||
37 | namespace OpenSim.Region.Physics.OdePlugin | ||
38 | { | ||
39 | /// <summary> | ||
40 | /// Various properties that ODE uses for AMotors but isn't exposed in ODE.NET so we must define them ourselves. | ||
41 | /// </summary> | ||
42 | |||
43 | public enum dParam : int | ||
44 | { | ||
45 | LowStop = 0, | ||
46 | HiStop = 1, | ||
47 | Vel = 2, | ||
48 | FMax = 3, | ||
49 | FudgeFactor = 4, | ||
50 | Bounce = 5, | ||
51 | CFM = 6, | ||
52 | StopERP = 7, | ||
53 | StopCFM = 8, | ||
54 | LoStop2 = 256, | ||
55 | HiStop2 = 257, | ||
56 | Vel2 = 258, | ||
57 | FMax2 = 259, | ||
58 | StopERP2 = 7 + 256, | ||
59 | StopCFM2 = 8 + 256, | ||
60 | LoStop3 = 512, | ||
61 | HiStop3 = 513, | ||
62 | Vel3 = 514, | ||
63 | FMax3 = 515, | ||
64 | StopERP3 = 7 + 512, | ||
65 | StopCFM3 = 8 + 512 | ||
66 | } | ||
67 | public class OdeCharacter : PhysicsActor | ||
68 | { | ||
69 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
70 | |||
71 | private Vector3 _position; | ||
72 | private d.Vector3 _zeroPosition; | ||
73 | // private d.Matrix3 m_StandUpRotation; | ||
74 | private bool _zeroFlag = false; | ||
75 | private bool m_lastUpdateSent = false; | ||
76 | private Vector3 _velocity; | ||
77 | private Vector3 _target_velocity; | ||
78 | private Vector3 _acceleration; | ||
79 | private Vector3 m_rotationalVelocity; | ||
80 | private float m_mass = 80f; | ||
81 | public float m_density = 60f; | ||
82 | private bool m_pidControllerActive = true; | ||
83 | public float PID_D = 800.0f; | ||
84 | public float PID_P = 900.0f; | ||
85 | //private static float POSTURE_SERVO = 10000.0f; | ||
86 | public float CAPSULE_RADIUS = 0.37f; | ||
87 | public float CAPSULE_LENGTH = 2.140599f; | ||
88 | public float m_tensor = 3800000f; | ||
89 | public float heightFudgeFactor = 0.52f; | ||
90 | public float walkDivisor = 1.3f; | ||
91 | public float runDivisor = 0.8f; | ||
92 | private bool flying = false; | ||
93 | private bool m_iscolliding = false; | ||
94 | private bool m_iscollidingGround = false; | ||
95 | private bool m_wascolliding = false; | ||
96 | private bool m_wascollidingGround = false; | ||
97 | private bool m_iscollidingObj = false; | ||
98 | private bool m_alwaysRun = false; | ||
99 | private bool m_hackSentFall = false; | ||
100 | private bool m_hackSentFly = false; | ||
101 | private int m_requestedUpdateFrequency = 0; | ||
102 | private Vector3 m_taintPosition = Vector3.Zero; | ||
103 | public uint m_localID = 0; | ||
104 | public bool m_returnCollisions = false; | ||
105 | // taints and their non-tainted counterparts | ||
106 | public bool m_isPhysical = false; // the current physical status | ||
107 | public bool m_tainted_isPhysical = false; // set when the physical status is tainted (false=not existing in physics engine, true=existing) | ||
108 | public float MinimumGroundFlightOffset = 3f; | ||
109 | |||
110 | private float m_tainted_CAPSULE_LENGTH; // set when the capsule length changes. | ||
111 | private float m_tiltMagnitudeWhenProjectedOnXYPlane = 0.1131371f; // used to introduce a fixed tilt because a straight-up capsule falls through terrain, probably a bug in terrain collider | ||
112 | |||
113 | |||
114 | private float m_buoyancy = 0f; | ||
115 | |||
116 | // private CollisionLocker ode; | ||
117 | |||
118 | private string m_name = String.Empty; | ||
119 | |||
120 | private bool[] m_colliderarr = new bool[11]; | ||
121 | private bool[] m_colliderGroundarr = new bool[11]; | ||
122 | |||
123 | // Default we're a Character | ||
124 | private CollisionCategories m_collisionCategories = (CollisionCategories.Character); | ||
125 | |||
126 | // Default, Collide with Other Geometries, spaces, bodies and characters. | ||
127 | private CollisionCategories m_collisionFlags = (CollisionCategories.Geom | ||
128 | | CollisionCategories.Space | ||
129 | | CollisionCategories.Body | ||
130 | | CollisionCategories.Character | ||
131 | | CollisionCategories.Land); | ||
132 | public IntPtr Body = IntPtr.Zero; | ||
133 | private OdeScene _parent_scene; | ||
134 | public IntPtr Shell = IntPtr.Zero; | ||
135 | public IntPtr Amotor = IntPtr.Zero; | ||
136 | public d.Mass ShellMass; | ||
137 | public bool collidelock = false; | ||
138 | |||
139 | public int m_eventsubscription = 0; | ||
140 | private CollisionEventUpdate CollisionEventsThisFrame = new CollisionEventUpdate(); | ||
141 | |||
142 | // unique UUID of this character object | ||
143 | public UUID m_uuid; | ||
144 | public bool bad = false; | ||
145 | |||
146 | public OdeCharacter(String avName, OdeScene parent_scene, Vector3 pos, CollisionLocker dode, Vector3 size, float pid_d, float pid_p, float capsule_radius, float tensor, float density, float height_fudge_factor, float walk_divisor, float rundivisor) | ||
147 | { | ||
148 | m_uuid = UUID.Random(); | ||
149 | |||
150 | if (pos.IsFinite()) | ||
151 | { | ||
152 | if (pos.Z > 9999999f) | ||
153 | { | ||
154 | pos.Z = parent_scene.GetTerrainHeightAtXY(127, 127) + 5; | ||
155 | } | ||
156 | if (pos.Z < -90000f) | ||
157 | { | ||
158 | pos.Z = parent_scene.GetTerrainHeightAtXY(127, 127) + 5; | ||
159 | } | ||
160 | _position = pos; | ||
161 | m_taintPosition.X = pos.X; | ||
162 | m_taintPosition.Y = pos.Y; | ||
163 | m_taintPosition.Z = pos.Z; | ||
164 | } | ||
165 | else | ||
166 | { | ||
167 | _position = new Vector3(((float)_parent_scene.WorldExtents.X * 0.5f), ((float)_parent_scene.WorldExtents.Y * 0.5f), parent_scene.GetTerrainHeightAtXY(128f, 128f) + 10f); | ||
168 | m_taintPosition.X = _position.X; | ||
169 | m_taintPosition.Y = _position.Y; | ||
170 | m_taintPosition.Z = _position.Z; | ||
171 | m_log.Warn("[PHYSICS]: Got NaN Position on Character Create"); | ||
172 | } | ||
173 | |||
174 | _parent_scene = parent_scene; | ||
175 | |||
176 | PID_D = pid_d; | ||
177 | PID_P = pid_p; | ||
178 | CAPSULE_RADIUS = capsule_radius; | ||
179 | m_tensor = tensor; | ||
180 | m_density = density; | ||
181 | heightFudgeFactor = height_fudge_factor; | ||
182 | walkDivisor = walk_divisor; | ||
183 | runDivisor = rundivisor; | ||
184 | |||
185 | // m_StandUpRotation = | ||
186 | // new d.Matrix3(0.5f, 0.7071068f, 0.5f, -0.7071068f, 0f, 0.7071068f, 0.5f, -0.7071068f, | ||
187 | // 0.5f); | ||
188 | |||
189 | for (int i = 0; i < 11; i++) | ||
190 | { | ||
191 | m_colliderarr[i] = false; | ||
192 | } | ||
193 | CAPSULE_LENGTH = (size.Z * 1.15f) - CAPSULE_RADIUS * 2.0f; | ||
194 | //m_log.Info("[SIZE]: " + CAPSULE_LENGTH.ToString()); | ||
195 | m_tainted_CAPSULE_LENGTH = CAPSULE_LENGTH; | ||
196 | |||
197 | m_isPhysical = false; // current status: no ODE information exists | ||
198 | m_tainted_isPhysical = true; // new tainted status: need to create ODE information | ||
199 | |||
200 | _parent_scene.AddPhysicsActorTaint(this); | ||
201 | |||
202 | m_name = avName; | ||
203 | } | ||
204 | |||
205 | public override int PhysicsActorType | ||
206 | { | ||
207 | get { return (int) ActorTypes.Agent; } | ||
208 | set { return; } | ||
209 | } | ||
210 | |||
211 | /// <summary> | ||
212 | /// If this is set, the avatar will move faster | ||
213 | /// </summary> | ||
214 | public override bool SetAlwaysRun | ||
215 | { | ||
216 | get { return m_alwaysRun; } | ||
217 | set { m_alwaysRun = value; } | ||
218 | } | ||
219 | |||
220 | public override uint LocalID | ||
221 | { | ||
222 | set { m_localID = value; } | ||
223 | } | ||
224 | |||
225 | public override bool Grabbed | ||
226 | { | ||
227 | set { return; } | ||
228 | } | ||
229 | |||
230 | public override bool Selected | ||
231 | { | ||
232 | set { return; } | ||
233 | } | ||
234 | |||
235 | public override float Buoyancy | ||
236 | { | ||
237 | get { return m_buoyancy; } | ||
238 | set { m_buoyancy = value; } | ||
239 | } | ||
240 | |||
241 | public override bool FloatOnWater | ||
242 | { | ||
243 | set { return; } | ||
244 | } | ||
245 | |||
246 | public override bool IsPhysical | ||
247 | { | ||
248 | get { return false; } | ||
249 | set { return; } | ||
250 | } | ||
251 | |||
252 | public override bool ThrottleUpdates | ||
253 | { | ||
254 | get { return false; } | ||
255 | set { return; } | ||
256 | } | ||
257 | |||
258 | public override bool Flying | ||
259 | { | ||
260 | get { return flying; } | ||
261 | set { flying = value; } | ||
262 | } | ||
263 | |||
264 | /// <summary> | ||
265 | /// Returns if the avatar is colliding in general. | ||
266 | /// This includes the ground and objects and avatar. | ||
267 | /// </summary> | ||
268 | public override bool IsColliding | ||
269 | { | ||
270 | get { return m_iscolliding; } | ||
271 | set | ||
272 | { | ||
273 | int i; | ||
274 | int truecount = 0; | ||
275 | int falsecount = 0; | ||
276 | |||
277 | if (m_colliderarr.Length >= 10) | ||
278 | { | ||
279 | for (i = 0; i < 10; i++) | ||
280 | { | ||
281 | m_colliderarr[i] = m_colliderarr[i + 1]; | ||
282 | } | ||
283 | } | ||
284 | m_colliderarr[10] = value; | ||
285 | |||
286 | for (i = 0; i < 11; i++) | ||
287 | { | ||
288 | if (m_colliderarr[i]) | ||
289 | { | ||
290 | truecount++; | ||
291 | } | ||
292 | else | ||
293 | { | ||
294 | falsecount++; | ||
295 | } | ||
296 | } | ||
297 | |||
298 | // Equal truecounts and false counts means we're colliding with something. | ||
299 | |||
300 | if (falsecount > 1.2*truecount) | ||
301 | { | ||
302 | m_iscolliding = false; | ||
303 | } | ||
304 | else | ||
305 | { | ||
306 | m_iscolliding = true; | ||
307 | } | ||
308 | if (m_wascolliding != m_iscolliding) | ||
309 | { | ||
310 | //base.SendCollisionUpdate(new CollisionEventUpdate()); | ||
311 | } | ||
312 | m_wascolliding = m_iscolliding; | ||
313 | } | ||
314 | } | ||
315 | |||
316 | /// <summary> | ||
317 | /// Returns if an avatar is colliding with the ground | ||
318 | /// </summary> | ||
319 | public override bool CollidingGround | ||
320 | { | ||
321 | get { return m_iscollidingGround; } | ||
322 | set | ||
323 | { | ||
324 | // Collisions against the ground are not really reliable | ||
325 | // So, to get a consistant value we have to average the current result over time | ||
326 | // Currently we use 1 second = 10 calls to this. | ||
327 | int i; | ||
328 | int truecount = 0; | ||
329 | int falsecount = 0; | ||
330 | |||
331 | if (m_colliderGroundarr.Length >= 10) | ||
332 | { | ||
333 | for (i = 0; i < 10; i++) | ||
334 | { | ||
335 | m_colliderGroundarr[i] = m_colliderGroundarr[i + 1]; | ||
336 | } | ||
337 | } | ||
338 | m_colliderGroundarr[10] = value; | ||
339 | |||
340 | for (i = 0; i < 11; i++) | ||
341 | { | ||
342 | if (m_colliderGroundarr[i]) | ||
343 | { | ||
344 | truecount++; | ||
345 | } | ||
346 | else | ||
347 | { | ||
348 | falsecount++; | ||
349 | } | ||
350 | } | ||
351 | |||
352 | // Equal truecounts and false counts means we're colliding with something. | ||
353 | |||
354 | if (falsecount > 1.2*truecount) | ||
355 | { | ||
356 | m_iscollidingGround = false; | ||
357 | } | ||
358 | else | ||
359 | { | ||
360 | m_iscollidingGround = true; | ||
361 | } | ||
362 | if (m_wascollidingGround != m_iscollidingGround) | ||
363 | { | ||
364 | //base.SendCollisionUpdate(new CollisionEventUpdate()); | ||
365 | } | ||
366 | m_wascollidingGround = m_iscollidingGround; | ||
367 | } | ||
368 | } | ||
369 | |||
370 | /// <summary> | ||
371 | /// Returns if the avatar is colliding with an object | ||
372 | /// </summary> | ||
373 | public override bool CollidingObj | ||
374 | { | ||
375 | get { return m_iscollidingObj; } | ||
376 | set | ||
377 | { | ||
378 | m_iscollidingObj = value; | ||
379 | if (value) | ||
380 | m_pidControllerActive = false; | ||
381 | else | ||
382 | m_pidControllerActive = true; | ||
383 | } | ||
384 | } | ||
385 | |||
386 | /// <summary> | ||
387 | /// turn the PID controller on or off. | ||
388 | /// The PID Controller will turn on all by itself in many situations | ||
389 | /// </summary> | ||
390 | /// <param name="status"></param> | ||
391 | public void SetPidStatus(bool status) | ||
392 | { | ||
393 | m_pidControllerActive = status; | ||
394 | } | ||
395 | |||
396 | public override bool Stopped | ||
397 | { | ||
398 | get { return _zeroFlag; } | ||
399 | } | ||
400 | |||
401 | /// <summary> | ||
402 | /// This 'puts' an avatar somewhere in the physics space. | ||
403 | /// Not really a good choice unless you 'know' it's a good | ||
404 | /// spot otherwise you're likely to orbit the avatar. | ||
405 | /// </summary> | ||
406 | public override Vector3 Position | ||
407 | { | ||
408 | get { return _position; } | ||
409 | set | ||
410 | { | ||
411 | if (Body == IntPtr.Zero || Shell == IntPtr.Zero) | ||
412 | { | ||
413 | if (value.IsFinite()) | ||
414 | { | ||
415 | if (value.Z > 9999999f) | ||
416 | { | ||
417 | value.Z = _parent_scene.GetTerrainHeightAtXY(127, 127) + 5; | ||
418 | } | ||
419 | if (value.Z < -90000f) | ||
420 | { | ||
421 | value.Z = _parent_scene.GetTerrainHeightAtXY(127, 127) + 5; | ||
422 | } | ||
423 | |||
424 | _position.X = value.X; | ||
425 | _position.Y = value.Y; | ||
426 | _position.Z = value.Z; | ||
427 | |||
428 | m_taintPosition.X = value.X; | ||
429 | m_taintPosition.Y = value.Y; | ||
430 | m_taintPosition.Z = value.Z; | ||
431 | _parent_scene.AddPhysicsActorTaint(this); | ||
432 | } | ||
433 | else | ||
434 | { | ||
435 | m_log.Warn("[PHYSICS]: Got a NaN Position from Scene on a Character"); | ||
436 | } | ||
437 | } | ||
438 | } | ||
439 | } | ||
440 | |||
441 | public override Vector3 RotationalVelocity | ||
442 | { | ||
443 | get { return m_rotationalVelocity; } | ||
444 | set { m_rotationalVelocity = value; } | ||
445 | } | ||
446 | |||
447 | /// <summary> | ||
448 | /// This property sets the height of the avatar only. We use the height to make sure the avatar stands up straight | ||
449 | /// and use it to offset landings properly | ||
450 | /// </summary> | ||
451 | public override Vector3 Size | ||
452 | { | ||
453 | get { return new Vector3(CAPSULE_RADIUS * 2, CAPSULE_RADIUS * 2, CAPSULE_LENGTH); } | ||
454 | set | ||
455 | { | ||
456 | if (value.IsFinite()) | ||
457 | { | ||
458 | m_pidControllerActive = true; | ||
459 | |||
460 | Vector3 SetSize = value; | ||
461 | m_tainted_CAPSULE_LENGTH = (SetSize.Z*1.15f) - CAPSULE_RADIUS*2.0f; | ||
462 | //m_log.Info("[SIZE]: " + CAPSULE_LENGTH.ToString()); | ||
463 | |||
464 | Velocity = Vector3.Zero; | ||
465 | |||
466 | _parent_scene.AddPhysicsActorTaint(this); | ||
467 | } | ||
468 | else | ||
469 | { | ||
470 | m_log.Warn("[PHYSICS]: Got a NaN Size from Scene on a Character"); | ||
471 | } | ||
472 | } | ||
473 | } | ||
474 | |||
475 | private void AlignAvatarTiltWithCurrentDirectionOfMovement(Vector3 movementVector) | ||
476 | { | ||
477 | movementVector.Z = 0f; | ||
478 | float magnitude = (float)Math.Sqrt((double)(movementVector.X * movementVector.X + movementVector.Y * movementVector.Y)); | ||
479 | if (magnitude < 0.1f) return; | ||
480 | |||
481 | // normalize the velocity vector | ||
482 | float invMagnitude = 1.0f / magnitude; | ||
483 | movementVector.X *= invMagnitude; | ||
484 | movementVector.Y *= invMagnitude; | ||
485 | |||
486 | // if we change the capsule heading too often, the capsule can fall down | ||
487 | // therefore we snap movement vector to just 1 of 4 predefined directions (ne, nw, se, sw), | ||
488 | // meaning only 4 possible capsule tilt orientations | ||
489 | if (movementVector.X > 0) | ||
490 | { | ||
491 | // east | ||
492 | if (movementVector.Y > 0) | ||
493 | { | ||
494 | // northeast | ||
495 | movementVector.X = (float)Math.Sqrt(2.0); | ||
496 | movementVector.Y = (float)Math.Sqrt(2.0); | ||
497 | } | ||
498 | else | ||
499 | { | ||
500 | // southeast | ||
501 | movementVector.X = (float)Math.Sqrt(2.0); | ||
502 | movementVector.Y = -(float)Math.Sqrt(2.0); | ||
503 | } | ||
504 | } | ||
505 | else | ||
506 | { | ||
507 | // west | ||
508 | if (movementVector.Y > 0) | ||
509 | { | ||
510 | // northwest | ||
511 | movementVector.X = -(float)Math.Sqrt(2.0); | ||
512 | movementVector.Y = (float)Math.Sqrt(2.0); | ||
513 | } | ||
514 | else | ||
515 | { | ||
516 | // southwest | ||
517 | movementVector.X = -(float)Math.Sqrt(2.0); | ||
518 | movementVector.Y = -(float)Math.Sqrt(2.0); | ||
519 | } | ||
520 | } | ||
521 | |||
522 | |||
523 | // movementVector.Z is zero | ||
524 | |||
525 | // calculate tilt components based on desired amount of tilt and current (snapped) heading. | ||
526 | // the "-" sign is to force the tilt to be OPPOSITE the direction of movement. | ||
527 | float xTiltComponent = -movementVector.X * m_tiltMagnitudeWhenProjectedOnXYPlane; | ||
528 | float yTiltComponent = -movementVector.Y * m_tiltMagnitudeWhenProjectedOnXYPlane; | ||
529 | |||
530 | //m_log.Debug("[PHYSICS] changing avatar tilt"); | ||
531 | d.JointSetAMotorParam(Amotor, (int)dParam.LowStop, xTiltComponent); | ||
532 | d.JointSetAMotorParam(Amotor, (int)dParam.HiStop, xTiltComponent); // must be same as lowstop, else a different, spurious tilt is introduced | ||
533 | d.JointSetAMotorParam(Amotor, (int)dParam.LoStop2, yTiltComponent); | ||
534 | d.JointSetAMotorParam(Amotor, (int)dParam.HiStop2, yTiltComponent); // same as lowstop | ||
535 | d.JointSetAMotorParam(Amotor, (int)dParam.LoStop3, 0f); | ||
536 | d.JointSetAMotorParam(Amotor, (int)dParam.HiStop3, 0f); // same as lowstop | ||
537 | } | ||
538 | |||
539 | /// <summary> | ||
540 | /// This creates the Avatar's physical Surrogate at the position supplied | ||
541 | /// </summary> | ||
542 | /// <param name="npositionX"></param> | ||
543 | /// <param name="npositionY"></param> | ||
544 | /// <param name="npositionZ"></param> | ||
545 | |||
546 | // WARNING: This MUST NOT be called outside of ProcessTaints, else we can have unsynchronized access | ||
547 | // to ODE internals. ProcessTaints is called from within thread-locked Simulate(), so it is the only | ||
548 | // place that is safe to call this routine AvatarGeomAndBodyCreation. | ||
549 | private void AvatarGeomAndBodyCreation(float npositionX, float npositionY, float npositionZ, float tensor) | ||
550 | { | ||
551 | //CAPSULE_LENGTH = -5; | ||
552 | //CAPSULE_RADIUS = -5; | ||
553 | int dAMotorEuler = 1; | ||
554 | _parent_scene.waitForSpaceUnlock(_parent_scene.space); | ||
555 | if (CAPSULE_LENGTH <= 0) | ||
556 | { | ||
557 | m_log.Warn("[PHYSICS]: The capsule size you specified in opensim.ini is invalid! Setting it to the smallest possible size!"); | ||
558 | CAPSULE_LENGTH = 0.01f; | ||
559 | |||
560 | } | ||
561 | |||
562 | if (CAPSULE_RADIUS <= 0) | ||
563 | { | ||
564 | m_log.Warn("[PHYSICS]: The capsule size you specified in opensim.ini is invalid! Setting it to the smallest possible size!"); | ||
565 | CAPSULE_RADIUS = 0.01f; | ||
566 | |||
567 | } | ||
568 | |||
569 | if(Shell != IntPtr.Zero) | ||
570 | { | ||
571 | try | ||
572 | { | ||
573 | d.GeomDestroy(Shell); | ||
574 | } | ||
575 | catch (System.AccessViolationException) | ||
576 | { | ||
577 | m_log.Error("[PHYSICS]: PrimGeom dead"); | ||
578 | } | ||
579 | // Remove any old entries | ||
580 | //string tShell; | ||
581 | //_parent_scene.geom_name_map.TryGetValue(Shell, out tShell); | ||
582 | //Console.WriteLine("**** Remove {0}", tShell); | ||
583 | if(_parent_scene.geom_name_map.ContainsKey(Shell)) _parent_scene.geom_name_map.Remove(Shell); | ||
584 | if(_parent_scene.actor_name_map.ContainsKey(Shell)) _parent_scene.actor_name_map.Remove(Shell); | ||
585 | } | ||
586 | |||
587 | Shell = d.CreateCapsule(_parent_scene.space, CAPSULE_RADIUS, CAPSULE_LENGTH); | ||
588 | _parent_scene.geom_name_map[Shell] = m_name; | ||
589 | _parent_scene.actor_name_map[Shell] = (PhysicsActor)this; | ||
590 | //Console.WriteLine("**** Create {2} Dicts: actor={0} name={1}", _parent_scene.actor_name_map.Count, _parent_scene.geom_name_map.Count, m_name); | ||
591 | |||
592 | d.GeomSetCategoryBits(Shell, (int)m_collisionCategories); | ||
593 | d.GeomSetCollideBits(Shell, (int)m_collisionFlags); | ||
594 | |||
595 | d.MassSetCapsuleTotal(out ShellMass, m_mass, 2, CAPSULE_RADIUS, CAPSULE_LENGTH); | ||
596 | Body = d.BodyCreate(_parent_scene.world); | ||
597 | d.BodySetPosition(Body, npositionX, npositionY, npositionZ); | ||
598 | |||
599 | _position.X = npositionX; | ||
600 | _position.Y = npositionY; | ||
601 | _position.Z = npositionZ; | ||
602 | |||
603 | |||
604 | m_taintPosition.X = npositionX; | ||
605 | m_taintPosition.Y = npositionY; | ||
606 | m_taintPosition.Z = npositionZ; | ||
607 | |||
608 | d.BodySetMass(Body, ref ShellMass); | ||
609 | d.Matrix3 m_caprot; | ||
610 | // 90 Stand up on the cap of the capped cyllinder | ||
611 | if (_parent_scene.IsAvCapsuleTilted) | ||
612 | { | ||
613 | d.RFromAxisAndAngle(out m_caprot, 1, 0, 1, (float)(Math.PI / 2)); | ||
614 | } | ||
615 | else | ||
616 | { | ||
617 | d.RFromAxisAndAngle(out m_caprot, 0, 0, 1, (float)(Math.PI / 2)); | ||
618 | } | ||
619 | |||
620 | |||
621 | d.GeomSetRotation(Shell, ref m_caprot); | ||
622 | d.BodySetRotation(Body, ref m_caprot); | ||
623 | |||
624 | d.GeomSetBody(Shell, Body); | ||
625 | |||
626 | |||
627 | // The purpose of the AMotor here is to keep the avatar's physical | ||
628 | // surrogate from rotating while moving | ||
629 | Amotor = d.JointCreateAMotor(_parent_scene.world, IntPtr.Zero); | ||
630 | d.JointAttach(Amotor, Body, IntPtr.Zero); | ||
631 | d.JointSetAMotorMode(Amotor, dAMotorEuler); | ||
632 | d.JointSetAMotorNumAxes(Amotor, 3); | ||
633 | d.JointSetAMotorAxis(Amotor, 0, 0, 1, 0, 0); | ||
634 | d.JointSetAMotorAxis(Amotor, 1, 0, 0, 1, 0); | ||
635 | d.JointSetAMotorAxis(Amotor, 2, 0, 0, 0, 1); | ||
636 | d.JointSetAMotorAngle(Amotor, 0, 0); | ||
637 | d.JointSetAMotorAngle(Amotor, 1, 0); | ||
638 | d.JointSetAMotorAngle(Amotor, 2, 0); | ||
639 | |||
640 | // These lowstops and high stops are effectively (no wiggle room) | ||
641 | if (_parent_scene.IsAvCapsuleTilted) | ||
642 | { | ||
643 | d.JointSetAMotorParam(Amotor, (int)dParam.LowStop, -0.000000000001f); | ||
644 | d.JointSetAMotorParam(Amotor, (int)dParam.LoStop3, -0.000000000001f); | ||
645 | d.JointSetAMotorParam(Amotor, (int)dParam.LoStop2, -0.000000000001f); | ||
646 | d.JointSetAMotorParam(Amotor, (int)dParam.HiStop, 0.000000000001f); | ||
647 | d.JointSetAMotorParam(Amotor, (int)dParam.HiStop3, 0.000000000001f); | ||
648 | d.JointSetAMotorParam(Amotor, (int)dParam.HiStop2, 0.000000000001f); | ||
649 | } | ||
650 | else | ||
651 | { | ||
652 | #region Documentation of capsule motor LowStop and HighStop parameters | ||
653 | // Intentionally introduce some tilt into the capsule by setting | ||
654 | // the motor stops to small epsilon values. This small tilt prevents | ||
655 | // the capsule from falling into the terrain; a straight-up capsule | ||
656 | // (with -0..0 motor stops) falls into the terrain for reasons yet | ||
657 | // to be comprehended in their entirety. | ||
658 | #endregion | ||
659 | AlignAvatarTiltWithCurrentDirectionOfMovement(Vector3.Zero); | ||
660 | d.JointSetAMotorParam(Amotor, (int)dParam.LowStop, 0.08f); | ||
661 | d.JointSetAMotorParam(Amotor, (int)dParam.LoStop3, -0f); | ||
662 | d.JointSetAMotorParam(Amotor, (int)dParam.LoStop2, 0.08f); | ||
663 | d.JointSetAMotorParam(Amotor, (int)dParam.HiStop, 0.08f); // must be same as lowstop, else a different, spurious tilt is introduced | ||
664 | d.JointSetAMotorParam(Amotor, (int)dParam.HiStop3, 0f); // same as lowstop | ||
665 | d.JointSetAMotorParam(Amotor, (int)dParam.HiStop2, 0.08f); // same as lowstop | ||
666 | } | ||
667 | |||
668 | // Fudge factor is 1f by default, we're setting it to 0. We don't want it to Fudge or the | ||
669 | // capped cyllinder will fall over | ||
670 | d.JointSetAMotorParam(Amotor, (int)dParam.FudgeFactor, 0f); | ||
671 | d.JointSetAMotorParam(Amotor, (int)dParam.FMax, tensor); | ||
672 | |||
673 | //d.Matrix3 bodyrotation = d.BodyGetRotation(Body); | ||
674 | //d.QfromR( | ||
675 | //d.Matrix3 checkrotation = new d.Matrix3(0.7071068,0.5, -0.7071068, | ||
676 | // | ||
677 | //m_log.Info("[PHYSICSAV]: Rotation: " + bodyrotation.M00 + " : " + bodyrotation.M01 + " : " + bodyrotation.M02 + " : " + bodyrotation.M10 + " : " + bodyrotation.M11 + " : " + bodyrotation.M12 + " : " + bodyrotation.M20 + " : " + bodyrotation.M21 + " : " + bodyrotation.M22); | ||
678 | //standupStraight(); | ||
679 | } | ||
680 | |||
681 | // | ||
682 | /// <summary> | ||
683 | /// Uses the capped cyllinder volume formula to calculate the avatar's mass. | ||
684 | /// This may be used in calculations in the scene/scenepresence | ||
685 | /// </summary> | ||
686 | public override float Mass | ||
687 | { | ||
688 | get | ||
689 | { | ||
690 | float AVvolume = (float) (Math.PI*Math.Pow(CAPSULE_RADIUS, 2)*CAPSULE_LENGTH); | ||
691 | return m_density*AVvolume; | ||
692 | } | ||
693 | } | ||
694 | public override void link(PhysicsActor obj) | ||
695 | { | ||
696 | |||
697 | } | ||
698 | |||
699 | public override void delink() | ||
700 | { | ||
701 | |||
702 | } | ||
703 | |||
704 | public override void LockAngularMotion(Vector3 axis) | ||
705 | { | ||
706 | |||
707 | } | ||
708 | |||
709 | // This code is very useful. Written by DanX0r. We're just not using it right now. | ||
710 | // Commented out to prevent a warning. | ||
711 | // | ||
712 | // private void standupStraight() | ||
713 | // { | ||
714 | // // The purpose of this routine here is to quickly stabilize the Body while it's popped up in the air. | ||
715 | // // The amotor needs a few seconds to stabilize so without it, the avatar shoots up sky high when you | ||
716 | // // change appearance and when you enter the simulator | ||
717 | // // After this routine is done, the amotor stabilizes much quicker | ||
718 | // d.Vector3 feet; | ||
719 | // d.Vector3 head; | ||
720 | // d.BodyGetRelPointPos(Body, 0.0f, 0.0f, -1.0f, out feet); | ||
721 | // d.BodyGetRelPointPos(Body, 0.0f, 0.0f, 1.0f, out head); | ||
722 | // float posture = head.Z - feet.Z; | ||
723 | |||
724 | // // restoring force proportional to lack of posture: | ||
725 | // float servo = (2.5f - posture) * POSTURE_SERVO; | ||
726 | // d.BodyAddForceAtRelPos(Body, 0.0f, 0.0f, servo, 0.0f, 0.0f, 1.0f); | ||
727 | // d.BodyAddForceAtRelPos(Body, 0.0f, 0.0f, -servo, 0.0f, 0.0f, -1.0f); | ||
728 | // //d.Matrix3 bodyrotation = d.BodyGetRotation(Body); | ||
729 | // //m_log.Info("[PHYSICSAV]: Rotation: " + bodyrotation.M00 + " : " + bodyrotation.M01 + " : " + bodyrotation.M02 + " : " + bodyrotation.M10 + " : " + bodyrotation.M11 + " : " + bodyrotation.M12 + " : " + bodyrotation.M20 + " : " + bodyrotation.M21 + " : " + bodyrotation.M22); | ||
730 | // } | ||
731 | |||
732 | public override Vector3 Force | ||
733 | { | ||
734 | get { return _target_velocity; } | ||
735 | set { return; } | ||
736 | } | ||
737 | |||
738 | public override int VehicleType | ||
739 | { | ||
740 | get { return 0; } | ||
741 | set { return; } | ||
742 | } | ||
743 | |||
744 | public override void VehicleFloatParam(int param, float value) | ||
745 | { | ||
746 | |||
747 | } | ||
748 | |||
749 | public override void VehicleVectorParam(int param, Vector3 value) | ||
750 | { | ||
751 | |||
752 | } | ||
753 | |||
754 | public override void VehicleRotationParam(int param, Quaternion rotation) | ||
755 | { | ||
756 | |||
757 | } | ||
758 | |||
759 | public override void VehicleFlags(int flags, bool remove) | ||
760 | { | ||
761 | } | ||
762 | |||
763 | public override void SetVolumeDetect(int param) | ||
764 | { | ||
765 | |||
766 | } | ||
767 | |||
768 | public override Vector3 CenterOfMass | ||
769 | { | ||
770 | get { return Vector3.Zero; } | ||
771 | } | ||
772 | |||
773 | public override Vector3 GeometricCenter | ||
774 | { | ||
775 | get { return Vector3.Zero; } | ||
776 | } | ||
777 | |||
778 | public override PrimitiveBaseShape Shape | ||
779 | { | ||
780 | set { return; } | ||
781 | } | ||
782 | |||
783 | public override Vector3 Velocity | ||
784 | { | ||
785 | get { | ||
786 | // There's a problem with Vector3.Zero! Don't Use it Here! | ||
787 | if (_zeroFlag) | ||
788 | return Vector3.Zero; | ||
789 | m_lastUpdateSent = false; | ||
790 | return _velocity; | ||
791 | } | ||
792 | set | ||
793 | { | ||
794 | if (value.IsFinite()) | ||
795 | { | ||
796 | m_pidControllerActive = true; | ||
797 | _target_velocity = value; | ||
798 | } | ||
799 | else | ||
800 | { | ||
801 | m_log.Warn("[PHYSICS]: Got a NaN velocity from Scene in a Character"); | ||
802 | } | ||
803 | } | ||
804 | } | ||
805 | |||
806 | public override Vector3 Torque | ||
807 | { | ||
808 | get { return Vector3.Zero; } | ||
809 | set { return; } | ||
810 | } | ||
811 | |||
812 | public override float CollisionScore | ||
813 | { | ||
814 | get { return 0f; } | ||
815 | set { } | ||
816 | } | ||
817 | |||
818 | public override bool Kinematic | ||
819 | { | ||
820 | get { return false; } | ||
821 | set { } | ||
822 | } | ||
823 | |||
824 | public override Quaternion Orientation | ||
825 | { | ||
826 | get { return Quaternion.Identity; } | ||
827 | set { | ||
828 | //Matrix3 or = Orientation.ToRotationMatrix(); | ||
829 | //d.Matrix3 ord = new d.Matrix3(or.m00, or.m10, or.m20, or.m01, or.m11, or.m21, or.m02, or.m12, or.m22); | ||
830 | //d.BodySetRotation(Body, ref ord); | ||
831 | } | ||
832 | } | ||
833 | |||
834 | public override Vector3 Acceleration | ||
835 | { | ||
836 | get { return _acceleration; } | ||
837 | } | ||
838 | |||
839 | public void SetAcceleration(Vector3 accel) | ||
840 | { | ||
841 | m_pidControllerActive = true; | ||
842 | _acceleration = accel; | ||
843 | } | ||
844 | |||
845 | /// <summary> | ||
846 | /// Adds the force supplied to the Target Velocity | ||
847 | /// The PID controller takes this target velocity and tries to make it a reality | ||
848 | /// </summary> | ||
849 | /// <param name="force"></param> | ||
850 | public override void AddForce(Vector3 force, bool pushforce) | ||
851 | { | ||
852 | if (force.IsFinite()) | ||
853 | { | ||
854 | if (pushforce) | ||
855 | { | ||
856 | m_pidControllerActive = false; | ||
857 | force *= 100f; | ||
858 | doForce(force); | ||
859 | // If uncommented, things get pushed off world | ||
860 | // | ||
861 | // m_log.Debug("Push!"); | ||
862 | // _target_velocity.X += force.X; | ||
863 | // _target_velocity.Y += force.Y; | ||
864 | // _target_velocity.Z += force.Z; | ||
865 | } | ||
866 | else | ||
867 | { | ||
868 | m_pidControllerActive = true; | ||
869 | _target_velocity.X += force.X; | ||
870 | _target_velocity.Y += force.Y; | ||
871 | _target_velocity.Z += force.Z; | ||
872 | } | ||
873 | } | ||
874 | else | ||
875 | { | ||
876 | m_log.Warn("[PHYSICS]: Got a NaN force applied to a Character"); | ||
877 | } | ||
878 | //m_lastUpdateSent = false; | ||
879 | } | ||
880 | |||
881 | public override void AddAngularForce(Vector3 force, bool pushforce) | ||
882 | { | ||
883 | |||
884 | } | ||
885 | |||
886 | /// <summary> | ||
887 | /// After all of the forces add up with 'add force' we apply them with doForce | ||
888 | /// </summary> | ||
889 | /// <param name="force"></param> | ||
890 | public void doForce(Vector3 force) | ||
891 | { | ||
892 | if (!collidelock) | ||
893 | { | ||
894 | d.BodyAddForce(Body, force.X, force.Y, force.Z); | ||
895 | //d.BodySetRotation(Body, ref m_StandUpRotation); | ||
896 | //standupStraight(); | ||
897 | |||
898 | } | ||
899 | } | ||
900 | |||
901 | public override void SetMomentum(Vector3 momentum) | ||
902 | { | ||
903 | } | ||
904 | |||
905 | |||
906 | /// <summary> | ||
907 | /// Called from Simulate | ||
908 | /// This is the avatar's movement control + PID Controller | ||
909 | /// </summary> | ||
910 | /// <param name="timeStep"></param> | ||
911 | public void Move(float timeStep, List<OdeCharacter> defects) | ||
912 | { | ||
913 | // no lock; for now it's only called from within Simulate() | ||
914 | |||
915 | // If the PID Controller isn't active then we set our force | ||
916 | // calculating base velocity to the current position | ||
917 | |||
918 | if (Body == IntPtr.Zero) | ||
919 | return; | ||
920 | |||
921 | if (m_pidControllerActive == false) | ||
922 | { | ||
923 | _zeroPosition = d.BodyGetPosition(Body); | ||
924 | } | ||
925 | //PidStatus = true; | ||
926 | |||
927 | d.Vector3 localpos = d.BodyGetPosition(Body); | ||
928 | Vector3 localPos = new Vector3(localpos.X, localpos.Y, localpos.Z); | ||
929 | |||
930 | if (!localPos.IsFinite()) | ||
931 | { | ||
932 | |||
933 | m_log.Warn("[PHYSICS]: Avatar Position is non-finite!"); | ||
934 | defects.Add(this); | ||
935 | // _parent_scene.RemoveCharacter(this); | ||
936 | |||
937 | // destroy avatar capsule and related ODE data | ||
938 | if (Amotor != IntPtr.Zero) | ||
939 | { | ||
940 | // Kill the Amotor | ||
941 | d.JointDestroy(Amotor); | ||
942 | Amotor = IntPtr.Zero; | ||
943 | } | ||
944 | |||
945 | //kill the Geometry | ||
946 | _parent_scene.waitForSpaceUnlock(_parent_scene.space); | ||
947 | |||
948 | if (Body != IntPtr.Zero) | ||
949 | { | ||
950 | //kill the body | ||
951 | d.BodyDestroy(Body); | ||
952 | |||
953 | Body = IntPtr.Zero; | ||
954 | } | ||
955 | |||
956 | if(Shell != IntPtr.Zero) | ||
957 | { | ||
958 | try | ||
959 | { | ||
960 | d.GeomDestroy(Shell); | ||
961 | } | ||
962 | catch (System.AccessViolationException) | ||
963 | { | ||
964 | m_log.Error("[PHYSICS]: PrimGeom dead"); | ||
965 | } | ||
966 | // Remove any old entries | ||
967 | //string tShell; | ||
968 | //_parent_scene.geom_name_map.TryGetValue(Shell, out tShell); | ||
969 | //Console.WriteLine("**** Remove {0}", tShell); | ||
970 | |||
971 | if(_parent_scene.geom_name_map.ContainsKey(Shell)) _parent_scene.geom_name_map.Remove(Shell); | ||
972 | if(_parent_scene.actor_name_map.ContainsKey(Shell)) _parent_scene.actor_name_map.Remove(Shell); | ||
973 | Shell = IntPtr.Zero; | ||
974 | } | ||
975 | |||
976 | return; | ||
977 | } | ||
978 | |||
979 | Vector3 vec = Vector3.Zero; | ||
980 | d.Vector3 vel = d.BodyGetLinearVel(Body); | ||
981 | |||
982 | float movementdivisor = 1f; | ||
983 | |||
984 | if (!m_alwaysRun) | ||
985 | { | ||
986 | movementdivisor = walkDivisor; | ||
987 | } | ||
988 | else | ||
989 | { | ||
990 | movementdivisor = runDivisor; | ||
991 | } | ||
992 | |||
993 | // if velocity is zero, use position control; otherwise, velocity control | ||
994 | if (_target_velocity.X == 0.0f && _target_velocity.Y == 0.0f && _target_velocity.Z == 0.0f && m_iscolliding) | ||
995 | { | ||
996 | // keep track of where we stopped. No more slippin' & slidin' | ||
997 | if (!_zeroFlag) | ||
998 | { | ||
999 | _zeroFlag = true; | ||
1000 | _zeroPosition = d.BodyGetPosition(Body); | ||
1001 | } | ||
1002 | if (m_pidControllerActive) | ||
1003 | { | ||
1004 | // We only want to deactivate the PID Controller if we think we want to have our surrogate | ||
1005 | // react to the physics scene by moving it's position. | ||
1006 | // Avatar to Avatar collisions | ||
1007 | // Prim to avatar collisions | ||
1008 | |||
1009 | d.Vector3 pos = d.BodyGetPosition(Body); | ||
1010 | float errX = _zeroPosition.X - pos.X; | ||
1011 | float errY = _zeroPosition.Y - pos.Y; | ||
1012 | if( (Math.Abs(errX) > 0.1f) || (Math.Abs(errY) > 0.1f) ) | ||
1013 | { | ||
1014 | vec.X = (_target_velocity.X - vel.X) * (PID_D) + (errX) * (PID_P * 2); | ||
1015 | vec.Y = (_target_velocity.Y - vel.Y) * (PID_D) + (errY) * (PID_P * 2); | ||
1016 | } | ||
1017 | else | ||
1018 | { // close, jump to lateral destination | ||
1019 | d.BodySetPosition(Body, _zeroPosition.X, _zeroPosition.Y, pos.Z); | ||
1020 | } | ||
1021 | if (flying) | ||
1022 | { | ||
1023 | vec.Z = (_target_velocity.Z - vel.Z) * (PID_D) + (_zeroPosition.Z - pos.Z) * PID_P; | ||
1024 | } | ||
1025 | } | ||
1026 | //PidStatus = true; | ||
1027 | } | ||
1028 | else | ||
1029 | { | ||
1030 | m_pidControllerActive = true; | ||
1031 | _zeroFlag = false; | ||
1032 | if (m_iscolliding && !flying) | ||
1033 | { | ||
1034 | // We're standing on something | ||
1035 | vec.X = ((_target_velocity.X / movementdivisor) - vel.X) * (PID_D); | ||
1036 | vec.Y = ((_target_velocity.Y / movementdivisor) - vel.Y) * (PID_D); | ||
1037 | } | ||
1038 | else if (m_iscolliding && flying) | ||
1039 | { | ||
1040 | // We're flying and colliding with something | ||
1041 | vec.X = ((_target_velocity.X/movementdivisor) - vel.X)*(PID_D / 16); | ||
1042 | vec.Y = ((_target_velocity.Y/movementdivisor) - vel.Y)*(PID_D / 16); | ||
1043 | } | ||
1044 | else if (!m_iscolliding && flying) | ||
1045 | { | ||
1046 | // we're in mid air suspended | ||
1047 | vec.X = ((_target_velocity.X / movementdivisor) - vel.X) * (PID_D/6); | ||
1048 | vec.Y = ((_target_velocity.Y / movementdivisor) - vel.Y) * (PID_D/6); | ||
1049 | } | ||
1050 | |||
1051 | if (m_iscolliding && !flying && _target_velocity.Z > 0.0f) | ||
1052 | { | ||
1053 | // We're colliding with something and we're not flying but we're moving | ||
1054 | // This means we're walking or running. | ||
1055 | d.Vector3 pos = d.BodyGetPosition(Body); | ||
1056 | vec.Z = (_target_velocity.Z - vel.Z)*PID_D + (_zeroPosition.Z - pos.Z)*PID_P; | ||
1057 | if (_target_velocity.X > 0) | ||
1058 | { | ||
1059 | vec.X = ((_target_velocity.X - vel.X)/1.2f)*PID_D; | ||
1060 | } | ||
1061 | if (_target_velocity.Y > 0) | ||
1062 | { | ||
1063 | vec.Y = ((_target_velocity.Y - vel.Y)/1.2f)*PID_D; | ||
1064 | } | ||
1065 | } | ||
1066 | else if (!m_iscolliding && !flying) | ||
1067 | { | ||
1068 | // we're not colliding and we're not flying so that means we're falling! | ||
1069 | // m_iscolliding includes collisions with the ground. | ||
1070 | |||
1071 | // d.Vector3 pos = d.BodyGetPosition(Body); | ||
1072 | if (Math.Abs(_target_velocity.X) > 0) | ||
1073 | { | ||
1074 | vec.X = ((_target_velocity.X - vel.X)/1.2f)*PID_D; | ||
1075 | } | ||
1076 | if (Math.Abs(_target_velocity.Y) > 0) | ||
1077 | { | ||
1078 | vec.Y = ((_target_velocity.Y - vel.Y)/1.2f)*PID_D; | ||
1079 | } | ||
1080 | } | ||
1081 | |||
1082 | if (flying) | ||
1083 | { | ||
1084 | vec.Z = (_target_velocity.Z - vel.Z) * (PID_D); | ||
1085 | } | ||
1086 | } | ||
1087 | if (flying) | ||
1088 | { | ||
1089 | vec.Z += ((-1 * _parent_scene.gravityz)*m_mass); | ||
1090 | |||
1091 | //Added for auto fly height. Kitto Flora | ||
1092 | //d.Vector3 pos = d.BodyGetPosition(Body); | ||
1093 | float target_altitude = _parent_scene.GetTerrainHeightAtXY(_position.X, _position.Y) + MinimumGroundFlightOffset; | ||
1094 | |||
1095 | if (_position.Z < target_altitude) | ||
1096 | { | ||
1097 | vec.Z += (target_altitude - _position.Z) * PID_P * 5.0f; | ||
1098 | } | ||
1099 | // end add Kitto Flora | ||
1100 | } | ||
1101 | if (vec.IsFinite()) | ||
1102 | { | ||
1103 | if (!vec.ApproxEquals(Vector3.Zero, 0.01f)) | ||
1104 | { | ||
1105 | doForce(vec); | ||
1106 | if (!_zeroFlag) | ||
1107 | { | ||
1108 | AlignAvatarTiltWithCurrentDirectionOfMovement(vec); | ||
1109 | } | ||
1110 | } | ||
1111 | } | ||
1112 | else | ||
1113 | { | ||
1114 | m_log.Warn("[PHYSICS]: Got a NaN force vector in Move()"); | ||
1115 | m_log.Warn("[PHYSICS]: Avatar Position is non-finite!"); | ||
1116 | defects.Add(this); | ||
1117 | // _parent_scene.RemoveCharacter(this); | ||
1118 | // destroy avatar capsule and related ODE data | ||
1119 | if (Amotor != IntPtr.Zero) | ||
1120 | { | ||
1121 | // Kill the Amotor | ||
1122 | d.JointDestroy(Amotor); | ||
1123 | Amotor = IntPtr.Zero; | ||
1124 | } | ||
1125 | //kill the Geometry | ||
1126 | _parent_scene.waitForSpaceUnlock(_parent_scene.space); | ||
1127 | |||
1128 | if (Body != IntPtr.Zero) | ||
1129 | { | ||
1130 | //kill the body | ||
1131 | d.BodyDestroy(Body); | ||
1132 | |||
1133 | Body = IntPtr.Zero; | ||
1134 | } | ||
1135 | |||
1136 | if(Shell != IntPtr.Zero) | ||
1137 | { | ||
1138 | try | ||
1139 | { | ||
1140 | d.GeomDestroy(Shell); | ||
1141 | } | ||
1142 | catch (System.AccessViolationException) | ||
1143 | { | ||
1144 | m_log.Error("[PHYSICS]: PrimGeom dead"); | ||
1145 | } | ||
1146 | // Remove any old entries | ||
1147 | //string tShell; | ||
1148 | //_parent_scene.geom_name_map.TryGetValue(Shell, out tShell); | ||
1149 | //Console.WriteLine("**** Remove {0}", tShell); | ||
1150 | |||
1151 | if(_parent_scene.geom_name_map.ContainsKey(Shell)) _parent_scene.geom_name_map.Remove(Shell); | ||
1152 | if(_parent_scene.actor_name_map.ContainsKey(Shell)) _parent_scene.actor_name_map.Remove(Shell); | ||
1153 | Shell = IntPtr.Zero; | ||
1154 | } | ||
1155 | } | ||
1156 | } | ||
1157 | |||
1158 | /// <summary> | ||
1159 | /// Updates the reported position and velocity. This essentially sends the data up to ScenePresence. | ||
1160 | /// </summary> | ||
1161 | public void UpdatePositionAndVelocity() | ||
1162 | { | ||
1163 | // no lock; called from Simulate() -- if you call this from elsewhere, gotta lock or do Monitor.Enter/Exit! | ||
1164 | d.Vector3 vec; | ||
1165 | try | ||
1166 | { | ||
1167 | vec = d.BodyGetPosition(Body); | ||
1168 | } | ||
1169 | catch (NullReferenceException) | ||
1170 | { | ||
1171 | bad = true; | ||
1172 | _parent_scene.BadCharacter(this); | ||
1173 | vec = new d.Vector3(_position.X, _position.Y, _position.Z); | ||
1174 | base.RaiseOutOfBounds(_position); // Tells ScenePresence that there's a problem! | ||
1175 | m_log.WarnFormat("[ODEPLUGIN]: Avatar Null reference for Avatar {0}, physical actor {1}", m_name, m_uuid); | ||
1176 | } | ||
1177 | |||
1178 | |||
1179 | // kluge to keep things in bounds. ODE lets dead avatars drift away (they should be removed!) | ||
1180 | if (vec.X < 0.0f) vec.X = 0.0f; | ||
1181 | if (vec.Y < 0.0f) vec.Y = 0.0f; | ||
1182 | if (vec.X > (int)_parent_scene.WorldExtents.X - 0.05f) vec.X = (int)_parent_scene.WorldExtents.X - 0.05f; | ||
1183 | if (vec.Y > (int)_parent_scene.WorldExtents.Y - 0.05f) vec.Y = (int)_parent_scene.WorldExtents.Y - 0.05f; | ||
1184 | |||
1185 | _position.X = vec.X; | ||
1186 | _position.Y = vec.Y; | ||
1187 | _position.Z = vec.Z; | ||
1188 | |||
1189 | // Did we move last? = zeroflag | ||
1190 | // This helps keep us from sliding all over | ||
1191 | |||
1192 | if (_zeroFlag) | ||
1193 | { | ||
1194 | _velocity.X = 0.0f; | ||
1195 | _velocity.Y = 0.0f; | ||
1196 | _velocity.Z = 0.0f; | ||
1197 | |||
1198 | // Did we send out the 'stopped' message? | ||
1199 | if (!m_lastUpdateSent) | ||
1200 | { | ||
1201 | m_lastUpdateSent = true; | ||
1202 | //base.RequestPhysicsterseUpdate(); | ||
1203 | |||
1204 | } | ||
1205 | } | ||
1206 | else | ||
1207 | { | ||
1208 | m_lastUpdateSent = false; | ||
1209 | try | ||
1210 | { | ||
1211 | vec = d.BodyGetLinearVel(Body); | ||
1212 | } | ||
1213 | catch (NullReferenceException) | ||
1214 | { | ||
1215 | vec.X = _velocity.X; | ||
1216 | vec.Y = _velocity.Y; | ||
1217 | vec.Z = _velocity.Z; | ||
1218 | } | ||
1219 | _velocity.X = (vec.X); | ||
1220 | _velocity.Y = (vec.Y); | ||
1221 | |||
1222 | _velocity.Z = (vec.Z); | ||
1223 | |||
1224 | if (_velocity.Z < -6 && !m_hackSentFall) | ||
1225 | { | ||
1226 | m_hackSentFall = true; | ||
1227 | m_pidControllerActive = false; | ||
1228 | } | ||
1229 | else if (flying && !m_hackSentFly) | ||
1230 | { | ||
1231 | //m_hackSentFly = true; | ||
1232 | //base.SendCollisionUpdate(new CollisionEventUpdate()); | ||
1233 | } | ||
1234 | else | ||
1235 | { | ||
1236 | m_hackSentFly = false; | ||
1237 | m_hackSentFall = false; | ||
1238 | } | ||
1239 | } | ||
1240 | } | ||
1241 | |||
1242 | /// <summary> | ||
1243 | /// Cleanup the things we use in the scene. | ||
1244 | /// </summary> | ||
1245 | public void Destroy() | ||
1246 | { | ||
1247 | m_tainted_isPhysical = false; | ||
1248 | _parent_scene.AddPhysicsActorTaint(this); | ||
1249 | } | ||
1250 | |||
1251 | public override void CrossingFailure() | ||
1252 | { | ||
1253 | } | ||
1254 | |||
1255 | public override Vector3 PIDTarget { set { return; } } | ||
1256 | public override bool PIDActive { set { return; } } | ||
1257 | public override float PIDTau { set { return; } } | ||
1258 | |||
1259 | public override float PIDHoverHeight { set { return; } } | ||
1260 | public override bool PIDHoverActive { set { return; } } | ||
1261 | public override PIDHoverType PIDHoverType { set { return; } } | ||
1262 | public override float PIDHoverTau { set { return; } } | ||
1263 | |||
1264 | public override Quaternion APIDTarget{ set { return; } } | ||
1265 | |||
1266 | public override bool APIDActive{ set { return; } } | ||
1267 | |||
1268 | public override float APIDStrength{ set { return; } } | ||
1269 | |||
1270 | public override float APIDDamping{ set { return; } } | ||
1271 | |||
1272 | |||
1273 | public override void SubscribeEvents(int ms) | ||
1274 | { | ||
1275 | m_requestedUpdateFrequency = ms; | ||
1276 | m_eventsubscription = ms; | ||
1277 | _parent_scene.addCollisionEventReporting(this); | ||
1278 | } | ||
1279 | public override void UnSubscribeEvents() | ||
1280 | { | ||
1281 | _parent_scene.remCollisionEventReporting(this); | ||
1282 | m_requestedUpdateFrequency = 0; | ||
1283 | m_eventsubscription = 0; | ||
1284 | } | ||
1285 | public void AddCollisionEvent(uint CollidedWith, ContactPoint contact) | ||
1286 | { | ||
1287 | if (m_eventsubscription > 0) | ||
1288 | { | ||
1289 | CollisionEventsThisFrame.addCollider(CollidedWith, contact); | ||
1290 | } | ||
1291 | } | ||
1292 | |||
1293 | public void SendCollisions() | ||
1294 | { | ||
1295 | if (m_eventsubscription > m_requestedUpdateFrequency) | ||
1296 | { | ||
1297 | if (CollisionEventsThisFrame != null) | ||
1298 | { | ||
1299 | base.SendCollisionUpdate(CollisionEventsThisFrame); | ||
1300 | } | ||
1301 | CollisionEventsThisFrame = new CollisionEventUpdate(); | ||
1302 | m_eventsubscription = 0; | ||
1303 | } | ||
1304 | } | ||
1305 | public override bool SubscribedEvents() | ||
1306 | { | ||
1307 | if (m_eventsubscription > 0) | ||
1308 | return true; | ||
1309 | return false; | ||
1310 | } | ||
1311 | |||
1312 | public void ProcessTaints(float timestep) | ||
1313 | { | ||
1314 | |||
1315 | if (m_tainted_isPhysical != m_isPhysical) | ||
1316 | { | ||
1317 | if (m_tainted_isPhysical) | ||
1318 | { | ||
1319 | // Create avatar capsule and related ODE data | ||
1320 | if (!(Shell == IntPtr.Zero && Body == IntPtr.Zero && Amotor == IntPtr.Zero)) | ||
1321 | { | ||
1322 | m_log.Warn("[PHYSICS]: re-creating the following avatar ODE data, even though it already exists - " | ||
1323 | + (Shell!=IntPtr.Zero ? "Shell ":"") | ||
1324 | + (Body!=IntPtr.Zero ? "Body ":"") | ||
1325 | + (Amotor!=IntPtr.Zero ? "Amotor ":"")); | ||
1326 | } | ||
1327 | AvatarGeomAndBodyCreation(_position.X, _position.Y, _position.Z, m_tensor); | ||
1328 | _parent_scene.AddCharacter(this); | ||
1329 | } | ||
1330 | else | ||
1331 | { | ||
1332 | _parent_scene.RemoveCharacter(this); | ||
1333 | // destroy avatar capsule and related ODE data | ||
1334 | if (Amotor != IntPtr.Zero) | ||
1335 | { | ||
1336 | // Kill the Amotor | ||
1337 | d.JointDestroy(Amotor); | ||
1338 | Amotor = IntPtr.Zero; | ||
1339 | } | ||
1340 | //kill the Geometry | ||
1341 | _parent_scene.waitForSpaceUnlock(_parent_scene.space); | ||
1342 | |||
1343 | if (Body != IntPtr.Zero) | ||
1344 | { | ||
1345 | //kill the body | ||
1346 | d.BodyDestroy(Body); | ||
1347 | Body = IntPtr.Zero; | ||
1348 | } | ||
1349 | |||
1350 | if(Shell != IntPtr.Zero) | ||
1351 | { | ||
1352 | try | ||
1353 | { | ||
1354 | d.GeomDestroy(Shell); | ||
1355 | } | ||
1356 | catch (System.AccessViolationException) | ||
1357 | { | ||
1358 | m_log.Error("[PHYSICS]: PrimGeom dead"); | ||
1359 | } | ||
1360 | // Remove any old entries | ||
1361 | //string tShell; | ||
1362 | //_parent_scene.geom_name_map.TryGetValue(Shell, out tShell); | ||
1363 | //Console.WriteLine("**** Remove {0}", tShell); | ||
1364 | |||
1365 | if(_parent_scene.geom_name_map.ContainsKey(Shell)) _parent_scene.geom_name_map.Remove(Shell); | ||
1366 | if(_parent_scene.actor_name_map.ContainsKey(Shell)) _parent_scene.actor_name_map.Remove(Shell); | ||
1367 | Shell = IntPtr.Zero; | ||
1368 | } | ||
1369 | } | ||
1370 | |||
1371 | m_isPhysical = m_tainted_isPhysical; | ||
1372 | } | ||
1373 | |||
1374 | if (m_tainted_CAPSULE_LENGTH != CAPSULE_LENGTH) | ||
1375 | { | ||
1376 | if (Shell != IntPtr.Zero && Body != IntPtr.Zero && Amotor != IntPtr.Zero) | ||
1377 | { | ||
1378 | |||
1379 | m_pidControllerActive = true; | ||
1380 | // no lock needed on _parent_scene.OdeLock because we are called from within the thread lock in OdePlugin's simulate() | ||
1381 | d.JointDestroy(Amotor); | ||
1382 | float prevCapsule = CAPSULE_LENGTH; | ||
1383 | CAPSULE_LENGTH = m_tainted_CAPSULE_LENGTH; | ||
1384 | //m_log.Info("[SIZE]: " + CAPSULE_LENGTH.ToString()); | ||
1385 | d.BodyDestroy(Body); | ||
1386 | AvatarGeomAndBodyCreation(_position.X, _position.Y, | ||
1387 | _position.Z + (Math.Abs(CAPSULE_LENGTH - prevCapsule) * 2), m_tensor); | ||
1388 | Velocity = Vector3.Zero; | ||
1389 | } | ||
1390 | else | ||
1391 | { | ||
1392 | m_log.Warn("[PHYSICS]: trying to change capsule size, but the following ODE data is missing - " | ||
1393 | + (Shell==IntPtr.Zero ? "Shell ":"") | ||
1394 | + (Body==IntPtr.Zero ? "Body ":"") | ||
1395 | + (Amotor==IntPtr.Zero ? "Amotor ":"")); | ||
1396 | } | ||
1397 | } | ||
1398 | |||
1399 | if (!m_taintPosition.ApproxEquals(_position, 0.05f)) | ||
1400 | { | ||
1401 | if (Body != IntPtr.Zero) | ||
1402 | { | ||
1403 | d.BodySetPosition(Body, m_taintPosition.X, m_taintPosition.Y, m_taintPosition.Z); | ||
1404 | |||
1405 | _position.X = m_taintPosition.X; | ||
1406 | _position.Y = m_taintPosition.Y; | ||
1407 | _position.Z = m_taintPosition.Z; | ||
1408 | } | ||
1409 | } | ||
1410 | |||
1411 | } | ||
1412 | |||
1413 | internal void AddCollisionFrameTime(int p) | ||
1414 | { | ||
1415 | // protect it from overflow crashing | ||
1416 | if (m_eventsubscription + p >= int.MaxValue) | ||
1417 | m_eventsubscription = 0; | ||
1418 | m_eventsubscription += p; | ||
1419 | } | ||
1420 | } | ||
1421 | } | ||