Moving kinematic bodies with a dynamic velocity in Pymunk

Viewed 99

I just started learning Pymunk so I'm quite confused, any help would be appreciated! I want to create a kinematic platform whose position is a sine wave,

x_plate(t) = A*np.sin(w*t)

subsequently with a velocity of

v_plate(t) = A*w*np.cos(w*t)

I'm quite confused so any help would be appreciated. Thanks!

Edit: I found a property called velocity_func that lets you set the body's velocity, problem is the parameters to set are

body.velocity_func = func(body, gravity, damping, dt)

and I'm not quite sure how to proceed.

1 Answers

Either you can assign position and velocity manually, or you can use the callback function, velocity_func, you already found.

If you use the callback you need to define a function and assign it to the velocity_func property of the body, like is described in the doc: http://www.pymunk.org/en/latest/pymunk.html#pymunk.Body.velocity_func

import pymunk
body = pymunk.Body(1,2, body_type=pymunk.Body.KINEMATIC)
def sine_wave(body, gravity, damping, dt):
    body.velocity = ... # a vector of the velocity x and y.

body.velocity_func = sine_wave

A full example of using this method is available in the planet demo: https://github.com/viblo/pymunk/blob/e7b05ab07defcc77f94e8fc49bcab5e223ff143d/examples/planet.py#L40

The nice with this solution is that Pymunk will automatically call this function each step.

Another way in case you dont like the callback is to just update the position and velocity manually each step. There is an example of this in the platformer example code here https://github.com/viblo/pymunk/blob/master/examples/platformer.py#L262

...
space.step(dt)
body.position = ... # position x and y.
body.velocity = ... # a 2d vector of the velocity.
...
Related