...

View Full Version : collision checking impossible???



warhammerdude20
02-24-2004, 01:19 AM
ive been searching far and wide for how to detect collisions with java for a game that im making(pong)... not any other language BUT java... am i on a wild goose chase? i think it must be possible cause i play java games all the time that would have to use collision checking... i was thinking something like this:

public void checkIfBounce(){
if(ball_x-radius==paddle_x+10){ // if ball&paddle are the same x
for(int i=-1;i<40;i++){ // 40 is the paddles length
if(paddle_y+i==ball_y)ballXspeed=-1;
}
}
}

i thought i could call this in the run() methed of an applet to see if the ball and the paddle had made a collision, then the for loop would test each possible position to see if they had contacted, but it doesnt work... anyone know why? if you know how to detect collisions in java or know a site that tells you how, please tell me...

thanks...

shmoove
02-24-2004, 10:06 AM
A for loop?!?! Sheesh! Why not just compare the ball's position with the top and bottom of the paddle?


// I guess you need circle to rectangle collision detection, right?
if (ball_x + radius >= paddle_x && ball_x - radius <= paddle_x + 10) {
if (ball_y +radius >= paddle_y && ball_y - radius <= paddle_y + 40) {
// BAM!!!
}
}

Off course depending on the ball's speed it could completely miss the paddle, but seeing as you set ballXspeed to -1 I don't think this is the case.
You could also just use one of the java.awt.Shape subclasses and their contains/intersects methods.

shmoove



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum