aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/PhysicsModules/ubOde/ODEScene.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Region/PhysicsModules/ubOde/ODEScene.cs246
1 files changed, 111 insertions, 135 deletions
diff --git a/OpenSim/Region/PhysicsModules/ubOde/ODEScene.cs b/OpenSim/Region/PhysicsModules/ubOde/ODEScene.cs
index 5f63a7b..0003085 100644
--- a/OpenSim/Region/PhysicsModules/ubOde/ODEScene.cs
+++ b/OpenSim/Region/PhysicsModules/ubOde/ODEScene.cs
@@ -34,15 +34,10 @@ using System.Linq;
34using System.Reflection; 34using System.Reflection;
35using System.Runtime.InteropServices; 35using System.Runtime.InteropServices;
36using System.Threading; 36using System.Threading;
37using System.IO;
38using System.Diagnostics;
39using log4net; 37using log4net;
40using Nini.Config; 38using Nini.Config;
41using Mono.Addins;
42using OdeAPI;
43using OpenSim.Framework; 39using OpenSim.Framework;
44using OpenSim.Region.Framework.Scenes; 40using OpenSim.Region.Framework.Scenes;
45using OpenSim.Region.Framework.Interfaces;
46using OpenSim.Region.PhysicsModules.SharedBase; 41using OpenSim.Region.PhysicsModules.SharedBase;
47using OpenMetaverse; 42using OpenMetaverse;
48 43
@@ -180,7 +175,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde
180// const d.ContactFlags comumContactFlags = d.ContactFlags.SoftERP | d.ContactFlags.SoftCFM |d.ContactFlags.Approx1 | d.ContactFlags.Bounce; 175// const d.ContactFlags comumContactFlags = d.ContactFlags.SoftERP | d.ContactFlags.SoftCFM |d.ContactFlags.Approx1 | d.ContactFlags.Bounce;
181 176
182// const d.ContactFlags comumContactFlags = d.ContactFlags.Bounce | d.ContactFlags.Approx1 | d.ContactFlags.Slip1 | d.ContactFlags.Slip2; 177// const d.ContactFlags comumContactFlags = d.ContactFlags.Bounce | d.ContactFlags.Approx1 | d.ContactFlags.Slip1 | d.ContactFlags.Slip2;
183 const d.ContactFlags comumContactFlags = d.ContactFlags.Bounce | d.ContactFlags.Approx1; 178 const SafeNativeMethods.ContactFlags comumContactFlags = SafeNativeMethods.ContactFlags.Bounce | SafeNativeMethods.ContactFlags.Approx1;
184 const float comumContactERP = 0.75f; 179 const float comumContactERP = 0.75f;
185 const float comumContactCFM = 0.0001f; 180 const float comumContactCFM = 0.0001f;
186 const float comumContactSLIP = 0f; 181 const float comumContactSLIP = 0f;
@@ -228,7 +223,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde
228 223
229 public int bodyFramesAutoDisable = 5; 224 public int bodyFramesAutoDisable = 5;
230 225
231 private d.NearCallback nearCallback; 226 private SafeNativeMethods.NearCallback nearCallback;
232 227
233 private Dictionary<uint,OdePrim> _prims = new Dictionary<uint,OdePrim>(); 228 private Dictionary<uint,OdePrim> _prims = new Dictionary<uint,OdePrim>();
234 private HashSet<OdeCharacter> _characters = new HashSet<OdeCharacter>(); 229 private HashSet<OdeCharacter> _characters = new HashSet<OdeCharacter>();
@@ -251,7 +246,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde
251 private int contactsPerCollision = 80; 246 private int contactsPerCollision = 80;
252 internal IntPtr ContactgeomsArray = IntPtr.Zero; 247 internal IntPtr ContactgeomsArray = IntPtr.Zero;
253 private IntPtr GlobalContactsArray = IntPtr.Zero; 248 private IntPtr GlobalContactsArray = IntPtr.Zero;
254 private d.Contact SharedTmpcontact = new d.Contact(); 249 private SafeNativeMethods.Contact SharedTmpcontact = new SafeNativeMethods.Contact();
255 250
256 const int maxContactsbeforedeath = 6000; 251 const int maxContactsbeforedeath = 6000;
257 private volatile int m_global_contactcount = 0; 252 private volatile int m_global_contactcount = 0;
@@ -356,7 +351,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde
356 /// </summary> 351 /// </summary>
357 private void Initialization() 352 private void Initialization()
358 { 353 {
359 d.AllocateODEDataForThread(~0U); 354 SafeNativeMethods.AllocateODEDataForThread(~0U);
360 355
361 SimulationLock = new Object(); 356 SimulationLock = new Object();
362 357
@@ -374,15 +369,15 @@ namespace OpenSim.Region.PhysicsModule.ubOde
374 // Create the world and the first space 369 // Create the world and the first space
375 try 370 try
376 { 371 {
377 world = d.WorldCreate(); 372 world = SafeNativeMethods.WorldCreate();
378 TopSpace = d.SimpleSpaceCreate(IntPtr.Zero); 373 TopSpace = SafeNativeMethods.SimpleSpaceCreate(IntPtr.Zero);
379 ActiveSpace = d.SimpleSpaceCreate(TopSpace); 374 ActiveSpace = SafeNativeMethods.SimpleSpaceCreate(TopSpace);
380 CharsSpace = d.SimpleSpaceCreate(TopSpace); 375 CharsSpace = SafeNativeMethods.SimpleSpaceCreate(TopSpace);
381 GroundSpace = d.SimpleSpaceCreate(TopSpace); 376 GroundSpace = SafeNativeMethods.SimpleSpaceCreate(TopSpace);
382 float sx = WorldExtents.X + 16; 377 float sx = WorldExtents.X + 16;
383 float sy = WorldExtents.Y + 16; 378 float sy = WorldExtents.Y + 16;
384 d.Vector3 ex =new d.Vector3(sx, sy, 0); 379 SafeNativeMethods.Vector3 ex =new SafeNativeMethods.Vector3(sx, sy, 0);
385 d.Vector3 px =new d.Vector3(sx * 0.5f, sx * 0.5f, 0); 380 SafeNativeMethods.Vector3 px =new SafeNativeMethods.Vector3(sx * 0.5f, sx * 0.5f, 0);
386 if(sx < sy) 381 if(sx < sy)
387 sx = sy; 382 sx = sy;
388 sx = (float)Math.Log(sx) * 1.442695f + 0.5f; 383 sx = (float)Math.Log(sx) * 1.442695f + 0.5f;
@@ -391,7 +386,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde
391 dp = 8; 386 dp = 8;
392 else if(dp < 4) 387 else if(dp < 4)
393 dp = 4; 388 dp = 4;
394 StaticSpace = d.QuadTreeSpaceCreate(TopSpace, ref px, ref ex, dp); 389 StaticSpace = SafeNativeMethods.QuadTreeSpaceCreate(TopSpace, ref px, ref ex, dp);
395 } 390 }
396 catch 391 catch
397 { 392 {
@@ -400,47 +395,47 @@ namespace OpenSim.Region.PhysicsModule.ubOde
400 } 395 }
401 396
402 // demote to second level 397 // demote to second level
403 d.SpaceSetSublevel(ActiveSpace, 1); 398 SafeNativeMethods.SpaceSetSublevel(ActiveSpace, 1);
404 d.SpaceSetSublevel(CharsSpace, 1); 399 SafeNativeMethods.SpaceSetSublevel(CharsSpace, 1);
405 d.SpaceSetSublevel(StaticSpace, 1); 400 SafeNativeMethods.SpaceSetSublevel(StaticSpace, 1);
406 d.SpaceSetSublevel(GroundSpace, 1); 401 SafeNativeMethods.SpaceSetSublevel(GroundSpace, 1);
407 402
408 d.GeomSetCategoryBits(ActiveSpace, (uint)(CollisionCategories.Space | 403 SafeNativeMethods.GeomSetCategoryBits(ActiveSpace, (uint)(CollisionCategories.Space |
409 CollisionCategories.Geom | 404 CollisionCategories.Geom |
410 CollisionCategories.Character | 405 CollisionCategories.Character |
411 CollisionCategories.Phantom | 406 CollisionCategories.Phantom |
412 CollisionCategories.VolumeDtc 407 CollisionCategories.VolumeDtc
413 )); 408 ));
414 d.GeomSetCollideBits(ActiveSpace, (uint)(CollisionCategories.Space | 409 SafeNativeMethods.GeomSetCollideBits(ActiveSpace, (uint)(CollisionCategories.Space |
415 CollisionCategories.Geom | 410 CollisionCategories.Geom |
416 CollisionCategories.Character | 411 CollisionCategories.Character |
417 CollisionCategories.Phantom | 412 CollisionCategories.Phantom |
418 CollisionCategories.VolumeDtc 413 CollisionCategories.VolumeDtc
419 )); 414 ));
420 d.GeomSetCategoryBits(CharsSpace, (uint)(CollisionCategories.Space | 415 SafeNativeMethods.GeomSetCategoryBits(CharsSpace, (uint)(CollisionCategories.Space |
421 CollisionCategories.Geom | 416 CollisionCategories.Geom |
422 CollisionCategories.Character | 417 CollisionCategories.Character |
423 CollisionCategories.Phantom | 418 CollisionCategories.Phantom |
424 CollisionCategories.VolumeDtc 419 CollisionCategories.VolumeDtc
425 )); 420 ));
426 d.GeomSetCollideBits(CharsSpace, 0); 421 SafeNativeMethods.GeomSetCollideBits(CharsSpace, 0);
427 422
428 d.GeomSetCategoryBits(StaticSpace, (uint)(CollisionCategories.Space | 423 SafeNativeMethods.GeomSetCategoryBits(StaticSpace, (uint)(CollisionCategories.Space |
429 CollisionCategories.Geom | 424 CollisionCategories.Geom |
430 // CollisionCategories.Land | 425 // CollisionCategories.Land |
431 // CollisionCategories.Water | 426 // CollisionCategories.Water |
432 CollisionCategories.Phantom | 427 CollisionCategories.Phantom |
433 CollisionCategories.VolumeDtc 428 CollisionCategories.VolumeDtc
434 )); 429 ));
435 d.GeomSetCollideBits(StaticSpace, 0); 430 SafeNativeMethods.GeomSetCollideBits(StaticSpace, 0);
436 431
437 d.GeomSetCategoryBits(GroundSpace, (uint)(CollisionCategories.Land)); 432 SafeNativeMethods.GeomSetCategoryBits(GroundSpace, (uint)(CollisionCategories.Land));
438 d.GeomSetCollideBits(GroundSpace, 0); 433 SafeNativeMethods.GeomSetCollideBits(GroundSpace, 0);
439 434
440 contactgroup = d.JointGroupCreate(maxContactsbeforedeath + 1); 435 contactgroup = SafeNativeMethods.JointGroupCreate(maxContactsbeforedeath + 1);
441 //contactgroup 436 //contactgroup
442 437
443 d.WorldSetAutoDisableFlag(world, false); 438 SafeNativeMethods.WorldSetAutoDisableFlag(world, false);
444 } 439 }
445 440
446 441
@@ -490,27 +485,27 @@ namespace OpenSim.Region.PhysicsModule.ubOde
490 maximumAngularVelocity = 0.49f * heartbeat *(float)Math.PI; 485 maximumAngularVelocity = 0.49f * heartbeat *(float)Math.PI;
491 maxAngVelocitySQ = maximumAngularVelocity * maximumAngularVelocity; 486 maxAngVelocitySQ = maximumAngularVelocity * maximumAngularVelocity;
492 487
493 d.WorldSetCFM(world, comumContactCFM); 488 SafeNativeMethods.WorldSetCFM(world, comumContactCFM);
494 d.WorldSetERP(world, comumContactERP); 489 SafeNativeMethods.WorldSetERP(world, comumContactERP);
495 490
496 d.WorldSetGravity(world, gravityx, gravityy, gravityz); 491 SafeNativeMethods.WorldSetGravity(world, gravityx, gravityy, gravityz);
497 492
498 d.WorldSetLinearDamping(world, 0.001f); 493 SafeNativeMethods.WorldSetLinearDamping(world, 0.001f);
499 d.WorldSetAngularDamping(world, 0.002f); 494 SafeNativeMethods.WorldSetAngularDamping(world, 0.002f);
500 d.WorldSetAngularDampingThreshold(world, 0f); 495 SafeNativeMethods.WorldSetAngularDampingThreshold(world, 0f);
501 d.WorldSetLinearDampingThreshold(world, 0f); 496 SafeNativeMethods.WorldSetLinearDampingThreshold(world, 0f);
502 d.WorldSetMaxAngularSpeed(world, maximumAngularVelocity); 497 SafeNativeMethods.WorldSetMaxAngularSpeed(world, maximumAngularVelocity);
503 498
504 d.WorldSetQuickStepNumIterations(world, m_physicsiterations); 499 SafeNativeMethods.WorldSetQuickStepNumIterations(world, m_physicsiterations);
505 500
506 d.WorldSetContactSurfaceLayer(world, contactsurfacelayer); 501 SafeNativeMethods.WorldSetContactSurfaceLayer(world, contactsurfacelayer);
507 d.WorldSetContactMaxCorrectingVel(world, 60.0f); 502 SafeNativeMethods.WorldSetContactMaxCorrectingVel(world, 60.0f);
508 503
509 HalfOdeStep = ODE_STEPSIZE * 0.5f; 504 HalfOdeStep = ODE_STEPSIZE * 0.5f;
510 odetimestepMS = (int)(1000.0f * ODE_STEPSIZE + 0.5f); 505 odetimestepMS = (int)(1000.0f * ODE_STEPSIZE + 0.5f);
511 506
512 ContactgeomsArray = Marshal.AllocHGlobal(contactsPerCollision * d.ContactGeom.unmanagedSizeOf); 507 ContactgeomsArray = Marshal.AllocHGlobal(contactsPerCollision * SafeNativeMethods.ContactGeom.unmanagedSizeOf);
513 GlobalContactsArray = Marshal.AllocHGlobal((maxContactsbeforedeath + 100) * d.Contact.unmanagedSizeOf); 508 GlobalContactsArray = Marshal.AllocHGlobal((maxContactsbeforedeath + 100) * SafeNativeMethods.Contact.unmanagedSizeOf);
514 509
515 SharedTmpcontact.geom.g1 = IntPtr.Zero; 510 SharedTmpcontact.geom.g1 = IntPtr.Zero;
516 SharedTmpcontact.geom.g2 = IntPtr.Zero; 511 SharedTmpcontact.geom.g2 = IntPtr.Zero;
@@ -565,7 +560,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde
565 #region Collision Detection 560 #region Collision Detection
566 561
567 // sets a global contact for a joint for contactgeom , and base contact description) 562 // sets a global contact for a joint for contactgeom , and base contact description)
568 private IntPtr CreateContacJoint(ref d.ContactGeom contactGeom,bool smooth) 563 private IntPtr CreateContacJoint(ref SafeNativeMethods.ContactGeom contactGeom,bool smooth)
569 { 564 {
570 if (m_global_contactcount >= maxContactsbeforedeath) 565 if (m_global_contactcount >= maxContactsbeforedeath)
571 return IntPtr.Zero; 566 return IntPtr.Zero;
@@ -578,18 +573,18 @@ namespace OpenSim.Region.PhysicsModule.ubOde
578 SharedTmpcontact.geom.pos = contactGeom.pos; 573 SharedTmpcontact.geom.pos = contactGeom.pos;
579 SharedTmpcontact.geom.normal = contactGeom.normal; 574 SharedTmpcontact.geom.normal = contactGeom.normal;
580 575
581 IntPtr contact = new IntPtr(GlobalContactsArray.ToInt64() + (Int64)(m_global_contactcount * d.Contact.unmanagedSizeOf)); 576 IntPtr contact = new IntPtr(GlobalContactsArray.ToInt64() + (Int64)(m_global_contactcount * SafeNativeMethods.Contact.unmanagedSizeOf));
582 Marshal.StructureToPtr(SharedTmpcontact, contact, true); 577 Marshal.StructureToPtr(SharedTmpcontact, contact, true);
583 return d.JointCreateContactPtr(world, contactgroup, contact); 578 return SafeNativeMethods.JointCreateContactPtr(world, contactgroup, contact);
584 } 579 }
585 580
586 private bool GetCurContactGeom(int index, ref d.ContactGeom newcontactgeom) 581 private bool GetCurContactGeom(int index, ref SafeNativeMethods.ContactGeom newcontactgeom)
587 { 582 {
588 if (ContactgeomsArray == IntPtr.Zero || index >= contactsPerCollision) 583 if (ContactgeomsArray == IntPtr.Zero || index >= contactsPerCollision)
589 return false; 584 return false;
590 585
591 IntPtr contactptr = new IntPtr(ContactgeomsArray.ToInt64() + (Int64)(index * d.ContactGeom.unmanagedSizeOf)); 586 IntPtr contactptr = new IntPtr(ContactgeomsArray.ToInt64() + (Int64)(index * SafeNativeMethods.ContactGeom.unmanagedSizeOf));
592 newcontactgeom = (d.ContactGeom)Marshal.PtrToStructure(contactptr, typeof(d.ContactGeom)); 587 newcontactgeom = (SafeNativeMethods.ContactGeom)Marshal.PtrToStructure(contactptr, typeof(SafeNativeMethods.ContactGeom));
593 return true; 588 return true;
594 } 589 }
595 590
@@ -614,14 +609,14 @@ namespace OpenSim.Region.PhysicsModule.ubOde
614 if (g1 == IntPtr.Zero || g2 == IntPtr.Zero) 609 if (g1 == IntPtr.Zero || g2 == IntPtr.Zero)
615 return; 610 return;
616 611
617 if (d.GeomIsSpace(g1) || d.GeomIsSpace(g2)) 612 if (SafeNativeMethods.GeomIsSpace(g1) || SafeNativeMethods.GeomIsSpace(g2))
618 { 613 {
619 // We'll be calling near recursivly if one 614 // We'll be calling near recursivly if one
620 // of them is a space to find all of the 615 // of them is a space to find all of the
621 // contact points in the space 616 // contact points in the space
622 try 617 try
623 { 618 {
624 d.SpaceCollide2(g1, g2, IntPtr.Zero, nearCallback); 619 SafeNativeMethods.SpaceCollide2(g1, g2, IntPtr.Zero, nearCallback);
625 } 620 }
626 catch (AccessViolationException) 621 catch (AccessViolationException)
627 { 622 {
@@ -636,8 +631,8 @@ namespace OpenSim.Region.PhysicsModule.ubOde
636 631
637 // get geom bodies to check if we already a joint contact 632 // get geom bodies to check if we already a joint contact
638 // guess this shouldn't happen now 633 // guess this shouldn't happen now
639 IntPtr b1 = d.GeomGetBody(g1); 634 IntPtr b1 = SafeNativeMethods.GeomGetBody(g1);
640 IntPtr b2 = d.GeomGetBody(g2); 635 IntPtr b2 = SafeNativeMethods.GeomGetBody(g2);
641 636
642 // d.GeomClassID id = d.GeomGetClass(g1); 637 // d.GeomClassID id = d.GeomGetClass(g1);
643 638
@@ -679,18 +674,18 @@ namespace OpenSim.Region.PhysicsModule.ubOde
679 // 674 //
680 */ 675 */
681 676
682 if (d.GeomGetCategoryBits(g1) == (uint)CollisionCategories.VolumeDtc || 677 if (SafeNativeMethods.GeomGetCategoryBits(g1) == (uint)CollisionCategories.VolumeDtc ||
683 d.GeomGetCategoryBits(g2) == (uint)CollisionCategories.VolumeDtc) 678 SafeNativeMethods.GeomGetCategoryBits(g2) == (uint)CollisionCategories.VolumeDtc)
684 { 679 {
685 int cflags; 680 int cflags;
686 unchecked 681 unchecked
687 { 682 {
688 cflags = (int)(1 | d.CONTACTS_UNIMPORTANT); 683 cflags = (int)(1 | SafeNativeMethods.CONTACTS_UNIMPORTANT);
689 } 684 }
690 count = d.CollidePtr(g1, g2, cflags, ContactgeomsArray, d.ContactGeom.unmanagedSizeOf); 685 count = SafeNativeMethods.CollidePtr(g1, g2, cflags, ContactgeomsArray, SafeNativeMethods.ContactGeom.unmanagedSizeOf);
691 } 686 }
692 else 687 else
693 count = d.CollidePtr(g1, g2, (contactsPerCollision & 0xffff), ContactgeomsArray, d.ContactGeom.unmanagedSizeOf); 688 count = SafeNativeMethods.CollidePtr(g1, g2, (contactsPerCollision & 0xffff), ContactgeomsArray, SafeNativeMethods.ContactGeom.unmanagedSizeOf);
694 } 689 }
695 catch (SEHException) 690 catch (SEHException)
696 { 691 {
@@ -726,7 +721,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde
726 721
727 722
728 // get first contact 723 // get first contact
729 d.ContactGeom curContact = new d.ContactGeom(); 724 SafeNativeMethods.ContactGeom curContact = new SafeNativeMethods.ContactGeom();
730 725
731 if (!GetCurContactGeom(0, ref curContact)) 726 if (!GetCurContactGeom(0, ref curContact))
732 return; 727 return;
@@ -802,8 +797,8 @@ namespace OpenSim.Region.PhysicsModule.ubOde
802// if (relVlenSQ > 0.01f) 797// if (relVlenSQ > 0.01f)
803// mu *= frictionMovementMult; 798// mu *= frictionMovementMult;
804 799
805 if(d.GeomGetClass(g2) == d.GeomClassID.TriMeshClass && 800 if(SafeNativeMethods.GeomGetClass(g2) == SafeNativeMethods.GeomClassID.TriMeshClass &&
806 d.GeomGetClass(g1) == d.GeomClassID.TriMeshClass) 801 SafeNativeMethods.GeomGetClass(g1) == SafeNativeMethods.GeomClassID.TriMeshClass)
807 smoothMesh = true; 802 smoothMesh = true;
808 break; 803 break;
809 804
@@ -817,7 +812,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde
817// mu *= frictionMovementMult; 812// mu *= frictionMovementMult;
818 p1.CollidingGround = true; 813 p1.CollidingGround = true;
819 814
820 if(d.GeomGetClass(g1) == d.GeomClassID.TriMeshClass) 815 if(SafeNativeMethods.GeomGetClass(g1) == SafeNativeMethods.GeomClassID.TriMeshClass)
821 smoothMesh = true; 816 smoothMesh = true;
822 break; 817 break;
823 818
@@ -843,7 +838,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde
843// if (Math.Abs(v2.X) > 0.1f || Math.Abs(v2.Y) > 0.1f) 838// if (Math.Abs(v2.X) > 0.1f || Math.Abs(v2.Y) > 0.1f)
844// mu *= frictionMovementMult; 839// mu *= frictionMovementMult;
845 840
846 if(d.GeomGetClass(g2) == d.GeomClassID.TriMeshClass) 841 if(SafeNativeMethods.GeomGetClass(g2) == SafeNativeMethods.GeomClassID.TriMeshClass)
847 smoothMesh = true; 842 smoothMesh = true;
848 } 843 }
849 else 844 else
@@ -873,7 +868,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde
873 SharedTmpcontact.surface.mu = mu; 868 SharedTmpcontact.surface.mu = mu;
874 SharedTmpcontact.surface.bounce = bounce; 869 SharedTmpcontact.surface.bounce = bounce;
875 870
876 d.ContactGeom altContact = new d.ContactGeom(); 871 SafeNativeMethods.ContactGeom altContact = new SafeNativeMethods.ContactGeom();
877 bool useAltcontact; 872 bool useAltcontact;
878 bool noskip; 873 bool noskip;
879 874
@@ -925,7 +920,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde
925 if (Joint == IntPtr.Zero) 920 if (Joint == IntPtr.Zero)
926 break; 921 break;
927 922
928 d.JointAttach(Joint, b1, b2); 923 SafeNativeMethods.JointAttach(Joint, b1, b2);
929 924
930 ncontacts++; 925 ncontacts++;
931 926
@@ -1073,12 +1068,12 @@ namespace OpenSim.Region.PhysicsModule.ubOde
1073 continue; 1068 continue;
1074 1069
1075 // do colisions with static space 1070 // do colisions with static space
1076 d.SpaceCollide2(chr.collider, StaticSpace, IntPtr.Zero, nearCallback); 1071 SafeNativeMethods.SpaceCollide2(chr.collider, StaticSpace, IntPtr.Zero, nearCallback);
1077 1072
1078 // no coll with gnd 1073 // no coll with gnd
1079 } 1074 }
1080 // chars with chars 1075 // chars with chars
1081 d.SpaceCollide(CharsSpace, IntPtr.Zero, nearCallback); 1076 SafeNativeMethods.SpaceCollide(CharsSpace, IntPtr.Zero, nearCallback);
1082 1077
1083 } 1078 }
1084 catch (AccessViolationException) 1079 catch (AccessViolationException)
@@ -1094,7 +1089,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde
1094 { 1089 {
1095 aprim.CollisionScore = 0; 1090 aprim.CollisionScore = 0;
1096 aprim.IsColliding = false; 1091 aprim.IsColliding = false;
1097 if(!aprim.m_outbounds && d.BodyIsEnabled(aprim.Body)) 1092 if(!aprim.m_outbounds && SafeNativeMethods.BodyIsEnabled(aprim.Body))
1098 aprim.clearSleeperCollisions(); 1093 aprim.clearSleeperCollisions();
1099 } 1094 }
1100 } 1095 }
@@ -1105,11 +1100,11 @@ namespace OpenSim.Region.PhysicsModule.ubOde
1105 { 1100 {
1106 foreach (OdePrim aprim in _activegroups) 1101 foreach (OdePrim aprim in _activegroups)
1107 { 1102 {
1108 if(!aprim.m_outbounds && d.BodyIsEnabled(aprim.Body) && 1103 if(!aprim.m_outbounds && SafeNativeMethods.BodyIsEnabled(aprim.Body) &&
1109 aprim.collide_geom != IntPtr.Zero) 1104 aprim.collide_geom != IntPtr.Zero)
1110 { 1105 {
1111 d.SpaceCollide2(StaticSpace, aprim.collide_geom, IntPtr.Zero, nearCallback); 1106 SafeNativeMethods.SpaceCollide2(StaticSpace, aprim.collide_geom, IntPtr.Zero, nearCallback);
1112 d.SpaceCollide2(GroundSpace, aprim.collide_geom, IntPtr.Zero, nearCallback); 1107 SafeNativeMethods.SpaceCollide2(GroundSpace, aprim.collide_geom, IntPtr.Zero, nearCallback);
1113 } 1108 }
1114 } 1109 }
1115 } 1110 }
@@ -1122,7 +1117,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde
1122 // colide active amoung them 1117 // colide active amoung them
1123 try 1118 try
1124 { 1119 {
1125 d.SpaceCollide(ActiveSpace, IntPtr.Zero, nearCallback); 1120 SafeNativeMethods.SpaceCollide(ActiveSpace, IntPtr.Zero, nearCallback);
1126 } 1121 }
1127 catch (Exception e) 1122 catch (Exception e)
1128 { 1123 {
@@ -1132,7 +1127,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde
1132 // and with chars 1127 // and with chars
1133 try 1128 try
1134 { 1129 {
1135 d.SpaceCollide2(CharsSpace,ActiveSpace, IntPtr.Zero, nearCallback); 1130 SafeNativeMethods.SpaceCollide2(CharsSpace,ActiveSpace, IntPtr.Zero, nearCallback);
1136 } 1131 }
1137 catch (Exception e) 1132 catch (Exception e)
1138 { 1133 {
@@ -1232,7 +1227,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde
1232 1227
1233 lock (OdeLock) 1228 lock (OdeLock)
1234 { 1229 {
1235 d.AllocateODEDataForThread(0); 1230 SafeNativeMethods.AllocateODEDataForThread(0);
1236 ((OdeCharacter) actor).Destroy(); 1231 ((OdeCharacter) actor).Destroy();
1237 } 1232 }
1238 } 1233 }
@@ -1403,16 +1398,16 @@ namespace OpenSim.Region.PhysicsModule.ubOde
1403 return StaticSpace; 1398 return StaticSpace;
1404 1399
1405 // else remove it from its current space 1400 // else remove it from its current space
1406 if (currentspace != IntPtr.Zero && d.SpaceQuery(currentspace, geom)) 1401 if (currentspace != IntPtr.Zero && SafeNativeMethods.SpaceQuery(currentspace, geom))
1407 { 1402 {
1408 if (d.GeomIsSpace(currentspace)) 1403 if (SafeNativeMethods.GeomIsSpace(currentspace))
1409 { 1404 {
1410 waitForSpaceUnlock(currentspace); 1405 waitForSpaceUnlock(currentspace);
1411 d.SpaceRemove(currentspace, geom); 1406 SafeNativeMethods.SpaceRemove(currentspace, geom);
1412 1407
1413 if (d.SpaceGetSublevel(currentspace) > 2 && d.SpaceGetNumGeoms(currentspace) == 0) 1408 if (SafeNativeMethods.SpaceGetSublevel(currentspace) > 2 && SafeNativeMethods.SpaceGetNumGeoms(currentspace) == 0)
1414 { 1409 {
1415 d.SpaceDestroy(currentspace); 1410 SafeNativeMethods.SpaceDestroy(currentspace);
1416 } 1411 }
1417 } 1412 }
1418 else 1413 else
@@ -1421,19 +1416,19 @@ namespace OpenSim.Region.PhysicsModule.ubOde
1421 " Geom:" + geom); 1416 " Geom:" + geom);
1422 } 1417 }
1423 } 1418 }
1424 else // odd currentspace is null or doesn't contain the geom? lets try the geom ideia of current space 1419 else
1425 { 1420 {
1426 currentspace = d.GeomGetSpace(geom); 1421 currentspace = SafeNativeMethods.GeomGetSpace(geom);
1427 if (currentspace != IntPtr.Zero) 1422 if (currentspace != IntPtr.Zero)
1428 { 1423 {
1429 if (d.GeomIsSpace(currentspace)) 1424 if (SafeNativeMethods.GeomIsSpace(currentspace))
1430 { 1425 {
1431 waitForSpaceUnlock(currentspace); 1426 waitForSpaceUnlock(currentspace);
1432 d.SpaceRemove(currentspace, geom); 1427 SafeNativeMethods.SpaceRemove(currentspace, geom);
1433 1428
1434 if (d.SpaceGetSublevel(currentspace) > 2 && d.SpaceGetNumGeoms(currentspace) == 0) 1429 if (SafeNativeMethods.SpaceGetSublevel(currentspace) > 2 && SafeNativeMethods.SpaceGetNumGeoms(currentspace) == 0)
1435 { 1430 {
1436 d.SpaceDestroy(currentspace); 1431 SafeNativeMethods.SpaceDestroy(currentspace);
1437 } 1432 }
1438 } 1433 }
1439 } 1434 }
@@ -1441,12 +1436,13 @@ namespace OpenSim.Region.PhysicsModule.ubOde
1441 1436
1442 // put the geom in the newspace 1437 // put the geom in the newspace
1443 waitForSpaceUnlock(StaticSpace); 1438 waitForSpaceUnlock(StaticSpace);
1444 d.SpaceAdd(StaticSpace, geom); 1439 if(SafeNativeMethods.SpaceQuery(StaticSpace, geom))
1440 m_log.Info("[Physics]: 'MoveGeomToStaticSpace' geom already in static space:" + geom);
1441 else
1442 SafeNativeMethods.SpaceAdd(StaticSpace, geom);
1445 1443
1446 return StaticSpace; 1444 return StaticSpace;
1447 } 1445 }
1448
1449
1450 #endregion 1446 #endregion
1451 1447
1452 1448
@@ -1485,7 +1481,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde
1485 return; 1481 return;
1486 } 1482 }
1487 1483
1488 d.AllocateODEDataForThread(~0U); 1484 SafeNativeMethods.AllocateODEDataForThread(~0U);
1489 1485
1490 ODEchangeitem item; 1486 ODEchangeitem item;
1491 1487
@@ -1585,7 +1581,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde
1585 double moveTime = 0; 1581 double moveTime = 0;
1586 double rayTime = 0; 1582 double rayTime = 0;
1587*/ 1583*/
1588 d.AllocateODEDataForThread(~0U); 1584 SafeNativeMethods.AllocateODEDataForThread(~0U);
1589 1585
1590 if (ChangesQueue.Count > 0) 1586 if (ChangesQueue.Count > 0)
1591 { 1587 {
@@ -1689,7 +1685,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde
1689 pobj.SendCollisions((int)(odetimestepMS)); 1685 pobj.SendCollisions((int)(odetimestepMS));
1690 if(pobj.Body != IntPtr.Zero && !pobj.m_isSelected && 1686 if(pobj.Body != IntPtr.Zero && !pobj.m_isSelected &&
1691 !pobj.m_disabled && !pobj.m_building && 1687 !pobj.m_disabled && !pobj.m_building &&
1692 !d.BodyIsEnabled(pobj.Body)) 1688 !SafeNativeMethods.BodyIsEnabled(pobj.Body))
1693 sleepers.Add(pobj); 1689 sleepers.Add(pobj);
1694 } 1690 }
1695 break; 1691 break;
@@ -1704,8 +1700,8 @@ namespace OpenSim.Region.PhysicsModule.ubOde
1704 1700
1705 // do a ode simulation step 1701 // do a ode simulation step
1706// tmpTime = Util.GetTimeStampMS(); 1702// tmpTime = Util.GetTimeStampMS();
1707 d.WorldQuickStep(world, ODE_STEPSIZE); 1703 SafeNativeMethods.WorldQuickStep(world, ODE_STEPSIZE);
1708 d.JointGroupEmpty(contactgroup); 1704 SafeNativeMethods.JointGroupEmpty(contactgroup);
1709// qstepTIme += Util.GetTimeStampMS() - tmpTime; 1705// qstepTIme += Util.GetTimeStampMS() - tmpTime;
1710 1706
1711 // update managed ideia of physical data and do updates to core 1707 // update managed ideia of physical data and do updates to core
@@ -1828,26 +1824,6 @@ namespace OpenSim.Region.PhysicsModule.ubOde
1828 1824
1829 } 1825 }
1830*/ 1826*/
1831/*
1832 // Finished with all sim stepping. If requested, dump world state to file for debugging.
1833 // TODO: This call to the export function is already inside lock (OdeLock) - but is an extra lock needed?
1834 // TODO: This overwrites all dump files in-place. Should this be a growing logfile, or separate snapshots?
1835 if (physics_logging && (physics_logging_interval > 0) && (framecount % physics_logging_interval == 0))
1836 {
1837 string fname = "state-" + world.ToString() + ".DIF"; // give each physics world a separate filename
1838 string prefix = "world" + world.ToString(); // prefix for variable names in exported .DIF file
1839
1840 if (physics_logging_append_existing_logfile)
1841 {
1842 string header = "-------------- START OF PHYSICS FRAME " + framecount.ToString() + " --------------";
1843 TextWriter fwriter = File.AppendText(fname);
1844 fwriter.WriteLine(header);
1845 fwriter.Close();
1846 }
1847
1848 d.WorldExportDIF(world, fname, physics_logging_append_existing_logfile, prefix);
1849 }
1850*/
1851 fps = (float)nodeframes * ODE_STEPSIZE / reqTimeStep; 1827 fps = (float)nodeframes * ODE_STEPSIZE / reqTimeStep;
1852 1828
1853 if(step_time < HalfOdeStep) 1829 if(step_time < HalfOdeStep)
@@ -2208,35 +2184,35 @@ namespace OpenSim.Region.PhysicsModule.ubOde
2208 2184
2209 lock (OdeLock) 2185 lock (OdeLock)
2210 { 2186 {
2211 d.AllocateODEDataForThread(~0U); 2187 SafeNativeMethods.AllocateODEDataForThread(~0U);
2212 2188
2213 if (TerrainGeom != IntPtr.Zero) 2189 if (TerrainGeom != IntPtr.Zero)
2214 { 2190 {
2215 actor_name_map.Remove(TerrainGeom); 2191 actor_name_map.Remove(TerrainGeom);
2216 d.GeomDestroy(TerrainGeom); 2192 SafeNativeMethods.GeomDestroy(TerrainGeom);
2217 2193
2218 } 2194 }
2219 2195
2220 if (TerrainHeightFieldHeightsHandler.IsAllocated) 2196 if (TerrainHeightFieldHeightsHandler.IsAllocated)
2221 TerrainHeightFieldHeightsHandler.Free(); 2197 TerrainHeightFieldHeightsHandler.Free();
2222 2198
2223 IntPtr HeightmapData = d.GeomHeightfieldDataCreate(); 2199 IntPtr HeightmapData = SafeNativeMethods.GeomHeightfieldDataCreate();
2224 2200
2225 TerrainHeightFieldHeightsHandler = GCHandle.Alloc(_heightmap, GCHandleType.Pinned); 2201 TerrainHeightFieldHeightsHandler = GCHandle.Alloc(_heightmap, GCHandleType.Pinned);
2226 2202
2227 d.GeomHeightfieldDataBuildSingle(HeightmapData, TerrainHeightFieldHeightsHandler.AddrOfPinnedObject(), 0, 2203 SafeNativeMethods.GeomHeightfieldDataBuildSingle(HeightmapData, TerrainHeightFieldHeightsHandler.AddrOfPinnedObject(), 0,
2228 heightmapHeight, heightmapWidth , 2204 heightmapHeight, heightmapWidth ,
2229 (int)heightmapHeightSamples, (int)heightmapWidthSamples, scale, 2205 (int)heightmapHeightSamples, (int)heightmapWidthSamples, scale,
2230 offset, thickness, wrap); 2206 offset, thickness, wrap);
2231 2207
2232 d.GeomHeightfieldDataSetBounds(HeightmapData, hfmin - 1, hfmax + 1); 2208 SafeNativeMethods.GeomHeightfieldDataSetBounds(HeightmapData, hfmin - 1, hfmax + 1);
2233 2209
2234 TerrainGeom = d.CreateHeightfield(GroundSpace, HeightmapData, 1); 2210 TerrainGeom = SafeNativeMethods.CreateHeightfield(GroundSpace, HeightmapData, 1);
2235 2211
2236 if (TerrainGeom != IntPtr.Zero) 2212 if (TerrainGeom != IntPtr.Zero)
2237 { 2213 {
2238 d.GeomSetCategoryBits(TerrainGeom, (uint)(CollisionCategories.Land)); 2214 SafeNativeMethods.GeomSetCategoryBits(TerrainGeom, (uint)(CollisionCategories.Land));
2239 d.GeomSetCollideBits(TerrainGeom, 0); 2215 SafeNativeMethods.GeomSetCollideBits(TerrainGeom, 0);
2240 2216
2241 PhysicsActor pa = new NullPhysicsActor(); 2217 PhysicsActor pa = new NullPhysicsActor();
2242 pa.Name = "Terrain"; 2218 pa.Name = "Terrain";
@@ -2245,14 +2221,14 @@ namespace OpenSim.Region.PhysicsModule.ubOde
2245 2221
2246// geom_name_map[GroundGeom] = "Terrain"; 2222// geom_name_map[GroundGeom] = "Terrain";
2247 2223
2248 d.Quaternion q = new d.Quaternion(); 2224 SafeNativeMethods.Quaternion q = new SafeNativeMethods.Quaternion();
2249 q.X = 0.5f; 2225 q.X = 0.5f;
2250 q.Y = 0.5f; 2226 q.Y = 0.5f;
2251 q.Z = 0.5f; 2227 q.Z = 0.5f;
2252 q.W = 0.5f; 2228 q.W = 0.5f;
2253 2229
2254 d.GeomSetQuaternion(TerrainGeom, ref q); 2230 SafeNativeMethods.GeomSetQuaternion(TerrainGeom, ref q);
2255 d.GeomSetPosition(TerrainGeom, m_regionWidth * 0.5f, m_regionHeight * 0.5f, 0.0f); 2231 SafeNativeMethods.GeomSetPosition(TerrainGeom, m_regionWidth * 0.5f, m_regionHeight * 0.5f, 0.0f);
2256 TerrainHeightFieldHeight = _heightmap; 2232 TerrainHeightFieldHeight = _heightmap;
2257 } 2233 }
2258 else 2234 else
@@ -2320,7 +2296,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde
2320 if (TerrainGeom != IntPtr.Zero) 2296 if (TerrainGeom != IntPtr.Zero)
2321 { 2297 {
2322 actor_name_map.Remove(TerrainGeom); 2298 actor_name_map.Remove(TerrainGeom);
2323 d.GeomDestroy(TerrainGeom); 2299 SafeNativeMethods.GeomDestroy(TerrainGeom);
2324 } 2300 }
2325 2301
2326 if (TerrainHeightFieldHeightsHandler.IsAllocated) 2302 if (TerrainHeightFieldHeightsHandler.IsAllocated)
@@ -2328,7 +2304,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde
2328 2304
2329 TerrainHeightFieldHeight = null; 2305 TerrainHeightFieldHeight = null;
2330 2306
2331 IntPtr HeightmapData = d.GeomOSTerrainDataCreate(); 2307 IntPtr HeightmapData = SafeNativeMethods.GeomOSTerrainDataCreate();
2332 2308
2333 const int wrap = 0; 2309 const int wrap = 0;
2334 float thickness = hfmin; 2310 float thickness = hfmin;
@@ -2337,16 +2313,16 @@ namespace OpenSim.Region.PhysicsModule.ubOde
2337 2313
2338 TerrainHeightFieldHeightsHandler = GCHandle.Alloc(_heightmap, GCHandleType.Pinned); 2314 TerrainHeightFieldHeightsHandler = GCHandle.Alloc(_heightmap, GCHandleType.Pinned);
2339 2315
2340 d.GeomOSTerrainDataBuild(HeightmapData, TerrainHeightFieldHeightsHandler.AddrOfPinnedObject(), 0, 1.0f, 2316 SafeNativeMethods.GeomOSTerrainDataBuild(HeightmapData, TerrainHeightFieldHeightsHandler.AddrOfPinnedObject(), 0, 1.0f,
2341 (int)heightmapWidthSamples, (int)heightmapHeightSamples, 2317 (int)heightmapWidthSamples, (int)heightmapHeightSamples,
2342 thickness, wrap); 2318 thickness, wrap);
2343 2319
2344// d.GeomOSTerrainDataSetBounds(HeightmapData, hfmin - 1, hfmax + 1); 2320// d.GeomOSTerrainDataSetBounds(HeightmapData, hfmin - 1, hfmax + 1);
2345 TerrainGeom = d.CreateOSTerrain(GroundSpace, HeightmapData, 1); 2321 TerrainGeom = SafeNativeMethods.CreateOSTerrain(GroundSpace, HeightmapData, 1);
2346 if (TerrainGeom != IntPtr.Zero) 2322 if (TerrainGeom != IntPtr.Zero)
2347 { 2323 {
2348 d.GeomSetCategoryBits(TerrainGeom, (uint)(CollisionCategories.Land)); 2324 SafeNativeMethods.GeomSetCategoryBits(TerrainGeom, (uint)(CollisionCategories.Land));
2349 d.GeomSetCollideBits(TerrainGeom, 0); 2325 SafeNativeMethods.GeomSetCollideBits(TerrainGeom, 0);
2350 2326
2351 PhysicsActor pa = new NullPhysicsActor(); 2327 PhysicsActor pa = new NullPhysicsActor();
2352 pa.Name = "Terrain"; 2328 pa.Name = "Terrain";
@@ -2355,7 +2331,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde
2355 2331
2356// geom_name_map[GroundGeom] = "Terrain"; 2332// geom_name_map[GroundGeom] = "Terrain";
2357 2333
2358 d.GeomSetPosition(TerrainGeom, m_regionWidth * 0.5f, m_regionHeight * 0.5f, 0.0f); 2334 SafeNativeMethods.GeomSetPosition(TerrainGeom, m_regionWidth * 0.5f, m_regionHeight * 0.5f, 0.0f);
2359 TerrainHeightFieldHeight = _heightmap; 2335 TerrainHeightFieldHeight = _heightmap;
2360 } 2336 }
2361 else 2337 else
@@ -2385,7 +2361,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde
2385 if (world == IntPtr.Zero) 2361 if (world == IntPtr.Zero)
2386 return; 2362 return;
2387 2363
2388 d.AllocateODEDataForThread(~0U); 2364 SafeNativeMethods.AllocateODEDataForThread(~0U);
2389 2365
2390 if (m_meshWorker != null) 2366 if (m_meshWorker != null)
2391 m_meshWorker.Stop(); 2367 m_meshWorker.Stop();
@@ -2419,7 +2395,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde
2419 ch.DoAChange(changes.Remove, null); 2395 ch.DoAChange(changes.Remove, null);
2420 2396
2421 if (TerrainGeom != IntPtr.Zero) 2397 if (TerrainGeom != IntPtr.Zero)
2422 d.GeomDestroy(TerrainGeom); 2398 SafeNativeMethods.GeomDestroy(TerrainGeom);
2423 TerrainGeom = IntPtr.Zero; 2399 TerrainGeom = IntPtr.Zero;
2424 2400
2425 if (TerrainHeightFieldHeightsHandler.IsAllocated) 2401 if (TerrainHeightFieldHeightsHandler.IsAllocated)
@@ -2438,7 +2414,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde
2438 GlobalContactsArray = IntPtr.Zero; 2414 GlobalContactsArray = IntPtr.Zero;
2439 } 2415 }
2440 2416
2441 d.WorldDestroy(world); 2417 SafeNativeMethods.WorldDestroy(world);
2442 world = IntPtr.Zero; 2418 world = IntPtr.Zero;
2443 //d.CloseODE(); 2419 //d.CloseODE();
2444 } 2420 }