Beispiel #1
0
 protected override void OnUpdateContactGroups(int group0, int group1, bool makeContacts)
 {
     if (nativeScene != IntPtr.Zero)
     {
         PhysXNativeScene.SetGroupCollisionFlag(nativeScene, group0, group1, makeContacts);
     }
 }
Beispiel #2
0
        protected override RayCastResult OnRayCast(Ray ray, int contactGroup)
        {
            if (float.IsNaN(ray.Origin.X))
            {
                Log.Fatal("PhysicsWorld.RayCast: Single.IsNaN(ray.Origin.X)");
            }
            if (float.IsNaN(ray.Direction.X))
            {
                Log.Fatal("PhysicsWorld.RayCast: Single.IsNaN(ray.Direction.X)");
            }
            if (ray.Direction == Vec3.Zero)
            {
                return(new RayCastResult());
            }

            Vec3  normalDir = ray.Direction;
            float length    = normalDir.Normalize();
            Vec3  origin    = ray.Origin;
            int   count     = PhysXNativeWrapper.PhysXNativeScene.RayCast(nativeScene, ref origin, ref normalDir, length,
                                                                          GetContactGroupMask(contactGroup), false);

            if (count == 0)
            {
                return(new RayCastResult());
            }

            unsafe
            {
                NativeRayCastResult *results      = PhysXNativeScene.GetLastRayCastResults(nativeScene);
                NativeRayCastResult *nativeResult = results;
                return(GetRayCastResultFromNative(nativeResult));
            }
        }
Beispiel #3
0
        unsafe public override void SetShapePairFlags(Shape shape1, Shape shape2, ShapePairFlags flags)
        {
            base.SetShapePairFlags(shape1, shape2, flags);

            Body body1 = shape1.Body;
            Body body2 = shape2.Body;

            if (body1.PushedToWorld && body2.PushedToWorld)
            {
                PhysXBody physXBody1 = (PhysXBody)body1;
                PhysXBody physXBody2 = (PhysXBody)body2;

                if (physXBody1.nativeBody != IntPtr.Zero && physXBody2.nativeBody != IntPtr.Zero)
                {
                    PhysXBody.ShapeData shapeData1 = physXBody1.shapesData[shape1.BodyIndex];
                    PhysXBody.ShapeData shapeData2 = physXBody2.shapesData[shape2.BodyIndex];

                    bool disableContacts = (flags & ShapePairFlags.DisableContacts) != 0;

                    foreach (IntPtr nativeShape1 in shapeData1.nativeShapes)
                    {
                        foreach (IntPtr nativeShape2 in shapeData2.nativeShapes)
                        {
                            PhysXNativeScene.SetShapePairFlags(physXBody1.scene.nativeScene, nativeShape1, nativeShape2,
                                                               disableContacts);
                        }
                    }
                }
            }
        }
Beispiel #4
0
        protected override void OnUpdateEnableDebugVisualization()
        {
            base.OnUpdateEnableDebugVisualization();

            PhysXNativeScene.SetEnableDebugVisualization(nativeScene, _EnableDebugVisualization);
            foreach (IPhysXJoint joint in Joints)
            {
                joint.SetVisualizationEnable(_EnableDebugVisualization);
            }
        }
Beispiel #5
0
        protected override Body[] OnVolumeCast(Sphere sphere, int contactGroup)
        {
            Vec3 origin     = sphere.Origin;
            int  shapeCount = PhysXNativeScene.OverlapSphereShapes(nativeScene, ref origin, sphere.Radius,
                                                                   GetContactGroupMask(contactGroup));

            if (shapeCount == 0)
            {
                return(emptyVolumeCastResult);
            }
            return(GetBodiesFromVolumeCastResult(shapeCount));
        }
Beispiel #6
0
        protected override Body[] OnVolumeCast(Capsule capsule, int contactGroup)
        {
            Vec3 origin     = capsule.GetCenter();
            Quat rotation   = Quat.FromDirectionZAxisUp(capsule.GetDirection());
            int  shapeCount = PhysXNativeScene.OverlapCapsuleShapes(nativeScene, ref origin, ref rotation, capsule.Radius,
                                                                    capsule.GetLength() * .5f, GetContactGroupMask(contactGroup));

            if (shapeCount == 0)
            {
                return(emptyVolumeCastResult);
            }
            return(GetBodiesFromVolumeCastResult(shapeCount));
        }
Beispiel #7
0
        protected override void OnSimulationStep()
        {
            //Vehicles

            // Take a simulation step.
            PhysXNativeScene.Simulate(nativeScene, StepSize);

            // Fetch simulation results
            if (!PhysXNativeScene.FetchResults(nativeScene, true))
            {
                Log.Fatal("PhysXPhysicsScene: OnSimulationStep: Error while PhysX fetching results.");
            }

            //we can get list of not sleeping bodies from PhysX.
            //joints too?
            //update from PhysX
            foreach (Body body in Bodies)
            {
                PhysXBody physXBody = (PhysXBody)body;
                if (!body.Static)
                {
                    physXBody.UpdateDataFromLibrary();
                }
            }

            foreach (IPhysXJoint joint in Joints)
            {
                joint.UpdateDataFromLibrary();
            }

            if (EnableCollisionEvents)
            {
                IntPtr contactList;
                int    contactCount;
                PhysXNativeScene.GetContactReportList(nativeScene, out contactList, out contactCount);
                unsafe
                {
                    ContactReport *pContact = (ContactReport *)contactList;
                    for (int n = 0; n < contactCount; n++)
                    {
                        Shape shape1       = PhysXPhysicsWorld.Instance.GetShapeByIdentifier(pContact->shapeIndex1);
                        Shape shape2       = PhysXPhysicsWorld.Instance.GetShapeByIdentifier(pContact->shapeIndex2);
                        Vec3  contactPoint = pContact->contactPoint;
                        Vec3  normal       = pContact->normal;
                        AddCollisionEvent(shape1, shape2, ref contactPoint, ref normal, -pContact->separation);
                        pContact++;
                    }
                }
            }
        }
