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