Пример #1
0
        public void DeletePairedActors()
        {
            using (CreateCoreAndScene())
            {
                Actor actorA, actorB;
                {
                    ActorDescription actorDesc = new ActorDescription()
                    {
                        Shapes = { new BoxShapeDescription(5, 6, 7) }
                    };

                    actorA = this.Scene.CreateActor(actorDesc);
                }
                {
                    ActorDescription actorDesc = new ActorDescription()
                    {
                        Shapes = { new BoxShapeDescription(2, 5, 7) }
                    };

                    actorB = this.Scene.CreateActor(actorDesc);
                }

                this.Scene.SetActorPairFlags(actorA, actorB, ContactPairFlag.All);
                ContactPairFlag pairFlags = this.Scene.GetActorPairFlags(actorA, actorB);

                actorB.Dispose();
                actorA.Dispose();
            }
        }
 public void OnContactNotify(ContactPair contactInformation, ContactPairFlag events)
 {
     if (contactNofityEvent != null)
     {
         contactNofityEvent(contactInformation, events);
     }
 }
Пример #3
0
        // PhysX calls OnContactNotify is the base class which you then provide the implementation for
        public override void OnContactNotify(ContactPair contactInformation, ContactPairFlag events)
        {
            Actor a = contactInformation.ActorA;
            Actor b = contactInformation.ActorB;

            // This shouldn't be O(n)
            foreach (ContactReportPair pair in _contactPairs)
            {
                if ((pair.ActorA == a || pair.ActorA == b) && (pair.ActorB == a || pair.ActorB == b))
                {
                    pair.Callback(a, b, events);
                }
            }
        }
Пример #4
0
		// PhysX calls OnContactNotify is the base class which you then provide the implementation for
		public override void OnContactNotify(ContactPair contactInformation, ContactPairFlag events)
		{
			Actor a = contactInformation.ActorA;
			Actor b = contactInformation.ActorB;

			// This shouldn't be O(n)
			foreach (ContactReportPair pair in _contactPairs)
			{
				if ((pair.ActorA == a || pair.ActorA == b) && (pair.ActorB == a || pair.ActorB == b))
				{
					pair.Callback(a, b, events);
				}
			}
		}
Пример #5
0
            public override void OnContactNotify(ContactPair contactInformation, ContactPairFlag events)
            {
                ContactStreamIterator iter;

                using (iter = new ContactStreamIterator(contactInformation.ContactStream))
                {
                    while (iter.GoToNextPair())
                    {
                        while (iter.GoToNextPatch())
                        {
                            while (iter.GoToNextPoint())
                            {
                                // Test each of the available 'information' functions/properties

                                int       numberOfPairs            = iter.GetNumberOfPairs();
                                Shape     shapeA                   = iter.GetShapeA();
                                Shape     shapeB                   = iter.GetShapeB();
                                bool      isShapeADeleted          = iter.IsDeletedShapeA();
                                bool      isShapeBDeleted          = iter.IsDeletedShapeB();
                                ShapeFlag shapeFlags               = iter.GetShapeFlags();
                                int       numberOfPatches          = iter.GetNumberOfPatches();
                                int       numberOfPatchesRemaining = iter.GetNumberOfPatchesRemaining();
                                Vector3   patchNormal              = iter.GetPatchNormal();
                                int       numberOfPoints           = iter.GetNumberOfPoints();
                                int       numberOfPointsRemaining  = iter.GetNumberOfPointsRemaining();
                                Vector3   point            = iter.GetPoint();
                                float     separation       = iter.GetSeperation();
                                int       featureIndex0    = iter.GetFeatureIndex0();
                                int       featureIndex1    = iter.GetFeatureIndex1();
                                float     pointNormalForce = iter.GetPointNormalForce();

                                // First collision should be AB
                                if (IsPairContacting(shapeA, A, B) && IsPairContacting(shapeB, A, B))
                                {
                                    ABHitCallback();
                                }

                                // Second collision should be BC, but only if AB has happened
                                if (IsPairContacting(shapeA, B, C) && IsPairContacting(shapeB, B, C))
                                {
                                    BCHitCallback();
                                }
                            }
                        }
                    }
                }

                Assert.IsTrue(iter.IsDisposed);
            }
Пример #6
0
 public override void OnContactNotify(ContactPair contactInformation, ContactPairFlag events)
 {
     Engine.OnContactNotify(contactInformation, events);
 }
