Search code examples
pythonpymunk

How do i set a velocity of a KINEMATIC body in pymunk?


Im trying to set the velocity of a KINEMATIC body in pymunk but i dont really know what does what or what values do i need to put, if anyone can explain it to me ill be greatfull.

here is what i did -

part_body = pymunk.Body(1, 1666, pymunk.Body.KINEMATIC)
part_shape = pymunk.Segment(part_body, (0, 150), (300, 150), 2)
part_shape.body.position = 0, 0      # Set the position of the body
part_shape.body.velocity = (0, 0)

i have no idea why i need to put a tuple and not an int, and what the tuple does.


Solution

  • The velocity has a direction. Some examples:

    if you set it to (1,0) it means move to the right with a velocity of 1, after 1 unit of time the object will be at position (1,0).

    If you set it to (10,0) it also means move to the right, but with a velocity of 10, so end up at position (10,0) after 1 unit of time.

    If you set it to (10,10) it will move to the right-up with a velocity of about 14 (sqrt(10**2 + 10**2)), so will en up at position (10,10) after 1 unit of time.