0
\$\begingroup\$

I'm attempting to make a simple 2D car game from scratch, but I can't figure out the collision detection between a vehicle and a road. The vehicle should also rotate appropriately to its position on the road, as following (the vehicle is a rectangle and the road is a curve.):

Vehicle collides with the road

As you can see, the vehicle collides with the road (but before it's being drawn, so that a user doesn't see that collision). Once the intersection is detected, the vehicle needs to be pushed away and rotated appropriately, as it's driving upwards:

Vehicle is driving upwards

The problem is the vehicle may have any rotation (assuming it won't be so fast to go through the road, missing the collision detection), as well as the road's curve may be directed towards any axis, making the algorithm hard to cover any possible situation.

Have you any idea how to achieve that?

\$\endgroup\$
2
  • \$\begingroup\$ This looks similar to another question that was asked recently. Reviewing the answer there may give you some useful leads. \$\endgroup\$
    – DMGregory
    Commented Apr 25, 2019 at 13:52
  • \$\begingroup\$ @DMGregory Thanks for your reply. However, the question you mentioned doesn't cover the collision detection, but the math and physics in general. \$\endgroup\$ Commented Apr 25, 2019 at 14:08

1 Answer 1

1
\$\begingroup\$

The answer is there's no silver bullet. You're doing discrete physics, and therefore you're missing a lot of information. For example you don't know which point on the car collided first, you can only make the assumption it's the deepest point. You also don't know 100% which part of the road that point hit. In your above drawing, you can only assume based on the car's velocity it was probably the inclined portion as the car moved forward into it. In general game physics just needs to look close to right. It appears you're fairly familiar with the base concepts of realtime physics simulation, but here's a great resource to help solidify those foundations: https://www.toptal.com/game/video-game-physics-part-i-an-introduction-to-rigid-body-dynamics

If you continue with discrete physics i'd recommend sitting down and thinking about what edge cases you might encounter, what information you have, and how you can use that information to iron out as many situations as possible. Start with the general case, then add exceptions in your algorithm until it's mostly right. If your game has only a few cars, you might consider implementing continuous detection using Conservative advancement. This method has some bad worst-case scenarios, but if you only have 10-20 cars you're very unlikely to see this have a huge impact. Erin Catto, the maker of box2D, has a great talk on his take with conservative advancement which he calls Bilateral Advancement. Here's some resources to get you started on those topics.

Conservative Advancement: https://wildbunny.co.uk/blog/2011/04/20/collision-detection-for-dummies/

Erin Catto GDC Talk: https://www.youtube.com/watch?v=7_nKOET6zwI

\$\endgroup\$
2
  • \$\begingroup\$ Thank you very much! I've been thinking of resolving all the edge cases, but, considering it's the best method in my current situation, I'm going to change my environment and give up using discrete physics, as it causes a lot of unnecessary difficulties and possible problems in future. \$\endgroup\$ Commented Apr 25, 2019 at 14:44
  • \$\begingroup\$ I would recommend keeping discrete for item pickups, enemies, etc. As they don't need the performance of continuous \$\endgroup\$
    – gjh33
    Commented Apr 25, 2019 at 14:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .