I'm making a topdown game and having trouble getting the player to move on key press. At the moment i'm using add_force or add_impulse to move the player in a direction, but the player doesn't stop.
I've thought of two ways to stop the player.
1. Equal and opposite force for when the keyup is detected.
2. Friction.
I'd like to use friction to stop the player and I'm trying to follow the tank.c example but being new to chipmunk, and pymunk, I have tried and failed to port this code:
Code: Select all
cpConstraint *pivot = cpSpaceAddConstraint(space, cpPivotJointNew2(tankControlBody, tankBody, cpvzero, cpvzero));
So far, I have something that looks like this:
Code: Select all
class Player(PhysicalObject):
BASE_SPEED = 5
VISIBLE_RANGE = 400
def __init__(self, image, position, world, movementType=None):
PhysicalObject.__init__(self, image, position, world)
self.mass = 100
self.CreateBody()
self.controlBody = pymunk.Body(pymunk.inf, pymunk.inf)
self.joint = pymunk.PivotJoint(self.body, self.controlBody, (0,0))
self.joint.max_force = 100
self.joint.bias_coef = 0
world.space.add(self.joint)
While we're here, I've read (and observed in the tank.c demo) that it's good practice to create a control body separate from the actual body of your player and set the mass/inertia of the control body to inf. I don't understand why this is so. Why not just directly control the actual body by pushing it around?