Sign in to follow this  
GameGeazer

Java collision detection

Recommended Posts

Please close this, I found a good page on the subject and was able to find the bug myself. I am currently using a vector of my platform objects which each contain a shape object the size of the platform. To detect if my player collided with one of the platforms in the vector I iterate through the vector and use the intersects method in the shape object to see if my player has intersected the platform. If my player has intersected the platform. if the player intersects the platform I place him right above the platform. here is the code I am describing public void platBounds(NPC npc, Vector<Platform> plat) { for(int i = 0; i <plat.size();i++) { if(plat.get(i).getShape().intersects(npc.getX(), npc.getVelY(), npc.getImage().getWidth(), npc.getImage().getHeight()) == true) { if(npc.getX()>plat.get(i).getX()-npc.getImage().getWidth()&&npc.getX()<(plat.get(i).getX()+plat.get(i).getImage().getWidth())) { npc.setY((int)(plat.get(i).getY()- npc.getImage().getHeight())); npc.setVelY(npc.getVelY()/3 *-1); } } } } I have a couple Questions: What is a more efficient way of doing this? I was thinking maybe only running this code for platforms that are in a certain distance from the player. //I'm not sure if this makes much sense I just posted it because this bug has been bothering me all day. There is a bug I have been running into when using the intersects method. if my shape is anywhere above or below my platform the rest of the code executes as if the two shapes have intersected. The bug is in the code above. This is my first game so any pointers on keeping my code neat would be nice;) [Edited by - GameGeazer on February 13, 2010 5:43:56 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this