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