Camera follow help

Started by Minigame, February 13, 2013, 06:28:04 AM

Previous topic - Next topic

Minigame

I have a click to walk system in my game app, and previously used a method that just followed the player from behind, but now am trying to base things around a user controlled POV that uses arrow key input (360 degree left / right rotation, and 45 degrees from the ground, or 15 degrees above ground level).
The user controlled camera works fine, but I can't get it to follow the player. I know about Camera.CAMERA_MOVE*, but it goes off screen from being called each tick. Is there a function to determine the distance from the player object, so that I could force a MOVEIN / MOVEOUT appropriately maybe?

EgonOlsen

I'm not 100% sure which kind of camera you have in mind here, but you have the position of the camera and you have the position of the player. With that, it's simple to calculate the distance between the two. For example by using http://www.jpct.net/doc/com/threed/jpct/SimpleVector.html#distance(com.threed.jpct.SimpleVector)

Minigame

Camera issues have been resolved, and another issue has occurred that I've toyed with for hours and cannot solve on my own.
The problem is trying to snap the player in a specific direction.. but I'm unable to determine a proper rotation value.

Directions are stored in an enum:

NORTH(0, (float) Math.PI * 2),
NORTHEAST(1, ((float) Math.PI * 7) / 4),
... etc


and here's what is being done to "try" to determine the next rotation direction, and definitely where the problem is coming from:

private void turnTo(Mob mob, SimpleVector dest) {
// TODO calculate correct angle between location and destination
float angle = mob.getTransformedCenter().calcAngle(dest); // problem here ??
System.out.println("angle="+angle);

// Find closest direction
Direction dir = Direction.getClosestDirectionForAngle(angle);

mob.updateDirection(dir);

EgonOlsen

The problem might come from the fact that there are always two angles that describe the angle between these two vectors with one being smaller than the other. The method will give you the smaller one. You have to determine in addition, if the destination is in front or behind the player and add 180 degrees to the result if its behind.