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