Hi guys, yes...the error was
"line 173, in add_static, for oo in o: TypeError: 'Body' object is not iterable".
@slembcke and
@vibro thanks for the advice, now everything works fine.
The next step I wanted to do is connect each ball to the static main ball"group" through a constrain, like a rope with some elasticity...Im thinking in use the Pin join or damped spring, but Im a little confuse how I can implement that.
To set a constrains between two objects should I do something like this?
pymunk.PinJoint(self, a, b, anchr1, anchr2)
or
pymunk.DampedSpring(self, a, b, anchr1, anchr2, rest_length, stiffness, damping)
Look foward to ear some advice.....
I include here the code of the exercise for better understanding.
Code: Select all
import pygame, sys, random
from pygame.locals import *
from pygame.color import *
import pymunk as pm
from pymunk import Vec2d
def add_ball(space): #balls
mass = 1
radius = 10
inertia = pm.moment_for_circle(mass, 0, radius, Vec2d(0,0))
body = pm.Body(mass, inertia)
x = random.randint(120,380)
body.position = x, 550
shape = pm.Circle(body, radius, Vec2d(0,0))
space.add(body, shape)
return shape
def add_group(space): #groups
radius = 50
body = pm.Body(pm.inf, pm.inf)
x = random.randint(120,380)
y = random.randint(200,350)
body.position = x, y
shape = pm.Circle(body, radius, Vec2d(0,0))
shape.friction = 1.00
space.add_static(shape)
return shape
def draw_ball(screen, ball): #draw the falling balls to the screen
p = int(ball.body.position.x), 600-int(ball.body.position.y)
pygame.draw.circle(screen, (0, 255, 072), p, int(ball.radius), 0)
def draw_group(screen, ball): #draw the static group on the screen
p = int(ball.body.position.x), 600-int(ball.body.position.y)
pygame.draw.circle(screen, (0, 136, 252), p, int(ball.radius), 0)
def main():
pygame.init()
background = (255, 255, 255)
w=640
h= 480
screen=pygame.display.set_mode((w,h),0,32)
pygame.display.set_caption("FCQView")
clock = pygame.time.Clock()
running = True
points=[(20,120),(140,140),(110,30)]
color=(255,255,0)
gravity = -900.0
#Create Physics
pm.init_pymunk()
space = pm.Space()
space.gravity = Vec2d(0.0, gravity)
space.resize_static_hash()
space.resize_active_hash()
#groups
group = add_group(space)
#balls
balls= []
ticks_to_next_ball = 10
#main loop
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == KEYDOWN and event.key == K_ESCAPE:
running = False
ticks_to_next_ball -= 1
if ticks_to_next_ball <= 0:
ticks_to_next_ball = 25
ball_shape = add_ball(space)
balls.append(ball_shape)
screen.fill(background)
# Draw stuff
draw_group(screen, group) #draw the groups
balls_to_remove = []
for ball in balls:
draw_ball(screen, ball)#draw the balls
if ball.body.position.y < 100:#remove the balls after they fall
balls_to_remove.append(ball)
for ball in balls_to_remove:
space.remove(ball, ball.body)
balls.remove(ball)
# Update physics
dt = 1.0/60.0
for x in range(1):
space.step(dt)
# Flip screen
pygame.display.flip()
clock.tick(50)
pygame.display.set_caption("fps: " + str(clock.get_fps()))
if __name__ == '__main__':
sys.exit(main())