Search code examples
cocos2d-iphonephysicschipmunkcar-analogy

Physics, turn like a car


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?


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.