Jump to content
  • Advertisement
Sign in to follow this  
GameGeazer

Java collision detection

This topic is 3080 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
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!