向量帮助
我正在尝试制作一款子弹可以向任何方向飞行的游戏。我想让它们成为具有方向和大小的向量,以使它们朝这个方向移动。我只是不确定如何实现这个?
谢谢
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有两个部分需要计算。首先,我从总距离开始。这应该很简单:
假设这是一个 2D 游戏,那么您应该使用 sine & 。余弦将总距离分解为 X 和 Y 分量(对于给定角度):
最后,距离 x/y 应根据子弹的起始位置进行偏移:
当然,您可以将所有这些包装在很好的课程,扩展和根据需要进行抛光。
There are two parts that need to be calculated. First, I'd start off with the total distance. This should be straightforward:
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):
Finally, the distance x/y should be offset based on the starting position of the bullet:
Of course, you could wrap all of this in a nice class, expanding & polishing as needed.
您可以有一个包含位置、方向向量和速度的项目符号类。每个时间步都可以更新子弹的位置,如下所示:
这假设方向是单位向量。
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:
This assumes that the direction was a unit vector.
我会从这样的事情开始:
然后您可以使用
bullet.position += Velocity
更新您的 Bullet 位置(矢量加法是通过单独添加组件来完成的)。请注意,速度矢量包含方向和速度(=矢量的大小)。如果您的 Bullet 每帧都变慢,您可以执行类似
bullet.velocity *= 0.98
的操作(其中 0.98 代表分数)。向量与标量的乘法是通过将每个分量与标量相乘来完成的...问候,
克里斯托夫
I would create start with something like 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
这有什么用吗?
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