import math
import pygame
from Box2D import *
pygame.init()
pygame.display.set_caption("Physics Test")
screen = pygame.display.set_mode((640, 480))
running = True
player = pygame.image.load("player.png").convert()
# World 정의
world = b2World(gravity=(0, 9.8), doSleep=True)
# Ground 정의
groundBodyDef = b2BodyDef()
groundBodyDef.position = (0, 400)
groundBody = world.CreateBody(groundBodyDef)
groundShape = b2PolygonShape()
groundShape.SetAsBox(500, 0)
groundBody.CreateFixture(shape=groundShape)
#groundBody = world.CreateStaticBody(position=(0, 400), shapes=b2PolygonShape(box=(500, 0)))
# 위 정의와 동일
# Wall 정의
wallBodyDef = b2BodyDef()
wallBodyDef.position = (300, 0)
wallBody = world.CreateBody(wallBodyDef)
wallShape = b2PolygonShape()
wallShape.SetAsBox(0, 400)
wallBody.CreateFixture(shape=wallShape)
#wallBody = world.CreateStaticBody(position=(300, 0), shapes=b2PolygonShape(box=(0, 400)))
# 위 정의와 동일
# Player 정의
playerBodyDef = b2BodyDef()
playerBodyDef.type = b2_dynamicBody;
playerBodyDef.position = (0, 0)
playerBodyDef.linearVelocity = (50, 0)
playerBodyDef.angularVelocity = 0.2
playerBody = world.CreateBody(playerBodyDef)
playerShape = b2PolygonShape()
playerShape.SetAsBox(player.get_width()/2, player.get_height()/2)
playerFixtureDef = b2FixtureDef(shape=playerShape)
playerFixtureDef.density = 1
playerFixtureDef.friction = 0.5
playerFixtureDef.restitution = 0.7
playerBody.CreateFixture(playerFixtureDef)
#playerBody = world.CreateDynamicBody(position=(0, 0), linearVelocity=(50, 0),
# angularVelocity=0.2)
#playerFixtureDef = playerBody.CreatePolygonFixture(box=(player.get_width()/2,
# player.get_height()/2), density=1, friction=0.5, restitution=0.7)
# 위 정의와 동일
timeStep = 1.0 / 300
vel_iters, pos_iters = 6, 2
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
playerBody.transform = ((0, 0), 0)
playerBody.linearVelocity = (50, 0)
playerBody.angularVelocity = 0.2
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
running = False
# Instruct the world to perform a single step of simulation. It is
# generally best to keep the time step and iterations fixed.
world.Step(timeStep, vel_iters, pos_iters)
# Clear applied body forces. We didn't apply any forces, but you
# should know about this function.
world.ClearForces()
screen.fill("white")
pygame.draw.rect(screen, "brown", (0, 400, 600, 20)) # Ground 그리기
pygame.draw.rect(screen, "black", (300, 0, 20, 400)) # Wall 그리기
rotated_player = pygame.transform.rotate(player, playerBody.angle * 180/math.pi)
# Unless rotating by 90 degree increments, the image will be padded larger to hold
# the new size. If the image has pixel alphas, the padded area will be transparent.
# Otherwise pygame will pick a color that matches the Surface colorkey or the topleft
# pixel value.
screen.blit(rotated_player, (playerBody.position[0] - rotated_player.get_width()/2,
playerBody.position[1] - rotated_player.get_height()/2))
pygame.display.flip()
pygame.quit()