Camera always behind object

Started by bLAZY, January 17, 2012, 10:58:21 AM

Previous topic - Next topic

bLAZY

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?

EgonOlsen

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()

bLAZY

I have to admit I don't know how to use it correctly. Probably don't understand 3D transormations. I found this on forum:
   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?

EgonOlsen

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.

bLAZY

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 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:)

EgonOlsen

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.

bLAZY

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.

EgonOlsen

You have get?Axis()-methods in Object3D and you can rotate around these with the rotateAxis()-method. That should do the trick.

bLAZY

:) 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 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?

EgonOlsen

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());

bLAZY

Exactly what I needed. Thanks this command I understood much more about it all. Thank you!