다른 속도 값을 설정하는 동적 객체가 있습니다. 그러나이 동적 인 몸체가 정적 인 몸체를 치면,이 충돌이 해결되어 되돌아 갈 때까지는 정적 인 몸체와 부분적으로 겹치게됩니다.Pymunk : 동적 객체가 정적 객체로 이동하지 못하도록합니다.
pymunk에서 속도가이 방향으로 적용 되더라도 정적 바디의 경계에서 정확하게 동적 바디를 정지시키는 방법이 있습니까? 충돌 충돌이있는 경우 두 가지 모양이 겹치기보다는 다른 방법으로 해결해야합니다.
힘을 가하는 것은 내가 일정한 속도를 갖고 싶어하기 때문에 실제로 선택 사항이 아닙니다.
는 (아래 코드는 작업을 두 번 실행해야합니다.) 당신이 몸을 수동으로 위치 또는 속도를 설정할 때 그것이 있기 때문에, 이상한 효과를 기대해야한다는 답을 일반적으로import pymunk
import pyglet
from PIL import Image
from PIL import ImageDraw
# setup of pyglet
window = pyglet.window.Window()
main_batch = pyglet.graphics.Batch()
keys = pyglet.window.key.KeyStateHandler()
window.push_handlers(keys)
# setup of pymunk
space = pymunk.Space()
"""MOVABLE CIRCLE"""
# creating pyglet sprite
circle_img = Image.new('RGBA', (50,50))
draw = ImageDraw.Draw(circle_img)
draw.ellipse((1, 1, 50-1, 50-1), fill=(255,0,0))
circle_img.save('circle.png')
pyglet_circle_img = pyglet.resource.image('circle.png')
pyglet_circle_img.anchor_x = pyglet_circle_img.width/2
pyglet_circle_img.anchor_y = pyglet_circle_img.height/2
circle_sprite = pyglet.sprite.Sprite(pyglet_circle_img, window.width/2, window.height/2, batch=main_batch)
# creating pymunk body and shape
mass = 2
radius = 25
moment = pymunk.moment_for_circle(mass, 0, radius)
circle_body = pymunk.Body(mass, moment)
circle_body.position = circle_sprite.position
circle_shape = pymunk.Circle(circle_body, 25)
circle_shape.elasticity = 0.0
space.add(circle_body, circle_shape)
"""STATIC SQUARE"""
# creating pyglet sprite
square_img = Image.new('RGBA', (70,70))
draw = ImageDraw.Draw(square_img)
draw.rectangle([(0, 0), (70-1, 70-1)], fill=(0,255,0))
square_img.save('square.png')
pyglet_square_img = pyglet.resource.image('square.png')
pyglet_square_img.anchor_x = pyglet_square_img.width/2
pyglet_square_img.anchor_y = pyglet_square_img.height/2
square_sprite = pyglet.sprite.Sprite(pyglet_square_img, 3*window.width/4, window.height/2, batch=main_batch)
# creating pymunk body and shape
square_body = pymunk.Body(body_type=pymunk.Body.KINEMATIC)
square_body.position = square_sprite.position
square_shape = pymunk.Poly(square_body, [(-35,-35),(-35,35),(35,35),(35,-35)])
square_shape.elasticity = 0.0
space.add(square_body, square_shape)
def update(dt):
space.step(dt)
circle_sprite.position = circle_body.position
print(circle_body.position)
key_pressed = False
if keys[pyglet.window.key.LEFT]:
circle_body.velocity = (-100,0)
key_pressed = True
elif keys[pyglet.window.key.RIGHT]:
circle_body.velocity = (100, 0)
key_pressed = True
if keys[pyglet.window.key.UP]:
circle_body.velocity = (0, 100)
key_pressed = True
elif keys[pyglet.window.key.DOWN]:
circle_body.velocity = (0, -100)
key_pressed = True
if not key_pressed:
circle_body.velocity = (0,0)
@window.event
def on_draw():
window.clear()
main_batch.draw()
pyglet.clock.schedule_interval(update, 1/60.)
pyglet.app.run()