Javascript 根据对象的角度移动对象
我正在尝试根据对象的旋转移动对象,我有4种方法:Javascript 根据对象的角度移动对象,javascript,math,typescript,Javascript,Math,Typescript,我正在尝试根据对象的旋转移动对象,我有4种方法: 前进 落后的 向上 向下 在这4种方法中,只有向前和向后似乎工作正常。其他两个似乎有不同的效果,根据旋转。当它被设置为90度旋转时,它的动作与向前的动作相同(向前在此旋转时在屏幕下方),但实际上所需的效果是它向右移动 这两种方法似乎工作正常: public get forward(): Vector2 { return new Vector2( Math.cos(this.rotation.degrees * (Ma
- 前进
- 落后的
- 向上
- 向下
向前
和向后
似乎工作正常。其他两个似乎有不同的效果,根据旋转。当它被设置为90度旋转时,它的动作与向前的动作相同(向前在此旋转时在屏幕下方),但实际上所需的效果是它向右移动
这两种方法似乎工作正常:
public get forward(): Vector2 {
return new Vector2(
Math.cos(this.rotation.degrees * (Math.PI / 180)),
Math.sin(this.rotation.degrees * (Math.PI / 180))
);
}
public get backward(): Vector2 {
return new Vector2(
-Math.cos(this.rotation.degrees * (Math.PI / 180)),
-Math.sin(this.rotation.degrees * (Math.PI / 180))
);
}
然而,这两种方法不能正常工作。我似乎不知道是什么原因造成的,我假设数学也会是类似的,也许我错了
public get up(): Vector2 {
return new Vector2(
Math.cos(this.rotation.degrees * (Math.PI / 180)),
-Math.sin(this.rotation.degrees * (Math.PI / 180))
);
}
public get down(): Vector2 {
return new Vector2(
-Math.cos(this.rotation.degrees * (Math.PI / 180)),
Math.sin(this.rotation.degrees * (Math.PI / 180))
);
}
您应该这样计算:
向上
与向前
成+90度,向下
与向前
成-90度(而向后
与向前
成180度)
换言之:
public get up(): Vector2 {
const phi = (this.rotation.degrees + 90) * (Math.PI / 180);
return new Vector2(
Math.cos(phi),
Math.sin(phi)
);
}
没有另一个否定cos显然是不正确的。例如,Just
-cos
,只是表示“沿x轴的相反方向移动,并保持y轴速度不变”。例如,向前
指向东北偏北的情况将创建一个向北西北的矢量。你应该这样计算:向上
与向前
成+90度,向下
与向前
成-90度(而向后
与向前
成180度)
换言之:
public get up(): Vector2 {
const phi = (this.rotation.degrees + 90) * (Math.PI / 180);
return new Vector2(
Math.cos(phi),
Math.sin(phi)
);
}
没有另一个否定cos显然是不正确的。例如,Just
-cos
,只是表示“沿x轴的相反方向移动,并保持y轴速度不变”。例如,向前
指向东北偏北的情况将创建一个向北西北的矢量。如果计算一次主方向的cos和sin,则矢量分量为:
forward: cos, sin
backward: -cos, -sin
up(really left): -sin, cos
down(really right): sin, -cos
如果只计算一次主方向的cos和sin,则向量分量为:
forward: cos, sin
backward: -cos, -sin
up(really left): -sin, cos
down(really right): sin, -cos
this.rotation.degrees是否为您提供了正确的度数?是的,它是一个设定值,它是这样创建的(最后一个参数):
实例化(Guy,Stage.topLeftQuad,new rotation(90))代码>this.rotation.degrees是否为您提供了正确的角度?是的,它是一个设定值,它是这样创建的(最后一个参数):实例化(Guy,Stage.topLeftQuad,new rotation(90))代码>这似乎有效。我使用的是画布,所以看起来像是在做-90
它移动的方向正确吗?是的,你可能需要翻转+90
/-90
,这取决于你在自己的坐标系中如何定义向上
和向下
,这似乎有效。我使用的是画布,所以看起来像是在做-90
它移动的方向正确吗?是的,您可能需要翻转+90
/-90
,这取决于您在自己的坐标系中如何定义向上
和向下