Question

I'm having difficulty getting the Chipmunk physics engine to do what I want. The only solution that appears to work requires some heavy vector math. Before diving into that rabbit hole for the other components of my game, I was hoping someone could fill me in on a better way to go about this. The desired gameplay is as follows:

  • A character moves around a finite space in a top-down view
  • Movement is always a constant velocity in whatever direction the character faces
  • The player taps on the screen, which causes the character to 'turn' towards the touched location

The basic idea is like driving a car. You cannot immediately turn around, but instead must first perform a u-turn. That car must also maintain a constant speed. How might I do this? Bonus question: how can you override whatever method chipmunk calls to update a body's position, and is this a good idea?

Was it helpful?

Solution

There is this tutorial on how to do top down controls using specially configure joints: http://chipmunk-physics.net/tutorials/ChipmunkTileDemo/

It's based on Chipmunk Pro, but the stuff about controlling the character is easily adapted to vanilla Chipmunk. The "Tank" demo that comes with the non-Pro Chipmunk source implements pretty much the same thing if you want to see some C code for it.

OTHER TIPS

You basically want to rotate the orientation of the player more gradual. You could do this at a constant rate, so when you tap the screen it will start rotating at a constant rate until it has reached the right orientation. This would give a circular turn circle. This will however affect your position, so you would have to keep turning until you would be on a collision course with the position you tapped. The path you would travel would be similar to that of the game Achtung die kurve.

So you would have to save the location and orientation of the player (x, y and phi coordinates). And to determine whether to stop turning you could do something like this:

dx = playerx - tapx;
dy = playery - tapy;
targetAngle = atan2(dy,dx);
if (phi > targetAngle)
    {
    if (phi - targetAngle > PI) omega = rotate;
    else omega = -rotate;
    }
else if (phi < targetAngle)
    {
    if (targetAngle - phi > PI) omega = -rotate;
    else omega = rotate;
    }
else omega = 0;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top