0

嗨,我想创建一个小型无人机模拟器。左右两台发动机。我基于 pymunk 库中的示例。但我不知道如何增加发动机功率以使其达到目标,即使物体旋转。这可以在这个库的帮助下轻松完成吗?感谢您的任何帮助

import sys,math
import pygame
from pygame.locals import *
from pygame.color import *

import pymunk
from pymunk.vec2d import Vec2d
import pymunk.pygame_util

width, height = 690,400
fps = 60
dt = 1./fps
JUMP_HEIGHT = 16.*2

def main():

    ### PyGame init
    pygame.init()
    screen = pygame.display.set_mode((width,height))

    clock = pygame.time.Clock()
    running = True

    ### Physics stuff
    space = pymunk.Space()
    space.gravity = 0,-1000
    draw_options = pymunk.pygame_util.DrawOptions(screen)

    # box walls
    static = [ pymunk.Segment(space.static_body, (10, 50), (680, 50), 3)
                , pymunk.Segment(space.static_body, (680, 50), (680, 370), 3)
                , pymunk.Segment(space.static_body, (680, 370), (10, 370), 3)
                , pymunk.Segment(space.static_body, (10, 370), (10, 50), 3)
                ]

    for s in static:
        s.friction = 1.
        s.group = 1
    space.add(static)

    # player
    body = pymunk.Body(5, pymunk.inf)
    body.position = 100,100

    left = pymunk.Poly(body, [(-30,25), (0, 25), (0, 0), (-30, 0)] )
    right = pymunk.Poly(body, [(0,0), (0, 25), (30, 25), (30, 0)] )

    left.color = 46,25,1,46
    right.color = 1,25,1,25

    space.add(body, left, right)

    while running:

        for event in pygame.event.get():
            if event.type == QUIT or \
                event.type == KEYDOWN and (event.key in [K_ESCAPE, K_q]):
                running = False

            elif event.type == KEYDOWN and event.key == K_LEFT:
                # here is the problem
                jump_v = math.sqrt(2.0 * JUMP_HEIGHT * abs(space.gravity.y))
                power = max(min(jump_v, 1000), 10) * 1.5
                impulse = power * Vec2d(0, 3)
                impulse.rotate(body.angle - 45)
                body.apply_impulse_at_world_point(impulse, (0,0) )

            elif event.type == KEYDOWN and event.key == K_RIGHT:
                # here is the problem
                jump_v = math.sqrt(2.0 * JUMP_HEIGHT * abs(space.gravity.y))
                power = max(min(jump_v, 1000), 10) * 1.5
                impulse = power * Vec2d(0, 3)
                impulse.rotate(body.angle + 45)
                body.apply_impulse_at_world_point(impulse, (0,0) )

        ### Clear screen
        screen.fill(pygame.color.THECOLORS["black"])

        ### Helper lines
        for y in [50,100,150,200,250,300]:
            color = pygame.color.THECOLORS['darkgrey']
            pygame.draw.line(screen, color, (10,y), (680,y), 1)

        # ### Draw stuff
        space.debug_draw(draw_options)

        pygame.display.flip()

        ### Update physics
        space.step(dt)

        clock.tick(fps)

if __name__ == '__main__':
    sys.exit(main())

4

1 回答 1

2

你的主要问题是你的身体有无限的时刻,这意味着它不能旋转。尝试将 Body 创建为:

body = pymunk.Body(5, 500)

您还需要在有意义的地方施加冲动。尝试:

body.apply_impulse_at_local_point(impulse, (5, 0))

和:

body.apply_impulse_at_local_point(impulse, (-5, 0))

为左和右。

于 2020-03-25T18:32:09.480 回答