xVel = velocity * Math.cos(angle);
yVel = velocity * Math.sin(angle);
This is what I use to convert a single initial velocity to x and y velocity, using the angle input by the user. Occasionally the xVel will be negative. I was wondering if using an absolute value Math function would be fine or would it mathematically skew my results.
This is what I was thinking for that:
xVel = velocity * Math.abs(Math.cos(angle));
Programming in Java BTW, even though that doesn’t really matter in this case.
EDIT: Due to a lack of some important information I will add some revisions.
Occasionally I will enter a value for an angle that is in the positive quadrant (i.e. 15°) and still get a negative xVel.
Also this program is assuming that the angle is being counted up from the x axis in a counter clockwise fashion.
You are getting a negative x velocity for a 15 degree angle because Math.cos and Math.sin accept the angle in radians and not degrees.
You must first convert your angle in degrees to an angle in radians like this: