Author Topic: Camera always behind object  (Read 2335 times)

Offline bLAZY

  • byte
  • *
  • Posts: 18
    • View Profile
Camera always behind object
« on: January 17, 2012, 10:58:21 am »
Hi,

could you give me some clue how to do that camera always looks on object the same way? Something like in Third Person Shooter. Should I calculate an angle and use translation or there is some better way?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11747
    • View Profile
    • http://www.jpct.net
Re: Camera always behind object
« Reply #1 on: January 17, 2012, 12:41:09 pm »
I tend to base such things on the object's virtual coordinate system. This might help you: http://www.jpct.net/jpct-ae/doc/com/threed/jpct/Object3D.html#getZAxis()

Offline bLAZY

  • byte
  • *
  • Posts: 18
    • View Profile
Re: Camera always behind object
« Reply #2 on: January 17, 2012, 07:51:56 pm »
I have to admit I don't know how to use it correctly. Probably don't understand 3D transormations. I found this on forum:
Code: [Select]
   private void moveCamera() {
      SimpleVector oldCamPos = camera.getPosition();
      oldCamPos.scalarMul(9f);
      SimpleVector carCenter = car.getTransformedCenter();
      SimpleVector zOffset = car.getZAxis();
      zOffset.scalarMul(-250f);
      SimpleVector camPos = new SimpleVector(carCenter);
      camPos.add(camYOffset);
      camPos.add(zOffset);
      camPos.add(oldCamPos);
      camPos.scalarMul(0.1f);
      camera.setPosition(camPos);
      camera.lookAt(carCenter);
   }

and there was also video. It works there exactly I would like to get. Could you clarify it for me?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11747
    • View Profile
    • http://www.jpct.net
Re: Camera always behind object
« Reply #3 on: January 17, 2012, 08:23:46 pm »
Well...that code looks like the one from the car example. What it does is pretty simple: It calculates a new camera position based on the car's orientation (i.e. behind the car by translating along the negative z-axis), adds a multiple of the current position to it and divides by (multiple+1). Then it does a lookAt() at the car. This simply smoothes out the camera movement. At the time that i wrote this, i was a quick hack to get something going. It turned out to work pretty well, so i kept it.

If it works for you, just use it...maybe tweak the parameters to fit your needs.

Offline bLAZY

  • byte
  • *
  • Posts: 18
    • View Profile
Re: Camera always behind object
« Reply #4 on: January 17, 2012, 09:54:27 pm »
Now it works almost. Already have such problems:
1. I call this method in ticker and screen shakes a little trying all the time adjust camera position even if there is no move, but I think I can solve it when I will call this only if there is some turning action and I will use another camera setter to keep camera in distance during game (object moves all the time forward like in AlienRunner),
2. moving left or right cause that camera is more and more closer and minimal distance is when object achives 180 degrees to default position (distance changing doesn't exist when there is no moving forward along Z axis ),
3. moving up and down behaves similiary but only when there was no left or right, if was e.g. 90 degrees right I can watch like it rotates form side,
4. I made mentioned object's moving forward in ticker by
Code: [Select]
thing.translate(0, 0, move); and even if I turn by making some rotation object moves forward along the old Z axis (e.g. when its rotatated the movement is from right to left, not "into the screen") why not the new ? Is it the same like with camera?

Today I have no more time. Maybe you have met this issues and know some solutions.
Thanks for previous help:)

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11747
    • View Profile
    • http://www.jpct.net
Re: Camera always behind object
« Reply #5 on: January 19, 2012, 08:08:39 am »
As said, it was a quick hack. The effect that you are viewing the object from above at a close range when doing fast left/right movements wasn't intended, but i really liked it. So i kept it for almost all of my games. If you don't want that, just remove the interpolation, i.e. don't take the former position into account when calculating the new one. But that will reduce the smoothness of the movement.

Offline bLAZY

  • byte
  • *
  • Posts: 18
    • View Profile
Re: Camera always behind object
« Reply #6 on: January 30, 2012, 05:16:49 pm »
I ask here for next clue.
I have camera behind object. My problem now is with rotating. I use object.rotateX() to rotate up and down and object.rotateY() to right and left. It's correct until I rotate object only up/down or left/right because the refernce axes are ok. When I use both rotations, e.g. first right (it's ok) and then up - object rotates around its initial x axis. How could I change reference coordinate system? Like it will be attached to screen (x,y parralel to screen , z orthogonal). I hope you know what I mean.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11747
    • View Profile
    • http://www.jpct.net
Re: Camera always behind object
« Reply #7 on: January 30, 2012, 05:48:32 pm »
You have get?Axis()-methods in Object3D and you can rotate around these with the rotateAxis()-method. That should do the trick.

Offline bLAZY

  • byte
  • *
  • Posts: 18
    • View Profile
Re: Camera always behind object
« Reply #8 on: January 30, 2012, 06:18:08 pm »
:) Yeah, just a moment ago I saw it and use of it solved problem. I thought that with changing reference coordinates it will be easy then with moving object "into the screen" by this
Code: [Select]
object.translate(0, 0, move); Rotating has mentioned function with setting reference, translation doesn't as I see. So it moves in one direction despite rotation, any clue to change this?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11747
    • View Profile
    • http://www.jpct.net
Re: Camera always behind object
« Reply #9 on: January 30, 2012, 06:49:01 pm »
I'm not sure if i got the question, but you can always translate along the transformed axes returned by get?Axis() as well, i.e. something like

obj.translate(obj.getZAxis());

Offline bLAZY

  • byte
  • *
  • Posts: 18
    • View Profile
Re: Camera always behind object
« Reply #10 on: January 30, 2012, 08:52:23 pm »
Exactly what I needed. Thanks this command I understood much more about it all. Thank you!