Beispiel #1
0
 def __init__(self, space, mass, radius, pos=(300,300), color=(0,240,0,255)):
     self.color = color
     self.mass = mass
     self.radius = radius
     ball_moment = cp.cpMomentForCircle(mass, 5, 0.0, vec2d(0,0))
     self.body = cp.cpBodyNew( mass, ball_moment )
     self.shape = cp.cpCircleShapeNew(self.body,
             radius, vec2d(0.0, 0.0) )
     self.shape.contents.e = 0.5
     self.set_position( pos )
     cp.cpSpaceAddBody(space, self.body)
     cp.cpSpaceAddShape( space, self.shape )
def create_cube(space, mass=5.0, xy=(0, 0)):
    p_num = 4
    P_ARR = vec2d * p_num
    p_arr = P_ARR(vec2d(0, 0))

    s = 5
    sprite = rabbyt.Sprite(shape=(-s, s, s, -s), xy=xy)
    for i, p in enumerate(sprite.shape):
        p_arr[i].x, p_arr[i].y = p

    moment = 500
    body = cp.cpBodyNew(mass, moment)
    body.contents.p = vec2d(*xy)
    cp.cpSpaceAddBody(space, body)

    shape = cp.cpPolyShapeNew(body, p_num, p_arr, vec2d(0, 0))
    shape.contents.u = 0.5
    shape.contents.collision_type = COLLTYPE_DEFAULT
    cp.cpSpaceAddShape(space, shape)

    anims = chipmunk_body_anims(body)
    sprite.x, sprite.y, sprite.rot = anims

    return shape, sprite
def create_cube(space, mass = 5.0, xy = (0,0)):
    p_num = 4
    P_ARR = vec2d * p_num
    p_arr = P_ARR(vec2d(0,0))
    
    s = 5
    sprite = rabbyt.Sprite(shape=(-s,s,s,-s), xy=xy)
    for i, p in enumerate(sprite.shape):
        p_arr[i].x, p_arr[i].y = p

    moment = 500
    body = cp.cpBodyNew(mass, moment)
    body.contents.p = vec2d(*xy)
    cp.cpSpaceAddBody(space, body)
    
    shape = cp.cpPolyShapeNew(body, p_num, p_arr, vec2d(0,0))
    shape.contents.u = 0.5
    shape.contents.collision_type = COLLTYPE_DEFAULT
    cp.cpSpaceAddShape(space, shape)

    anims = chipmunk_body_anims(body)
    sprite.x, sprite.y, sprite.rot = anims

    return shape, sprite
Beispiel #4
0
    def __init__(self, space, mass, width, height, pos, color=(255,0,0,255)):
        poly = [
            [ width/2, height/2 ],
            [ width/2, -height/2 ],
            [ -width/2, -height/2 ],
            [ -width/2, height/2 ],
            ]
        p_num = len(poly)
        P_ARR = vec2d * p_num
        p_arr = P_ARR(vec2d(0,0))
        for i, (x,y) in enumerate( poly ):
            p_arr[i].x = x
            p_arr[i].y = y

        moment = cp.cpMomentForPoly( mass, p_num, p_arr, vec2d(0,0))
        self.color = color
        self.body = cp.cpBodyNew( mass, moment )
        self.body.contents.p = vec2d( *pos )
        cp.cpSpaceAddBody( space, self.body )
        self.shape = shape = cp.cpPolyShapeNew(
            self.body, p_num, p_arr, vec2d(0,0)
            )
        shape.contents.u = 0.5
        cp.cpSpaceAddShape(space, shape)
if __name__ == "__main__":
    pygame.init()
    pygame.display.set_mode((800, 600), pygame.OPENGL | pygame.DOUBLEBUF)
    rabbyt.set_viewport((800, 600))
    rabbyt.set_default_attribs()

    ### Physics stuff
    cp.cpInitChipmunk()
    space = cp.cpSpaceNew()
    space.contents.gravity = vec2d(0.0, -900.0)

    cp.cpSpaceResizeStaticHash(space, 50.0, 2000)
    cp.cpSpaceResizeActiveHash(space, 50.0, 100)

    wallBody = cp.cpBodyNew(1e100, 1e100)
    wallShape = cp.cpSegmentShapeNew(wallBody, vec2d(-300, -300),
                                     vec2d(300, -300), 0.0)
    wallShape.contents.u = 1.0
    wallShape.contents.collision_type = COLLTYPE_DEFAULT
    cp.cpSpaceAddStaticShape(space, wallShape)

    shapes = []
    sprites = []

    for x in range(-200, 200, 15):
        for y in range(-200, 400, 15):
            shape, sprite = create_cube(space,
                                        xy=(x + random.random() * 10, y))
            shapes.append(shape)
            sprites.append(sprite)
if __name__ == "__main__":
    pygame.init()
    pygame.display.set_mode((800,600), pygame.OPENGL | pygame.DOUBLEBUF)
    rabbyt.set_viewport((800, 600))
    rabbyt.set_default_attribs()

    ### Physics stuff
    cp.cpInitChipmunk()
    space = cp.cpSpaceNew()
    space.contents.gravity = vec2d(0.0, -900.0)
    
    cp.cpSpaceResizeStaticHash(space, 50.0, 2000)
    cp.cpSpaceResizeActiveHash(space, 50.0, 100)

    wallBody = cp.cpBodyNew(1e100, 1e100)
    wallShape = cp.cpSegmentShapeNew(wallBody, vec2d(-300, -300),
            vec2d(300, -300), 0.0)
    wallShape.contents.u = 1.0
    wallShape.contents.collision_type = COLLTYPE_DEFAULT
    cp.cpSpaceAddStaticShape(space, wallShape)


    shapes = []
    sprites = []

    for x in range(-200, 200, 15):
        for y in range(-200, 400, 15):
            shape, sprite = create_cube(space, xy = (x+random.random()*10,y))
            shapes.append(shape)
            sprites.append(sprite)
Beispiel #7
0
from pyglet import clock
from pyglet.gl import *

import pymunk._chipmunk as cp
import pymunk.util as u
from pymunk import Vec2d as vec2d
import ctypes

from numpy import arange, array, size
import numpy

import primitives as draw


INFINITY = float("infinity")
BACKGROUND = cp.cpBodyNew(INFINITY, INFINITY)

import reply

def cpvrotate(v1, v2):
    return vec2d(v1.x*v2.x - v1.y*v2.y, v1.x*v2.y + v1.y*v2.x)
def cpvadd(v1, v2):
    return vec2d(v1.x + v2.x, v1.y + v2.y)
dt = 1.0/30.0


class Ball:
    def __init__(self, space, mass, radius, pos=(300,300), color=(0,240,0,255)):
        self.color = color
        self.mass = mass
        self.radius = radius
Beispiel #8
0
 def __init__(self, mass, inertia):
     self._body = cp.cpBodyNew(mass, inertia)
     self._bodycontents =  self._body.contents 
     self._position_callback = None # To prevent the gc to collect the callbacks.
     self._velocity_callback = None # To prevent the gc to collect the callbacks.
Beispiel #9
0
 def __init__(self, mass, inertia):
     self._body = cp.cpBodyNew(mass, inertia)
     self._bodycontents =  self._body.contents