Computer Science Canada Need help for java game |
Author: | KeyLimePie [ Fri May 24, 2013 12:56 pm ] | ||
Post subject: | Need help for java game | ||
My game should exit when the ball touches one of the lines. Can someone help me think of a way to achieve this? Heres is my code so far: (its basically just a ball that can be moved with the arrow keys and some lines)
|
Author: | andrew. [ Fri May 24, 2013 12:57 pm ] |
Post subject: | RE:Need help for java game |
Well how do you tell if the ball has reached a line? |
Author: | KeyLimePie [ Fri May 24, 2013 1:01 pm ] |
Post subject: | Re: RE:Need help for java game |
andrew. @ Fri May 24, 2013 12:57 pm wrote: Well how do you tell if the ball has reached a line?
Thats what im trying to ask... |
Author: | Nathan4102 [ Fri May 24, 2013 1:07 pm ] |
Post subject: | RE:Need help for java game |
You have the ball's x and y coords, and you have the line's x and y coords. Using those, you can figure out when the ball is touching the line. |
Author: | KeyLimePie [ Fri May 24, 2013 1:11 pm ] |
Post subject: | Re: RE:Need help for java game |
Nathan4102 @ Fri May 24, 2013 1:07 pm wrote: You have the ball's x and y coords, and you have the line's x and y coords. Using those, you can figure out when the ball is touching the line.
yeah i know i could do that, but wouldnt it be really inefficient to do that for every single line? Im probably going to have around 50 lines, and thats a hassle to do each one. |
Author: | Panphobia [ Fri May 24, 2013 1:30 pm ] |
Post subject: | RE:Need help for java game |
Just add an if statement that asks whether the ball is within the Parameters of the line |
Author: | KeyLimePie [ Mon May 27, 2013 11:22 am ] |
Post subject: | Re: RE:Need help for java game |
Panphobia @ Fri May 24, 2013 1:30 pm wrote: Just add an if statement that asks whether the ball is within the Parameters of the line
I added an if statement for ballX and ballY at a specified location. How do you specify the exact parameters of the line in the if statement? Example code: if (ballX == 500 && ballY == 500); { System.exit (0); } Thanks ![]() |
Author: | Insectoid [ Mon May 27, 2013 12:47 pm ] |
Post subject: | RE:Need help for java game |
Given 2 points on a line, you can get its equation in the form ax+by+c=0. You then can calculate the distance between the centre of the ball and the line using the forumla abs(ax+by+c)/sqrt(a^2+b^2). If the distance is less than or equal to the radius of the ball, they have collided. |
Author: | KeyLimePie [ Tue May 28, 2013 12:38 pm ] |
Post subject: | Re: RE:Need help for java game |
Insectoid @ Mon May 27, 2013 12:47 pm wrote: Given 2 points on a line, you can get its equation in the form ax+by+c=0. You then can calculate the distance between the centre of the ball and the line using the forumla abs(ax+by+c)/sqrt(a^2+b^2). If the distance is less than or equal to the radius of the ball, they have collided.
is there a more efficient way to detect the collision? is it possible to detect specific colors for the lines? |
Author: | KeyLimePie [ Thu May 30, 2013 12:31 pm ] |
Post subject: | Re: Need help for java game |
PLEASE HELP ME ![]() ![]() ![]() ![]() ![]() |
Author: | DemonWasp [ Thu May 30, 2013 1:01 pm ] | ||||
Post subject: | RE:Need help for java game | ||||
There isn't really a more efficient way (at least, not in terms of computer time). But, Java does have a library that could help (hint: in Java, this is almost invariably true: either the standard library covers your use case or there is a free open-source library that helps). In this case, there is an existing Line2D class that you can use to represent a line segment (notably not a line, which is infinite). That class also has a method called ptLineDist which will tell you how far a point is from that line. As Insectoid mentioned, if that distance is less than the ball's radius, then the ball has collided with that line. Since you are almost certainly using Ready To Program Java, here's a link to the Java 1.4.2 API for Line2D: http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/geom/Line2D.html Once you have represented your lines with Line2D objects, you can also use those to draw the lines themselves. Here's a short code snippet that should help you:
One other piece of advice: instead of drawing each line 4+ times to make them look thicker, try setting the Graphics object up to draw thicker lines. I didn't know how to do that, so I: 1) Googled "java draw thick line". The first result was this: http://stackoverflow.com/questions/2839508/java2d-increase-the-line-width 2) Read that code snippet. 3) Reduce the code snippet to the minimum required:
put that as the first line of paint and you should get thicker lines. |
Author: | KeyLimePie [ Tue Jun 04, 2013 1:52 pm ] | ||||
Post subject: | Re: RE:Need help for java game | ||||
DemonWasp @ Thu May 30, 2013 1:01 pm wrote: There isn't really a more efficient way (at least, not in terms of computer time). But, Java does have a library that could help (hint: in Java, this is almost invariably true: either the standard library covers your use case or there is a free open-source library that helps).
In this case, there is an existing Line2D class that you can use to represent a line segment (notably not a line, which is infinite). That class also has a method called ptLineDist which will tell you how far a point is from that line. As Insectoid mentioned, if that distance is less than the ball's radius, then the ball has collided with that line. Since you are almost certainly using Ready To Program Java, here's a link to the Java 1.4.2 API for Line2D: http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/geom/Line2D.html Once you have represented your lines with Line2D objects, you can also use those to draw the lines themselves. Here's a short code snippet that should help you:
One other piece of advice: instead of drawing each line 4+ times to make them look thicker, try setting the Graphics object up to draw thicker lines. I didn't know how to do that, so I: 1) Googled "java draw thick line". The first result was this: http://stackoverflow.com/questions/2839508/java2d-increase-the-line-width 2) Read that code snippet. 3) Reduce the code snippet to the minimum required:
put that as the first line of paint and you should get thicker lines. Thank you for your suggestions. ![]() int ballX = 50; int ballY = 500; static int rx = 20; static int ry = 20; g.fillOval (ballX, ballY, rx, ry); If I decide to change my entire game to vertical and horizontal lines (like a maze), would it be easier to calculate the collision detection because my final ISU is due really soon? Thanks! ![]() |
Author: | DemonWasp [ Tue Jun 04, 2013 4:37 pm ] |
Post subject: | RE:Need help for java game |
You find the centre point of the ball by already knowing it (ballX, ballY). Note that you're drawing the ball incorrectly: g.fillOval(ballX, ballY, rx, ry) draws from (ballX, ballY) to (ballX+rx, ballY+ry). That means that it's drawing a ball centered at (ballX+rx/2, ballY+ry/2). You need to figure out how to draw a ball centred at (ballX, ballY). That shouldn't be too hard: try drawing a ball with a radius of 5 first, then switch the 5 out for a variable. You calculate the distance from the ball's centre coordinates (ballX, ballY) to the line. Then you compare that with the ball's radius. If the distance is less than that radius, then the ball and the line must have collided. You'll have to figure out how to turn that into code yourself. Switching to strictly vertical and horizontal lines won't really make your life any easier. Honestly you should have maybe 3 lines of code to detect a collision between the ball and any single line. You should only have to add two lines of code to turn that into checking for collisions between the ball and ALL lines. |
Author: | KeyLimePie [ Wed Jun 05, 2013 1:01 pm ] | ||||
Post subject: | Re: RE:Need help for java game | ||||
DemonWasp @ Tue Jun 04, 2013 4:37 pm wrote: You find the centre point of the ball by already knowing it (ballX, ballY). Note that you're drawing the ball incorrectly: g.fillOval(ballX, ballY, rx, ry) draws from (ballX, ballY) to (ballX+rx, ballY+ry). That means that it's drawing a ball centered at (ballX+rx/2, ballY+ry/2).
You need to figure out how to draw a ball centred at (ballX, ballY). That shouldn't be too hard: try drawing a ball with a radius of 5 first, then switch the 5 out for a variable. You calculate the distance from the ball's centre coordinates (ballX, ballY) to the line. Then you compare that with the ball's radius. If the distance is less than that radius, then the ball and the line must have collided. You'll have to figure out how to turn that into code yourself. Switching to strictly vertical and horizontal lines won't really make your life any easier. Honestly you should have maybe 3 lines of code to detect a collision between the ball and any single line. You should only have to add two lines of code to turn that into checking for collisions between the ball and ALL lines. I fixed the radius of the ball. The collision detection checks for the line (infinitely) but I do not know how to implement the math equation in the code so that it checks for the specific max and min points of a line.
Here is the code I currently have:
Thanks! ![]() |
Author: | DemonWasp [ Wed Jun 05, 2013 2:09 pm ] |
Post subject: | RE:Need help for java game |
Your code seems to have all the critical bits there (even if it is a bit messy). You are right, however, that you're checking the line, rather than the line segment. This is my fault: I told you to use ptLineDist when I should have said ptSegDist. If you look carefully at http://docs.oracle.com/javase/6/docs/api/java/awt/geom/Line2D.html , you'll see that ptSegDist will find the distance to the line segment, rather than the infinite-extension of the line. (Sorry!) |
Author: | KeyLimePie [ Thu Jun 06, 2013 12:20 pm ] |
Post subject: | Re: RE:Need help for java game |
DemonWasp @ Wed Jun 05, 2013 2:09 pm wrote: Your code seems to have all the critical bits there (even if it is a bit messy).
You are right, however, that you're checking the line, rather than the line segment. This is my fault: I told you to use ptLineDist when I should have said ptSegDist. If you look carefully at http://docs.oracle.com/javase/6/docs/api/java/awt/geom/Line2D.html , you'll see that ptSegDist will find the distance to the line segment, rather than the infinite-extension of the line. (Sorry!) Thank you! I fixed my game. Is there a way to check for the other lines without repeating it? |
Author: | DemonWasp [ Thu Jun 06, 2013 12:37 pm ] | ||||||
Post subject: | RE:Need help for java game | ||||||
Yes, of course there is! Instead of:
You should be putting all of your lines (and any other repeated objects) into a collection of some kind. There are a few basic types (List, Set, Map), but the best one for this scenario is a List:
Of course, if you're using a newer version of Java, such as 1.7 or better, the syntax gets a bit better:
|
Author: | KeyLimePie [ Sun Jun 09, 2013 9:42 pm ] | ||||||||
Post subject: | Re: RE:Need help for java game | ||||||||
DemonWasp @ Thu Jun 06, 2013 12:37 pm wrote: Yes, of course there is!
Instead of:
You should be putting all of your lines (and any other repeated objects) into a collection of some kind. There are a few basic types (List, Set, Map), but the best one for this scenario is a List:
Of course, if you're using a newer version of Java, such as 1.7 or better, the syntax gets a bit better:
Thank you! I added the list into my game and it checks for all the lines in my game. ![]() I want to clear all the lines on the screen after the collision occurs. I tried to use GC.dispose (); but it displays an error if I add it to my if dist statement. How do I dispose the frame if the ball touches the lines (lose) and the white box (win) so that I can make a new frame?
|
Author: | DemonWasp [ Sun Jun 09, 2013 10:32 pm ] |
Post subject: | Re: RE:Need help for java game |
KeyLimePie @ Sun Jun 09, 2013 9:42 pm wrote: I want to clear all the lines on the screen after the collision occurs. I tried to use GC.dispose (); but it displays an error if I add it to my if dist statement. How do I dispose the frame if the ball touches the lines (lose) and the white box (win) so that I can make a new frame?
I'm not a Swing expert, but I think you want to call .dispose() on the JFrame. Is 'GC' a JFrame object? If it is, can you post the exact error you're encountering? If not, can you post your whole source code so that I can run it? |
Author: | KeyLimePie [ Tue Jun 11, 2013 3:30 am ] |
Post subject: | Re: RE:Need help for java game |
DemonWasp @ Sun Jun 09, 2013 10:32 pm wrote: KeyLimePie @ Sun Jun 09, 2013 9:42 pm wrote: I want to clear all the lines on the screen after the collision occurs. I tried to use GC.dispose (); but it displays an error if I add it to my if dist statement. How do I dispose the frame if the ball touches the lines (lose) and the white box (win) so that I can make a new frame?
I'm not a Swing expert, but I think you want to call .dispose() on the JFrame. Is 'GC' a JFrame object? If it is, can you post the exact error you're encountering? If not, can you post your whole source code so that I can run it? My game works now. Thank you for helping me. ![]() ![]() |