Beispiel #8
0
        protected override Body[] OnVolumeCast(Box box, int contactGroup)
        {
            Vec3 origin      = box.Center;
            Vec3 halfExtents = box.Extents;
            Quat rotation    = box.Axis.ToQuat();

            int shapeCount = PhysXNativeScene.OverlapOBBShapes(nativeScene, ref origin, ref halfExtents, ref rotation,
                                                               GetContactGroupMask(contactGroup));

            if (shapeCount == 0)
            {
                return(emptyVolumeCastResult);
            }
            return(GetBodiesFromVolumeCastResult(shapeCount));
        }
Beispiel #9
0
        protected override Body[] OnVolumeCast(Bounds bounds, int contactGroup)
        {
            Vec3 origin      = bounds.GetCenter();
            Vec3 halfExtents = bounds.GetSize() * .5f;
            Quat rotation    = Quat.Identity;

            int shapeCount = PhysXNativeScene.OverlapOBBShapes(nativeScene, ref origin, ref halfExtents, ref rotation,
                                                               GetContactGroupMask(contactGroup));

            if (shapeCount == 0)
            {
                return(emptyVolumeCastResult);
            }
            return(GetBodiesFromVolumeCastResult(shapeCount));
        }
Beispiel #10
0
        unsafe protected override Line[] OnGetDebugVisualizationData()
        {
            int lineCount;

            PhysXNativeScene.GetDebugVisualizationData(nativeScene, out lineCount, IntPtr.Zero);
            if (lineCount == 0)
            {
                return(null);
            }
            Line[] lines = new Line[lineCount];
            fixed(Line *pLines = lines)
            {
                PhysXNativeScene.GetDebugVisualizationData(nativeScene, out lineCount, (IntPtr)pLines);
            }

            return(lines);
        }
Beispiel #11
0
        unsafe protected override RayCastResult[] OnRayCastPiercing(Ray ray, int contactGroup)
        {
            if (float.IsNaN(ray.Origin.X))
            {
                Log.Fatal("PhysicsWorld.RayCast: Single.IsNaN(ray.Origin.X)");
            }
            if (float.IsNaN(ray.Direction.X))
            {
                Log.Fatal("PhysicsWorld.RayCast: Single.IsNaN(ray.Direction.X)");
            }
            if (ray.Direction == Vec3.Zero)
            {
                return(emptyPiercingRayCastResult);
            }

            Vec3  normalDir = ray.Direction;
            float length    = normalDir.Normalize();
            Vec3  origin    = ray.Origin;
            int   count     = PhysXNativeWrapper.PhysXNativeScene.RayCast(nativeScene, ref origin, ref normalDir,
                                                                          length, GetContactGroupMask(contactGroup), true);

            if (count == 0)
            {
                return(emptyPiercingRayCastResult);
            }

            NativeRayCastResult *results = PhysXNativeScene.GetLastRayCastResults(nativeScene);

            RayCastResult[]      items        = new RayCastResult[count];
            NativeRayCastResult *nativeResult = results;

            for (int n = 0; n < count; n++)
            {
                items[n] = GetRayCastResultFromNative(nativeResult);
                nativeResult++;
            }

            return(items);
        }
Beispiel #12
0
        unsafe Body[] GetBodiesFromVolumeCastResult(int shapeCount)
        {
            int *shapeIdentifiers = PhysXNativeScene.GetLastVolumeCastShapeIdentifiers(nativeScene);

            for (int n = 0; n < shapeCount; n++)
            {
                Shape shape = PhysXPhysicsWorld.Instance.GetShapeByIdentifier(shapeIdentifiers[n]);
                tempSetOfBodies.AddWithCheckAlreadyContained(shape.Body);
            }
            Body[] result = new Body[tempSetOfBodies.Count];
            tempSetOfBodies.CopyTo(result, 0);

            //clear and shrink tempSetOfBodies
            {
                if (tempSetOfBodies.Count > tempSetOfBodiesMaxSize)
                {
                    tempSetOfBodiesMaxSize = tempSetOfBodies.Count;
                }
                if (tempSetOfBodies.Count * 2 < tempSetOfBodiesMaxSize)
                {
                    tempSetOfBodiesShrinkCounter++;
                    if (tempSetOfBodiesShrinkCounter == 100)
                    {
                        tempSetOfBodies        = new Set <Body>(64);
                        tempSetOfBodiesMaxSize = 0;
                    }
                }
                else
                {
                    tempSetOfBodiesShrinkCounter = 0;
                }
                tempSetOfBodies.Clear();
            }

            return(result);
        }
Beispiel #13
0
        unsafe void UpdateGravity()
        {
            Vec3 value = Gravity;

            PhysXNativeScene.SetGravity(nativeScene, ref value);
        }