2017-11-02 3 views
0

다른 속도 값을 설정하는 동적 객체가 있습니다. 그러나이 동적 인 몸체가 정적 인 몸체를 치면,이 충돌이 해결되어 되돌아 갈 때까지는 정적 인 몸체와 부분적으로 겹치게됩니다.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() 

답변

0

물리 엔진을 속여서 ( 무언가를 텔레포트했을 때 무언가를 텔레포트했을 때처럼 이상한 효과를 얻는 것처럼)

또한 오브젝트가 충돌 할 때 작은 겹침이 예상되는 경우, 즉 충돌이 해결되는 경우입니다. 공간에는 두 가지 속성이 있습니다.이 속성은 비트 제어를 위해 collision_slopcollision_bias입니다.

그러나 몇 가지 수동 수정을 시도해 볼 수 있습니다. 한 가지 방법은 충돌이 발생하면 물체를 멀리 옮기는 것입니다. 콜리 전 콜백으로이 작업을 수행 할 수 있습니다. 일부는 금고 실패도 이상 계정 추가 할 필요가 실제 코드에서

circle_shape.collision_type = 1 
h = space.add_wildcard_collision_handler(circle_shape.collision_type) 
def f(arbiter, space, data): 
    ps = arbiter.contact_point_set 
    arbiter.shapes[0].body.position += ps.normal * ps.points[0].distance 
h.post_solve = f 

(: 여기

당신이 중복을 방지하기 위해 예에 업데이트 기능을하기 전에 바로 넣을 수 있습니다 빠른 예입니다 더 복잡한 모양이있는 경우를 대비 한 점)

관련 문제