Sunday, 15 September 2013

Rotating camera around an off center sprite

Rotating camera around an off center sprite

I am making a 2d top down game. When the user moves their mouse I want the
sprite to basically point to the mouse. I was able to get that part
working if the sprite is in the center of the screen it works fine.
My problem is I want the sprite to be in the lower fourth of the screen
So what I have tried is setting the camera to the players position
rotating it and then moving it back to having the player at 1/4 of the way
up the screen but in that he case he just orbits the center of the camera
instead of the camera rotating around him.

This is my code so far that I have worked out.
mouseX = Gdx.input.getX();
mouseY = Gdx.input.getY();
Vector3 mouse = new Vector3(mouseX, mouseY, 0);
Main.cam.unproject(mouse);
Vector3 pos = new Vector3(currx, curry, 0);
pos.sub(mouse);
vec.set(pos.x, pos.y);
angle = vec.angle();
Main.cam.position.set(currx, curry, 0f);
float camAngle = -getCameraCurrentXYAngle(Main.cam)+180;
e.setRotations(angle);
setPosition(currx, curry);
Main.cam.rotate((camAngle - angle)+180);
s_.setRotation(angle);
Main.cam.position.set(currx, curry+(Main.VIEWPORT_HEIGHT/4), 0f);



public float getCameraCurrentXYAngle(OrthographicCamera cam){
return (float)Math.atan2(cam.up.x,
cam.up.y)*MathUtils.radiansToDegrees;
}

No comments:

Post a Comment