然后用这个速度和加速度和初始位置找到下一个位置(2D)。唯一棘手的部分是矢量的创建!
答案 0 :(得分:1)
只需使用标准矢量数学。距离是毕达哥拉斯定理,幅度是三角学:
from math import *
class Vector2D:
def __init__(self, x, y):
self.x = float(x)
self.y = float(y)
def direction(self):
return degrees(atan(self.y / self.x))
def magnitude(self):
return sqrt(self.x ** 2 + self.y ** 2)
答案 1 :(得分:0)
您可以创建一个类,然后该类的每个实例(对象)都是velocity
对象(向量)。一个非常简单的例子是 -
class Velocity:
def __init__(self, mag, direction):
self.mag = mag
self.direction = direction
然后你可以创建速度对象,如 -
v1 = Velocity(5,5)
v2 = Velocity(10,15)
您可以访问每个速度的大小和方向 -
print(v1.mag)
>> 5
print(v1.direction)
>> 5
以上是一个简约示例,您需要添加您希望我们的速度对象支持的任何操作(读取函数)。
答案 2 :(得分:0)
您可以创建一个velocity
元组,其中幅度位于索引0处,方向位于索引1.然后将加速度定义为浮点数和startingPos
,其中x位于索引0且y位于指数1。
#0: magnitude 1: direction (degrees above x axis)
velocity = (2.3, 55)
acceleration = 3.2
#0: x 1: y
startingPos = (-10, 0)