java 根据角度和速度寻找速度向量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14086721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding velocity vector based on angle and speed
提问by Tohmas
I am programming some movement for AI for a game based on angle and speed.Its a 2D program based on x,y axis. I currently have a position vector as-well as a velocity vector which accounts for speed and current direction of the AI.Every time I move the AI I just add the velocity vector to the current position to get my new position.At the moment the movement is 8-directional.What I wanted to implement was angle based movement (more realistic type of movement) and wondered if it is possible to calculate the velocity vector from an angle and speed constant.
我正在为基于角度和速度的游戏编写一些 AI 运动。它是基于 x、y 轴的 2D 程序。我目前有一个位置向量以及一个速度向量,它说明了 AI 的速度和当前方向。每次我移动 AI 时,我只是将速度向量添加到当前位置以获得我的新位置。此刻运动是 8 向的。我想要实现的是基于角度的运动(更现实的运动类型),并想知道是否可以从角度和速度常数计算速度矢量。
Thanks in advance!
提前致谢!
回答by Mikhail
This better be possible.
这最好是可能的。
This is how I envision your program works
这就是我想象你的程序的工作方式
position_X += velocity_X
position_Y += velocity_Y
Through trigonometry you can do
通过三角学你可以做
velocity_X = velocity*cos(angle)
velocity_Y = velocity*sin(angle)
A few things to keep in mind include
要记住的几件事包括
- Most likely Y increases as you go from top to bottom
- The inverse problem has two solutions, you should look into a 2 input arctangent function. See https://gamedev.stackexchange.com/questions/14602/what-are-atan-and-atan2-used-for-in-games
- 从上到下,Y 很可能会增加
- 逆问题有两个解决方案,您应该查看 2 个输入反正切函数。见https://gamedev.stackexchange.com/questions/14602/what-are-atan-and-atan2-used-for-in-games