简体   繁体   English

pygame.math.Vector2 构造函数参数表示什么?

[英]What do the pygame.math.Vector2 constructor parameters indicate?

I'm trying to figure out how to create a unit vector / normalized Vector2 instance given an angle (degrees).我试图弄清楚如何在给定角度(度)的情况下创建单位向量/归一化 Vector2 实例。 I encountered the following as I was reading pygame's documentation at https://www.pygame.org/docs/ref/math.html#pygame.math.Vector2 :我在阅读 pygame 在https://www.pygame.org/docs/ref/math.html#pygame.math.Vector2的文档时遇到了以下问题:

Vector2() -> Vector2
Vector2(int) -> Vector2
Vector2(float) -> Vector2
Vector2(Vector2) -> Vector2
Vector2(x, y) -> Vector2
Vector2((x, y)) -> Vector2

To my understanding, they generate the following vectors:据我了解,它们生成以下向量:

Vector2() -> (0, 0)
Vector2(Vector2) -> (Vector2.x, Vector2.y)
Vector2(x, y) -> (x, y)
Vector2((x, y)) -> (x, y)

But what do int and float parameter datatypes indicate?但是intfloat参数数据类型表示什么? Also, please correct if I have misunderstood the above assumptions.另外,如果我误解了上述假设,请更正。

Moreover, can I instantiate a normalized Vector2 object given an angle or do I need to do some trigonometry with math library first?此外,我可以实例化一个标准化的Vector2 object 给定一个角度,还是我需要先用math库做一些三角函数?

I'm trying to figure out how to create a unit vector / normalized Vector2 instance given an angle我试图弄清楚如何在给定角度的情况下创建单位向量/归一化 Vector2 实例

The length of a unit vector is 1, so the simplest way to create a unit vector with a given angle is:单位向量的长度为 1,因此创建具有给定角度的单位向量的最简单方法是:

v = pygame.math.Vector2(1, 0)
v.rotate_ip(angle)

Alternatively, you can create the vector with the trigonomentry functions sin and cos :或者,您可以使用三角函数sincos创建向量:

angle_radian = math.radians(angle_degree)
v = pygame.math.Vector2(math.cos(angle_radian), math.sin(angle_radian))

All vector constructors construct a vector with the specified scalars.所有向量构造函数构造一个具有指定标量的向量。 The constructors with only one argument create a vector where the x and y components are equal.只有一个参数的构造函数创建一个向量,其中 x 和 y 分量相等。

You can easily find out yourself by using print :您可以使用print轻松找到自己:

print(pygame.math.Vector2())
print(pygame.math.Vector2(1))
print(pygame.math.Vector2(1.5))
print(pygame.math.Vector2(2, 3))
print(pygame.math.Vector2((2.5, 3.5)))

Output: Output:

[0, 0]
[1, 1]
[1.5, 1.5]
[2, 3]
[2.5, 3.5]

int and float indicates that you can construct the vector with integral and floating point numbers, however this has no effect on the behavior of the object. intfloat表示您可以使用整数和浮点数构造向量,但这对 object 的行为没有影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM