Advertisement

Moving around with glulookat()...

Started by July 04, 2001 04:44 PM
2 comments, last by Jackthemeangiant 23 years, 7 months ago
Hi, To move around in a world before, I was just using gltranslat(), and glrotate() with a heading that i would increase or decrease when the mouse is moved, and used some trig to move with this heading. but with glulookat, you no longer have an angle that you are facing, so what is the best way to move around a world with glulookat? i do have an idea in the back of my head, but i don''t think it will work thanks, Jack
Direction Vectors & parametric equations.

gluLookAt(camPos.x,camPos.y,camPos.z,
PointLookingAt.x,PointLookingAt.y,PointLookingAt.z,
0,1,0);

store your camera position and the point the camera is looking at in an array, then to move the camera in a certain direction by 1 unit:

// Calculate Direction Vector

DirectionVector.x = PointLookingAt.x - CamPos.x
DirectionVector.y = PointLookingAt.y - CamPos.y
DirectionVector.z = PointLookingAt.z - CamPos.z

// Find Length of DirectionVector

Mag = SquareRoot((DirectionVector.x*DirectionVector.x)+(DirectionVector.y*DirectionVector.y)+(DirectionVector.z*DirectionVector.z))

// Make DirectionVector into a unit vector (of length 1 unit)

DirectionVector.x = DirectionVector.x / Mag
DirectionVector.y = DirectionVector.y / Mag
DirectionVector.z = DirectionVector.z / Mag

// Update Camera location

CamPos.x = CamPos.x + 1*DirectionVector.x
CamPos.y = CamPos.y + 1*DirectionVector.y
CamPos.z = CamPos.z + 1*DirectionVector.z

// Update Point Co-ords which the camera is looking at

PointLookingAt.x = PointLookingAt.x + 1*DirectionVector.x
PointLookingAt.y = PointLookingAt.y + 1*DirectionVector.y
PointLookingAt.z = PointLookingAt.z + 1*DirectionVector.z

this moves you one unit in the direction of DirectionVector, thus you could rotate the DirectionVector and move in a different direction.

"You are just as irritating to me as an irrational term that accidentially creeps into your equation and cannot be factorized out."
Advertisement
I do a similar thing, but i store the direction vector as unit length instead.

Here's my simple camera class that wraps gluLookAt().
Call cCamera::MoveCameraPosition() and pass which keys are down, along with the number of ticks passed since you last called it, and it will compute a unit length direction vector. Call cCamera::SetDirectionVector() to set the view matrix using these values. This enables you to compute the new position, and only move there if your collision routines say its ok.

Just make a simple header file for it, with the position and angle values and proc defs etc... or whatever
Here's the code:

    ////	FUNCTION: cCamera::CalculateDirectionVector()////	PURPOSE: Constructs a direction vector from the yaw and pitch values//void cCamera::CalculateDirectionVector(void){    float PitchCosine = (float)cos(Pitch);    Direction.x = (float)sin(Yaw) * PitchCosine;    // Apply pitch inversion factor    Direction.y = (float)sin(Pitch) * (InvertPitch ? 1 : -1);    Direction.z = (float)cos(Yaw) * -PitchCosine;}////	FUNCTION: cCamera::MoveCameraPosition()////	PURPOSE: Takes the keys pressed and the mouse movements and makes //			 appropriate changes to the Position and Direction vectors.//void cCamera::MoveCameraPosition(const bool Foward, const bool Back, const bool Left, const bool Right, const long MouseX, const long MouseY, const long Ticks){    // Update yaw value    Yaw += (float)MouseX / 150;    // Make sure the value doesn't get too large    if (Yaw > PI_TIMESTWO)    {        Yaw -= PI_TIMESTWO;    }     else if (Yaw < -PI_TIMESTWO)    {        Yaw += PI_TIMESTWO;    }    float NewAngle = Pitch + (float)MouseY / 200;        // Check the new angle is ok, stop the camera doing loops    if (NewAngle > -PI_OVERTWO && NewAngle < PI_OVERTWO)    {        Pitch = NewAngle;    }    // Compute direction vector using the yaw and pitch values    CalculateDirectionVector();    // Vector to hold the direction that the viewing position will move    cVector MoveDirection;    // These just move the position towards the direction being looked at    if (Foward)    {        MoveDirection.x += Direction.x;        MoveDirection.y += Direction.y;        MoveDirection.z += Direction.z;    }    if (Back)    {        MoveDirection.x -= Direction.x;        MoveDirection.y -= Direction.y;        MoveDirection.z -= Direction.z;    }    if (Left)    {        MoveDirection.x += Direction.z;        MoveDirection.z -= Direction.x;    }    if (Right)    {        MoveDirection.x -= Direction.z;        MoveDirection.z += Direction.x;    }    // Make the vector unit length before scaling    MoveDirection.Normalize();    // Scale vector by scale factor    MoveDirection = MoveDirection * (Ticks * MovementSpeed);    // Update position    Position.x += MoveDirection.x;    Position.y += MoveDirection.y;    Position.z += MoveDirection.z;}////	FUNCTION: cCamera::SetCameraMatrix()////	PURPOSE: Calls the gluLookAt function with the position and direction values//void cCamera::SetCameraMatrix(void){    // Do camera transformation    gluLookAt(Position.x, Position.y, Position.z, // Position              Position.x + Direction.x,    // Add position and direction vectors              Position.y + Direction.y,    // to get point being looked at              Position.z + Direction.z,              0.0f, 1.0f, 0.0f);           // Standard world up vector}  

Have FUN!!

FatalXC

Edited by - FatalXC on July 5, 2001 8:39:21 AM
Great,

Thanks alot guys, I will give it a shot.

Later,
Jack

This topic is closed to new replies.

Advertisement