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, -10), 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, 24)
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, 18)
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 = b2DistanceJointDef()
jointDef.Initialize(box1Body, box2Body, box1Body.worldCenter, box2Body.worldCenter)
jointDef.frequencyHz = 4.0
jointDef.dampingRatio = 0.5
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

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 사용 예제 - 7. Distance Joint

Distance Joint는 두개의 body를 고정된 길이로 연결하여, 마치 스프링 연결과 같은 역활을 합니다. 이 때 스프링의 특성은 frequencyHz와 dampingRatio 속성을 사용하여 정의되는데


  • frequencyHz : 진동수를 나타내며, 일반적으로 time step의 빈도수의 절반보다 작은 값을 가져야 합니다. 즉, time step이 60Hz 라면 frequencyHz는 30Hz 미만의 값을 가져야 합니다.
  • dampingRatio : 감쇄율을 나타내며, 일반적으로 0과 1사이의 값을 가집니다.



,