What @Nishant Shreshth says is true. But it is also super important to use floating points (like float
or double
) instead of int
for your dir
, x
and y
variable. Otherwise the decimal part your dir
value might be truncated, which is pretty much of a problem when you use radians and trigonometric functions.
Also note that you probably want to use Math.atan2(spdY, spdX)
instead of the default atan method you use. Your approach will fail as soon as spdX
gets zero: you will divide by zero. atan2
will take care of these edge cases. Wikipedia gives details on this function.