Hello all!
I made a camera class, it has a camera eyepoint in the scene graph, and calling movement methods of this class will move the eyepoint accordingly.. for example calling pan(dx,dy) will call eye.TranslateLocal(dx,dy,0).. etc.
The problem is with the orbit method.. I want the camera to rotate around its target while looking at it. horizontal rotation is no problem, it's just rotation around the parent Z axis, like this:
Public Sub Orbit(ByVal angle As Single)
mEye.LocalTransform *= Transform.Tranlation(-mLocalTarget) * Transform.RotationZ(-angle) * Transform.Tranlation(mLocalTarget)
End Sub
where the Transform class is similar to the matrix class of directx.
now when I try to rotate vertically, I have to rotate around the target's location and the axis is the eyepoint's local x.
first I tried to do this:
Public Sub Tilt(ByVal angle As Single)
Dim relation As Vector3 = mLocalTarget - mEye.LocalPosition
mEye.LocalTransform = Transform.Tranlation(-mLocalTarget) * Transform.Tranlation(relation) * Transform.RotationX(angle) * Transform.Tranlation(-relation) * Transform.Tranlation(mLocalTarget) * mEye.LocalTransform
End Sub
but this made the eyepoint rotate around a point somewhere between it and the target...
I then tried this:
Public Sub Tilt(ByVal angle As Single)
Dim relation As Vector3 = mLocalTarget - mEye.LocalPosition
Dim z As New Vector3(0, 0, 1)
Dim rNorm As Vector3 = relation
rNorm.Normalize()
Dim left As Double = Math.Acos(Vector3.Dot(rNorm, z))
Dim res As Double = left + angle
If (res > 0) AndAlso (res < Math.PI) Then
Dim x As Vector3 = Vector3.Cross(z, relation)
x.Normalize()
Dim m As Matrix = Matrix.RotationAxis(x, angle)
relation = Vector3.TransformNormal(relation, m)
mEye.LocalRotationMatrix *= m
mEye.LocalPosition = mLocalTarget - relation
End If
End Sub
and this worked quite fine, untill it randomally starts to sway around with not obvious reason.. I spent days trying different ways and nothing worked. it's either a precision problem or something similar.. Please help!! I'm using managed directx9
[Edited by - Elias on May 26, 2007 1:01:01 PM]