I have a ball, and I want it to bounce inside of a octagon made using a poly from Pymunk, and rendered with Lines in Pygame. How would I make the octagon/poly hollow. Also, is Pymunk the best option for python physics? Are there better ones out there? Thanks
Here is the code im using so far, it has everything but the hollow octagon.
import pygame, sys, pymunk
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 = 700, -700
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)
Its not possible to make a Poly shape hollow directly. Instead I think the easiest is to build the octagon from segment shapes instead. For easy handling you can attach all of them to the same body.