import pygame
from pygame.locals import *
from Box2D import *
from Box2D.b2 import *

SCREEN_WD = 640
SCREEN_HT = 480
TARGET_FPS = 60
PPM = 20.0

screen = pygame.display.set_mode((SCREEN_WD, SCREEN_HT), 0, 32)
pygame.display.set_caption("PyBox2D_Example")
clock = pygame.time.Clock()

world = b2World(gravity = (0, 0), doSleep = True)

ground1BodyDef = b2BodyDef()
ground1BodyDef.position.Set(0, 0)
ground1Body = world.CreateBody(ground1BodyDef)
ground1Shape = b2PolygonShape()
ground1Shape.SetAsBox(50, 1)
ground1Body.CreateFixture(shape = ground1Shape)

box1BodyDef = b2BodyDef()
box1BodyDef.type = b2_dynamicBody
box1BodyDef.position.Set(16, 20)
box1Body = world.CreateBody(box1BodyDef)
box1Shape = b2PolygonShape()
box1Shape.SetAsBox(1, 1)
box1FixtureDef = b2FixtureDef()
box1FixtureDef.shape = box1Shape
box1FixtureDef.density = 1
box1FixtureDef.friction = 0.3
box1Body.CreateFixture(box1FixtureDef)

box2BodyDef = b2BodyDef()
box2BodyDef.type = b2_dynamicBody
box2BodyDef.position.Set(16, 15)
box2Body = world.CreateBody(box2BodyDef)
box2Shape = b2PolygonShape()
box2Shape.SetAsBox(1, 1)
box2FixtureDef = b2FixtureDef()
box2FixtureDef.shape = box2Shape
box2FixtureDef.density = 1
box2FixtureDef.friction = 0.3
box2Body.CreateFixture(box2FixtureDef)

jointDef = b2PrismaticJointDef()
worldAxis = b2Vec2(0, 1.0)
jointDef.Initialize(box1Body, box2Body, box1Body.worldCenter, worldAxis)
jointDef.lowerTranslation = -5.0
jointDef.upperTranslation = 2.5
jointDef.enableLimit = True
jointDef.maxMotorForce = 1.0
jointDef.motorSpeed = 0
jointDef.enableMotor = True
prismaticJoint = world.CreateJoint(jointDef)

timeStep = 1.0 / 60
velIters = 10
posIters = 10

colors = {
    staticBody  : (255,255,255,255),
    dynamicBody : (127,127,127,255),
}

def my_draw_polygon(polygon, body, fixture):
    vertices=[(body.transform*v)*PPM for v in polygon.vertices]
    vertices=[(v[0], SCREEN_HT-v[1]) for v in vertices]
    pygame.draw.polygon(screen, colors[body.type], vertices)
polygonShape.draw=my_draw_polygon

def my_draw_circle(circle, body, fixture):
    position=body.transform*circle.pos*PPM
    position=(position[0], SCREEN_HT-position[1])
    pygame.draw.circle(screen, colors[body.type], [int(x) for x in position], int(circle.radius*PPM))
circleShape.draw=my_draw_circle

prismaticJoint.__SetMotorSpeed(15)

running = True
while running:
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False
            continue
        if event.type == KEYDOWN and event.key == K_ESCAPE:
            running = False
            continue

    screen.fill((0, 0, 0, 0))

    for body in world.bodies:
        for fixture in body.fixtures:
            fixture.shape.draw(body, fixture)
                     
    world.Step(timeStep, velIters, posIters)
    pygame.display.flip()
    clock.tick(TARGET_FPS)

pygame.quit()
print("done")


[Python] pyBox2D 사용 예제 - 9. Prismatic Joint

Prismatic Joint는 두개의 body를 가변 가능한 길이로 연결하는데, 마치 유압 실린더와 같은 역활을 합니다.


아래의 코드로 가동 범위를 설정한 후에,

jointDef.lowerTranslation = -5.0
jointDef.upperTranslation = 2.5
jointDef.enableLimit = True


움직일 속도를 세팅합니다.

prismaticJoint.__SetMotorSpeed(15)


,