Computer Science Canada Collision Detection in Java Applet |
Author: | huskiesgoaler34 [ Tue Jun 07, 2011 12:40 pm ] | ||
Post subject: | Collision Detection in Java Applet | ||
Hey, I have one problem that I am having in my game. How do I check to see that there is a collision between the yellow circles and the Pac Man Sprite. I tried to add some code which returns the values of their coordinates but I don't know where to go from there. What I want the program to do is to replace the yellow circle with a black circle over it when the PacMan "eats" it (or in fact passes over it). Can someone help please. I will like help to get one collision for one circle. This will give my enough knowledge to finish the rest. Thanks in advance.
|
Author: | Tony [ Tue Jun 07, 2011 2:03 pm ] |
Post subject: | RE:Collision Detection in Java Applet |
![]() |
Author: | huskiesgoaler34 [ Tue Jun 07, 2011 3:03 pm ] |
Post subject: | Re: Collision Detection in Java Applet |
Oh man...that ruined my day... ![]() |
Author: | RandomLetters [ Tue Jun 07, 2011 3:49 pm ] |
Post subject: | RE:Collision Detection in Java Applet |
Since you can only move in orthogonal directions in pacman, d simplifies to x2-x1 or y2-y1 depending on direction |
Author: | huskiesgoaler34 [ Tue Jun 07, 2011 4:48 pm ] |
Post subject: | Re: Collision Detection in Java Applet |
ok thanks for the heads up |
Author: | RandomLetters [ Tue Jun 07, 2011 5:05 pm ] |
Post subject: | RE:Collision Detection in Java Applet |
To use collision though, you'll need to reconsider how you draw circles. No matter what type of collision you use, you'll need to keep the dots in an array, so that you can get their coordinates. for example, public class PacMan extends... { Food[] foods; } class Food { int x, y; } |
Author: | huskiesgoaler34 [ Tue Jun 07, 2011 5:07 pm ] |
Post subject: | Re: Collision Detection in Java Applet |
would it be better to use an arraylist |
Author: | RandomLetters [ Tue Jun 07, 2011 5:10 pm ] |
Post subject: | RE:Collision Detection in Java Applet |
Sure. I just like the simple dot notation with arrays. |
Author: | huskiesgoaler34 [ Sun Jun 12, 2011 3:58 pm ] | ||||
Post subject: | Re: Collision Detection in Java Applet | ||||
ok, so i tried everything and i am trying to come up with a very simple solution. if the pacman is at x and y, then there is a collision because the PacMan is over the circle (food). now for some reason, as soon as i start the program, a collision is detected. why is that? here is my code that deals with the collision detection:
The variable value is passed to the paint method and the if statement is executed:
|
Author: | RandomLetters [ Sun Jun 12, 2011 5:25 pm ] |
Post subject: | RE:Collision Detection in Java Applet |
if (collision = true) that's not a method call, or a comparison. that statements takes a variable collision and sets it to true. |
Author: | Zren [ Sun Jun 12, 2011 5:40 pm ] |
Post subject: | RE:Collision Detection in Java Applet |
public boolean collision () { return (x == 335 && y == 255); } is the short form of: public boolean collision () { collision = false; if (x == 335 && y == 255){ collision = true; return (collision); }else { collision = false; return (collision); } } Remember that if statement deal with comparisons which are boolean statements. No point in spinning up pointless variables. |
Author: | huskiesgoaler34 [ Sun Jun 12, 2011 7:02 pm ] | ||||
Post subject: | Re: Collision Detection in Java Applet | ||||
Ok, I got the collision checking to work for the first piece of food. Now, I am wondering, when I draw the black circle over the yellow circle, is there a way to stop the flickering. Here is my code:
This draws the original yellow food.
This is the collision checking to see if the food has been eaten. If so, then draw black circle over the original yellow circle. Is there anyway to prevent the flickering? |
Author: | huskiesgoaler34 [ Sun Jun 12, 2011 7:15 pm ] |
Post subject: | Re: Collision Detection in Java Applet |
Is there a way to "terminate" the original drawing of the food command? For example: If collision is detected; don't draw circle to screen |
Author: | Zren [ Sun Jun 12, 2011 7:25 pm ] |
Post subject: | RE:Collision Detection in Java Applet |
What about when there isn't a collision (after you ate it)? You don't want it to show then. Make an ArrayList of food and remove them as their eaten ( or a fixed array with a Food object with an isNommed flag. |