[java] Hit detection

Started by
1 comment, last by Paclaw 17 years, 6 months ago
Ok well im writing my first game, that i am actually gonna finish :), but its pong and i have the ball moving(left and right only) now i need help witht the hit detection for the ball hitting the paddle. I have the detection down for the paddle's width but im not sure how to keep track of the changing y value. Any tips or ne thing will be appreciated.
Advertisement
Ok the last code i posted was wrong. I now have a code that gets the ball to bounce when y = 20 (the b value) but i cant get the ball to bounce off ne other part of the rectangle.
Please help

heres what i have for hit detection
a is the value of ball x coordinate
b is the value of ball y coordinate
y is the value for the rectangles y coordinate
and 5 is the width of the rectange(its really 10 but i want it to be 5)

if(a == 5 && b ==y)
{
x_p=+1;
}

[Edited by - Swattkidd on September 27, 2006 8:54:17 PM]
Is y the top of the paddle?

if so what is the height of the paddle?

Knowing that you need to check that b is greater than the start of your paddle and that it is smaller than the end of your paddle

small ascii art
-- <--- y start
| p |
| a |
| d | O ))) <---- ball with motion blurrrrr
| d |
| l |
| e |
-- <--- y + height end
^
|
|
5

if( ( a == 5 ) && ( ( b > y ) && (b < ( y + height) ) )

instead of your if.


PS: write any instead of ne please.

hope it helps!
Paclaw

This topic is closed to new replies.

Advertisement