I'm fairly new to Greenfoot and am using it to create a game for college. I'm creating a top down shooter styled game but I've encountered a small problem when trying to use the mouse to control where the character is facing.
I have looked through some similar scenarios and attempted to adapt some of the code to work in my game but have found that the character and the mouse position aren't lined up. For example if my mouse is at the top of my world to make the character face that way it will be facing left, 90 degrees anti clockwise from where I want it to be.
Apart from that small problem I've come across using this code, I was wondering if someone could roughly explain how the code works to help give me a better understanding of the math's and general principles involved.
Many thanks.
private void setRotation() { if(Greenfoot.mouseMoved(null) || Greenfoot.mouseDragged(null)) { MouseInfo mouse = Greenfoot.getMouseInfo(); mouseX = mouse.getX(); mouseY = mouse.getY(); } setRotation(pointTo(mouseX,mouseY)); } public int pointTo(int x, int y) { int dX = x - getX(); int dY = y - getY(); double rotation = Math.atan2(dY, dX); rotation = Math.toDegrees(rotation); return (int)rotation; }