Advertisement

Rotated Rectangle Collision Phsyics for Tank Game

Started by February 14, 2015 07:55 PM
1 comment, last by nerdboy64 10 years ago

I'm making a simple top-down tank game using rotated rectangles to approximate the shape of a tank as seen from above. I followed this tutorial on how to use the separating axis theorem to detect collisions between rotated rectangles, and so far it's working perfectly. At the moment, though, any collision simply causes the vehicle to stop all movement, which isn't the most realistic response in all cases. For example:

  • If a tank collides with a wall at a very shallow angle, it should scrape along the wall rather than freeze in place.
  • If a tank rotating in place comes into contact with a wall, the force of rotation should push the tank away from the wall rather than stop completely.

However, I have no idea how to implement this in my game. Does anyone have any suggestions or tutorials? I am writing it in C++ with SFML for the graphics, and would prefer not to have to add any additional libraries.

My website: Brass Watch Games

Come check out Shipyard, my first real game project (Very WIP): Game Website Development Journal

Shipyard is a 2D turn-based strategy with a sci-fi theme, in which you build ships from individual parts rather than being given a selection of predefined models.

You need to replace the collision handling to use physics solvers instead of "stop all movement". Most collision detection code is just that - detection.

Here's what certain engines do:

Physics is handled on a separate thread / module. In order to obtain desirable results, movement solving is done over several iterations PER frame.

Since you're building using C++, I'd recommend looking into the free Chipmunk Physics C++ library. Alternatively you can write quasi-physics by following your own rules as mentioned.

I'm rusty in physics but you should be able to calculate angle of incidence (into the wall or another tank) based on movement vector of both objects. Since the wall is static it will work itself out. When two objects collide, a third party (collision handler with body A and body B) will decide the results. If both objects were tanks, you'll be happy that both can conserve momentum and "bounce" or "slide" correctly based on the same application of physics.

If you want to step it up - and your tanks are not all the same size, you can take weight and momentum into account.

Advertisement

Thanks for the reply, though I did my handling slightly different than you suggested. I used the angle of incidence, but rather than use a third party, the moving tank assesses collisions with the other object assuming it isn't moving. It's not entirely realistic, but it's a decent temporary solution.

My website: Brass Watch Games

Come check out Shipyard, my first real game project (Very WIP): Game Website Development Journal

Shipyard is a 2D turn-based strategy with a sci-fi theme, in which you build ships from individual parts rather than being given a selection of predefined models.

This topic is closed to new replies.

Advertisement