phaser + box2d top down racing physic problem

Recommended Posts


I'm working on a top down racing game. Yesterday I decided to buy the box2d plugin for phaser to get collision physics. There is a simple top down racing game included as a tutorial which I tried out and the collision part was good enough for my needs. But the actual driving physics were not because it was more like a spaceship. If you would stop pressing forward the car would continue in current direction until slowed down. But when I turn during this sliding a car should go where the wheels point and not in its current direction like a spaceship.

So I decided to add my own driving physics to the demo which makes the car go in the direction of the wheels and instead of activating car.body.thrust(power) on key UP I call my function that ultimately sets car.body.x and car.body.y so it moves correct. The problem is that now I'm not colliding with the walls anymore because the box2d simulation is just getting the coordinates and doesn't move the car any more.

I would like to use box2d as much as possible but I couldn't figure out how to make the car not slide like a spaceship and if possible I want to avoid having to create my own collision detection.  Any tips on how to approach this?

Share this post

Link to post
Share on other sites

You'd need someway of associating the forces acting on the car with the grip and direction of the wheels. I'm not exactly sure of what happens when forces go through a wheeled object but it is certainly something that you'd have to set up and something that would control.

I only have rudimentary knowledge of such things but I'd guess that the wheel direction and the grip of those wheels with the surface you're travelling on (which takes in to account downforce and gravity etc to determine the actual grip) coerce the current momentum vector acting on the body, slowing the body slightly (due to grip/drag) and changing the direction of momentum.

I'd imagine there are a number of tutorials out there for box2d (or any other physics engine really, for example, this is a p2 demo, I imagine the code is kicking around somewhere, from memory, old memory, I think it might be to do with constraints?) which you could find.

Certainly you're right that with a powerful physics engine at your disposal you shouldn't be mucking with cartesian points manually, for the reasons you point out.

Share this post

Link to post
Share on other sites



Hi, I agree with @mattstyles his advice on how to approach the car physics if you really have 4 seperate tires that determine the car its physics. You will really need to add the effect of friction to the tires. Its the reason why cars behave like they do.  As a suggestion: If you are in some way stuck and in need of a simplified model, here is an example/concept of a  top down car physics model using P2 physics (not Box2d, but should be the same procedure):

This model considers the car as a single rigidbody, and the modeling is mainly focussed on directly manipulating velocity. The circle below is a steering wheel. The wall collision handling is not really implemented, but you could sort of override velocity when it is colliding with walls(simulating crash). To make this model useful to you, you could for example add tire sprites below the main body purely for visuals. Then you can give the two front tires the same rotation values as the steeringwheel its rotation (or half, or some other relation between your tires and steering wheel)... Of course you will still need box2d if you want some realistic collisions in your racing game.

Another general suggestion if you cannot proceed: maybe you can try modeling A bike first, so that you reduce your problem to two tires only and then see how you can extend your model to 4 tires?

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.