face/turn the character in the direction the mouse is facing?

hello! im working on my first ever game, and i want the player to be able to turn freely by pointing the mouse in a certain direction. I am definately not advanced with java yet, so ignore the dumb questions please :stuck_out_tongue:

another thing im trying to do is have the camera follow the player, so that the player is always in the middle of the screen, unless if there comming to the edge of the map.

any help is much appreciated, thanks!

EDIT: forgot to post any code, whoops


package game;

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.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.tiled.TiledMap;

public class Play extends BasicGameState{
	
	private Input input;
	
	TiledMap tm;
	int mapHeight, mapWidth;
	float playerX;
	float playerY;
	float playerCenterX;
	float playerCenterY;
	float playerRotation;
	float speed = 0.2f;
	int cameraX = 0;
	int cameraY = 0;
	private Image player;
	static int WIDTH = 800;
	static int HEIGHT = 600;
	Rectangle camera;
	
    public Play(int state){
         
    }
     
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
    	camera = new Rectangle(0, 0, 800, 600);
		gc.setVSync(true);
		tm = new TiledMap("res/map1.tmx");
		player = new Image("res/character01.png");
		mapWidth = tm.getWidth()*tm.getTileWidth();
		mapHeight = tm.getHeight()*tm.getTileHeight();
		playerX = 400 - player.getWidth() / 2;
	    playerY = 300 - player.getHeight() / 2;
    }


    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException { 
    	tm.render(0, 0);
    	playerCenterX = playerX + player.getWidth() / 2;
    	playerCenterY = playerY + player.getHeight() / 2;
    	player.draw(cameraX + playerX, cameraY + playerY);
    }

	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
	    input = gc.getInput();

	    if (input.isKeyDown(Input.KEY_W)) {
	         playerY -= speed * delta;
	    }
	    
	    if (input.isKeyDown(Input.KEY_A)) {
	         playerX -= speed * delta;
	    }

	    if (input.isKeyDown(Input.KEY_S)) {
	         playerY += speed * delta;
	    }
	      
	    if (input.isKeyDown(Input.KEY_D)) {
	         playerX += speed * delta;
	    }	      
	}

	public int getID() {
        return 1;
         
    }
	
}

I swear this question was asked and answered like yesterday… but I can’t find the thread… maybe I’m crazy??

You want to figure out an offset based on the player and or the screen and draw the map at that offset.

As for the direction, you can use vectors! Vectors give you awesome superpowers!

No, you ain’t crazy :slight_smile:

This is a good post aswell :slight_smile:

[quote=“jonjava,post:2,topic:38858”]
Hahaha true, true! :smiley:

In 2D if vectors give you superpowers, then what do complex numbers give you…since vectors are effectively complex numbers with the interesting parts removed?

A complex. ;D