Forum Thread
by member
Character Rotation Based On Mouse (Forums : Coding & Scripting : Character Rotation Based On Mouse) Locked
Thread Options
Jan 26 2013, 5:58pm Anchor

How would I rotate an image in Slick2D to point to the mouse at all times?

Jan 26 2013, 7:54pm Anchor

get the direction vector pointing from the character to the mouse via vector subtraction then convert it to an angle via atan2

Jan 26 2013, 9:06pm Anchor

Still doesn't work. Here's the source code:

package main;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class StatePlay extends BasicGameState
  private int id;
  private Vector2f mapPosition, playerPos, mousePos;
  private Image map;
  private float cameraX = 0, cameraY = 0, mouseX = 0, mouseY = 0, diffX = 0, diffY = 0, cameraX2 = 0, cameraY2 = 0;
  private Image player;
  public StatePlay(int id)
  { = id;

  public void init(GameContainer gc, StateBasedGame sbg) throws SlickException
    mapPosition = new Vector2f(0, 0);
    playerPos = new Vector2f (590, 360);
    mousePos = new Vector2f (gc.getInput().getMouseX(), gc.getInput().getMouseY());
    map = new Image("res/backGround.png");
    player = new Image("res/player.png");

  public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException
    g.translate(-cameraX, -cameraY);
    g.translate(-cameraX2, -cameraY2);
    g.drawImage(map, mapPosition.x, mapPosition.y);
    g.drawImage(player, playerPos.x, playerPos.y);
  public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
    Input input = gc.getInput();
    mousePos.x = input.getMouseX();
    mousePos.y = input.getMouseY();
    diffX = mouseX - gc.getWidth();
    diffY = mouseY - gc.getHeight();
    cameraX = playerPos.x - gc.getWidth() * 0.4f;
    cameraY = playerPos.y - gc.getHeight() * 0.4f;
    cameraX2 = diffX * 0.06f;
    cameraY2 = diffY * 0.06f;
    if (input.isKeyDown(Input.KEY_A))
      playerPos.x -= 3;
    if (input.isKeyDown(Input.KEY_W))
      playerPos.y -= 3;
    if (input.isKeyDown(Input.KEY_S))
      playerPos.y += 3;
    if (input.isKeyDown(Input.KEY_D))
      playerPos.x += 3;
    Vector2f degrees = playerPos.sub(mousePos);
    player.setRotation((float) Math.atan2(degrees.y, degrees.x));

  public int getID()
    return id;
Jan 26 2013, 9:18pm Anchor

oh I forgot to mention you also need to normalize the vector before you atan2 it.

If it still doesnt work one common issue is to make sure tha the rotation angle format and your input format match. AKA Radians vs Degrees.

Here's an example function I did for getting the direction vector:

actionscript code:
    public static function GetNormalizedDirectionVector(vec1X:Number, vec1Y:Number, vec2X:Number, vec2Y:Number):DoVector2f
      vec1X = vec2X - vec1X  ;
      vec1Y = vec2Y - vec1Y;
      var ret: DoVector2f = new DoVector2f();
      ret.x = vec1X;
      ret.y = vec1Y;
      return ret;

This tutorial might also be of help to you :

Jan 26 2013, 9:41pm Anchor

Can you repost that in java form?

Jan 27 2013, 9:42pm Anchor

It would be exactly the same except for the function declaration.

public static function GetNormalizedDirectionVector(vec1X:Number, vec1Y:Number, vec2X:Number, vec2Y:Number):DoVector2f

would become something like

public Vector2 GetNormalizedDirectionVector(float vec1X, float vec1Y,float vec2X,float vec2Y)

Reply to Thread
click to sign in and post

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.