Sign in to follow this  

Looking for some pointers on collision detection.

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to make a simple Arkanoid/Breakout-style game for me to practice game development techniques. I got stuck on collision detection -- I've made some research and found some algorithms and maths behind the collision detection, but as I'm completely new to this area, I'm still pretty lost. I'm aiming for a very simple game -- just the ball, paddle, bricks and walls around the play-field. I'd appreciate any hints on what way I should approach the detection of collisions and some resources regarding this topic.

Share this post


Link to post
Share on other sites
The simplest way would probably be just to check if the bounding rectangle of the ball and the block/paddle intersects.

The article below should cover what you are looking for I think. :)
http://www.gamedev.net/reference/articles/article735.asp

Share this post


Link to post
Share on other sites
Ok, im not very expierenced with collision detection but ill try to help you out :P


Say we have 2 squars, and we want to see if they collided. We could right a fucntion like so...


bool DetectCollision( Player A, Player B )
{
if(A.x + A.width < B.x) // A is totally to the left of B
// No collision
else if(A.x > B.x + B.width) // A is to the right of B
// No collision
else if(A.y + A.height < B.y) // A lies under B
// No collision
else if(A.y > B.y + B.height) // A lies above B
// No collision
else
return true // There is a collision!

return false // No collision
}


ok, basicly, each one of those if's, checks if it is to the left, right, up, and down. I hop

Share this post


Link to post
Share on other sites
Thanks for the input, it's not exactly what I've been looking for though -- I should've been more specific in my question, my bad.

What I'm more confused about is: when you've got a moving ball and a brick, then with static collision check the ball could fly 'through' the brick. I'm trying to make the detection so that it takes the previous and the current position of the ball and checks if there is another object in the trajectory of the ball -- if so, calculate the coordinates of the contact and reflect the ball. I'm not quite sure about how to implement this.

Also I'm a bit unsure about the application design thing. I was thinking of classes like Ball, Paddle, Brick that encapsulate the behaviour of the objects. Those classes contain the mathematical representation of the objects: Circle and Rectangle. I don't know how exactly I should perform the checks between two objects -- should I iterate through all pairs of objects and ask one of them if it collides with the other (and if so, handle the collision)? Or some other approach?

Share this post


Link to post
Share on other sites
I think I know what you mean. Correct me if I'm wrong but you mean something like: An object can move more than one "pixel" (or point, or whatever unit you use) every frame. If that's what you mean, you can do it like this if you are using position and speed vectors:

(example pseudo code)

bool CheckCollision(Ball ball, Brick brick)
{
double ballvelocity = ceil(ball.speedvector.length());

for (int i = 0; i <= ballvelocity; i++)
{
vec2 deltaspeed = ball.speedvector;
vec2 tempballpos = ball.positionvector;

deltaspeed.scale(1/ballvelocity);
deltaspeed.scale(i);

tempballpos.add(deltaspeed);

/*
Perform the collision check here, based on tempballpos and the position vector for the brick,
and their dimensions and return true if collision is detected.
If you want to, you could also return the position the ball were in when collision occured,
but then you need to change the type of the function obviously and somehow return the tempballpos vector. :P
*/
}

return false;
}


As you see, what the code is doing is basically taking the speed vector of the ball, get the length l of that vector, and loop l times while you check each position the ball is in for collisions. This way, low FPS or very fast balls that causes the ball to jump several pixels/points/whatever every frame won't affect the collision detection since the collision detection will check every pixel it moves.

As for the application design, I'd make one base class (maybe CObject or CEntity, or whatever you want), include the neccesary sprite-loading stuff and collision detection stuff plus everything else you want all your objects to have, and just derive Ball, Paddle and Brick from there. Might be better solutions here too but that's what I would have done. :)

Not sure if this is what you're looking for, and maybe there is better implementations for it, but this is how I've done it in the past on similar problems.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this