Collission detection

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

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 on other sites
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 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 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 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 on other sites
Calculus! I'm guessing you're using calculus to calculate resultant forces and the like.

If all you want is simple collision detection, Bounding boxes should do just fine.

1. 1
Rutin
35
2. 2
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• 14
• Forum Statistics

• Total Topics
633343
• Total Posts
3011431
• Who's Online (See full list)

There are no registered users currently online

×