java - Object wont move based on angle given? -
so i've been trying make object move based on 2 speeds creates using atan(). every test results in same movement of going straight up.
so decided take out , replace inputted angles oddly work the 4 angles 90 180 270 0.
i'm not sure why goes directions here's code:
public void update() { updatedir(); move(); } public void updatedir(){ dir = math.atan(spdy/spdx); dir =20; //i know here used test if change dir = dir * math.pi / 180.0; system.out.println("dir: " + dir); } public void move() { x += (spd*math.cos(dir)); y -= (spd * math.sin(dir)); }
any reason why goes in 4 directions?
what @nishant shreshth says true. super important use floating points (like float
or double
) instead of int
dir
, x
, y
variable. otherwise decimal part dir
value might truncated, pretty of problem when use radians , trigonometric functions.
also note want use math.atan2(spdy, spdx)
instead of default atan method use. approach fail spdx
gets zero: divide zero. atan2
take care of these edge cases. wikipedia gives details on function.
Comments
Post a Comment