向量帮助

发布于 2024-08-21 09:59:45 字数 85 浏览 6 评论 0原文

我正在尝试制作一款子弹可以向任何方向飞行的游戏。我想让它们成为具有方向和大小的向量,以使它们朝这个方向移动。我只是不确定如何实现这个?

谢谢

I'm trying to make a game where the bullets can fly in any direction. I would ike to make them vectors with a direction and magnitude to make them go the direction. I'm just not sure how to implement this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暗恋未遂 2024-08-28 09:59:45

有两个部分需要计算。首先,我从总距离开始。这应该很简单:

total_distance = velocity * time

假设这是一个 2D 游戏,那么您应该使用 sine & 。余弦将总距离分解为 X 和 Y 分量(对于给定角度):

distance_y = total_distance * sin(2 * pi * angle / 360)
distance_x = total_distance * cos(2 * pi * angle / 360)

最后,距离 x/y 应根据子弹的起始位置进行偏移:

pos_x = distance_x + start_pos_x
pos_y = distance_y + start_pos_y

当然,您可以将所有这些包装在很好的课程,扩展和根据需要进行抛光。

There are two parts that need to be calculated. First, I'd start off with the total distance. This should be straightforward:

total_distance = velocity * time

Assuming this is a 2D game, you should then use sine & cosine to break the total distance up into the X and Y components (for a given angle):

distance_y = total_distance * sin(2 * pi * angle / 360)
distance_x = total_distance * cos(2 * pi * angle / 360)

Finally, the distance x/y should be offset based on the starting position of the bullet:

pos_x = distance_x + start_pos_x
pos_y = distance_y + start_pos_y

Of course, you could wrap all of this in a nice class, expanding & polishing as needed.

A君 2024-08-28 09:59:45

您可以有一个包含位置、方向向量和速​​度的项目符号类。每个时间步都可以更新子弹的位置,如下所示:

position += direction * veclocity;

这假设方向是单位向量。

You could have a bullet class which contains a position, direction vector, and a velocity. Every time step you could update the bullet's position like so:

position += direction * veclocity;

This assumes that the direction was a unit vector.

巨坚强 2024-08-28 09:59:45

我会从这样的事情开始:

struct Vector3f {
    float x, y, z;
};

struct Bullet {
    Vector3f position;
    Vector3f velocity;
};

inline const Vector3f& Vector3f::operator+=(const Vector &other)
{
    x += other.x;
    y += other.y;
    z += other.z;
    return *this;
}

inline const Vector3f& Vector3f::operator*=(float v)
{
    x *= v;
    y *= v;
    z *= v;
    return *this;
}

然后您可以使用 bullet.position += Velocity 更新您的 Bullet 位置(矢量加法是通过单独添加组件来完成的)。请注意,速度矢量包含方向和速度(=矢量的大小)。

如果您的 Bullet 每帧都变慢,您可以执行类似 bullet.velocity *= 0.98 的操作(其中 0.98 代表分数)。向量与标量的乘法是通过将每个分量与标量相乘来完成的...

问候,
克里斯托夫

I would create start with something like this:

struct Vector3f {
    float x, y, z;
};

struct Bullet {
    Vector3f position;
    Vector3f velocity;
};

inline const Vector3f& Vector3f::operator+=(const Vector &other)
{
    x += other.x;
    y += other.y;
    z += other.z;
    return *this;
}

inline const Vector3f& Vector3f::operator*=(float v)
{
    x *= v;
    y *= v;
    z *= v;
    return *this;
}

You can then update your Bullet position with bullet.position += velocity (vector addition is done by adding the the components separately). Note, that the velocity vector contains both, the direction and the speed (=magnitude of the vector).

And if your Bullet should become slower every frame, you can do something like bullet.velocity *= 0.98 (where 0.98 represents the fraction). Vector multiplication with a scalar is done by multiplying each component with the scalar...

Regards,
Christoph

转身泪倾城 2024-08-28 09:59:45

这有什么用吗?

http://www.cs.cmu.edu/~ajw/doc/ svl.html

google 是一个很棒的工具

this any use?

http://www.cs.cmu.edu/~ajw/doc/svl.html

google is a wonderful tool

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文