Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pymunk body object being problematic

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):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

>Solution :

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))

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading