i making game in java (no libraries). it's 2d top-down game player can walk , faced towards mouse cursor.
public player(int x, int y, int health, int tileid) { super(x, y, health); tile = new tile(tileid, false); mouseinput = new mousehandler(screen); } public void tick() { // executed game tick. // x = playerx , y = playery int cursorx = mouseinput.getmousepos()[0]; int cursory = mouseinput.getmousepos()[1]; float x = cursorx - x; float y = cursory - y; rotation = math.atan2(y, x); }
it looks long player @ (0,0) if player moves , mouse coordinates become negative begins show strange behaviour (look @ video below)
youtube: https://www.youtube.com/watch?v=m6zhcrwvt3y
the rotation of sprite done in class 'screen.java'
by using:
if (rotation < 360) rotation++ else rotation = 0
i verified rotation working correctly.
edit:
public bufferedimage rotate(bufferedimage img, double degree) { affinetransform tx = new affinetransform(); tx.rotate(degree, 4, 4); affinetransformop op = new affinetransformop(tx,affinetransformop.type_bilinear); bufferedimage image = op.filter(img,null); return image; }
okay fixed it.
the problem game scale making 2d game , set width, height , scale. didn't divide mousex , mousey scale.
public void mousemoved(mouseevent e) { mousex = e.getx() / game.getscale(); mousey = e.gety() / game.getscale(); }
i found problem accident when messing gamescale.