[pymunk] Creating objects inside a circle

Official forum for the Chipmunk2D Physics Library.
BilbyCoder
Posts: 2
Joined: Sun Oct 12, 2008 6:44 am
Contact:

[pymunk] Creating objects inside a circle

Post by BilbyCoder »

Hi,

I'm using pymunk and I'm trying to create a "container" for the other objects. I'm generating a circle shape which I'm adding to the space using add_static. When I create smaller circles inside this circle they accelerate left or right (their x position is randomly generated) until they reach the edge of the static circle. Then they drop vertically down.

The code I'm using:

Code: Select all

import sys, random
import pygame
from pygame.locals import *
from pygame.color import *
import pymunk as pm

def add_ball(space):
    mass = 1
    radius = 14
    inertia = pm.moment_for_circle(mass, 0, radius, (0, 0))
    body = pm.Body(mass, inertia)
    x = random.randint(250, 350)
    body.position = x, 300
    shape = pm.Circle(body, radius, (0, 0))
    space.add(body, shape)
    return shape

def draw_ball(screen, ball):
    p = int(ball.body.position.x), 600 - int(ball.body.position.y)
    pygame.draw.circle(screen, THECOLORS['blue'], p, int(ball.radius), 2)


def main():
    pygame.init()
    screen = pygame.display.set_mode((600, 600))
    clock = pygame.time.Clock()
    running = True

    pm.init_pymunk()
    space = pm.Space()
    space.gravity = (0.0, -900.0)

    balls = []

    ticks_to_next_ball = 10

    #static ball
    mass = pm.inf
    radius = 300
    inertia = pm.inf
    body = pm.Body(mass, inertia)
    body.position = 300, 300
    shape = pm.Circle(body, radius, (0, 0))
    space.add_static(shape)

    balls.append(shape)

    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'])

        x = 0
        for ball in balls:
            x += 1
            if ball.body.position.y < 0:
                space.remove(ball)
                balls.remove(ball)
            else:
                draw_ball(screen, ball)

        print x
        
        space.step(1/50.0)
        pygame.draw.circle(screen, THECOLORS['black'], (450, 450), 2)
        pygame.display.flip()
        clock.tick(50)


if __name__== '__main__':
    sys.exit(main())

If anyone can help it would be appreciated
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: [pymunk] Creating objects inside a circle

Post by slembcke »

Circles are a solid shape, you cannot put objects inside of them.

If you want to do that, you will have to make an approximate circle out of line segment shapes.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
BilbyCoder
Posts: 2
Joined: Sun Oct 12, 2008 6:44 am
Contact:

Re: [pymunk] Creating objects inside a circle

Post by BilbyCoder »

Thank you. Obvious in hindsight really. I presume the behavior of the smaller circles moving to the side is them being "pushed out" of an intersecting collision.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: [pymunk] Creating objects inside a circle

Post by slembcke »

Exactly.

A trick you can do if you want the big circle to still collide as a big circle and not a collection of line segments is to use the layer bitmask (read of on cpShapes for more info). You could use layers in a number of different ways, though I see the following working nicely.
  • Layer 1 - All your normal objects, level geometry, and a big circle. (not the interior objects)
  • Layer 2 - The big line segment circle and the interior objects.
This way the circle will still roll nicely, and generally act as a perfect circle at least to the outside world.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Knertified
Posts: 4
Joined: Tue Oct 28, 2008 8:22 pm
Contact:

Re: [pymunk] Creating objects inside a circle

Post by Knertified »

I have the same problem and tried creating a about 8 cpSegmentShape's to build an Octagon. I drop a bunch of circles inside and they all shoot outside. It's the exact same problem when using cpCircleShapeNew. I don't think it matters either way. Any ideas?

Thanks!!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: [pymunk] Creating objects inside a circle

Post by slembcke »

Are you doing anything to prevent the collisions with the circle? If you allow the objects to collide with the circle, they will get pushed out. Polygons and circles are solid objects, so they will push other objects out of themselves.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Knertified
Posts: 4
Joined: Tue Oct 28, 2008 8:22 pm
Contact:

Re: [pymunk] Creating objects inside a circle

Post by Knertified »

I somehow mixed up something. I think I was still drawing the circle and the line segmented circle at the same time lol. The line segmented circle works almost perfectly. For some reason when a lot is going on... which doesn't take much on the iPhone (25 or so squares in a 20 sided polygon), the squares start to fall out. For example, when I get under about 20fps, it's like the system isn't quick enough to check for collisions and lets them slip through. I'm going to do some more forum searching to see if I'm missing something.

Thanks,
Mike
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: [pymunk] Creating objects inside a circle

Post by slembcke »

Are you using a variable size time step?

Chipmunk works with variable sized time steps, but I strongly recommend that you don't.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Knertified
Posts: 4
Joined: Tue Oct 28, 2008 8:22 pm
Contact:

Re: [pymunk] Creating objects inside a circle

Post by Knertified »

The only timing information I can find is this:
[[Director sharedDirector] setAnimationInterval:1.0/60];

Now, I understand that is really a cococs2d method so I suppose it's possible cocos is doing something weird and I might need to dig a bit deeper. I'm not sure if your familiar with cocos or not but I'm basically just modifying the Chipmunk_Accel.m file and leaving the framework intact.

A side note, I changed the squares to circles and it can handle almost 2x as many circles before I start seeing problems... About 50. It's like the iPhone just stops dead for a second and all the smaller circles just blow out of the larger segmented circle. After that the FPS shoots way up because there are only 10 or so left. lol.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: [pymunk] Creating objects inside a circle

Post by slembcke »

Hmm. Not familiar with Cocos. It's possible that it's using variable timesteps.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests