I have a ball/circle inside of a poly,but It just goes to a random direction. I think it is because of Pymunk to Pygame position. Are you supposed to make the lists an int to make it work with pygame?(thats what I did) or do you convert the points another way? I also noticed if I print the Container's postion it comes out as (0,0)
import pygame, sys, pymunk
<blockquote class="imgur-embed-pub" lang="en" data-id="a/yN0u9Ia" data-context="false" ><a href="//imgur.com/a/yN0u9Ia"></a></blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script>
pygame.init()
screen = pygame.display.set_mode((1000, 1000))
clock = pygame.time.Clock()
space = pymunk.Space()
space.gravity = (0, 500)
#Varibales
CircleRadius = 10
OctogonPoints = [(400, 200), (800, 200), (1082.505, 483.18), (1082.505, 883.18), (800, 1166.36), (400, 1166), (117.495, 883), (117.495, 483.18)]
OctogonScaleingFactor = 0.6
ScaledOctogonPoints = []
PygameOctogonPonits = []
for x, y in OctogonPoints:
changedX = x * OctogonScaleingFactor
changedY = y * OctogonScaleingFactor
changedX += 140
changedY += 0
ScaledOctogonPoints.append((changedX, changedY))
for x,y in ScaledOctogonPoints:
PygameChangedX = int(x)
PygameChangedY = int(y)
PygameOctogonPonits.append((PygameChangedX, PygameChangedY))
# create ball
BallBody = pymunk.Body(1, 100, body_type=pymunk.Body.DYNAMIC)
BallBody.position = 500, 400
BallBody.velocity = (0, 0)
BallShape = pymunk.Circle(BallBody, CircleRadius)
BallShape.elasticity = 0.9
BallShape.collision_type = 1
space.add(BallBody, BallShape)
# Create Container
ContainerBody = pymunk.Body(1000,1000,pymunk.Body.STATIC)
ContainerShape = pymunk.Poly(ContainerBody, ScaledOctogonPoints, radius=1)
ContainerShape.collision_type = 2
ContainerShape.elasticity= 1
space.add(ContainerBody, ContainerShape)
#handle collisions
def Collision(arbiter, space, data):
print("Collision")
global CircleRadius, BallShape
CircleRadius += 2
space.remove(BallShape)
BallShape = pymunk.Circle(BallBody, CircleRadius)
BallShape.elasticity = 0.9
BallShape.collision_type = 1
space.add(BallShape)
#handler = space.add_collision_handler(1, 2)
#handler.separate = Collision
#main game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill((0, 0, 0))
#draw bodys in pygame
Bx, By = BallBody.position
pygame.draw.circle(screen, (255, 0, 0), (int(Bx), int(By)), CircleRadius)
Octogon = pygame.draw.lines(screen, (255, 255, 255), True, PygameOctogonPonits, 1)
space.step(1 / 50)
pygame.display.update()
clock.tick(120)
Video https://imgur.com/a/yN0u9Ia
I tried ChatGPT but I had no hope, same with things online
I found out the problem - it turns out that polys are filled and are not hollow.