Author Topic: Camera follow help  (Read 2408 times)

Offline Minigame

  • int
  • **
  • Posts: 55
    • View Profile
Camera follow help
« on: February 13, 2013, 06:28:04 am »
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?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Camera follow help
« Reply #1 on: February 13, 2013, 07:11:25 am »
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)

Offline Minigame

  • int
  • **
  • Posts: 55
    • View Profile
Re: Camera follow help
« Reply #2 on: February 13, 2013, 08:44:32 pm »
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:
Code: [Select]
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:
Code: [Select]
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);

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Camera follow help
« Reply #3 on: February 14, 2013, 08:35:54 am »
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.