Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by Joncom

Here&039;s a method that sets the entity velocity when given an #angle.

setVelocityByAngle: function(angle, velocity) {
    var slope = Math.tan(angle);
    var x_factor = (Math.abs(angle) < 1.57 ? 1 : -1); // 1.57 rads ~~ 90 degrees
    var y_factor = (angle > 0 ? 1 : -1);
    var rise = (Math.abs(slope) / (1 + Math.abs(slope)));
    var run = (1 / (1 + Math.abs(slope)));
    this.vel.y = y_factor * velocity * rise;
    this.vel.x = x_factor * velocity * run;
}

It works perfectly if angle is between -Math.PI and Math.PI.
In other words between -180 and 180 degrees.

/><br />
<br />
However!<br />
If <code>angle</code> is positive and greater than <code>Math.PI</code>, the entity cannot move upward.<br />
If <code>angle</code> is negative and less than <code>-Math.PI</code>, the entity cannot move downward.<br />
<br />
How can this be modified so as to support angles outside this range, such as <code>1.5*Math.PI</code>, which should move upward, but does not?			</div>
		</div>
			<div class=

1 decade ago by quidmonkey

Just use the standard equation:
this.vel.x *= Math.cos(angle);
this.vel.y *= Math.sin(angle);

1 decade ago by Joncom

Thank you quidmonkey.
Works perfectly!

setVelocityByAngle: function(angle, velocity) {
    this.vel.x = Math.cos(angle) * velocity;
    this.vel.y = Math.sin(angle) * velocity;
}
Page 1 of 1
« first « previous next › last »