[java] Hit detection

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

Recommended Posts

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.

Share on other sites
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.

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]

Share on other sites
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) ) )

PS: write any instead of ne please.

hope it helps!

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
19
5. 5

• 14
• 26
• 11
• 11
• 9
• Forum Statistics

• Total Topics
631771
• Total Posts
3002253
×