OnCollision Get Relative Velocity.

Topics: Developer Forum
Mar 23, 2011 at 8:00 PM

I've created a a player character with 2 fixtures, a wheel and a square connected by a RevoluteJoint which has a motor, and I've performed an ignore collision for both of them.

 

//Make sure the two fixtures don't collide with each other
            wheel.CollisionFilter.IgnoreCollisionWith(fixture);
            fixture.CollisionFilter.IgnoreCollisionWith(wheel);

I've then created a onCollide function and added to the wheel's OnCollisionEventHandler.

 

        protected virtual bool onCollide(Fixture f1, Fixture f2, Contact contact)
        {

            Vector2 contactVector = Vector2.Zero;
            float radius = 0;
            float turningVelocity = 0;
            Vector2 outVector = Vector2.Zero;
            //get gravity direction
            Vector2 gravNormal = -GameManager.physicsManager.world.Gravity;
            FarseerPhysics.Collision.Manifold man = new FarseerPhysics.Collision.Manifold();
            //get collision manifold
            contact.GetManifold(out man);
            //get position of contact
            outVector = man.LocalPoint;
            //get position on other body that collision occured on
            contactVector = man.LocalPoint -f2.Body.Position;
            //get position on body that collision occured on
            outVector = outVector - f1.Body.Position;

            //get direction of collision point relative to body center
            outVector.Normalize();
            //normalize direction of gravity
            gravNormal.Normalize();
            //get radian direction of gravity
            float gravAngle = (float)Math.Atan2(gravNormal.X, gravNormal.Y);
            //get radian direction of contact
            float contactAngle = (float)Math.Atan2(outVector.X, outVector.Y);
            //convert to degrees
            gravAngle = MathHelper.ToDegrees(gravAngle);
            contactAngle = MathHelper.ToDegrees(contactAngle);
            //get radius between contact point, and center of body
            radius = Vector2.Distance(contactVector, f2.Body.Position);
            //get circumfrence of other body
            turningVelocity = radius * 2f * (float)Math.PI;
            //get linear velocity of turning object at contact point
            turningVelocity = turningVelocity * f2.Body.AngularVelocity;
            //get x and y of linearTurningVelocity
            Vector2 linearTurningVelocity = new Vector2(outVector.X * turningVelocity, outVector.Y * turningVelocity);
            //If angle of impact is roughly in the direction of gravity, set relative velocity = this velocity
            if (contactAngle > gravAngle - 60 && contactAngle < gravAngle + 60)
            {
                ReferenceVelocity = f2.Body.LinearVelocity + linearTurningVelocity;
            }

            return true;
        }

 

The above function get's the collision manifold, determines whether or not the collision took place roughly in the direction of gravity, and then sets a relative velocity based on the linearVelocity at the point where the object collided (derived from it's angular velocity), and it's linearVelocity.

The problem is that I keep getting collisions even when the character is jumping in the air. Any Suggestions?