Results 1 to 6 of 6

Thread: C++: Camera Math - Pitch, RotateY -- Confirm If Correct

Threaded View

  1. #1

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    C++: Camera Math - Pitch, RotateY -- Confirm If Correct

    Check this out and let me know if this is correct:

    I am having my mind blown trying to get a proper 3rd person camera setup.
    I got this far, and I can get it to move and follow something but its all messed up and at times goes into weird and crazy positions. Sometimes it even just locks. So I figure might as well see if the math I am using is correct, if so then it is obviously my implimentation which needs work.

    Thanks.
    PHP Code:
    // Rotates camera's eye around the world Y-axis
    // Using your standard Y-axis rotation matrix, we'll rotate around the Y-axis
    // ca == cosf(angle)    sa == sinf(angle)
    // [ ca 0 -sa ]
    // [ 0  1   0 ]
    // [ sa 0  ca ]
    void Camera::RotateY(USINT angle, const D3DVECTOR &vTarget)
    {
        
    float xxx=0zzz=0;
        
    float sinAng=0cosAng=0;
        
        
    // Get sin()/cos() of angle
        /*angle *= D3D.ToRad
        sinAng = sinf(angle);
        cosAng = cosf(angle);*/
        
    cosAng m_VectorCircle[angle].u;
        
    sinAng m_VectorCircle[angle].v;
        
        
    // First translate to the world's origin
        
    m_Eye -= vTarget;
        
        
    // Save off forward components for computation?????????
        
    xxx m_Eye.x;
        
    zzz m_Eye.z;
        
        
    // Rotate forward vector
        
    m_Eye.xxx cosAng zzz sinAng;
        
    m_Eye.xxx * -sinAng zzz cosAng;
        
        
    // Translate back
        
    m_Eye += vTarget;
        
        
    SetTarget(vTarget); // Build the camera's axes 
    }


    void Camera::Pitch(USINT angle, const D3DVECTOR &vTarget)
    {
        
    // Compute the sin and cos of the angle 
        //float sinAng = sinf(angle);
        //float cosAng = cosf(angle);
        
    float cosAng m_VectorCircle[angle].u;
        
    float sinAng m_VectorCircle[angle].v;

        
    m_Eye -= vTarget// Translate to world's origin

        // Calculate new eye X component
        
    float xxx = (m_Right.m_Right.* (1.0f cosAng) + cosAng) * m_Eye.+
                    (
    m_Right.m_Right.* (1.0f cosAng) + m_Right.sinAng) * m_Eye.+
                    (
    m_Right.m_Right.* (1.0f cosAng) - m_Right.sinAng) * m_Eye.z;

        
    // Calculate new Y component       
        
    float yyy = (m_Right.m_Right.* (1.0f cosAng) - m_Right.sinAng) * m_Eye.+
                    (
    m_Right.m_Right.* (1.0f cosAng) + cosAng) * m_Eye.+
                    (
    m_Right.m_Right.* (1.0f cosAng) + m_Right.sinAng) * m_Eye.z;

        
    // Calculate new Z component
        
    float zzz = (m_Right.m_Right.* (1.0f cosAng) + m_Right.sinAng) * m_Eye.+
                    (
    m_Right.m_Right.* (1.0f cosAng) - m_Right.sinAng) * m_Eye.+
                    (
    m_Right.m_Right.* (1.0f cosAng) + cosAng) * m_Eye.z;

        
    m_Eye D3DVECTOR(xxxyyyzzz); // Set the eye
        
    m_Eye += vTarget// Translate back
        
        // Get vector from target to eye
        
    D3DVECTOR Vec m_Eye vTarget;
        
    //Vec.normalize();
        
    normalize(Vec);
        
        
    // Position eye so that's always "kCamDist" from target
        
    m_Eye vTarget + (Vec m_Away);
        
        
    SetTarget(vTarget); // Build the camera's axes
    }
        
    // Builds the camera's axes by using the eye, target, and world's up vector
    void Camera::SetTarget(const D3DVECTOR &PlayerPos)
    {
        
    // Compute new forward vector (line of sight vector)
        
    m_Forward PlayerPos m_Eye;
        
    //forward.normalize();
        
    normalize(m_Forward);
        
        
    // Compute new right vector
        //m_Right = kWorldUp.crossProduct(forward);
        
    m_Right CrossProduct(m_Upm_Forward);
        
    //m_Right.normalize();
        
    normalize(m_Right);
        
        
    // Compute new camera up vector
        //m_Up = forward.crossProduct(right);
        
    m_Up CrossProduct(m_Forwardm_Right);
    }



    // Normalizes the CVector (ie makes it a unit vector)
    void normalize(D3DVECTOR &v)
    {
        
    //assert(!TEqual(magnitude(v), 0.0f, .001f)); // Make sure length isn't zero
        
        
    float oneOverLen 1.0f magnitude(v);
        
        
    v.*= oneOverLen;
        
    v.*= oneOverLen;
        
    v.*= oneOverLen;
    }

    // Checks for two floats to be equal within a certain tolerance
    bool TEqual(float afloat bfloat t)
    {
        return ((
    t) && (t));
    }

    // Returns the magnitude (ie length or norm) of the CVector
    float magnitude(D3DVECTOR &v)
    {
        return 
    sqrtf((v.v.x) + (v.v.y) + (v.v.z));

    Last edited by Electroman; Dec 8th, 2004 at 07:21 PM. Reason: Added Language to title.
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width