0

I want to scrible on pymunk screen(pygame),which becomes a line like object and falls due to physics.

I tried to make the code for most part, now i am able to get the vertices of points on screen, I want the line to be drawn on screen joining these points and do physics:

import pymunk
import pymunk.pygame_util
import random

# Initialize Pygame and Pymunk
pygame.init()
screen = pygame.display.set_mode((600, 600))
clock = pygame.time.Clock()
space = pymunk.Space()
space.gravity = (0, 1000)
draw_options = pymunk.pygame_util.DrawOptions(screen)

# Define a function to convert screen coordinates to space coordinates
def to_space(x, y):
    return x, y

# Define a list to store the vertices of the polygon
vertices = []

# Define a variable to indicate if the polygon is complete
complete = False


# Define the main game loop
running = True
draging=False

while running:
    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN and not complete:
            draging=True
            # Add the vertex to the list
            vertices.append(pymunk.Vec2d(*to_space(*event.pos)))
        elif event.type == pygame.MOUSEMOTION and not complete and draging==True:
            # Add the vertex to the list
            if not pymunk.Vec2d(*to_space(*event.pos)) in vertices:
                vertices.append(pymunk.Vec2d(*to_space(*event.pos)))
        elif event.type == pygame.MOUSEBUTTONUP and not complete:
            draging=False
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE and len(vertices) >= 3:

            # PROBLEM HERE,NEED TO ADD THE SHAPE/BODY TO SPACE,FROM "vertices"


            # Clear the list of vertices and set the complete flag
            vertices=[]
            complete = False
    
    # Draw the screen
    screen.fill((255, 255, 255))
    if not complete:
        # Draw the vertices of the polygon
        for vertex in vertices:
            pygame.draw.circle(screen, (0, 0, 255), vertex.int_tuple, 5)

    space.debug_draw(draw_options)
    
    # Update the space and the screen
    space.step(1 / 60)
    pygame.display.flip()
    clock.tick(60)

# Clean up
pygame.quit()

I don't find a lot of helpful resource on this,Only 170 questions on pymunk in SO : | , I have vertices/points which i need to connect and add as a line(not a straight line), also i only know the points,nothing else like position,center of gravity...

Neptotech -vishnu
  • 1,096
  • 8
  • 23
  • Did you look at the playground example http://www.pymunk.org/en/latest/examples.html#playground-py ? It does what I think it is you are looking for – viblo Mar 31 '23 at 19:15
  • @viblo , not exactly i look for it, it specifies mass=5.0(https://github.com/viblo/pymunk/blob/1af1d70346cc1b7347d72ad333baa4f8c7f827f1/pymunk/examples/playground.py#L97), for every size of object created,but i want the mass to varry according to size,physics... – Neptotech -vishnu Mar 31 '23 at 23:55
  • Ah, but that is a small change. Instead of setting mass on the body or shape, set the shape density instead. Then pymunk will calculate the correct mass for you. – viblo May 19 '23 at 20:15

0 Answers0