XNA Circle to Wall Collision Response

This topic is 3741 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Ok, so far I've been working on creating a game where you play as a circle (dont ask), well, the game also reads in a 2d collision map, I use this 2d collision map to test for per pixel collision. Ok, now to the question. I need a way to figure out the reaction. Currently I have a cheap "Velocity.X *= -.05f;" "Velocity.Y *= -.05f;" just for temp use. Now I really need correct velocity mathematics. Please help with this problem! Remember this is for 1 moving object and a static object. Thanks beforehand!

Share on other sites
Please, I really need help with this. :(

Share on other sites
PROTIP: Use mathematical reflection.

Share on other sites
I googled "mathematical reflection" and found nothing connecting to collisions.

The words themselves just sounds like what I'm currently doing (reversing the velocitys.) This works, but its really ugly and very cheap feeling.

Share on other sites
you should ask on math/phys forums. really. I was implementing 2d ball-ball collision some time ago and its not definately easy thing to do. or, if you are not planning to count in things like rotation and surface properties, and you are planning to do only circle-static object collision I can help you. you need to compute direction of the circle after collision. if you know direction of the circle before collision and normal of that point it bounces to, you need to rotate one vector around another. just like light bounces from mirror. here is code I made some time ago to do it
        public vec2 rotate(vec2 vectorToRotate, vec2 normal)        {            vectorToRotate.normalize();            normal.normalize();            vec2 result = new vec2();            vec2 a = normal;            vec2 b = vectorToRotate;            result.x = (a.x * a.x - a.y * a.y) * b.x - 2 * a.y * a.x * b.y;            result.y = 2 * a.y * a.x * b.x - (a.x * a.x - a.y * a.y) * b.y;            return result;        }

I hope there is no bug. I guess its unclear to you, but you should try to make this function on your own. hint: you know sinus and cosinus of angle of any normalized vector(x and y), and you just add some value(its angle between normal and direction * 2, remember how light bounces from mirror) to that angle. as you see you even dont have to use things like atan or acos, you can use formulas like cos(x+y).

gl

Share on other sites
Programming it yourself is good practice, but if you're not interested in that then there's also a good 2d physics engine called Farseer.

http://www.codeplex.com/FarseerPhysics/Wiki/View.aspx?title=home

Share on other sites
Thanks for the help, but I'm still not getting the exact math that I'm doing. To further help, I'll post some of my source.

public void Update()        {            gamepad = GamePad.GetState(playerIndex);            LThumbstick = gamepad.ThumbSticks.Left;            LThumbstick.Y *= -1;            RThumbstick.Y = gamepad.ThumbSticks.Right.X;            RThumbstick.X = gamepad.ThumbSticks.Right.Y;            acceleration = LThumbstick / 2;            velocity += acceleration;            if (velocity.X > MAX_SPEED)                velocity.X = MAX_SPEED;            else if (velocity.X < -MAX_SPEED)                velocity.X = -MAX_SPEED;            if (velocity.Y > MAX_SPEED)                velocity.Y = MAX_SPEED;            else if (velocity.Y < -MAX_SPEED)                velocity.Y = -MAX_SPEED;            // Test for brakes then, multiply the resistance factor            resistance.X = 1.0f - (0.15f * (gamepad.Triggers.Left));            resistance.Y = resistance.X;            velocity *= resistance;            // Slow it down for friction, that magical thing!            velocity *= FRICTION_FACTOR;            int trys = 0;            // Test for collision, if a collision is detected, flip the velocity.            while (levelMap.isColliding(position + velocity - origin, new Vector2(tankTexture.Width, tankTexture.Height), tankTexture))            {                Vector2 posFromOrigin = (levelMap.lastCollision - origin) / 39;                //pointOfCollision = TurnToFace(Vector2.Zero, new Vector2(-posFromOrigin.Y,posFromOrigin.X), 0, 10000);                //currentRotation = MathHelper.ToRadians(MathHelper.ToDegrees(pointOfCollision)+180);                // TOP: {X:-0.2564103 Y:-0.948718} +                // RIGHT: {X:0.8461539 Y:-0.4615385} -                // LEFT: {X:-0.974359 Y:-0.1282051} +                // BOTTOM: {X:-0.2820513 Y:0.9230769} -                /*                if (posFromOrigin.X * posFromOrigin.Y < 0)                {                    posFromOrigin.X = -Math.Abs(posFromOrigin.X);                    posFromOrigin.Y = -Math.Abs(posFromOrigin.Y);                }                velocity *= posFromOrigin * 0.9f;                */                velocity *= -0.5f;                trys++;                if (trys > 1000)                    throw new Exception("You are stuck");            }            // Check for collisions with other ships            for(int x = 0; x < ships.Count; x++)                if(this != ships[x] && isColliding(ships[x]))                    velocity *= -0.5f;            position += velocity;            if (RThumbstick != Vector2.Zero)                currentRotation = TurnToFace(Vector2.Zero, RThumbstick, currentRotation, 0.75f);            if(velocity.X >= 0.8f || velocity.Y >= 0.8f || velocity.Y <= -0.8f || velocity.X <= -0.8f)                particles.Add(new Particle(position, 255, trailParticle));            for (int x = 0; x < particles.Count; x++)            {                particles[x].Update();                if (particles[x].finished)                {                    particles.RemoveAt(x);                    x--;                }            }            calcMatrix();        }`

@snorfys, I already posted in farseer forums, It seems that my current bitmap terrain collision system wouldn't work with it.