Sign in to follow this  

Problems concerning rotation around a specific point and axis... Help!!

Recommended Posts

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 have no idea why.. I'm not that good in this kind of math)... Then I 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

'Calculate the current angle between the sight vector and the z vector
        Dim current As Double = Math.Acos(Vector3.Dot(rNorm, z))

'if the angle is in [0,180] after rotation, apply the rotation
        Dim res As Double = current + angle
        If (res > 0) AndAlso (res < Math.PI) Then
           Dim x As Vector3 = Vector3.Cross(z, relation)
           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

Share this post

Link to post
Share on other sites
This topic is now closed to further replies.
Sign in to follow this