🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Straffing in 3D

Started by
1 comment, last by GameDev.net 17 years, 11 months ago
Hello. I am still modifying the Lesson 10 code (if you read my other post), and I decided to add straffing to the demo. After searching for a while, I am yet to find a reliable way of straffing. Could anyone point out to me how you would do it? Biubidboy.
Advertisement
Actually, strafing is the same as moving forward, except you have a difference of 90 degrees for the movement angle. So all you would need to do is to remove or add 90 degrees to your movement vector and you will get strafing. I did not check the code for lesson 10, but here is a sample of mine...

// begin sample
// these are used to store the 'movement vector'
float fz = 0;
float fx = 0;

// if the player moves forward
if( g_bKeys[ g_nForward ] )
{
fz += wcosf( g_fRotY ) / ( ( g_bKeys[ g_nLeft ] || g_bKeys[ g_nRight ] ) ? sqrtf( 2.0f ) : 1.0f );
fx += wsinf( g_fRotY ) / ( ( g_bKeys[ g_nLeft ] || g_bKeys[ g_nRight ] ) ? sqrtf( 2.0f ) : 1.0f );
}

// if the player moves backward
if( g_bKeys[ g_nBackward ] )
{
fz -= wcosf( g_fRotY ) / ( ( g_bKeys[ g_nLeft ] || g_bKeys[ g_nRight ] ) ? sqrtf( 2.0f ) : 1.0f );
fx -= wsinf( g_fRotY ) / ( ( g_bKeys[ g_nLeft ] || g_bKeys[ g_nRight ] ) ? sqrtf( 2.0f ) : 1.0f );
}

// if the player strafes left
if( g_bKeys[ g_nLeft ] )
{
fz -= wcosf( g_fRotY - 90.0f ) / ( ( g_bKeys[ g_nForward ] || g_bKeys[ g_nBackward ] ) ? sqrtf( 2.0f ) : 1.0f );
fx -= wsinf( g_fRotY - 90.0f ) / ( ( g_bKeys[ g_nForward ] || g_bKeys[ g_nBackward ] ) ? sqrtf( 2.0f ) : 1.0f );
}

// if the player strafes right
if( g_bKeys[ g_nRight ] )
{
fz += wcosf( g_fRotY - 90.0f ) / ( ( g_bKeys[ g_nForward ] || g_bKeys[ g_nBackward ] ) ? sqrtf( 2.0f ) : 1.0f );
fx += wsinf( g_fRotY - 90.0f ) / ( ( g_bKeys[ g_nForward ] || g_bKeys[ g_nBackward ] ) ? sqrtf( 2.0f ) : 1.0f );
}

// fTime is the time elapsed in seconds since the last frame,
// for fluid and constant speed movement
// so faster computers dont go faster than slower computers
fx *= fTime * g_fRunSpeed;
fz *= fTime * g_fRunSpeed;

// do physics
// code stripped

// update the player position
g_fPosX += fx;
g_fPosZ += fz;
// end sample

I used the square root of 2 if it was moving diagonally, to keep movement circular so the user doesn't move faster when strafing. Have fun with that, feel free to email me if you have questions about my code, I'll post a transcript here with the pertinent information.

-- Wolf -- Unwanted_Wan At Hotmail Dot You Know What --
Here is the transcript of the important parts of the email biubidboy sent me:

What exactly are the functions 'wsinf' and 'wcosf'? Apparently they aren't defined for me.

Here is my reply:

I forgot to mention about 'wsinf' and 'wcosf'...

#define _USE_MATH_DEFINES // must be present to use M_PI
#include

// this will convert degrees to radiants,
// to be used with trigonometric functions
float deg2radf( float f )
{
return ( float ) ( f * M_PI / 180.0f );
}

// sin
float wsinf( float f )
{
return sinf( deg2radf( f ) );
}

// cos
float wcosf( float f )
{
return cosf( deg2radf( f ) );
}

-- Wolf -- Unwanted_Wan At Hotmail Dot You Know What --

This topic is closed to new replies.

Advertisement