translating topdown surface friction into pymunk

Official forum for the Chipmunk2D Physics Library.
Post Reply
mordrax
Posts: 8
Joined: Sun Mar 06, 2011 8:59 am
Contact:

translating topdown surface friction into pymunk

Post by mordrax »

Been struggling with this for a couple of days, hard to find code examples on the net.

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));
into pymunk.
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)
I don't know how to add the constraint of the space/player to the space.

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?
markhula
Posts: 188
Joined: Wed Feb 02, 2011 4:23 am
Contact:

Re: translating topdown surface friction into pymunk

Post by markhula »

I'm no expert; but can confirm the tank demo is the way to go.
Adding the constraint to space is to simulate friction; as gravity is of course 0.
There are other issues (depending on your type of game) but with some help you can get pretty good results.
I'm sure Scott will give much more detail to your questions. :-)

Cheers
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: translating topdown surface friction into pymunk

Post by slembcke »

Using a control body and constraints makes it easy to make Chipmunk calculate the friction forces for you.

Using constraints also solves an number of problems with "creeping" when objects are pushing each other. In a top down environment, the friction forces will always be a constant magnitude, but you have to clamp them so that when an object is moving slowly the force doesn't cause it to move in the opposite direction in a single time step. This means that the object has some left over force that gets thrown away. Now say you have a very small object pushing a large one. Because the friction forces are only calculated once at the beginning, the large object will apply enough force to stop the velocity from the last frame. When the solver calculates the collision forces from the the small object, it will cause it to move again slightly. This causes objects to creep along the ground. The only good way to solve this problem is using threshold velocities (ick) or proper constraints (higher CPU usage, but much better quality).
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
mordrax
Posts: 8
Joined: Sun Mar 06, 2011 8:59 am
Contact:

Re: translating topdown surface friction into pymunk

Post by mordrax »

Ah ok, that helps me understand a bit more the use of control bodies but my main problem is still how to port this:

Code: Select all

cpConstraint *pivot = cpSpaceAddConstraint(space, cpPivotJointNew2(tankControlBody, tankBody, cpvzero, cpvzero));
into pymunk?
Or more specifically, what's the equivlent of cpSpaceAddConstraint in pymunk. I've searched the doco but couldn't work out how to do this.
viblo
Posts: 206
Joined: Tue Aug 21, 2007 3:12 pm
Contact:

Re: translating topdown surface friction into pymunk

Post by viblo »

Joints/Constraints are added with the Space.add method, just as in you wrote in the first code snippet.

However, as pymunk is python and not C, the constructor to PivotJoint is defined as

Code: Select all

def __init__(self, a, b, *args):
    pass

#and the straight conversion from c to python is
pivot1 = PivotJoint(tankControlBody, tankBody, Vec2d.zero(), Vec2d.zero())
# but ofc it works equally well with 0 tuples instead of the zero() methods:
pivot2 = PivotJoint(tankControlBody, tankBody, (0,0), (0,0))

mySpace.add(pivot1, pivot2)
Depending on if you send in one or two arguments to args, it will either use the cpPivotJointNew or cpPivotJointNew2 method to create the joint. The difference between these two methods is that cpPivotJointNew want one pivot point as argument, and the cpPivotJointNew2 want to anchor points. So, if you send in one Vec2d pymunk will use cpPivotJointNew, but if you send in two Vec2d it will use cpPivotJointNew2.

Full PivotJoint constructor documentation is here: http://pymunk.googlecode.com/svn/tags/p ... l#__init__
http://www.pymunk.org - A python library built on top of Chipmunk to let you easily get cool 2d physics in your python game/app
mordrax
Posts: 8
Joined: Sun Mar 06, 2011 8:59 am
Contact:

Re: translating topdown surface friction into pymunk

Post by mordrax »

WOHOO!! All i did was add an extra pivot point to my joint

Code: Select all

pymunk.PivotJoint(self.body, self.controlBody, (0,0), (0,0))
and it works!
I played around with the max force and the impulse vector's magnitude and got it to do what i was doing before with manually moving the position of the player!
Can't believe all that I was missing was the extra (0,0) at the end.

Right now I still don't understand this fully (what everyone's said hasn't really sunk in) but I'll slowly work it out. Thanks alot for the help everyone :)
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests