Reputation: 1516
I want to make my camera move behind the model when it rotates, just like in a Third Person Perspective game - to have it "look" always on the back of the model. Im provided with a framework so the syntax may be bit different than normal opengl. I use the standard camera
Mat4 mv = MatrixMath.lookAt(this.eyeX,this.eyeY,this.eyeZ,this.at,this.up);
and to rotate the camera i tried
this.mouseRotation += 20.0f;
this.eyeX = (float) Math.sin(this.mouseRotation / 180.0f * 3.14f) * 2.0f;
this.eyeZ = (float) Math.cos(this.mouseRotation / 180.0f * 3.14f) * 2.0f;
mouseRotation is the angle which the model is located so obviously the camera should be also move to a position located 20 degrees further on the "circle". But instead of this the object rotates around itself, and the camera makes a circular movement but not around the model, but next to it, still looking at the same point.
Any ideas how to make this work?
Upvotes: 1
Views: 225
Reputation: 9134
From your example, I believe the short answer is to add this.at
(which should be the position of your object) into your this.eye
so that that the eye is positioned relative to the object.
In more detail, say your object's position is at this.at
, and you want the camera to follow the object at some distance, say 'd' "behind" the object. If you have a unit vector (i.e., one's whose length is 1.0) pointing out of the front of the object, then the this.at - d
should be the position of the camera (i.e., this.eye
). In order to get the camera to rotate around the object, first apply a rotation to 'd' (in the example above, you appear to be rotating around the 'Y' axis [since you're only modifying eyeX and eyeZ]), so that
this.eye = this.at - rotation(Y) * d;
Upvotes: 1