Rotating towards a point

Hi!

I’m trying to rotate my dibily, so it’s facing the point I specify. Currently, when it’s facing north it’s at 90 degrees.
I ran a test using this code, with very inaccurate results.

dibily.setRotation((float)Math.toDegrees(Math.atan2(input.getMouseX(), input.getMouseY())));

This is a fairly simple problem on these forums - I have lost my reference for trig and the likes in a hardware error, and I can’t recall the exact procedure.

Thanks guys!

Skål! 8)

Try this


dibily.setRotation((float)(Math.atan((input.getMouseY() - dibily.getY())/(input.getMouseX() - dibily.getX())));

That’s what I use to rotate to my mouse, if that doesn’t work there are other things to try.

Nope, didn’t work. You forgot degrees too. This is what I executed.

public void facePoint(int x, int y) {
		setRotation((float)Math.toDegrees(Math.atan((y - getY())/(x - getY()))));
	}

Got it working!

public void facePoint(int x, int y) {
		setRotation((float)Math.toDegrees(Math.atan2((y - getY()), (x - getY()))));
	}

EDIT: Did not. Read further.

Yay : D! Sorry if I wasn’t much help, I was away when you replied.

I have now used the code a little further and it turns out it’s still not entirely accurate. It works, but the margin of error is big (something close to 50 degrees). If anyone has further input it is much appriciated.

Alright. Finally this is working out! :slight_smile:

public void facePoint(int x, int y) {
		setRotation((float)Math.toDegrees(Math.atan2(y - getY(), x - getX())));
}