【问题标题】:python box2d pygame screen coordinates to box2d gridpython box2d pygame屏幕坐标到box2d网格
【发布时间】:2022-07-01 23:53:56
【问题描述】:

我不知道从鼠标坐标中获取世界坐标的正确方法。

在这样的问题中:mouseWorld Coordinates Box2D 有一个方法:box2d.coordPixelsToWorld(x,y);但我在 box2d 的 Python 库中找不到此方法。

position = pygame.mouse.get_pos() 

position = ??? * ??? 

body.position = position

更多上下文代码:


PPM = 20.0  # pixels per meter
TARGET_FPS = 60
TIME_STEP = 1.0 / TARGET_FPS
SCREEN_WIDTH, SCREEN_HEIGHT = 1280, 720  # 640, 480

# --- pygame setup ---
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), 0, 32, display=1)
pygame.display.set_caption('Simple pygame example')
clock = pygame.time.Clock()

body = world.CreateStaticBody(
        position=(i, 1),
        shapes=box2d.b2PolygonShape(box=(1, 1)),
    ))

bodies.append(body)

colors = {
    box2d.b2_staticBody: (255, 255, 255, 255),
    box2d.b2_dynamicBody: (127, 127, 127, 255),
}

# --- main game loop ---
running = True
while running:
    # Check the event queue
    for event in pygame.event.get():
        if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
            # The user closed the window or pressed escape
            running = False

    screen.fill((0, 0, 0, 0))
    
    position = pygame.mouse.get_pos() 

    position = ??? * ??? 

    body.position = position

    for body in bodies:  # or: world.bodies
        # The body gives us the position and angle of its shapes
        # body
        for fixture in body.fixtures:
            # The fixture holds information like density and friction,
            # and also the shape.
            shape = fixture.shape

            # Naively assume that this is a polygon shape. (not good normally!)
            # We take the body's transform and multiply it with each
            # vertex, and then convert from meters to pixels with the scale
            # factor.
            vertices = [(body.transform * v) * PPM for v in shape.vertices]

            # But wait! It's upside-down! Pygame and Box2D orient their
            # axes in different ways. Box2D is just like how you learned
            # in high school, with positive x and y directions going
            # right and up. Pygame, on the other hand, increases in the
            # right and downward directions. This means we must flip
            # the y components.
            vertices = [(v[0], SCREEN_HEIGHT - v[1]) for v in vertices]

            pygame.draw.polygon(screen, colors[body.type], vertices)

    # Make Box2D simulate the physics of our world for one step.
    # Instruct the world to perform a single step of simulation. It is
    # generally best to keep the time step and iterations fixed.
    # See the manual (Section "Simulating the World") for further discussion
    # on these parameters and their implications.
    world.Step(TIME_STEP, 10, 10)

    # Flip the screen and try to keep at the target FPS
    pygame.display.flip()
    clock.tick(TARGET_FPS)

pygame.quit()

if __name__ == "__main__":
    print('Done!')

编辑:PPM 值是每米像素的数量。 如果我将 pygame.mouse.get_pos().x 与 PPM 分开,则它是正确的值。但是 y 值偏离了 3 '米'

EDIT2:因为它是 (SCREEN_HEIGHT - y) / PPM

【问题讨论】:

    标签: python pygame box2d


    【解决方案1】:

    虽然不熟悉 Box2d for Python,但如果您正在寻找的是将鼠标 X,Y 转换为世界 X,Y 只需使用 Box2d 转换因子、PIXEL_PER_METERS 或您使用的任何东西转换鼠标 x、y,所以 f.ex 浮动 PIXEL_PER_METER = 100.f; 因此 screenpos 1200, 600 在“box2d 单位/米”中是 12,6

    • 你已经做到了,但有点偏离,你考虑浮点/整数转换了吗? Python 将毕竟调用 C++ Box2d 函数,因此请确保您没有将任何浮点数转换为整数,但仔细观察,您似乎并没有这样做。
    • Camera ScreenToWorld 或 WorldToScreen -> 它们是如何实现的? 我想你在某处有类似的东西, 此问题是相机功能问题的典型症状。

    但是如何将鼠标转换为世界的答案是: - 如果你想要 Box2d 单位的世界:用比例因子转换 - 将 Camera.Offset 值添加到鼠标 x,y 以获得世界位置

    例如: Camera.MinWorld = 400,100 Mouse.x + Camera.MinWorld.x , Mouse.y + Camera.MinWorld.y = 世界位置

    世界位置到屏幕位置: pos.x - Camera.MinWorld.x , pos.y - Camera.MinWorld.y

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-07
      • 1970-01-01
      • 2012-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多