views:

110

answers:

5

Hello,

First of all, my question isn't really specific to C# or XNA, but my code examples will use these. Also, sorry for the title, I didn't know how to express my question properly.

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.

+2  A: 

The Separating Axis Theorem is your friend :)

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

TJMonk15
Thanks for the link.
Jesse Emond
A: 

If two point objects have the same position, then they have collided.

mcandre
I don't think it is quite that simple. Very few graphics are the size of just one point.
Adkins
If two circle objects are within the sum of their radii, then they have collided.
mcandre
I'm actually looking for a solution to if two objects with a certain width and height collided during their Update() call.
Jesse Emond
@mcandre imagine you have a wall that is 2 units wide, and a ball that is 1 unit in diameter. If the ball is, lets say, 2 units from the wall, and is moving at 6 units per frame, in its next update, neither its starting position, nor ending position are "colliding" with the wall, but a collision has indeed taken place part way through the balls movement. That is what XGhost27X is asking about.
TJMonk15
+2  A: 

Hi,

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.

Trefex
Thanks, I'll definitely take a look! :)
Jesse Emond
+2  A: 

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.

awshepard
Thanks for the link.
Jesse Emond
A: 

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.

Jaroslav Jandek