![]() |
2d Pong HELP!!
Hey everyone,
I tried looking around pretty much everywhere i could think of but i havnt been able to come up with a solution to my problem, which ill explain in a minute. This is my first time making any sort of videogame so im a complete beginner to some of the methods involved. I am attempting to make a 2d pong java applet. So far I have the background colored. The paddle and ball on the screen. User input works to move the paddle up and down. The ball moves randomly and resets to its original position after reaching a certain x-coordinate (best way i could think of). The ball also bounces off the right,top and bottom 'walls'. This idea of the game is basically a single player game of squash, so that for now, i can avoid AI. Sorry for going on so much, but my problem is that I cannot find a way to create collsion detection between the paddle (a rectangle made with g.fillRect) and the ball (made with g.fillOval). I have tried using the Rectangle class and creating an intersects boolean but with no success, my code compiled but it seemed to have no effect. Here is my code, thanks a lot for any help and thanks for reading this. :
import java.applet.*; |
Re: 2d Pong HELP!!
I've now got the ball being reset after it gets passed the paddle (not that it even hits off it but still). so it resets back to a random y coordinate at the same x coordinate. I've also included a "computer score" which is updated each time the ball goes past the paddle.
Has anybody ne ideas on the collision detection, i am quickly running out of ideas and none of em seem to work. This is the updated code, please HELP!!!! :
|
Re: 2d Pong HELP!!
You could try to use some triginomitry by using sin and cos with right triangles.
|
Re: 2d Pong HELP!!
Possibly by using the distance formula. If the distance between the paddle and ball is less than such a number, you've got a collision. But I guess it depends on the coordinate system you're using.
http://www.purplemath.com/modules/distform.htm |
Re: 2d Pong HELP!!
There is a couple of ways you can do this. First I would simply check if the balls x coordinate is less than (x_paddle_position + paddle_width).
You can get more info here: http://www.edenwaith.com/products/pi.../collision.php |
| All times are GMT -5. The time now is 4:23 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC