Collision
help, Im new to game programming, attemptimg to make my first game. Something like pong or break out sort of thing. Its seems most of the articles that talk about collision are dealing with 3D. Can anyone point me in the right direction, or give me some ideas on how to detect collisions? thanks
If your game will be 2d you can try to make collision detection on a per pixel base. or you search on google articles about quadtrees or octrees.
easy. boundry checking (im guessing thats the correct word)
for pong, the paddles are rectangles, so something like
(for the left paddle)
hope that helps, its really simply though.
Alan
edit: also, it might be a good idea to make collision, drawing etc.. all OO, i recently rewrote an old pong game of mine to make it OO, its a million times easier now for me to add extra paddles, extra balls, collision detections etc..
Edited by - Bezzant on February 23, 2002 1:17:24 PM
for pong, the paddles are rectangles, so something like
(for the left paddle)
if(ball.x - ball.width/2 <= paddle.x + paddle.width/2 && ball.y - ball.height/2 < paddle.y + paddle.height/2 && ball.y + ball.height/2 > paddle.y - paddle.height/2){//bounce ball back, playsound, whatever}
hope that helps, its really simply though.
Alan
edit: also, it might be a good idea to make collision, drawing etc.. all OO, i recently rewrote an old pong game of mine to make it OO, its a million times easier now for me to add extra paddles, extra balls, collision detections etc..
Edited by - Bezzant on February 23, 2002 1:17:24 PM
ok, you need an idea on how to make a simple colision detection for a pong game? ok, I will give you a very simple idea, but I think it's a lot more fun to come up with the code by yourself (code the first game issuposed to be chalanging and fun!)
Hey, I'm suposing have some C knowledge, but if you don't the idea will work with any language....
You have to keep track of the position of the rectangle of the thing that sends the ball back and the position of the ball itself... to make the code very simple, let's supose booth are rectangles or squares... so you have a situation like this:
_
| |
| | **
| | **
|_|
The ball of the pong is the 4 *. the thing that sends the ball back is on the left (let's call "the thing" racquet).
a simple function to keep track of colision betwen two objects like these would look like this:
int colision (sp1x,sp1y,sp1x2,sp1y2,sp2x,sp2y,sp2x2,sp2y2)
{
if (sp1x>sp2x2||sp1x2sp2y2||sp1y2 {
return 0; /*no colision!*/
}
else
{
return 1; /*colision!*/
}
}
sp1x is the left coordinate of the object 1.
sp1x2 is the right coordinate of the object 1.
sp1y is the top coordinate of the object 1.
sp1y2 is the bottom coordinate of the object 1.
likewise:
sp2x is the left coordinate of the object 2.
sp2x2 is the right coordinate of the object 2.
sp2y is the top coordinate of the object 2.
sp2y2 is the bottom coordinate of the object 2.
you should keep track of these position during the execution of your pong...
what this function does isn't quite test if the two objects colided, but rather test if they didn't...
I hope it helps... my first colision detection method was a mess compared to this... hehh. If you want to look at my own pong (I coded it a couple years ago when I learned C language) it's avaliable for download at: http://pong-story.com/ or direct at http://www.pong-story.com/pcpong.htm (mine is the arcade simulation, pong v2.7, which graphics were used, I guess, for the logo of the page...)
it's coded with the ugly BGI graphics lib... if you want to try something more sexy than TC and BGI graphics try using allegro and DJGPP(http://www.talula.demon.co.uk/allegro/).
I guess it's all... good luck with your programing...
I forgot to mention this works for any two objects, anywhere on the screen or any direction....
Edited by - nicolasldc on February 23, 2002 1:31:27 PM
Edited by - nicolasldc on February 23, 2002 1:33:44 PM
Hey, I'm suposing have some C knowledge, but if you don't the idea will work with any language....
You have to keep track of the position of the rectangle of the thing that sends the ball back and the position of the ball itself... to make the code very simple, let's supose booth are rectangles or squares... so you have a situation like this:
_
| |
| | **
| | **
|_|
The ball of the pong is the 4 *. the thing that sends the ball back is on the left (let's call "the thing" racquet).
a simple function to keep track of colision betwen two objects like these would look like this:
int colision (sp1x,sp1y,sp1x2,sp1y2,sp2x,sp2y,sp2x2,sp2y2)
{
if (sp1x>sp2x2||sp1x2sp2y2||sp1y2 {
return 0; /*no colision!*/
}
else
{
return 1; /*colision!*/
}
}
sp1x is the left coordinate of the object 1.
sp1x2 is the right coordinate of the object 1.
sp1y is the top coordinate of the object 1.
sp1y2 is the bottom coordinate of the object 1.
likewise:
sp2x is the left coordinate of the object 2.
sp2x2 is the right coordinate of the object 2.
sp2y is the top coordinate of the object 2.
sp2y2 is the bottom coordinate of the object 2.
you should keep track of these position during the execution of your pong...
what this function does isn't quite test if the two objects colided, but rather test if they didn't...
I hope it helps... my first colision detection method was a mess compared to this... hehh. If you want to look at my own pong (I coded it a couple years ago when I learned C language) it's avaliable for download at: http://pong-story.com/ or direct at http://www.pong-story.com/pcpong.htm (mine is the arcade simulation, pong v2.7, which graphics were used, I guess, for the logo of the page...)
it's coded with the ugly BGI graphics lib... if you want to try something more sexy than TC and BGI graphics try using allegro and DJGPP(http://www.talula.demon.co.uk/allegro/).
I guess it's all... good luck with your programing...
I forgot to mention this works for any two objects, anywhere on the screen or any direction....
Edited by - nicolasldc on February 23, 2002 1:31:27 PM
Edited by - nicolasldc on February 23, 2002 1:33:44 PM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement