It is currently Mon Jun 26, 2017 12:28 am

All times are UTC - 5 hours




 Page 1 of 1 [ 20 posts ] 
Author Message
 Post subject: Caligari VMK 1 Introduction
PostPosted: Tue Feb 13, 2007 7:04 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
3D vector class is created using inline function calls.


Offline
 Profile  
 
 Post subject: Re: GameDev VMK 7 - Vector3 class
PostPosted: Wed Aug 08, 2007 11:09 am 

Joined: Sat Aug 04, 2007 6:15 am
Posts: 5
i want to know why you make this fonction friend ??? without friend it works ?

Quote:
inline friend Vector3 operator* (const float &fValue,const Vector3 v) {

return Vector3(v.m_fX*fValue,v.m_fY*fValue,v.m_fZ*fValue);

}//operator*


Offline
 Profile  
 
 Post subject: Re: GameDev VMK 7 - Vector3 class
PostPosted: Wed Aug 08, 2007 4:39 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
You need the keyword friend infront of the operator* function because otherwise the code will not build. You should get the following error:

error C2804: binary 'operator *' has too many parameters

error C2333: 'Vector3::operator`*'' : error in function declaration; skipping function body


friend plays a key role here because it allows you to declare operator* as having 2 parameters, rather than one.


Last edited by Marek on Tue Dec 11, 2007 2:57 pm, edited 1 time in total.

Offline
 Profile  
 
 Post subject: Re: GameDev VMK 7 - Vector3 class
PostPosted: Thu Oct 11, 2007 1:46 pm 

Joined: Tue May 01, 2007 2:55 pm
Posts: 96
Location: Behind you
In the tutorial you say you use inline function calls "just to expose to something new". Are they also optimizing here, or doesn't it make too much of difference? (or is not using them better even?)


Offline
 Profile  
 
 Post subject: Re: GameDev VMK 7 - Vector3 class
PostPosted: Thu Oct 11, 2007 1:52 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
Yes, by using an inline function you can gain a boost in speed of execution of your program at the cost of having a larger program.


Offline
 Profile  
 
 Post subject: Re: GameDev VMK 7 - Vector3 class
PostPosted: Thu Oct 11, 2007 2:01 pm 

Joined: Tue May 01, 2007 2:55 pm
Posts: 96
Location: Behind you
If the executable is big enough in size, the OS will need to move bits of it from and to the swap file a lot, which will cause a decrease in performance.


Offline
 Profile  
 
 Post subject: Re: GameDev VMK 7 - Vector3 class
PostPosted: Thu Oct 11, 2007 2:05 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
Yes, that is why large functions should never be made inline. With simple math calculation it will be fine. If you take a look at any game engine, you will see that all of them will use inline functions for their "math engine".



 


Offline
 Profile  
 
 Post subject: Re: GameDev VMK 7 - Vector3 class
PostPosted: Thu Oct 11, 2007 2:08 pm 

Joined: Tue May 01, 2007 2:55 pm
Posts: 96
Location: Behind you
okey, thanks

So, probably a lot more optimisation can be done by making more functions inline? (just a side question, is that also what your compiler does when you tell it to optimise? )

(I just wasn't sure at what size it was going to matter)


Offline
 Profile  
 
 Post subject: Re: GameDev VMK 7 - Vector3 class
PostPosted: Thu Oct 11, 2007 2:31 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
Optimization is a topic all on its own. I didn't use very much optimization when developing the game engine because you should not worry too much about speed, but rather about functionality. There is always something that can be done to make code run faster (ie rewrite heavily used functions in assembly). I am going to make a separate VMK series that talks about optimizing code so that is where I will discuss how to improve the game engine.


Offline
 Profile  
 
 Post subject: Re: GameDev VMK 7 - Vector3 class
PostPosted: Thu Oct 11, 2007 4:09 pm 

Joined: Tue May 01, 2007 2:55 pm
Posts: 96
Location: Behind you
Good to hear that.

It's kind of random to ask this question with this VMK, but anyway:

I noticed that you are often using the virtual keyword (especially for destructors), why are you doing this?


Offline
 Profile  
 
 Post subject: Re: GameDev VMK 7 - Vector3 class
PostPosted: Thu Oct 11, 2007 4:53 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
the virtual keyword is needed when you inherit classes from other classes. If you don't use virtual you run the risk of having your classes destruct in the wrong order. To stay safe, I always (or at least I SHOULD always) be using the virtual keyword for all my classes. This way if I inherit them in the future I won't forget about the destruction problem.


Offline
 Profile  
 
 Post subject: Re: GameDev VMK 7 - Vector3 class
PostPosted: Mon Oct 29, 2007 7:04 am 

Joined: Fri Oct 12, 2007 4:41 pm
Posts: 24
Your adding the const keyword after classes, what does this mean?

operator*(float &fValue) const also why are you passing by reference if your not going to change the values?


Offline
 Profile  
 
 Post subject: Re: GameDev VMK 7 - Vector3 class
PostPosted: Mon Oct 29, 2007 10:47 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
When the const keyword is placed after a function definition like:

Vector3 operator*(const float &fValue) const


This means that the return value (Vector3 in this case) will be a constant. ie you will not be able to change it.

When overloading the operators, you must pass by reference or else your overloaded operators will not work. If this was my own function, then yes, if you don't plan on changing the value, don't pass by reference..... although, if you are passing in a large data structure into a function, passing by reference is faster than passing in by value because all values in the data structure do not have to get initialized.


Last edited by Marek on Sun Aug 31, 2008 7:20 am, edited 1 time in total.

Offline
 Profile  
 
 Post subject: Re: GameDev VMK 7 - Vector3 class
PostPosted: Fri Nov 09, 2007 4:57 am 

Joined: Tue May 01, 2007 2:55 pm
Posts: 96
Location: Behind you
I was having a look at inheritance once again (as I wanted the Player class to be a seperate class, with a Camera member variable (a relativeCam which is derived from Camera, actually). Anyway, that was why I was having a look at inheritance and now I wonder why vector is not inheriting from Vertex.

Though conceptually they represent different ideas, they have the very same values - even represented by the same names if I'm not mistaken.


Offline
 Profile  
 
 Post subject: Re: GameDev VMK 7 - Vector3 class
PostPosted: Fri Nov 09, 2007 7:22 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
I believe I placed the Vertex class in the ObjectLib and the Vector class in the MathLib. Because of this, I did not want to inherit one from the other. If they were both in the same project then I probably would have inherited.


Offline
 Profile  
 
 Post subject: About const methods
PostPosted: Sun Aug 31, 2008 2:29 am 

Joined: Wed Aug 06, 2008 7:53 pm
Posts: 182
Location: Russia
A keyword “constâ€


Offline
 Profile  
 
 Post subject:
PostPosted: Fri Sep 04, 2009 1:10 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
I noticed there is a typo in the Vector3 class's rotateX and rotateY functions. I don't remember if these function were written in this VMK or in a later VMK. However the correct implementation is:

//------------------------------------------------------------------------
// Name: RotateY(:)
// Desc: rotate this vector about the Y axis by desired amount of radians (YAW)
//
inline Vector3 Vector3::rotateY(float fRadians) {
   Vector3 v;

   v.m_fX =  m_fX*cos(fRadians) + m_fZ*sin(fRadians);
   v.m_fY =  m_fY;
   v.m_fZ = -m_fX*sin(fRadians) + m_fZ*cos(fRadians);

   return v;
} //RotateY

//------------------------------------------------------------------------
// Name: RotateX(:)
// Desc: rotate this vector about the X axis by desired amount of radians (PITCH)
//
inline Vector3 Vector3::rotateX(float fRadians) {
   static Vector3 v;

   v.m_fX = m_fX;         
   v.m_fY = m_fY*cos(fRadians) - m_fZ*sin(fRadians);
   v.m_fZ = m_fY*sin(fRadians) + m_fZ*cos(fRadians);

   return v;
} //RotateX


Wikipedia Reference

I noticed that in the video's I have the sign reversed.


Offline
 Profile  
 
 Post subject: We need to update class Player as well
PostPosted: Sat Sep 05, 2009 1:42 pm 

Joined: Wed Aug 06, 2008 7:53 pm
Posts: 182
Location: Russia
The position of minus sign in front of SINE is different in various books, which is really confusing. The position of ‘minus’ depends on the “handness” of co-ordinate system and on the direction of ‘positive’ rotation.
We works with OpenGL, so co-ordinate system is right-handed. Positive rotation angle is: X->Y, Y->Z, Z->X. (Therefore Wiki’s page is right for our case).

Fortunately, the only place where GameEngine uses 3D-vector rotation is inside Camera/Player classes. But when I made this change the players movements controllable by arrow-keys stop working correctly. So, it seems that after this fix of the Vector class (a ‘canonization’ actually) we need to update Player class as well. Meanwhile I had to return the minuses in all Vector::Rotate() methods back to the previous positions.

Marek, are you tested this change, in particular under different angles of view? What is yours results?
We are also looking forward for Player class update.


My code now looks like this:

struct Vector3 {
...

// Ugly, but we need it.
// Set to (+1.0f) for original (supposedly incorrect).
// Set to (-1.0f) for canonical (correct).
   #define FIX_MINUS    1.0f  // original  (defective?)
// #define FIX_MINUS   -1.0f  // canonical (correct)


    void RotateX( const float radians ) // Pitch
    {
    //  const float SIN = ( float )::sinf( radians );
    //  const float COS = ( float )::cosf( radians );
        float SIN, COS;
        sin_cos( radians, SIN, COS );
//         y = + COS * y + SIN * z;
//         z = - SIN * y + COS * z;
        y = + COS * y + SIN * z * FIX_MINUS;
        z = - SIN * y * FIX_MINUS + COS * z;
    }

    void RotateY( const float radians ) // Yaw
    {
    //  const float SIN = ( float )::sinf( radians );
    //  const float COS = ( float )::cosf( radians );
        float SIN, COS;
        sin_cos( radians, SIN, COS );
//         x = + COS * x - SIN * z;
//         z = + SIN * x + COS * z;
        x = + COS * x - SIN * z * FIX_MINUS;
        z = + SIN * x * FIX_MINUS + COS * z;
    }

    void RotateZ( const float radians ) // Roll
    {
    //  const float SIN = ( float )::sinf( radians );
    //  const float COS = ( float )::cosf( radians );
        float SIN, COS;
        sin_cos( radians, SIN, COS );
//         x = + COS * x + SIN * y;
//         y = - SIN * x + COS * y;
        x = + COS * x + SIN * y * FIX_MINUS;
        y = - SIN * x * FIX_MINUS + COS * y;
    }

    Vector3 GetRotateX( const float radians ) const // Pitch
    {
    //  const float SIN = ( float )::sinf( radians );
    //  const float COS = ( float )::cosf( radians );
        float SIN, COS;
        sin_cos( radians, SIN, COS );
        return Vector3(
//             + x, 
//             + COS * y + SIN * z,
//             - SIN * y + COS * z
            + x, 
            + COS * y + SIN * z * FIX_MINUS,
            - SIN * y * FIX_MINUS + COS * z
            );
    }

    Vector3 GetRotateY( const float radians ) const // Yaw
    {
    //  const float SIN = ( float )::sinf( radians );
    //  const float COS = ( float )::cosf( radians );
        float SIN, COS;
        sin_cos( radians, SIN, COS );
        return Vector3(
//             + COS * x - SIN * z,
//             + y,
//             + SIN * x + COS * z
            + COS * x - SIN * z * FIX_MINUS,
            + y,
            + SIN * x * FIX_MINUS + COS * z
            );
    }

    Vector3 GetRotateZ( const float radians ) const // Roll
    {
    //  const float SIN = ( float )::sinf( radians );
    //  const float COS = ( float )::cosf( radians );
        float SIN, COS;
        sin_cos( radians, SIN, COS );
        return Vector3(
//             + COS * x + SIN * y,
//             - SIN * x + COS * y,
//             + z
            + COS * x + SIN * y * FIX_MINUS,
            - SIN * x * FIX_MINUS + COS * y,
            + z
            );
    }
...

};



Offline
 Profile  
 
 Post subject:
PostPosted: Sat Sep 05, 2009 2:32 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
I made the change in Ghost Toast and there I wasn't using the rotateX or rotateY functions for anything so nothing broke. However you are correct, if the function was previously being used somewhere (I'm pretty sure we used it inside the physics engine VMK series to turn the space ship) then you'll need to place a minus sign infront of the argument to the rotateX or rotateY functions so that everything still works properly.


Offline
 Profile  
 
 Post subject: the wickedness of pleasure
PostPosted: Sun Sep 06, 2009 4:09 pm 

Joined: Wed Aug 06, 2008 7:53 pm
Posts: 182
Location: Russia
OK.
I found exactly 3 places in GE where one should put "--" in front of argument of rotateY( a ), all of them in Player and Camera modules.

:!:
So, guys if you fixed Vector3 according to this proposal you also should update 3 places where there are following statements:

// old code:
m_lookDirection = m_lookDirectionHome.rotateY( m_RY ); // Beware of Dogma - It'll trap your mind. :)

// new code:
m_lookDirection = m_lookDirectionHome.rotateY( -m_RY ); // Note minus in front of m_RY

(your names might be mangled by Hungarian type-prefixes: m_fRY instead of m_RY etc).


Marek, and what about this question?




Nice, I updated Vector3 code and rid of ugly temporary macro constant FIX_MINUS:

struct Vector3
{
    union
    {
        float v[ 3 ];
        struct { float x, y, z; };
    };

    ...

    void RotateX( const float radians ) // Pitch
    {
    //  const float SIN = ( float )::sinf( radians );
    //  const float COS = ( float )::cosf( radians );
        float SIN, COS;
        sin_cos( radians, SIN, COS );
        y = + COS * y - SIN * z;
        z = + SIN * y + COS * z;
    }

    void RotateY( const float radians ) // Yaw
    {
    //  const float SIN = ( float )::sinf( radians );
    //  const float COS = ( float )::cosf( radians );
        float SIN, COS;
        sin_cos( radians, SIN, COS );
        x = + COS * x + SIN * z;
        z = - SIN * x + COS * z;
    }

    void RotateZ( const float radians ) // Roll
    {
    //  const float SIN = ( float )::sinf( radians );
    //  const float COS = ( float )::cosf( radians );
        float SIN, COS;
        sin_cos( radians, SIN, COS );
        x = + COS * x - SIN * y;
        y = + SIN * x + COS * y;
    }

    Vector3 GetRotateX( const float radians ) const // Pitch
    {
    //  const float SIN = ( float )::sinf( radians );
    //  const float COS = ( float )::cosf( radians );
        float SIN, COS;
        sin_cos( radians, SIN, COS );
        return Vector3(
            + x, 
            + COS * y - SIN * z,
            + SIN * y + COS * z
            );
    }

    Vector3 GetRotateY( const float radians ) const // Yaw
    {
    //  const float SIN = ( float )::sinf( radians );
    //  const float COS = ( float )::cosf( radians );
        float SIN, COS;
        sin_cos( radians, SIN, COS );
        return Vector3(
            + COS * x + SIN * z,
            + y,
            - SIN * x + COS * z
            );
    }

    Vector3 GetRotateZ( const float radians ) const // Roll
    {
    //  const float SIN = ( float )::sinf( radians );
    //  const float COS = ( float )::cosf( radians );
        float SIN, COS;
        sin_cos( radians, SIN, COS );
        return Vector3(
            + COS * x - SIN * y,
            + SIN * x + COS * y,
            + z
            );
    }

...

};




_________________
«Computer scientists deal with algorithms that you may call practical in theory but unpractical in practice.» © Timothy Gowers
Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 20 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  

cron