Lift, drag or buoyancy using Pymunk

594 Views Asked by At

I am new to Pymunk. Thanks to some good tutorials I managed to make my first simulation quite easily. I am now wondering if it is possible to simulate air drag and/or buoyancy easily. Any example of that ?

1

There are 1 best solutions below

0
viblo On

There is no´easy built in way to do it currently (as of Pymunk 5.6).

There is a damping property on the space object. But this is quite basic so not sure if it covers your use case.

Otherwise you need to implement something yourself. You can get some inspiration from these:

There is one example of drag in the arrows.py example in Pymunk: https://github.com/viblo/pymunk/blob/08fb141b81c0240513fc16e276d5ade5b0506512/examples/arrows.py#L139

drag_constant = 0.0002

pointing_direction = Vec2d(1,0).rotated(flying_arrow.angle)
flight_direction = Vec2d(flying_arrow.velocity)
flight_speed = flight_direction.normalize_return_length()
dot = flight_direction.dot(pointing_direction)
# (1-abs(dot)) can be replaced with (1-dot) to make arrows turn 
# around even when fired straight up. Might not be as accurate, but 
# maybe look better.
drag_force_magnitude = (1-abs(dot)) * flight_speed **2 * drag_constant * flying_arrow.mass
arrow_tail_position = Vec2d(-50, 0).rotated(flying_arrow.angle)
flying_arrow.apply_impulse_at_world_point(drag_force_magnitude * -flight_direction, arrow_tail_position)

flying_arrow.angular_velocity *= 0.5

The Chipmunk github repo contains a full example of Bouyancy, here: https://github.com/slembcke/Chipmunk2D/blob/master/demo/Buoyancy.c This code is in c, using Chipmunk (a c library which Pymunk uses to do the actual physics calculations). If you are a somewhat experienced programmer it usually quite easy to convert Chipmunk examples to Python/Pymunk.