chain for the beginner

Official forum for the Chipmunk2D Physics Library.
Post Reply
sega74rus
Posts: 3
Joined: Sun Jan 02, 2011 1:42 pm
Contact:

chain for the beginner

Post by sega74rus »

Hello.
trying to master the Chipmunk in python,
I want to make the chain: the object is associated with previous joint.
But it turns out wrong. Tell me how to do it.

ps. sorry if the wrong words, using a translator

Code: Select all

import sys, random
import pygame
from pygame.locals import *
from pygame.color import *
import pymunk
import math


def main():
    pygame.init()
    screen = pygame.display.set_mode((600, 600))
    pygame.display.set_caption("this Chipmunk")
    clock = pygame.time.Clock()
    running = True
    
    pymunk.init_pymunk()
    space = pymunk.Space()
    space.gravity = (0.0, -900.0)
    
    lines = add_L(space)
    balls = []
    
    ticks_to_next_ball = 10

    
    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(THECOLORS["white"])
                
        balls_to_remove = []
        for ball in balls:
            if ball.body.position.y < 150:
                balls_to_remove.append(ball)
            draw_ball(screen, ball)
        
        for ball in balls_to_remove:
            space.remove(ball, ball.body)
            balls.remove(ball)
        
        draw_lines(screen, lines)
        
        #pygame.draw.circle(screen, THECOLORS["red"], (300,300), 5)
        #pygame.draw.circle(screen, THECOLORS["green"], (200,300), 25, 2)
        timetick = 50
        space.step(1./timetick)
        
        pygame.display.flip()
        clock.tick(timetick)
        
def to_pygame(p):
    return int(p.x), int(-p.y+600)

def add_ball(space):
    """Add a ball to the given space at a random position"""
    mass = 1
    radius = 14
    inertia = pymunk.moment_for_circle(mass, 0, radius, (0,0))
    body = pymunk.Body(mass, inertia)
    x = random.randint(120,380)
    body.position = x, 550
    shape = pymunk.Circle(body, radius, (0,0))
    shape.friction = 1
    space.add(body, shape)
    return shape
    

def draw_ball(screen, ball):
    """Draw a ball shape"""
    x,y = int(ball.body.position.x), 600-int(ball.body.position.y)
    pygame.draw.circle(screen, THECOLORS["red"], (x,y), int(ball.radius), 2)
    x1 = math.cos(-ball.body.angle)*int(ball.radius) + x
    y1 = math.sin(-ball.body.angle)*int(ball.radius) + y
    pygame.draw.lines(screen, THECOLORS["red"], False, [(x,y),(x1,y1)])
    
def add_L(space):

    point1 = pymunk.Body(pymunk.inf, pymunk.inf)
    point1.position = (100,300)
    point2 = pymunk.Body(10, 1000)
    point2.position = (300,300)
    point3 = pymunk.Body(pymunk.inf, pymunk.inf)
    point3.position = (500,300)
    
    body1 = pymunk.Body(10, 10000)
    body1.position = (100,300)    
    l1 = pymunk.Segment(body1, (0, 0), (200, 0.0), 5.0) 

    body2 = pymunk.Body(10, 10000)
    body2.position = (300,300)    
    l2 = pymunk.Segment(body2, (0, 0), (200, 0.0), 5.0)
    
    rot1 = pymunk.PinJoint(body1, point1, (0,0), (0,0)) 
    rot2 = pymunk.PinJoint(point2,body1, (0,0), (0,0)) 
    rot3 = pymunk.PinJoint(body2, point2, (0,0), (0,0)) 
    rot4 = pymunk.PinJoint(body2, point3, (0,0), (0,0))
    
    space.add(l1, l2, body1,body2, rot1,rot2,rot3,rot4) 
    
    l1.friction = 0.5
    l2.friction = 0.5
    return (l1,l2),(point1,point2,point3)
    

def draw_lines(screen, data):
    lines = data[0]
    for line in lines:
        body = line.body
        pv1 = body.position + line.a.rotated(body.angle)
        pv2 = body.position + line.b.rotated(body.angle)
        p1 = to_pygame(pv1)
        p2 = to_pygame(pv2)
        pygame.draw.lines(screen, THECOLORS["gray"], False, [p1,p2])   
    
    points = data[1] 
    for point in points:
        #print dir(point)
        x,y = int(point.position.x), 600-int(point.position.y)
        pygame.draw.circle(screen, THECOLORS["red"], ( x,y ), 5)
        
if __name__ == '__main__':
    sys.exit(main())
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: chain for the beginner

Post by slembcke »

I've never used pymunk, but the author reads these every few days.

I don't see where you are making a chain. In the add_L() function? I don't understand what the joints are for. A drawing might help.

Maybe you just want to add both segment shapes to the same body so they are solid:

Code: Select all

    body = pymunk.Body(...)
    body.position = ...
    l1 = pymunk.Segment(body, (...), (...), 5.0)     
    l2 = pymunk.Segment(body, (...), (...), 5.0)
Don't worry about language, I understand the translator well enough.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
sega74rus
Posts: 3
Joined: Sun Jan 02, 2011 1:42 pm
Contact:

Re: chain for the beginner

Post by sega74rus »

hello slembcke
I want to get just such a model.
I now have is 2 body falls down, or rotates relative to another point
Image
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests