Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect collision between a moving object and an immobile one

First of all, my question isn't really specific to C# or XNA, but my code examples will use these.

I am currently trying to make a Pong clone and I've run into a problem with collision-detection.

Each object basicly has a specific Velocity(which is a Vector2), Position(Vector2, also) and Speed(just a float). On every Update() call of the object, the position is changed this way:

Velocity.Normalize();
Position += Velocity * Speed;

At first, I only checked if there currently was a collision between two objects with a simple Intersects() call from the rectangles of the objects. I quickly realized that I couldn't only check if the object was currently colliding with another, but rather if the object collided with an object on its way. Only checking if two objects were currently colliding made the ball go through the paddle when the speed was too high.

I tried different things to fix the problem, but none of them seemed to work. I only need a way to check if two objects collided on their way, and if they did, if it was from the horizontal, vertical or both(to change the ball's velocity accordingly).

I don't necessarily want the solution right away, maybe just the basic idea of how to implement this, and I'll code it myself.

Thanks for your time.

like image 302
Jesse Emond Avatar asked Jul 07 '10 14:07

Jesse Emond


4 Answers

The Separating Axis Theorem is your friend :)

http://www.codeproject.com/KB/GDI-plus/PolygonCollision.aspx

like image 137
FallenAvatar Avatar answered Sep 22 '22 04:09

FallenAvatar


As a starting point, have a look here.

http://www.flipcode.com/archives/Theory_Practice-Issue_01_Collision_Detection.shtml

This is a very good introduction about all the different collision ways. Maybe your case is explained here.

like image 31
Trefex Avatar answered Sep 21 '22 04:09

Trefex


I think this link: http://www.gamasutra.com/view/feature/3383/simple_intersection_tests_for_games.php might be what you're looking for. It describes the sphere-plane sweep test, useful for when you have fast moving objects that may pass through a plane within a one-frame interval.

It gives you the intersection point as well, which you can use to reflect your trajectory about the plane normal and continue the path of the object.

like image 29
awshepard Avatar answered Sep 20 '22 04:09

awshepard


You are having problem with the fact that if one object is too fast, it can pass the immobile object before an Update() is called with the detection (like it passes through the immobile object).

Extend the shape of the object along the move vector with the size of speed: Square [0,0][2,2] with velocity [1,0] and speed 10 will create a shape of Rectangle [0,0][12,2] => it is now positioned at coords [0,0] with size [12,2].

Now intersected the rectangle with the immobile object. Now you know if they collided.

like image 34
Jaroslav Jandek Avatar answered Sep 19 '22 04:09

Jaroslav Jandek