在Python中计算对象的轨迹

时间:2018-06-05 06:01:14

标签: python angle pyglet direction

我正在制作一个子弹类,它会创建一个沿目标方向移动的黄色三角形,并继续以恒定速度移动。然而,当我发射子弹时,它以倾斜的角度移动。我很确定问题出在这个声明中:     self.heading = Vector2D(sin(dir.y),cos(dir.x)) 完整的类代码如下:

from vector2d import Vector2D
from vector2d import Point2D
from matrix33 import Matrix33
from graphics import egi
from random import randint
from math import sin, cos, radians
from random import random, randrange, uniform
from pyglet import clock

class Bullet(object):
    def __init__(self, world=None, scale=5.0, mass=1.0):
        self.world = world
        self.pos = Vector2D(250, 40)

        self.color = 'YELLOW'
        self.scale = Vector2D(scale, scale)
        self.speed = 10
        self.max_speed = 100
        dir = self.world.target.pos - self.pos
        self.heading = Vector2D(sin(dir.y), cos(dir.x))
        self.heading.truncate(self.max_speed) # <-- new force limiting code
        self.side = self.heading.perp()

        self.shape = [
            Point2D(-1.0,  0.8),
            Point2D( 1.0,  0.0),
            Point2D(-1.0, -0.8)
        ]

    def update(self):
        self.pos = self.pos + self.heading

    def render(self):
        pts = self.world.transform_points(self.shape, self.pos,
                                      self.heading, self.side, self.scale)
        egi.set_pen_color(name=self.color)
        egi.closed_shape(pts, True)

非常感谢任何帮助。提前谢谢!

0 个答案:

没有答案