[SOLVED] Calculating angle to mouse

I need help calculating the angle to the mouse from entities,

if(orders.get(0) instanceof MoveOrder){
        		MoveOrder mo = (MoveOrder)orders.get(0);
        		double xd = mo.x - x;
        		double yd = mo.y - y;
        		double rotation = Math.toDegrees(Math.atan2(yd,xd))+270;
        		rot = rotation;
        		System.out.println(rot);
        		orders.remove(0);
        	}

This is my current code, but it doesn’t seem to work, and the movement code is:

while(rot > 360)rot-= 360;
		while(rot < 0)rot+= 360;
		double movementAngle = Math.toRadians(rot);
        x += Math.sin(movementAngle) * 0.5;
        y += Math.cos(movementAngle) * 0.5;

can anyone help?

everything you have looks correct to me. I assume it’s messing up at the stage of getting the angle from the mouse, right? make sure you’re getting the right positions in your MoveOrder and entity position.

Can you be more specific in “doesn’t work”?

this should help:
angle = Math.toDegrees(Math.atan2(MouseY - entity.y, MouseX - entity.x));

It seems to go in completely random directions, and doesn’t move to the mouse at all…

to move: (Worked in my game. see Walking sprites in show case)
x += delta * Math.sin(Math.toRadians(entity.angle));
y += delta * Math.cos(Math.toRadians(entity.angle));

you need to find out where you are going wrong. are you getting the correct angle from your first segment?

Okay, i’ve done some testing, and it seems to work fine x Axis, but messes up somewhere on the y Axis, as in, if i tell it to go from (0,0) to (10,0) it works fine, if i want to move just in the x Axis it has to be

MoveOrder mo = (MoveOrder)orders.get(0);
        		double xd = mo.x - x;
        		double yd = mo.y - y;
        		double rotation = Math.toDegrees(Math.atan2(yd,xd))+90;

However, if i want to move in the y Axis it has to be

MoveOrder mo = (MoveOrder)orders.get(0);
        		double xd = mo.x - x;
        		double yd = mo.y - y;
        		double rotation = Math.toDegrees(Math.atan2(yd,xd))+270;

Which even further confuses me…

I’ve got it to move in the right direction by doing it like this

double movementAngleX = Math.toRadians(rot);
		double movementAngleY = Math.toRadians(rot+180);
        x += Math.sin(movementAngleX) * 0.5;
        y += Math.cos(movementAngleY) * 0.5;
        if(orders.size() > 0){
        	if(orders.get(0) instanceof MoveOrder){
        		MoveOrder mo = (MoveOrder)orders.get(0);
        		double xd = mo.x - x;
        		double yd = mo.y - y;
        		double rotation = Math.toDegrees(Math.atan2(yd,xd))+90;
        		rot = rotation;
        		System.out.println(rot);
        		orders.remove(0);
        	}
        }

But then the rotation on the image is messed up, the rendering code is (im using LWJGL)

        
        GL11.glTranslated(x+32, y+32, 0);
        GL11.glRotated(rot, 0, 0, -1);
        GL11.glBegin(GL11.GL_QUADS);
        GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex2f(-32, -32);
        GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex2f(32, -32);
        GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex2f(32, 32);
        GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex2f(-32, 32); 
        GL11.glEnd();

Anyone got any ideas?

i have made a mistake
this is y coord code:
y -= delta * Math.cos(Math.toRadians(entity.angle));

and try GL11.Rotatef with

angle = angle % 360.0f;
GL11.Rotatef((float)angle…)

Thanks! Got it to work,

double movementAngle = Math.toRadians(rot);
        x += Math.sin(movementAngle) * 0.5;
        y -= Math.cos(movementAngle) * 0.5;
        if(orders.size() > 0){
        	if(orders.get(0) instanceof MoveOrder){
        		MoveOrder mo = (MoveOrder)orders.get(0);
        		double xd = mo.x - x;
        		double yd = mo.y - y;
        		double rotation = Math.toDegrees(Math.atan2(yd,xd))+90;
        		rot = rotation;
        		orders.remove(0);
        	}
        }

        float angle = (float) (rot % 360.0f);
        GL11.glRotated(angle+180.0f, 0, 0, 1);

glRotate() can handle angles outside 360 degrees, but you should keep the actual stored rotation (rot) to 0-360 degrees.