我的火箭正在按照handleCollision
中的定义击中这个惯性对象。我正在传递一枚火箭,其theta值为.r
,其量值为.power
。
我想要更新我的.rotation
& .magnitude
根据Wikipedia
当从左侧发生碰撞时,我的惯性向右移动。
但是当从右侧发生碰撞时,它会错误地移动180度。因此,如果火箭向上并且与惯性物体成45度角,则物体将以45度角向上移动。
我在这里缺少什么?我认为它可能是atan函数的问题所以我转换为y组件&向量的x分量首先是弧度,同样的问题。
handleCollision(rocket) {
var angle = rocket.r * Math.PI / 180.0;
var rr = this.rotation * Math.PI / 180;
var rocketVector = {'x' : r.power * Math.cos(angle), 'y' : r.power * Math.sin(angle)};
var inertiaVector = {'x' : this.magnitude * Math.cos(rr), 'y' : this.magnitude * Math.sin(rr)};
var rMass = 10;
var shipMass = 10;
var x = (rMass * rocketVector.x) + (shipMass * inertiaVector.x);
var y = (rMass * rocketVector.y) + (shipMass * inertiaVector.y);
var xDividedByMass = x / (rMass + shipMass);
var yDividedByMass = y / (rMass + shipMass);
var yRadians = (yDividedByMass * Math.PI / 180);
var xRadians = (xDividedByMass * Math.PI / 180);
var theta = Math.atan( yRadians / xRadians);
theta = theta * 180 / Math.PI;
console.log(theta);
var hypotenuse = Math.sqrt((xDividedByMass * xDividedByMass) + (yDividedByMass * yDividedByMass));
this.magnitude = hypotenuse;
this.rotation = theta;
if (this.rotation < 0) {
this.rotation += 360;
} else if (this.rotation > 360) {
this.rotation -= 360;
}
}
答案 0 :(得分:1)
如果xDividedbyMass> 0,那么你很棒,因为你是象限I或IV,其中arctangent踢出它的值。如果你不喜欢负角度,可以像你一样添加360。 但是如果x <0且y> 0,则将获得负角度并且想要添加180以获得Q II(切线具有180的周期)。如果x <0,并且y <0,则您在QIII中,并且arctan在Q1中为您提供了必须添加180的内容。 逻辑看起来像这样。
if ((x > 0) && (y<0)) {
this.rotation += 360;
} else if (x<0) {
this.rotation += 180;
}