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