Let's say I have an object like this...
假设我有一个像这样的对象......
var objA = {
x:0,
y:0,
speed:{x:0, y:0},
angle:0,
turnSpeed:8
}
...and I each "step" in a game I want to move it towards objB, which is at coordinates (10, 10). ObjA is already moving and suddenly needs to move to objB's position. Let's say objA has a function turnTo()
which can turn it towards objB each step based on its turnSpeed
. I could do something like...
...而我在游戏中每一步“我想将它移向objB,它位于坐标(10,10)处。 ObjA已经移动,突然需要转向objB的位置。假设objA有一个函数turnTo(),它可以根据其turnSpeed将其转向objB每一步。我可以做点什么......
objA.speed.x += Math.cos((90 - obj.angle) * Math.PI / 180);
objA.speed.y += -Math.sin((90 - obj.angle) * Math.PI / 180);
...to move objA towards objB based on the angle, but if objA is already moving, it is unlikely to ever hit objB because this doesn't take into account objA's current speed.
...根据角度将objA移向objB,但如果objA已经移动,则不太可能击中objB,因为这并未考虑objA的当前速度。
How can I calculate a path to objB so that objA will hit it precisely?
如何计算objB的路径,以便objA精确地命中它?
1 个解决方案
#1
2
First, take a look at Coordinates for my Javascript game - based on an angle, when do I use Sin Cos and Tan?
首先,看一下我的Javascript游戏的坐标 - 基于一个角度,我什么时候使用Sin Cos和Tan?
You know the x and y coordinates of objB (I assume they remain constant) and you can get the x and y coordinates of objA using JavaScript, thus you can solve the problem as a triangle from any point objA is to objB using simple trig.
你知道objB的x和y坐标(我假设它们保持不变)你可以使用JavaScript获得objA的x和y坐标,因此你可以使用简单的trig从objA到objB的任何一点解决问题。
#1
2
First, take a look at Coordinates for my Javascript game - based on an angle, when do I use Sin Cos and Tan?
首先,看一下我的Javascript游戏的坐标 - 基于一个角度,我什么时候使用Sin Cos和Tan?
You know the x and y coordinates of objB (I assume they remain constant) and you can get the x and y coordinates of objA using JavaScript, thus you can solve the problem as a triangle from any point objA is to objB using simple trig.
你知道objB的x和y坐标(我假设它们保持不变)你可以使用JavaScript获得objA的x和y坐标,因此你可以使用简单的trig从objA到objB的任何一点解决问题。