Pymunk body object being problematic

56 views Asked by At

I am using this video tutorial: Video

I am making a ball (with 0 mass at the moment) in pymunk and trying to show it on pygame but it is not working

I tried doing this to make a ball in pymunk and pygame and I was expecting a ball with no movement(I will make it move later on):

import pymunk
import pygame
pygame.init()
space=pymunk.Space()
FPS=60
clock = pygame.time.Clock()
body = pymunk.Body()
body.position = 400,400
shape=pymunk.Circle(body,10)
space.add(body,shape)
(width, height) = (600, 400)
display = pygame.display.set_mode((width, height))
def main():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
              return
        display.fill((255,255,255))
        x,y=body.position
        pygame.draw.circle(display,(255,0,0),(int(x),int(y)),10)
        clock.tick(FPS)
        space.step(1/FPS)
main()
pygame.display.quit()
pygame.quit()

I got an error saying that the position is Vec2d(Nan,Nan) Then, I ran this:

print(body.position)
                                                         
Output:Vec2d(nan, nan)

body.position = 400, 400
                                                         
print(body.position)

Output:Vec2d(nan, nan)
1

There are 1 answers

3
Rabbid76 On BEST ANSWER

When creating the body, you must specify a mass and a moment not equal to 0 (see pymunk.Body):

body = pymunk.Body()

body = pymunk.Body(1, 1)

In addition, pygame.disaply.flip() is missing after drawing the objects in the scene and if the body should fall, you must also specify the gravity (e.g. space.gravity = (0, 100))