Results 1 to 2 of 2

Thread: Viewing transformations in 3D

  1. #1

    Viewing transformations in 3D

    I am trying to make a simple flight simulator. I am not being able to set up the viewing sytem for this. Can anybody help in this regard. I want all motions of a flying object. I want to do this via the keyboard. Please keep in mind that i have just started OpenGL.

  2. #2
    Member Contributor
    Join Date
    May 2000
    Ottawa, Ontario, Canada

    Re: Viewing transformations in 3D

    Sure .. here is what I stole...from super bible i think...
    //************************************************** ************************
    // Update the position of the camera.This function can vary wildly depending
    // on how motion is acheived. For a flight sim, motion will always be along the
    // viewer's Z axis. For a first person walking around, motion will always be in
    // the XY plane. Even if the camera tilts down (to look at the ground), or up
    // (to look at the sky), the player still only moves along the XY plane.
    // This function must be called continuously, or the timed motion will not work.
    // If this function is called only when a key press is made, then the time between
    // calls will be huge and the camera will 'rocket' to the new location with each key
    // press
    void UpdatePosition(struct CAMERA *pCamera)
    float fLinearVelocity = 0.0f;
    float fAngularVelocityY = 0.0f;
    float fAngularVelocityX = 0.0f;
    float fAngularVelocityZ = 0.0f;
    float fTime, fXDelta,fAngle, fZDelta;
    double fSeconds;
    float elevation = 0.0f;

    LARGE_INTEGER currentTime;

    // Keyboard Input
    // Check for forward or backwards Motion
    fLinearVelocity = 6.0f;

    fLinearVelocity = -6.0f;
    // check for elevation
    elevation = 0.25f;

    elevation = -0.25f;

    // Check for spin left/right (Y Axis rotation)
    fAngularVelocityY = -45.0f;

    fAngularVelocityY = 45.0f;

    // Check for looking up and down
    if(GetAsyncKeyState(0x41)) // A key
    fAngularVelocityX = -9.50f;

    if(GetAsyncKeyState(0x5a)) // Z key
    fAngularVelocityX = 9.50f;

    // Adjust position and orientation. Get the time since the last
    // check. If the velocity = 0 (no keypress or mouse movement)
    // then the motion will be nil...
    // D = vt
    fTime = (float)(currentTime.QuadPart - lastTime.QuadPart)/
    lastTime = currentTime;

    fSeconds = (double)(currentTime.QuadPart - globeTime.QuadPart)/

    // calc wheel rotation
    wheel_angle = (float(fSeconds * 18.0f));
    wheel_rotation = fmod(wheel_angle, 360.0f);

    // Update Rotation angles (clamp the X rotation)
    fAngle = fTime * fAngularVelocityX;
    fAngle += pCamera->orientation[0];

    if((fAngle < 90.0f) && (fAngle > -90.0f))
    pCamera->orientation[0] = fAngle;

    pCamera->orientation[1] += fTime * fAngularVelocityY;

    // Update linear position
    fTime = fTime * fLinearVelocity;
    fXDelta = fTime * (float)(sin(DEGTORAD(pCamera->orientation[1])));
    fXDelta += pCamera->position[0];
    fZDelta = fTime * (float)(cos(DEGTORAD(pCamera->orientation[1])));
    fZDelta += pCamera->position[2];

    pCamera->position[1] += elevation;

    pCamera->position[0] = fXDelta;
    pCamera->position[2] = fZDelta;

    // Halt the camera at the boundaries of the Virtual World.

    if(pCamera->position[0] > 150.0f)
    pCamera->position[0] = 150.0f;

    if(pCamera->position[0] < -150.0f)
    pCamera->position[0] = -150.0f;

    if(pCamera->position[2] > 150.0f)
    pCamera->position[2] = 150.0f;

    if(pCamera->position[2] < -150.0f)
    pCamera->position[2] = -150.0f;

Similar Threads

  1. Help me understand viewing transformations
    By marce in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 08-08-2009, 08:13 PM
  2. Viewing transformations and Coordinates
    By dvwood in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 01-29-2009, 06:15 PM
  3. N00b help - specifically viewing, transformations
    By ankitmcgill in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 10-21-2008, 08:38 AM
  4. Navigating using Viewing Transformations
    By rangers99 in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 07-12-2002, 09:24 PM
  5. viewing transformations help please!!!
    By in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 07-05-2000, 04:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Proudly hosted by Digital Ocean