2

I'm working on a simples 2D top-down game and I started using Pymunk to handle physics.

I've created a GameObject class that contains basically a pymunk.Body, a pymunk.Shape and a pygame.Surface.

Here is the code part were everything goes wrong

player = GameObject([-1, 0], [1, 1], (46, 150, 80))
obj = GameObject([1, 0], [1, 1], (255, 255, 255))

SPACE.add(player.getBody(), player.getShape())
SPACE.add(obj.getBody(), obj.getShape())

while True:
    CLOCK.tick(FPS)
    SPACE.step(1/FPS)

    for event in pygame.event.get():
        eventHandler(event)
    
    player.setPosition([.2, 0], True)
    print(player.getPosition())

Well, using VSCode debugging i saw that in the second iteration of the while True:, after SPACE.step(1/FPS), all my Vec2d, that are associated with the position of the bodies, becomes NaN. So my player.getPosition() returns Vec2d(NaN, NaN) and the object disapears (in the rendering stage, cuz well, there's no position to render it :) ).

Anyone knows why SPACE.step is destroying every thing? Thx

1 Answers1

0

Usually when this happens its because one or several bodies have either 0 or infinity as their mass or moment. For example if two bodies that both have infinite mass collides, something "strange" will happen, for example NaN positions. Make sure you have set the mass and moment properly. Also double check that you set the body_type if needed.

viblo
  • 4,159
  • 4
  • 20
  • 28