Sign in to follow this  

Java collision detection

This topic is 2895 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Sign in to follow this