Пример #7
0
 private void CapsuleAndGroundPlaneContact(Actor a, Actor b, ContactPairFlag events)
 {
     // The capsule hit the ground, add some bounce
     // Can be done with materials, but this just an example to demonstrate contact report
     _demo.ContactReportActor.AddForce(new Vector3(0, 5000, 0), ForceMode.Force);
 }
Пример #8
0
        public override void OnContactNotify(ContactPair contactInfo, ContactPairFlag events)
        {
            using (ContactStreamIterator iter = new ContactStreamIterator(contactInfo.ContactStream))
            {
                //if we are looking at the player car
                if (contactInfo.ActorB.Group == PhysXConsts.VehicleId)
                {
                    Vehicle vehicle = (Vehicle)contactInfo.ActorB.UserData;

                    while (iter.GoToNextPair())
                    {
                        while (iter.GoToNextPatch())
                        {
                            while (iter.GoToNextPoint())
                            {
                                if (contactInfo.ActorA.Group == PhysXConsts.TrackId && iter.GetShapeB() is WheelShape)
                                {
                                    continue; //we dont want to know each time a wheel is touching the ground
                                }
                                Vector3 pos = iter.GetPoint();

                                if (contactInfo.ActorA.UserData is NonCar)
                                {
                                    HandleNonCarCollision(vehicle, (NonCar)contactInfo.ActorA.UserData, pos, iter.GetPatchNormal(), contactInfo.NormalForce, events);
                                    return;
                                }

                                float force = contactInfo.NormalForce.Length(); // iter.GetPointNormalForce();
                                if (force == 0)
                                {
                                    continue;
                                }

                                //GameConsole.WriteEvent(force.ToString());

                                if (force > 0)
                                {
                                    if (contactInfo.ActorA.Group == PhysXConsts.VehicleId)
                                    {
                                        Vehicle vehicle2 = (Vehicle)contactInfo.ActorA.UserData;
                                        //2 vehicle collision
                                        force = iter.GetPointNormalForce();
                                        Vector3 normal = contactInfo.NormalForce;
                                        normal.Normalize();
                                        if (vehicle.Driver is CpuDriver && vehicle2.Driver is PlayerDriver)
                                        {
                                            ((CpuDriver)vehicle.Driver).OnPlayerHit(force);
                                        }
                                        else if (vehicle2.Driver is CpuDriver && vehicle.Driver is PlayerDriver)
                                        {
                                            ((CpuDriver)vehicle2.Driver).OnPlayerHit(force);
                                        }

                                        // stop cars rearing up if they are both trying to push each other.
                                        if (vehicle.Chassis.Wheels.Find(a => !a.IsRear && a.InAir) != null)
                                        {
                                            vehicle.Chassis.Actor.AddForceAtPosition(Vector3.Down * 0.35f * force, pos, ForceMode.Force);
                                        }
                                        if (vehicle2.Chassis.Wheels.Find(a => !a.IsRear && a.InAir) != null)
                                        {
                                            vehicle2.Chassis.Actor.AddForceAtPosition(Vector3.Down * 0.35f * force, pos, ForceMode.Force);
                                        }

                                        vehicle.OnCollision(force * 2, pos, normal, true);
                                        vehicle2.OnCollision(force * 2, pos, normal, true);
                                    }
                                    else
                                    {
                                        Vector3 normal = contactInfo.NormalForce;
                                        normal.Normalize();
                                        if (iter.GetPointNormalForce() != 0)
                                        {
                                            force = iter.GetPointNormalForce();
                                        }

                                        vehicle.OnCollision(force, pos, iter.GetPatchNormal(), true);
                                        //return;
                                    }
                                }
                            }
                        }
                    }
                }

                // a non-car object sliding along the track
                else if (contactInfo.ActorB.Group == PhysXConsts.NonCarId && contactInfo.ActorA.Group == PhysXConsts.TrackId)
                {
                    if (contactInfo.ActorB.LinearVelocity.Length() > 4)
                    {
                        while (iter.GoToNextPair())
                        {
                            while (iter.GoToNextPatch())
                            {
                                while (iter.GoToNextPoint())
                                {
                                    Vector3 pos = iter.GetPoint();

                                    GameVars.SparksEmitter.Update(pos);
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #9
0
        private void HandleNonCarCollision(Vehicle vehicle, NonCar nonCar, Vector3 pos, Vector3 patchnormal, Vector3 normalforce, ContactPairFlag events)
        {
            if (nonCar.LastTouchTime + 0.1f > GameEngine.TotalSeconds)
            {
                nonCar.LastTouchTime = GameEngine.TotalSeconds;
                return;
            }

            nonCar.LastVehicleToHit = vehicle;

            if (nonCar.IsAttached)
            {
                if (normalforce == Vector3.Zero)
                {
                    return;
                }

                nonCar.Hit = true;

                float speed = vehicle.Chassis.LastSpeeds.GetMax();

                if (speed < 20)
                {
                    return;
                }

                normalforce = Vector3.Normalize(normalforce);

                Vector3 directedForce = normalforce * speed * vehicle.Config.Mass;
                float   factor        = (1 / (nonCar.Config.TorqueRequiredToMove * nonCar.Config.MassWhenAttached)) * 0.005f;

                nonCar.Rotation.X += -directedForce.X * factor;
                nonCar.Rotation.Z += directedForce.Z * factor;

                nonCar.LastTouchTime     = GameEngine.TotalSeconds;
                nonCar.LastForcePosition = pos;
                nonCar.LastForce         = directedForce;

                //multiply force to match the force generated by hitting a solid wall/barrier etc
                vehicle.OnCollision(directedForce.Length() * 15, pos, normalforce, true);
            }
            else
            {
                vehicle.OnCollision(normalforce.Length(), pos, normalforce, false);
            }

            nonCar.LastTouchTime = GameEngine.TotalSeconds;
        }
Пример #10
0
		private void CapsuleAndGroundPlaneContact(Actor a, Actor b, ContactPairFlag events)
		{
			// The capsule hit the ground, add some bounce
			// Can be done with materials, but this just an example to demonstrate contact report
			_contactReportActor.AddForce(new Vector3(0, 5000, 0), ForceMode.Force);
		}
Пример #11
0
			public override void OnContactNotify( ContactPair contactInformation, ContactPairFlag events )
			{
				ContactStreamIterator iter;
				using( iter = new ContactStreamIterator( contactInformation.ContactStream ) )
				{
					while( iter.GoToNextPair() )
					{
						while( iter.GoToNextPatch() )
						{
							while( iter.GoToNextPoint() )
							{
								// Test each of the available 'information' functions/properties

								int numberOfPairs = iter.GetNumberOfPairs();
								Shape shapeA = iter.GetShapeA();
								Shape shapeB = iter.GetShapeB();
								bool isShapeADeleted = iter.IsDeletedShapeA();
								bool isShapeBDeleted = iter.IsDeletedShapeB();
								ShapeFlag shapeFlags = iter.GetShapeFlags();
								int numberOfPatches = iter.GetNumberOfPatches();
								int numberOfPatchesRemaining = iter.GetNumberOfPatchesRemaining();
								Vector3 patchNormal = iter.GetPatchNormal();
								int numberOfPoints = iter.GetNumberOfPoints();
								int numberOfPointsRemaining = iter.GetNumberOfPointsRemaining();
								Vector3 point = iter.GetPoint();
								float separation = iter.GetSeperation();
								int featureIndex0 = iter.GetFeatureIndex0();
								int featureIndex1 = iter.GetFeatureIndex1();
								float pointNormalForce = iter.GetPointNormalForce();

								// First collision should be AB
								if( IsPairContacting( shapeA, A, B ) && IsPairContacting( shapeB, A, B ) )
								{
									ABHitCallback();
								}

								// Second collision should be BC, but only if AB has happened
								if( IsPairContacting( shapeA, B, C ) && IsPairContacting( shapeB, B, C ) )
								{
									BCHitCallback();
								}
							}
						}
					}
				}

				Assert.IsTrue( iter.IsDisposed );
			}
Пример #12
0
        public override void OnContactNotify(ContactPair contactInfo, ContactPairFlag events)
        {
            using (ContactStreamIterator iter = new ContactStreamIterator(contactInfo.ContactStream))
            {
                //if we are looking at the player car
                if (contactInfo.ActorB.Group == PhysXConsts.VehicleId)
                {
                    Vehicle vehicle = (Vehicle)contactInfo.ActorB.UserData;

                    while (iter.GoToNextPair())
                    {
                        while (iter.GoToNextPatch())
                        {
                            while (iter.GoToNextPoint())
                            {
                                if (contactInfo.ActorA.Group == PhysXConsts.TrackId && iter.GetShapeB() is WheelShape)
                                    continue; //we dont want to know each time a wheel is touching the ground

                                Vector3 pos = iter.GetPoint();

                                if (contactInfo.ActorA.UserData is NonCar)
                                {
                                    HandleNonCarCollision(vehicle, (NonCar)contactInfo.ActorA.UserData, pos, iter.GetPatchNormal(), contactInfo.NormalForce, events);
                                    return;
                                }

                                float force = contactInfo.NormalForce.Length(); // iter.GetPointNormalForce();
                                if (force == 0) continue;

                                //GameConsole.WriteEvent(force.ToString());

                                if (force > 0)
                                {
                                    if (contactInfo.ActorA.Group == PhysXConsts.VehicleId)
                                    {
                                        Vehicle vehicle2 = (Vehicle)contactInfo.ActorA.UserData;
                                        //2 vehicle collision
                                        force = iter.GetPointNormalForce();
                                        Vector3 normal = contactInfo.NormalForce;
                                        normal.Normalize();
                                        if (vehicle.Driver is CpuDriver && vehicle2.Driver is PlayerDriver)
                                        {
                                            ((CpuDriver)vehicle.Driver).OnPlayerHit(force);
                                        }
                                        else if (vehicle2.Driver is CpuDriver && vehicle.Driver is PlayerDriver)
                                        {
                                            ((CpuDriver)vehicle2.Driver).OnPlayerHit(force);
                                        }

                                        // stop cars rearing up if they are both trying to push each other.
                                        if (vehicle.Chassis.Wheels.Find(a => !a.IsRear && a.InAir) != null)
                                        {
                                            vehicle.Chassis.Actor.AddForceAtPosition(Vector3.Down * 0.35f * force, pos, ForceMode.Force);
                                        }
                                        if (vehicle2.Chassis.Wheels.Find(a => !a.IsRear && a.InAir) != null)
                                        {
                                            vehicle2.Chassis.Actor.AddForceAtPosition(Vector3.Down * 0.35f * force, pos, ForceMode.Force);
                                        }

                                        vehicle.OnCollision(force * 2, pos, normal, true);
                                        vehicle2.OnCollision(force * 2, pos, normal, true);
                                    }
                                    else
                                    {
                                        Vector3 normal = contactInfo.NormalForce;
                                        normal.Normalize();
                                        if (iter.GetPointNormalForce() != 0)
                                            force = iter.GetPointNormalForce();

                                        vehicle.OnCollision(force, pos, iter.GetPatchNormal(), true);
                                        //return;
                                    }
                                }
                            }
                        }
                    }
                }

                // a non-car object sliding along the track
                else if (contactInfo.ActorB.Group == PhysXConsts.NonCarId && contactInfo.ActorA.Group == PhysXConsts.TrackId)
                {
                    if (contactInfo.ActorB.LinearVelocity.Length() > 4)
                    {
                        while (iter.GoToNextPair())
                        {
                            while (iter.GoToNextPatch())
                            {
                                while (iter.GoToNextPoint())
                                {
                                    Vector3 pos = iter.GetPoint();

                                    GameVars.SparksEmitter.Update(pos);
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #13
0
        private void HandleNonCarCollision(Vehicle vehicle, NonCar nonCar, Vector3 pos, Vector3 patchnormal, Vector3 normalforce, ContactPairFlag events)
        {
            if (nonCar.LastTouchTime + 0.1f > Engine.TotalSeconds)
            {
                nonCar.LastTouchTime = Engine.TotalSeconds;
                return;
            }

            nonCar.LastVehicleToHit = vehicle;

            if (nonCar.IsAttached)
            {
                if (normalforce == Vector3.Zero) return;

                nonCar.Hit = true;

                float speed = vehicle.Chassis.LastSpeeds.GetMax();

                if (speed < 20) return;

                normalforce = Vector3.Normalize(normalforce);

                Vector3 directedForce = normalforce * speed * vehicle.Config.Mass;
                float factor = (1 / (nonCar.Config.TorqueRequiredToMove * nonCar.Config.MassWhenAttached)) * 0.005f;

                nonCar.Rotation.X += -directedForce.X * factor;
                nonCar.Rotation.Z += directedForce.Z * factor;

                nonCar.LastTouchTime = Engine.TotalSeconds;
                nonCar.LastForcePosition = pos;
                nonCar.LastForce = directedForce;

                //multiply force to match the force generated by hitting a solid wall/barrier etc
                vehicle.OnCollision(directedForce.Length() * 15, pos, normalforce, true);
            }
            else
            {
                vehicle.OnCollision(normalforce.Length(), pos, normalforce, false);
            }

            nonCar.LastTouchTime = Engine.TotalSeconds;
        }
Пример #14
0
 public abstract void SetShapePairFlags(ActorShape shape1, ActorShape shape2, ContactPairFlag flags);
Пример #15
0
 public abstract void SetActorPairFlags(Actor actor1, Actor actor2, ContactPairFlag flags);
Пример #16
0
 public abstract void SetActorGroupPairFlags(ushort g1, ushort g2, ContactPairFlag flags);