Jump to content
  • Advertisement
Sign in to follow this  
Latitudus

Collission detection

This topic is 4907 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

I'm making collission detection to my game. I have two players who both can rotate 360º and move in the direction they're facing. I have bounding boxes on the players (20x20) and I am checking if they collide. But now I don't know what to do when they collide. Whatever I try the players get stuck inside eachother. I have noticed the most people on theese forums talk about C or C++, but this game is in java so I'm not sure if you can help me.

Share this post


Link to post
Share on other sites
Advertisement
Show me some code. I don't know Java too well, but it is similar to C++(sort of). The only thing I can think of is if you reverse movement vectors, but don't move the players outside. If the boxes or intersecting, you reverse the vectors so they move the other way, but after a frame they are still intersecting due to not enough movement, then you will rereverse the vectors so they move back towards each other, which could be why they get stuck.

Share this post


Link to post
Share on other sites
Well, this is how I do my collisions between two entities with given bounding rectangles in C++. The theory should carry over to JAVA with no problem at all.

What you do is always keep a record of both entities previous position. Then, when they collide, you're going to want to set them both back to their previous positions. This is one way of doing it.

This may be the best way for what you're doing but you could also have it test a unit of velocity ahead each cycle and if there will be a collision don't even move them or make them explode (however you need for the game).

Hope this isn't too confusing,
toXic1337

Share this post


Link to post
Share on other sites
You might be able to predict if two objects are going to collide using some linear algebra and time. You can take their direction and velocity vectors, and see if they collide in the future (Basic Physics s=ut+0.5at^2 v^2=u^2+2as etc.)

That way you can anticipate a response before it actually happens...

Also handy for none fixed frame rates.

Share this post


Link to post
Share on other sites
Collision detection is done using a few ninfty calculus equations, but I don't have a reference handy to document the exact method. Look at NeheGL's collision detection code for help.

However, if you're dealing with a collision detection mechanism for that situation alone, you could simply calculate the distance between the players and stop their motion if they got too close. If you have a point that represents the center of that player, you esentially do this:

square root of((point1.x * point2.x) + (point1.y * point2.y) + (point1.z * point2.z))

If this number gets below a certain threshold, you could just reset the player to the position they were in before they moved.

However, this method is very limited and brutally inefficient. It would work for the scenario that you're describing, but I suggest you look at NeheGL's collision detection lesson. The book "3D Game Engine Programming" has what you need as well, and I suggest you get it ASAP, for all aspects of game programming.

By the way, the forums you're looking for is Math and Physics, it deals with all of this stuff.

Share this post


Link to post
Share on other sites
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!