I'm having some issues when it comes to collision detection, i understand the theory of that when objects touch the program will do something....that bits easy, so for a university assignment i've been given some skeleton code and asked to modify it to create a different game, the game i'm creating is similar to pacman (using same looking player), the character will be "eating" circles(munchies), so i've got all my code working up till collision detection, i'm having issues figuring where to place the call (CollisionDetection(); ) and also how to actually get the program to detect the collision and then delete the object.

just a few notes when the collision occur i want the monster to increase in size, and also increment the score int by 1, (these both work when for loop is implemented), oh and obviously delete the munchie which it collided with, the println is just for testing reasons.

the if statement in the collision detection method is messed up because you cannot compare a single int to an array of them, you could do a for loop to go through them if you wanted, but this is nto the right way for collision anyways.

here is how to do collision:

you create 2 rectangles for the two entities, you then check if the rectangles intersect.

As h3ckboy said, you have two shapes and you see if they overlap. If so, they are colliding. To test if rectangles A and B are colliding, if any vertex for rectangle A is inside rectangle B, they are colliding. So you would write a method isPointInRect and call it four times. You can also do circle-circle collision:

This is a method inside my sprite class which defines the image, height, width, name and coordinates of a sprite in my games. Calling this method from the main class would return a true if colliding or false if not colliding.

it creates 1 rectangle using parameters within the calling sprite then creates a second rectangle based off the info of the second sprite. The method then uses the rectangle1.interects(rectangle2) method to see if they intersect.

I've been thinking about how to check if the mouse is over an object. Right now all I can come up with is to iterate through all objects and pass that object into the collidesWith() method to see if the mouse is over it or not. Not a very good routine, but once I hit that point I do more research.

The rectangles are actually created with the sprite, I declared them in the method just for this post without thinking. So each of my sprites contains a me and a him rectangle whos coords, height and width get changed to the parameters of the method when checking for collision.

Xyle, only (minor) issue with your approach is it allocates two Rectangle objects every time the method is called.

Allocate them once for all and it solves this problem. Moreover, Java uses object pooling efficiently in such situations...

Personally, I use Xyle's approach for very minimalist 2D games. Maybe mixing bounding spheres and bounding rectangles would be better for a Pacman-like game.

For more complex 2D games (large levels with scrolling, etc...), I use spatial subdivisions (regular finite grid) and java.awt.Polygon (it is better for non spherical rotating objects) rather than Rectangle. I will release the source code of my GTA-like blue print if it can help someone.

No, java isn't going to do anything to reuse your Rectangles. It is going to allocate the memory every time.

This is what I do. Everytime I create a sprite, it has 1 me rectangle and 1 him rectangle for any ingame collision detection. The rectangles are created only once for each sprite used. So far its worked great.

I just looked over the posted code. The program approach is a very bad one but I'm guessing it's supposed to be for students that haven't been introduced to OOP yet. The way the munchies are implemented is as a few parallel arrays:

1 2 3

privateint[] munchieXPos; // x coordinate of top left corner of each munchie imageprivateint[] munchieYPos; // y coordinate of top left corner of each munchie imageprivateint[] munchieAnimationCount; // current animation counter for each munchie

so if you wanted to access the first munchie(number 0) you could index all the arrays with that number.munchieXPos[0]munchieYPos[0]munchieAnimationCount[0]So your collision check would need to be changed to do that. Notice how other places in the code where munchies are checked the i counter variable for the loops are used to index the arrays.

1

munchieXPos[i]

.As far as removing the munchies The easiest way with that code would be to add a new boolean array to track the active state of the munchie, something like munchieActive[]. Make sure to initialize the array the same as the others are. Change the reset function to loop threw them all and set them to true, make sure they are only checked for collision and drawn if their active state is true, and is set to false when there is a collision.

Hope this helps, I'm purposely trying to not just post code as this is for an assignment and if you cant figure this out the later projects will become impossible. Hope this helps.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org