This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

 

  Math Library
  Submitted by



Here's my hat in the ring for math libraries. It's the math system for the engine I'm currently developing. I'm posting it in the hopes that someone else may find it usefull, but because I also want people to nitpick about it, any errors I may have missed, and ways to improve it. I am still working on the classes and adding to it, so any help is, of course, appreciated. The engine also works standalone, the project included compiles as a static library, which can easily be linked into a project. The include files are all linked through the GEKXMath.h file, so you only have to include it. The source is also commented (decently), compatible with (and a project file for) DoXygen source commentor. So far, it sports quite a few features:
  • 2D/3D/4D vectors (seperate classes, I opted to seperate them instead of templating them so that I didn't have to specialize anything). I have overloaded basic operators for conveniance (like plus, minus, multiple, divide. I didn't want to overload actuall functions (like dot or cross product), because I felt it gets confusing to overload these, everyone has a different spin on what symbols to use.
  • A quaternion class, with everything I've found that I've needed (so far at least).
  • A 4x4 matrix class (and conversions between quaternions and matrices in the classes), with rotations, scaling, etc (everything a matrix should be able to do).
  • A plane class, simple so far, I've just starting adding interesection between containment objects.
  • A bounding sphere, and an axis aligned bounding box class, with intersections tests against themselves and each other.
  • A 6 plane view frustum class, and a function to generate the planes from a projection matrix, and visibility checks against points, bounding spheres, and axis aligned bounding boxes.

  • Currently browsing [GEKxMath.zip] (24,025 bytes) - [GEKxMath/GEKxMath.h] - (4,017 bytes)

    /***************************************************/
    /* Game Excelleration Kit vX                       */
    /* Copyright (C) 2001 by HourGlass Software NonInc */
    /***************************************************/
    #ifndef __GEK_Math__
    #define __GEK_Math__
    /*! 
    	\author MoJo
        \file GEKXMath.h
        \brief GEKvX Math Main Header
    */

    /*! \mainpage GEKvX Math Library \section disclamer Disclamer Game Excelleration Kit vX

    <center><img src="..\..\hourglass.jpg" alt="HourGlass SoftWare"></center> Copyright (C) 2001 by HourGlass Software NonInc

    This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

    Please read the file LICENSE.TXT for additional details.

    <a href="http://www.doxygen.org>Doxygen</a> was used for all documentation. */


    #define _PI 3.14159265358979323846f //!< Extended floating point definition of PI #define _2_PI 6.28318530717958623200f //!< Extended floating point definition of (PI * 2) #define _PI_2 1.57079632679489661923f //!< Extended floating point definition of (PI / 2) #define _INV_PI 0.31830988618379069122f //!< Extended floating point definition of Inverse PI #define _DEGTORAD(x) (x * (_PI / 180.0f)) //!< Macro to convert degrees to radians #define _RADTODEG(x) (x * (180.0f / _PI)) //!< Macro to convert radians to degrees #define _EPSILON 1.0e-5f //!< Small offset to account for floating point error

    int __fastcall F2ID(float fValue); //!< Rounds a value down and returns the integer int __fastcall F2IU(float fValue); //!< Rounds a value up and returns the integer float __fastcall ABS(float fValue); //!< Returns the absolute value of a value float __fastcall SIN(float fValue); //!< Returns the sin of a value float __fastcall COS(float fValue); //!< Returns the cos of a value float TAN(float fValue); //!< Returns the tangent of a value float SQRT(float fValue); //!< Returns the square root of a value float __fastcall ISQRT(float fValue); //!< Returns the inverse square root of a value int NextSquare(int iValue); //!< Returns the next possible squared value

    template <typename CLASS> inline void SWAP(CLASS &kValueA, CLASS &kValueB) { CLASS kTemp = kValueA; kValueA = kValueB; kValueB = kTemp; }

    struct GEKVECTOR2; //!< 2D Vector Class struct GEKVECTOR3; //!< 3D Vector Class struct GEKVECTOR4; //!< 4D Vector Class struct GEKQUATERNION; //!< 4D Quaternion Class struct GEKMATRIX; //!< 4x4 Matrix Class struct GEKPLANE; //!< Standard Plane Equation typedef GEKVECTOR4 GEKCOLOR; //!< RGBA Color Class struct GEKSPHERE; //!< Basic Bounding Sphere struct GEKAABB; //!< Basic Axis Aligned Bounding Box struct GEKRAY; //!< Basic Ray class

    #include ".\Include\GEKRect.h" #include ".\Include\GEKVector2.h" #include ".\Include\GEKVector3.h" #include ".\Include\GEKVector4.h" #include ".\Include\GEKQuaternion.h" #include ".\Include\GEKMatrix.h" #include ".\Include\GEKPlane.h"

    #include ".\Include\GEKCurve.h" #include ".\Include\GEKContain.h" #include ".\Include\GEKFrustum.h"

    #include ".\Inline\GEKRect.inl" #include ".\Inline\GEKVector2.inl" #include ".\Inline\GEKVector3.inl" #include ".\Inline\GEKVector4.inl" #include ".\Inline\GEKQuaternion.inl" #include ".\Inline\GEKMatrix.inl" #include ".\Inline\GEKPlane.inl"

    #endif

    Currently browsing [GEKxMath.zip] (24,025 bytes) - [GEKxMath/Include/GEKContain.h] - (1,294 bytes)

    /***************************************************/
    /* Game Excelleration Kit vX                       */
    /* Copyright (C) 2001 by HourGlass Software NonInc */
    /***************************************************/
    #ifndef __GEKContain__
    #define __GEKContain__
    /*! 
    	\author MoJo
        \file GEKContain.h
        \brief Basic Containment
    */

    struct GEKRAY { public: GEKVECTOR3 m_kOrigin; GEKVECTOR3 m_kVector;

    public: GEKRAY(void); GEKRAY(const GEKVECTOR3 &kOrigin, const GEKVECTOR3 &kVector);

    inline float GetLength(void) { return m_kVector.GetLength(); }; inline GEKVECTOR3 GetEnd(void) { return (m_kOrigin + m_kVector); }; };

    struct GEKAABB { public: GEKVECTOR3 m_kMin; GEKVECTOR3 m_kMax;

    public: bool Collision(const GEKAABB &kAABB) const; bool Collision(const GEKSPHERE &kSphere) const;

    void Extend(const GEKVECTOR3 &kPoint); inline GEKVECTOR3 GetSize(void) { return (m_kMax - m_kMin); }; inline GEKVECTOR3 GetCenter(void) { return ((m_kMax + m_kMin) / 2.0f); }; };

    struct GEKSPHERE { public: GEKVECTOR3 m_kOrigin; float m_fRadius;

    public: GEKSPHERE(void); GEKSPHERE(const GEKVECTOR3 &kOrigin, float fRadius);

    bool Collision(const GEKAABB &kAABB) const; bool Collision(const GEKSPHERE &kSphere) const; };

    #endif

    Currently browsing [GEKxMath.zip] (24,025 bytes) - [GEKxMath/Include/GEKCurve.h] - (732 bytes)

    /***************************************************/
    /* Game Excelleration Kit vX                       */
    /* Copyright (C) 2001 by HourGlass Software NonInc */
    /***************************************************/
    #ifndef __GEKCurve__
    #define __GEKCurve__
    /*! 
    	\author MoJo
        \file GEKCurve.h
        \brief Curve Calculation
    */

    #include <math.h>

    template <class CLASS> CLASS GEKXBernstein(float fFactor, CLASS *pkBase) { CLASS kA(pkBase[0] * powf(fFactor, 3.0f)); CLASS kB(pkBase[1] * (3.0f * powf(fFactor, 2.0f) * (1.0f - fFactor))); CLASS kC(pkBase[2] * (3.0f * fFactor * powf((1.0f - fFactor), 2.0f))); CLASS kD(pkBase[3] * powf((1.0f - fFactor), 3.0f)); return ((kA + kB) + (kC + kD)); }

    #endif

    Currently browsing [GEKxMath.zip] (24,025 bytes) - [GEKxMath/Include/GEKFrustum.h] - (616 bytes)

    /***************************************************/
    /* Game Excelleration Kit vX                       */
    /* Copyright (C) 2001 by HourGlass Software NonInc */
    /***************************************************/
    #ifndef __GEKFrustum__
    #define __GEKFrustum__
    /*! 
    	\author MoJo
        \file GEKCollide.h
        \brief Basic View Frustum
    */

    struct GEKFRUSTUM { public: GEKPLANE m_akFrustum[6];

    public: void Generate(const GEKMATRIX &kMatrix);

    bool Visible(const GEKVECTOR3 &kPoint) const; bool Visible(const GEKSPHERE &kSphere) const; bool Visible(const GEKAABB &kAABB) const; };

    #endif

    Currently browsing [GEKxMath.zip] (24,025 bytes) - [GEKxMath/Include/GEKMatrix.h] - (5,569 bytes)

    /***************************************************/
    /* Game Excelleration Kit vX                       */
    /* Copyright (C) 2001 by HourGlass Software NonInc */
    /***************************************************/
    #ifndef __GEKMatrix__
    #define __GEKMatrix__
    /*! 
    	\author MoJo
        \file GEKMatrix.h
        \brief 4x4 Matrix Class
    */

    //! 4x4 Matrix Class struct GEKMATRIX { public: union { float d[16]; float m[4][4];

    struct { float _11, _12, _13, _14; float _21, _22, _23, _24; float _31, _32, _33, _34; float _41, _42, _43, _44; };

    struct { GEKVECTOR4 rx; GEKVECTOR4 ry; GEKVECTOR4 rz;

    union { struct { GEKVECTOR3 t; float w; };

    struct { float x, y, z, w; }; }; }; };

    public: inline GEKMATRIX(void); inline GEKMATRIX(float *pfData); inline GEKMATRIX(float _11, float _12, float _13, float _14, float _21, float _22, float _23, float _24, float _31, float _32, float _33, float _34, float _41, float _42, float _43, float _44); inline GEKMATRIX(const GEKMATRIX &kMatrix); inline GEKMATRIX(const GEKVECTOR3 &kAxis, float fAngle); inline GEKMATRIX(const GEKVECTOR3 &kEuler);

    //@{ //! Assignment Operators inline GEKMATRIX operator = (const GEKMATRIX &kMatrix); void operator *= (const GEKMATRIX &kMatrix); //@} //@{ //! Casting Operators inline float operator () (int iRow, int iCol) const; inline float operator () (int iRow, int iCol); inline operator const float * (void) const; inline operator float * (void); //@} //! Binary Operator GEKMATRIX operator * (const GEKMATRIX &kMatrix);

    inline void SetIdentity(void); //!< Sets a matrix to identity void SetEuler(const GEKVECTOR3 &kEuler); //!< Sets the XYZ euler angles of a rotation matrix void SetRotation(const GEKVECTOR3 &kAxis, float fAngle); //!< Sets the axial angle of a rotation matrix void SetQuaternion(const GEKQUATERNION &kQuat); //!< Converts a quaternion to a rotation matrix inline void SetScaling(const GEKVECTOR3 &kScale); //!< Sets the scaling factor of a matrix inline GEKVECTOR3 GetScaling(void); //!< Gets the scaling factor of a matrix void Transpose(void); //!< Transposes a matrix inline float Determinant(void); //!< Calculates the determinant of a matrix GEKMATRIX Inverse(void); //!< Calculates the inverse of a matrix void Invert(void); //!< Inverts a matrix //! Rotates a matrix preserving translations void Rotate(const GEKMATRIX &kMatrix);

    //@{ //! Rotates a point by a rotation matrix GEKVECTOR3 Rotate(const GEKVECTOR2 &kVector); GEKVECTOR3 Rotate(const GEKVECTOR3 &kVector); GEKVECTOR3 Rotate(const GEKVECTOR4 &kVector); GEKVECTOR3 Rotate(const GEKVECTOR2 &kVector) const; GEKVECTOR3 Rotate(const GEKVECTOR3 &kVector) const; GEKVECTOR3 Rotate(const GEKVECTOR4 &kVector) const; //@} //@{ //! Transforms a point by a matrix GEKVECTOR4 Transform(const GEKVECTOR2 &kVector); GEKVECTOR4 Transform(const GEKVECTOR3 &kVector); GEKVECTOR4 Transform(const GEKVECTOR4 &kVector); GEKVECTOR4 Transform(const GEKVECTOR2 &kVector) const; GEKVECTOR4 Transform(const GEKVECTOR3 &kVector) const; GEKVECTOR4 Transform(const GEKVECTOR4 &kVector) const; //@} };

    /*! \brief Creates an orthographic projection matrix \param fXMin [in] the minimum X value of the view volume \param fYMin [in] the minimum Y value of the view volume \param fXMax [in] the maximum X value of the view volume \param fYMax [in] the maximum Y value of the view volume \param fNear [in] the distance from the viewer to the near clipping plane \param fFar [in] the distance from the viewer to the far clipping plane

    The orthographic matrix is defined as follows: <pre> fX 0 0 fTX 0 fY 0 fTY 0 0 -fZ -fTZ 0 0 0 1

    fX = (2.0f / (fXMax - fXMin)) fY = (2.0f / (fYMax - fYMin)) fZ = (-2.0f / (fFar - fNear)) fTX = -((fXMax + fXMin) / (fXMax - fXMin)) fTY = -((fYMax + fYMin) / (fYMax - fYMin)) fTZ = -((fFar + fNear) / (fFar - fNear)) </pre> */
    GEKMATRIX GEKMatrixOrtho(float fXMin, float fYMin, float fXMax, float fYMax, float fNear, float fFar);

    /*! \brief Creates a perspective projection matrix \param fFOV [in] the field of view angle of the view volume \param fAspect [in] the width to height aspect ratio of the view volume \param fNear [in] the distance from the viewer to the near clipping plane \param fFar [in] the distance from the viewer to the far clipping plane The perspective matrix is defined as follows: <pre> fX 0 fA 0 0 fY fB 0 0 0 fC fD 0 0 -1 0

    fX = ((2.0f * fNear) / (fXMax - fXMin)) fY = ((2.0f * fNear) / (fYMax - fYMin)) fA = ((fXMax + fXMin) / (fXMax - fXMin)) fB = ((fYMax + fYMin) / (fYMax - fYMin)) fC = -((fFar + fNear) / (fFar - fNear)) fD = -((2.0f * fFar * fNear) / (fFar - fNear)) </pre> */
    GEKMATRIX GEKMatrixPerspective(float fFOV, float fAspect, float fNear, float fFar);

    /*! \brief Creates a matrix to mirror coordinates around a plane \param kPlane [in] the plane to mirror coordinates by */ GEKMATRIX GEKMatrixMirror(const GEKPLANE &kPlane);

    /*! \brief Creates a view matrix based upon a direction \param kOrigin [in] the origin of the view matrix \param kTarget [in] the target of the view matrix */ GEKMATRIX GEKMatrixTarget(const GEKVECTOR3 &kOrigin, const GEKVECTOR3 &kTarget);

    #endif

    Currently browsing [GEKxMath.zip] (24,025 bytes) - [GEKxMath/Include/GEKPlane.h] - (1,392 bytes)

    /***************************************************/
    /* Game Excelleration Kit vX                       */
    /* Copyright (C) 2001 by HourGlass Software NonInc */
    /***************************************************/
    #ifndef __GEKPlane__
    #define __GEKPlane__
    /*! 
    	\author MoJo
        \file GEKPlane.h
        \brief Plane Class
    */

    //! Generates a normal from 3 planar points GEKVECTOR3 GEKNormalize(const GEKVECTOR3 &kVector1, const GEKVECTOR3 &kVector2, const GEKVECTOR3 &kVector3);

    //! Plane Class struct GEKPLANE { public: GEKVECTOR3 n; float d;

    public: inline GEKPLANE(void); inline GEKPLANE(const GEKPLANE &kPlane); inline GEKPLANE(float fA, float fB, float fC, float fD); inline GEKPLANE(const GEKVECTOR3 &kNormal, float fDistance); GEKPLANE(const GEKVECTOR3 &kVector1, const GEKVECTOR3 &kVector2, const GEKVECTOR3 &kVector3);

    //! Assignment Operator inline GEKPLANE operator = (const GEKPLANE &kPlane);

    //! Returns the distance from a plane to a point float Distance(const GEKVECTOR3 &kPoint) const;

    /*! \brief Returns the if a ray and a plane intersect \param kRay [in] the ray to check against */ bool Intersect(const GEKRAY &kRay) const;

    /*! \brief Returns the point of intersection of a ray to a plane \param kRay [in] the ray to check against */ GEKVECTOR3 Intersection(const GEKRAY &kRay) const; };

    #endif

    Currently browsing [GEKxMath.zip] (24,025 bytes) - [GEKxMath/Include/GEKQuaternion.h] - (2,560 bytes)

    /***************************************************/
    /* Game Excelleration Kit vX                       */
    /* Copyright (C) 2001 by HourGlass Software NonInc */
    /***************************************************/
    #ifndef __GEKQuaternion__
    #define __GEKQuaternion__
    /*! 
        \author MoJo
        \file GEKQuaternion.h
        \brief 4D Quaternion Class
    */

    //! 4D Quaternion Class struct GEKQUATERNION { public: float x; float y; float z; float w;

    public: inline GEKQUATERNION(void); inline GEKQUATERNION(const GEKMATRIX &kMatrix); inline GEKQUATERNION(const GEKQUATERNION &kQuat); inline GEKQUATERNION(float fX, float fY, float fZ, float fW); inline GEKQUATERNION(const GEKVECTOR3 &kAxis, float fAngle); inline GEKQUATERNION(const GEKVECTOR3 &kEuler);

    //! Casting Operator inline operator float * (void);

    //@{ //! Assignment Operators void operator *= (const GEKQUATERNION &kQuat); inline GEKQUATERNION operator = (const GEKQUATERNION &kQuat); inline operator -= (const GEKQUATERNION &kQuat); inline operator += (const GEKQUATERNION &kQuat); inline operator /= (float fScalar); inline operator *= (float fScalar); //@} //@{ //! Binary Operators GEKQUATERNION operator * (const GEKQUATERNION &kQuat); inline GEKQUATERNION operator - (const GEKQUATERNION &kQuat); inline GEKQUATERNION operator + (const GEKQUATERNION &kQuat); inline GEKQUATERNION operator / (float fScalar); inline GEKQUATERNION operator * (float fScalar); //@} inline void SetIdentity(void); //!< Sets a quaternion to identity void SetEuler(const GEKVECTOR3 &kEuler); //!< Sets the XYZ euler angles of a quaternion void SetRotation(const GEKVECTOR3 &kAxis, float fAngle); //!< Sets the axial angle of a quaternion void SetMatrix(const GEKMATRIX &kMatrix); //!< Converts a rotation matrix to a quaternion inline float Dot(const GEKQUATERNION &kQuat); //!< Calculates the dot product of two quaternions inline float LengthSqr(void); //!< Calculates the square length of a quaternion inline float Length(void); //!< Calculates the length of a quaternion GEKQUATERNION Normal(void); //!< Calculates the normal of a quaternion void Normalize(void); //!< Normalizes a quaternion GEKQUATERNION Slerp(const GEKQUATERNION &kQuat, float fFactor); //!< Calculates the spherical interpolation of two quaternions GEKQUATERNION Lerp(const GEKQUATERNION &kQuat, float fFactor); //!< Calculates the linear interpolation of two quaternions };

    #endif

    Currently browsing [GEKxMath.zip] (24,025 bytes) - [GEKxMath/Include/GEKRect.h] - (658 bytes)

    /***************************************************/
    /* Game Excelleration Kit vX                       */
    /* Copyright (C) 2001 by HourGlass Software NonInc */
    /***************************************************/
    #ifndef __GEKRect__
    #define __GEKRect__
    /*! 
        \author MoJo
        \file GEKRect.h
        \brief Basic Rectangle Class
    */

    //! Basic Rectangle Class struct GEKRECT { public: float m_fXPos; float m_fYPos; float m_fXSize; float m_fYSize;

    public: inline GEKRECT(void); inline GEKRECT(int iXPos, int iYPos, int iXSize, int iYSize); inline GEKRECT(float fXPos, float fYPos, float fXSize, float fYSize); };

    #endif

    Currently browsing [GEKxMath.zip] (24,025 bytes) - [GEKxMath/Include/GEKVector2.h] - (3,477 bytes)

    /***************************************************/
    /* Game Excelleration Kit vX                       */
    /* Copyright (C) 2001 by HourGlass Software NonInc */
    /***************************************************/
    #ifndef __GEKVECTOR2__
    #define __GEKVECTOR2__
    /*! 
        \author MoJo
        \file GEKVector2.h
        \brief 2D Vector Class
    */

    //! 2D Vector Class struct GEKVECTOR2 { public: union { float xy[2]; float uv[2]; struct { float x, y; }; struct { float u, v; }; };

    public: inline GEKVECTOR2(void); inline GEKVECTOR2(const GEKVECTOR2 &kVector); inline GEKVECTOR2(const GEKVECTOR3 &kVector); inline GEKVECTOR2(const GEKVECTOR4 &kVector); inline GEKVECTOR2(float fX, float fY);

    inline float &operator [] (int iAxis); inline bool operator < (const GEKVECTOR2 &kVector) const; inline bool operator > (const GEKVECTOR2 &kVector) const;

    //@{ //! Casting operator inline operator float * (void); inline operator const float * (void) const; //@} //@{ //! Assignment operators inline GEKVECTOR2 operator = (const GEKVECTOR2 &kVector); inline GEKVECTOR2 operator = (const GEKVECTOR3 &kVector); inline GEKVECTOR2 operator = (const GEKVECTOR4 &kVector); inline operator -= (const GEKVECTOR2 &kVector); inline operator += (const GEKVECTOR2 &kVector); inline operator /= (float fScalar); inline operator *= (float fScalar); //@} //@{ //! Binary Operators inline GEKVECTOR2 operator - (const GEKVECTOR2 &kVector) const; inline GEKVECTOR2 operator + (const GEKVECTOR2 &kVector) const; inline GEKVECTOR2 operator - (const GEKVECTOR2 &kVector); inline GEKVECTOR2 operator + (const GEKVECTOR2 &kVector); inline GEKVECTOR2 operator / (float fScalar) const; inline GEKVECTOR2 operator * (float fScalar) const; inline GEKVECTOR2 operator / (float fScalar); inline GEKVECTOR2 operator * (float fScalar); //@} inline void SetLength(float fLength); //!< Sets the length of a vector inline float GetLengthSqr(void) const; //!< Calculates the square length of a vector inline float GetLengthSqr(void); //!< Calculates the square length of a vector inline float GetLength(void) const; //!< Calculates the length of a vector inline float GetLength(void); //!< Calculates the length of a vector inline float GetAngle(const GEKVECTOR2 &kVector); //!< Calculates the angle between two vectors inline float Distance(const GEKVECTOR2 &kVector) const; //!< Calculates the distance to a vector inline float Distance(const GEKVECTOR2 &kVector); //!< Calculates the distance to a vector inline float Dot(const GEKVECTOR2 &kVector) const; //!< Calculates the dot product of two vectors inline float Dot(const GEKVECTOR2 &kVector); //!< Calculates the dot product of two vectors inline GEKVECTOR2 Lerp(const GEKVECTOR2 &kVector, float fFactor) const; //!< Calculates the linear interpolation of two vectors inline GEKVECTOR2 Lerp(const GEKVECTOR2 &kVector, float fFactor); //!< Calculates the linear interpolation of two vectors inline GEKVECTOR2 Normal(void) const; //!< Calculates the normal of a vector inline GEKVECTOR2 Normal(void); //!< Calculates the normal of a vector inline void Normalize(void); //!< Normalizes a vector };

    inline GEKVECTOR2 operator - (const GEKVECTOR2 &kVector); //!< Negative Operator inline GEKVECTOR2 operator - (GEKVECTOR2 &kVector); //!< Negative Operator #endif

    Currently browsing [GEKxMath.zip] (24,025 bytes) - [GEKxMath/Include/GEKVector3.h] - (3,676 bytes)

    /***************************************************/
    /* Game Excelleration Kit vX                       */
    /* Copyright (C) 2001 by HourGlass Software NonInc */
    /***************************************************/
    #ifndef __GEKVECTOR3__
    #define __GEKVECTOR3__
    /*! 
        \author MoJo
        \file GEKVector3.h
        \brief 3D Vector Class
    */

    //! 3D Vector Class struct GEKVECTOR3 { public: union { float xyz[3]; struct { float x, y, z; }; };

    public: inline GEKVECTOR3(void); inline GEKVECTOR3(const GEKVECTOR2 &kVector); inline GEKVECTOR3(const GEKVECTOR3 &kVector); inline GEKVECTOR3(const GEKVECTOR4 &kVector); inline GEKVECTOR3(float fX, float fY, float fZ);

    inline float &operator [] (int iAxis); inline bool operator < (const GEKVECTOR3 &kVector) const; inline bool operator > (const GEKVECTOR3 &kVector) const;

    //@{ //! Casting operator inline operator float * (void); inline operator const float * (void) const; //@} //@{ //! Assignment operators inline GEKVECTOR3 operator = (const GEKVECTOR2 &kVector); inline GEKVECTOR3 operator = (const GEKVECTOR3 &kVector); inline GEKVECTOR3 operator = (const GEKVECTOR4 &kVector); inline operator -= (const GEKVECTOR3 &kVector); inline operator += (const GEKVECTOR3 &kVector); inline operator /= (float fScalar); inline operator *= (float fScalar); //@} //@{ //! Binary Operators inline GEKVECTOR3 operator - (const GEKVECTOR3 &kVector) const; inline GEKVECTOR3 operator + (const GEKVECTOR3 &kVector) const; inline GEKVECTOR3 operator - (const GEKVECTOR3 &kVector); inline GEKVECTOR3 operator + (const GEKVECTOR3 &kVector); inline GEKVECTOR3 operator / (float fScalar) const; inline GEKVECTOR3 operator * (float fScalar) const; inline GEKVECTOR3 operator / (float fScalar); inline GEKVECTOR3 operator * (float fScalar); //@} inline void SetLength(float fLength); //!< Sets the length of a vector inline float GetLengthSqr(void) const; //!< Calculates the square length of a vector inline float GetLengthSqr(void); //!< Calculates the square length of a vector inline float GetLength(void) const; //!< Calculates the length of a vector inline float GetLength(void); //!< Calculates the length of a vector inline float GetAngle(const GEKVECTOR3 &kVector); //!< Calculates the angle between two vectors inline GEKVECTOR3 Cross(const GEKVECTOR3 &kVector) const; //!< Calculates the cross product of two vectors inline GEKVECTOR3 Cross(const GEKVECTOR3 &kVector); //!< Calculates the cross product of two vectors inline float Distance(const GEKVECTOR3 &kVector) const; //!< Calculates the distance to a vector inline float Distance(const GEKVECTOR3 &kVector); //!< Calculates the distance to a vector inline float Dot(const GEKVECTOR3 &kVector) const; //!< Calculates the dot product of two vectors inline float Dot(const GEKVECTOR3 &kVector); //!< Calculates the dot product of two vectors inline GEKVECTOR3 Lerp(const GEKVECTOR3 &kVector, float fFactor) const; //!< Calculates the linear interpolation of two vectors inline GEKVECTOR3 Lerp(const GEKVECTOR3 &kVector, float fFactor); //!< Calculates the linear interpolation of two vectors inline GEKVECTOR3 Normal(void) const; //!< Calculates the normal of a vector inline GEKVECTOR3 Normal(void); //!< Calculates the normal of a vector inline void Normalize(void); //!< Normalizes a vector };

    inline GEKVECTOR3 operator - (const GEKVECTOR3 &kVector); //!< Negative Operator inline GEKVECTOR3 operator - (GEKVECTOR3 &kVector); //!< Negative Operator #endif

    Currently browsing [GEKxMath.zip] (24,025 bytes) - [GEKxMath/Include/GEKVector4.h] - (3,731 bytes)

    /***************************************************/
    /* Game Excelleration Kit vX                       */
    /* Copyright (C) 2001 by HourGlass Software NonInc */
    /***************************************************/
    #ifndef __GEKVECTOR4__
    #define __GEKVECTOR4__
    /*! 
        \author MoJo
        \file GEKVector4.h
        \brief 4D Vector Class
    */

    //! 4D Vector Class struct GEKVECTOR4 { public: union { float xyzw[4]; float rgba[4]; struct { float x, y, z, w; }; struct { float r, g, b, a; }; };

    public: inline GEKVECTOR4(void); inline GEKVECTOR4(const GEKVECTOR2 &kVector); inline GEKVECTOR4(const GEKVECTOR3 &kVector); inline GEKVECTOR4(const GEKVECTOR4 &kVector); inline GEKVECTOR4(float fX, float fY, float fZ, float fW);

    inline float &operator [] (int iAxis); inline bool operator < (const GEKVECTOR4 &kVector) const; inline bool operator > (const GEKVECTOR4 &kVector) const;

    //@{ //! Casting operator inline operator float * (void); inline operator const float * (void) const; //@} //@{ //! Assignment operators inline GEKVECTOR4 operator = (const GEKVECTOR2 &kVector); inline GEKVECTOR4 operator = (const GEKVECTOR3 &kVector); inline GEKVECTOR4 operator = (const GEKVECTOR4 &kVector); inline operator -= (const GEKVECTOR4 &kVector); inline operator += (const GEKVECTOR4 &kVector); inline operator /= (float fScalar); inline operator *= (float fScalar); //@} //@{ //! Binary Operators inline GEKVECTOR4 operator - (const GEKVECTOR4 &kVector) const; inline GEKVECTOR4 operator + (const GEKVECTOR4 &kVector) const; inline GEKVECTOR4 operator - (const GEKVECTOR4 &kVector); inline GEKVECTOR4 operator + (const GEKVECTOR4 &kVector); inline GEKVECTOR4 operator / (float fScalar) const; inline GEKVECTOR4 operator * (float fScalar) const; inline GEKVECTOR4 operator / (float fScalar); inline GEKVECTOR4 operator * (float fScalar); //@} inline void SetLength(float fLength); //!< Sets the length of a vector inline float GetLengthSqr(void) const; //!< Calculates the square length of a vector inline float GetLengthSqr(void); //!< Calculates the square length of a vector inline float GetLength(void) const; //!< Calculates the length of a vector inline float GetLength(void); //!< Calculates the length of a vector inline float GetAngle(const GEKVECTOR4 &kVector); //!< Calculates the angle between two vectors inline GEKVECTOR4 Cross(const GEKVECTOR4 &kVector) const; //!< Calculates the cross product of two vectors inline GEKVECTOR4 Cross(const GEKVECTOR4 &kVector); //!< Calculates the cross product of two vectors inline float Distance(const GEKVECTOR4 &kVector) const; //!< Calculates the distance to a vector inline float Distance(const GEKVECTOR4 &kVector); //!< Calculates the distance to a vector inline float Dot(const GEKVECTOR4 &kVector) const; //!< Calculates the dot product of two vectors inline float Dot(const GEKVECTOR4 &kVector); //!< Calculates the dot product of two vectors inline GEKVECTOR4 Lerp(const GEKVECTOR4 &kVector, float fFactor) const; //!< Calculates the linear interpolation of two vectors inline GEKVECTOR4 Lerp(const GEKVECTOR4 &kVector, float fFactor); //!< Calculates the linear interpolation of two vectors inline GEKVECTOR4 Normal(void) const; //!< Calculates the normal of a vector inline GEKVECTOR4 Normal(void); //!< Calculates the normal of a vector void Normalize(void); //!< Normalizes a vector };

    inline GEKVECTOR4 operator - (const GEKVECTOR4 &kVector); //!< Negative Operator inline GEKVECTOR4 operator - (GEKVECTOR4 &kVector); //!< Negative Operator #endif

    Currently browsing [GEKxMath.zip] (24,025 bytes) - [GEKxMath/Source/GEKContain.cpp] - (1,972 bytes)

    #include "..\GEKXMath.h"

    GEKRAY::GEKRAY(void) { }

    GEKRAY::GEKRAY(const GEKVECTOR3 &kOrigin, const GEKVECTOR3 &kVector) { m_kOrigin = kOrigin; m_kVector = kVector; }

    bool GEKAABB::Collision(const GEKAABB &kAABB) const { if(m_kMin > kAABB.m_kMax) return false; if(m_kMax < kAABB.m_kMin) return false; return true; }

    inline bool GEK_AABB_Sphere_Collision(const GEKSPHERE &kSphere, const GEKAABB &kAABB) { float fDistance = 0.0f; for(int iAxis = 0; iAxis < 3; iAxis++) { if(kSphere.m_kOrigin[iAxis] < kAABB.m_kMin[iAxis]) { float fValue = (kSphere.m_kOrigin[iAxis] - kAABB.m_kMin[iAxis]); fDistance += (fValue * fValue); } else if(kSphere.m_kOrigin[iAxis] > kAABB.m_kMax[iAxis]) { float fValue = (kSphere.m_kOrigin[iAxis] - kAABB.m_kMax[iAxis]); fDistance += (fValue * fValue); } }

    if(fDistance <= (kSphere.m_fRadius * kSphere.m_fRadius)) return true; return false; }

    bool GEKAABB::Collision(const GEKSPHERE &kSphere) const { return GEK_AABB_Sphere_Collision(kSphere, *this); }

    void GEKAABB::Extend(const GEKVECTOR3 &kPoint) { if(kPoint.x < m_kMin.x) m_kMin.x = kPoint.x; if(kPoint.y < m_kMin.y) m_kMin.y = kPoint.y; if(kPoint.z < m_kMin.z) m_kMin.z = kPoint.z; if(kPoint.x > m_kMax.x) m_kMax.x = kPoint.x; if(kPoint.y > m_kMax.y) m_kMax.y = kPoint.y; if(kPoint.z > m_kMax.z) m_kMax.z = kPoint.z; }

    GEKSPHERE::GEKSPHERE(void) { m_fRadius = 0.0f; }

    GEKSPHERE::GEKSPHERE(const GEKVECTOR3 &kOrigin, float fRadius) { m_kOrigin = kOrigin; m_fRadius = fRadius; }

    bool GEKSPHERE::Collision(const GEKAABB &kAABB) const { return GEK_AABB_Sphere_Collision(*this, kAABB); }

    bool GEKSPHERE::Collision(const GEKSPHERE &kSphere) const { GEKVECTOR3 kDistance(m_kOrigin - kSphere.m_kOrigin); float fDistance = kDistance.GetLengthSqr();

    float fRadius = (m_fRadius + kSphere.m_fRadius); if(fDistance <= (fRadius * fRadius)) return true; return false; }

    Currently browsing [GEKxMath.zip] (24,025 bytes) - [GEKxMath/Source/GEKCurve.cpp] - (26 bytes)

    #include "..\GEKXMath.h"
     

    Currently browsing [GEKxMath.zip] (24,025 bytes) - [GEKxMath/Source/GEKFrustum.cpp] - (2,436 bytes)

    #include "..\GEKXMath.h"

    void GEKFRUSTUM::Generate(const GEKMATRIX &kMatrix) { // Extract the near plane m_akFrustum[0].n.x = (kMatrix[ 3] + kMatrix[ 2]); m_akFrustum[0].n.y = (kMatrix[ 7] + kMatrix[ 6]); m_akFrustum[0].n.z = (kMatrix[11] + kMatrix[10]); m_akFrustum[0].d = (kMatrix[15] + kMatrix[14]); // Extract the far plane m_akFrustum[1].n.x = (kMatrix[ 3] - kMatrix[ 2]); m_akFrustum[1].n.y = (kMatrix[ 7] - kMatrix[ 6]); m_akFrustum[1].n.z = (kMatrix[11] - kMatrix[10]); m_akFrustum[1].d = (kMatrix[15] - kMatrix[14]);

    // Extract the left plane m_akFrustum[2].n.x = (kMatrix[ 3] + kMatrix[ 0]); m_akFrustum[2].n.y = (kMatrix[ 7] + kMatrix[ 4]); m_akFrustum[2].n.z = (kMatrix[11] + kMatrix[ 8]); m_akFrustum[2].d = (kMatrix[15] + kMatrix[12]);

    // Extract the right plane m_akFrustum[3].n.x = (kMatrix[ 3] - kMatrix[ 0]); m_akFrustum[3].n.y = (kMatrix[ 7] - kMatrix[ 4]); m_akFrustum[3].n.z = (kMatrix[11] - kMatrix[ 8]); m_akFrustum[3].d = (kMatrix[15] - kMatrix[12]);

    // Extract the bottom plane m_akFrustum[4].n.x = (kMatrix[ 3] + kMatrix[ 1]); m_akFrustum[4].n.y = (kMatrix[ 7] + kMatrix[ 5]); m_akFrustum[4].n.z = (kMatrix[11] + kMatrix[ 9]); m_akFrustum[4].d = (kMatrix[15] + kMatrix[13]);

    // Extract the top plane m_akFrustum[5].n.x = (kMatrix[ 3] - kMatrix[ 1]); m_akFrustum[5].n.y = (kMatrix[ 7] - kMatrix[ 5]); m_akFrustum[5].n.z = (kMatrix[11] - kMatrix[ 9]); m_akFrustum[5].d = (kMatrix[15] - kMatrix[13]); }

    bool GEKFRUSTUM::Visible(const GEKVECTOR3 &kPoint) const { for(int iIndex = 0; iIndex < 6; iIndex++) { if(m_akFrustum[iIndex].Distance(kPoint) <= 0.0f) return false; }

    return true; }

    bool GEKFRUSTUM::Visible(const GEKSPHERE &kSphere) const { for(int iIndex = 0; iIndex < 6; iIndex++) { float fDistance = m_akFrustum[iIndex].Distance(kSphere.m_kOrigin); if(fDistance <= -kSphere.m_fRadius) return false; }

    return true; }

    bool GEKFRUSTUM::Visible(const GEKAABB &kAABB) const { GEKVECTOR3 kPoint; for(int iIndex = 0; iIndex < 6; iIndex++) { kPoint.x = ((m_akFrustum[iIndex].n.x < 0.0f) ? kAABB.m_kMin.x : kAABB.m_kMax.x); kPoint.y = ((m_akFrustum[iIndex].n.y < 0.0f) ? kAABB.m_kMin.y : kAABB.m_kMax.y); kPoint.z = ((m_akFrustum[iIndex].n.z < 0.0f) ? kAABB.m_kMin.z : kAABB.m_kMax.z); if(m_akFrustum[iIndex].Distance(kPoint) <= 0.0f) return false; }

    return true; }

    Currently browsing [GEKxMath.zip] (24,025 bytes) - [GEKxMath/Source/GEKMatrix.cpp] - (14,221 bytes)

    #include "..\GEKXMath.h"

    GEKMATRIX GEKMATRIX::operator * (const GEKMATRIX &kMatrix) { return GEKMATRIX(((_11 * kMatrix._11) + (_12 * kMatrix._21) + (_13 * kMatrix._31) + (_14 * kMatrix._41)), ((_11 * kMatrix._12) + (_12 * kMatrix._22) + (_13 * kMatrix._32) + (_14 * kMatrix._42)), ((_11 * kMatrix._13) + (_12 * kMatrix._23) + (_13 * kMatrix._33) + (_14 * kMatrix._43)), ((_11 * kMatrix._14) + (_12 * kMatrix._24) + (_13 * kMatrix._34) + (_14 * kMatrix._44)),

    ((_21 * kMatrix._11) + (_22 * kMatrix._21) + (_23 * kMatrix._31) + (_24 * kMatrix._41)), ((_21 * kMatrix._12) + (_22 * kMatrix._22) + (_23 * kMatrix._32) + (_24 * kMatrix._42)), ((_21 * kMatrix._13) + (_22 * kMatrix._23) + (_23 * kMatrix._33) + (_24 * kMatrix._43)), ((_21 * kMatrix._14) + (_22 * kMatrix._24) + (_23 * kMatrix._34) + (_24 * kMatrix._44)),

    ((_31 * kMatrix._11) + (_32 * kMatrix._21) + (_33 * kMatrix._31) + (_34 * kMatrix._41)), ((_31 * kMatrix._12) + (_32 * kMatrix._22) + (_33 * kMatrix._32) + (_34 * kMatrix._42)), ((_31 * kMatrix._13) + (_32 * kMatrix._23) + (_33 * kMatrix._33) + (_34 * kMatrix._43)), ((_31 * kMatrix._14) + (_32 * kMatrix._24) + (_33 * kMatrix._34) + (_34 * kMatrix._44)),

    ((_41 * kMatrix._11) + (_42 * kMatrix._21) + (_43 * kMatrix._31) + (_44 * kMatrix._41)), ((_41 * kMatrix._12) + (_42 * kMatrix._22) + (_43 * kMatrix._32) + (_44 * kMatrix._42)), ((_41 * kMatrix._13) + (_42 * kMatrix._23) + (_43 * kMatrix._33) + (_44 * kMatrix._43)), ((_41 * kMatrix._14) + (_42 * kMatrix._24) + (_43 * kMatrix._34) + (_44 * kMatrix._44))); }

    void GEKMATRIX::operator *= (const GEKMATRIX &kMatrix) { *this = GEKMATRIX(((_11 * kMatrix._11) + (_12 * kMatrix._21) + (_13 * kMatrix._31) + (_14 * kMatrix._41)), ((_11 * kMatrix._12) + (_12 * kMatrix._22) + (_13 * kMatrix._32) + (_14 * kMatrix._42)), ((_11 * kMatrix._13) + (_12 * kMatrix._23) + (_13 * kMatrix._33) + (_14 * kMatrix._43)), ((_11 * kMatrix._14) + (_12 * kMatrix._24) + (_13 * kMatrix._34) + (_14 * kMatrix._44)),

    ((_21 * kMatrix._11) + (_22 * kMatrix._21) + (_23 * kMatrix._31) + (_24 * kMatrix._41)), ((_21 * kMatrix._12) + (_22 * kMatrix._22) + (_23 * kMatrix._32) + (_24 * kMatrix._42)), ((_21 * kMatrix._13) + (_22 * kMatrix._23) + (_23 * kMatrix._33) + (_24 * kMatrix._43)), ((_21 * kMatrix._14) + (_22 * kMatrix._24) + (_23 * kMatrix._34) + (_24 * kMatrix._44)),

    ((_31 * kMatrix._11) + (_32 * kMatrix._21) + (_33 * kMatrix._31) + (_34 * kMatrix._41)), ((_31 * kMatrix._12) + (_32 * kMatrix._22) + (_33 * kMatrix._32) + (_34 * kMatrix._42)), ((_31 * kMatrix._13) + (_32 * kMatrix._23) + (_33 * kMatrix._33) + (_34 * kMatrix._43)), ((_31 * kMatrix._14) + (_32 * kMatrix._24) + (_33 * kMatrix._34) + (_34 * kMatrix._44)),

    ((_41 * kMatrix._11) + (_42 * kMatrix._21) + (_43 * kMatrix._31) + (_44 * kMatrix._41)), ((_41 * kMatrix._12) + (_42 * kMatrix._22) + (_43 * kMatrix._32) + (_44 * kMatrix._42)), ((_41 * kMatrix._13) + (_42 * kMatrix._23) + (_43 * kMatrix._33) + (_44 * kMatrix._43)), ((_41 * kMatrix._14) + (_42 * kMatrix._24) + (_43 * kMatrix._34) + (_44 * kMatrix._44))); }

    void GEKMATRIX::Rotate(const GEKMATRIX &kMatrix) { GEKMATRIX kBase(*this); _11 = ((kBase._11 * kMatrix._11) + (kBase._12 * kMatrix._21) + (kBase._13 * kMatrix._31)); _12 = ((kBase._11 * kMatrix._12) + (kBase._12 * kMatrix._22) + (kBase._13 * kMatrix._32)); _13 = ((kBase._11 * kMatrix._13) + (kBase._12 * kMatrix._23) + (kBase._13 * kMatrix._33));

    _21 = ((kBase._21 * kMatrix._11) + (kBase._22 * kMatrix._21) + (kBase._23 * kMatrix._31)); _22 = ((kBase._21 * kMatrix._12) + (kBase._22 * kMatrix._22) + (kBase._23 * kMatrix._32)); _23 = ((kBase._21 * kMatrix._13) + (kBase._22 * kMatrix._23) + (kBase._23 * kMatrix._33));

    _31 = ((kBase._31 * kMatrix._11) + (kBase._32 * kMatrix._21) + (kBase._33 * kMatrix._31)); _32 = ((kBase._31 * kMatrix._12) + (kBase._32 * kMatrix._22) + (kBase._33 * kMatrix._32)); _33 = ((kBase._31 * kMatrix._13) + (kBase._32 * kMatrix._23) + (kBase._33 * kMatrix._33)); }

    void GEKMATRIX::SetEuler(const GEKVECTOR3 &kEuler) { SetIdentity(); float fCOSx = COS(kEuler.x); float fSINx = SIN(kEuler.x); float fCOSy = COS(kEuler.y); float fSINy = SIN(kEuler.y); float fCOSz = COS(kEuler.z); float fSINz = SIN(kEuler.z);

    _11 = (fCOSy * fCOSz); _12 = (fCOSy * fSINz); _13 = (-fSINy);

    float fSINxSINy = (fSINx * fSINy); float fCOSxSINy = (fCOSx * fSINy);

    _21 = ((fSINxSINy * fCOSz) - (fCOSx * fSINz)); _22 = ((fSINxSINy * fSINz) + (fCOSx * fCOSz)); _23 = (fSINx * fCOSy);

    _31 = ((fCOSxSINy * fCOSz) + (fSINx * fSINz)); _32 = ((fCOSxSINy * fSINz) - (fSINx * fCOSz)); _33 = (fCOSx * fCOSy); }

    void GEKMATRIX::SetRotation(const GEKVECTOR3 &kAxis, float fAngle) { float fCOS = COS(fAngle); float fSIN = SIN(fAngle); GEKVECTOR3 kNormal(((GEKVECTOR3 &)kAxis).Normal());

    _11 = ((kNormal.x * kNormal.x) * (1.0f - fCOS) + fCOS); _12 = ((kNormal.x * kNormal.y) * (1.0f - fCOS) - (kNormal.z * fSIN)); _13 = ((kNormal.x * kNormal.z) * (1.0f - fCOS) + (kNormal.y * fSIN));

    _21 = ((kNormal.y * kNormal.x) * (1.0f - fCOS) + (kNormal.z * fSIN)); _22 = ((kNormal.y * kNormal.y) * (1.0f - fCOS) + fCOS); _23 = ((kNormal.y * kNormal.z) * (1.0f - fCOS) - (kNormal.x * fSIN));

    _31 = ((kNormal.z * kNormal.x) * (1.0f - fCOS) - (kNormal.y * fSIN)); _32 = ((kNormal.z * kNormal.y) * (1.0f - fCOS) + (kNormal.x * fSIN)); _33 = ((kNormal.z * kNormal.z) * (1.0f - fCOS) + fCOS); }

    void GEKMATRIX::SetQuaternion(const GEKQUATERNION &kQuat) { float f2X = (kQuat.x * 2.0f); float f2Y = (kQuat.y * 2.0f); float f2Z = (kQuat.z * 2.0f); float fWX = (kQuat.w * f2X); float fWY = (kQuat.w * f2Y); float fWZ = (kQuat.w * f2Z); float fXX = (kQuat.x * f2X); float fXY = (kQuat.x * f2Y); float fXZ = (kQuat.x * f2Z); float fYY = (kQuat.y * f2Y); float fYZ = (kQuat.y * f2Z); float fZZ = (kQuat.z * f2Z); _11 = (1.0f - fYY - fZZ); _12 = (fXY + fWZ); _13 = (fXZ - fWY);

    _21 = (fXY - fWZ); _22 = (1.0f - fXX - fZZ); _23 = (fYZ + fWX);

    _31 = (fXZ + fWY); _32 = (fYZ - fWX); _33 = (1.0f - fXX - fYY); }

    void GEKMATRIX::Transpose(void) { GEKMATRIX kMatrix(*this); _11 = kMatrix._11; _12 = kMatrix._21; _13 = kMatrix._31; _14 = kMatrix._41; _21 = kMatrix._12; _22 = kMatrix._22; _23 = kMatrix._32; _24 = kMatrix._42; _31 = kMatrix._13; _32 = kMatrix._23; _33 = kMatrix._33; _34 = kMatrix._43; _41 = kMatrix._14; _42 = kMatrix._24; _43 = kMatrix._34; _44 = kMatrix._44; }

    GEKMATRIX GEKMATRIX::Inverse(void) { /* if((ABS(_44 - 1.0f) > 0.001f)|| (ABS(_14) > 0.001f)|| (ABS(_24) > 0.001f)|| (ABS(_34) > 0.001f)) return *this;*/

    GEKMATRIX kResult; float fDetInverse = (1.0f / Determinant()); kResult._11 = ( fDetInverse * ((_22 * _33) - (_23 * _32))); kResult._12 = (-fDetInverse * ((_12 * _33) - (_13 * _32))); kResult._13 = ( fDetInverse * ((_12 * _23) - (_13 * _22))); kResult._14 = 0.0f;

    kResult._21 = (-fDetInverse * ((_21 * _33) - (_23 * _31))); kResult._22 = ( fDetInverse * ((_11 * _33) - (_13 * _31))); kResult._23 = (-fDetInverse * ((_11 * _23) - (_13 * _21))); kResult._24 = 0.0f;

    kResult._31 = ( fDetInverse * ((_21 * _32) - (_22 * _31))); kResult._32 = (-fDetInverse * ((_11 * _32) - (_12 * _31))); kResult._33 = ( fDetInverse * ((_11 * _22) - (_12 * _21))); kResult._34 = 0.0f;

    kResult._41 = -((_41 * kResult._11) + (_42 * kResult._21) + (_43 * kResult._31)); kResult._42 = -((_41 * kResult._12) + (_42 * kResult._22) + (_43 * kResult._32)); kResult._43 = -((_41 * kResult._13) + (_42 * kResult._23) + (_43 * kResult._33)); kResult._44 = 1.0f; return kResult; }

    void GEKMATRIX::Invert(void) { *this = Inverse(); }

    GEKVECTOR3 GEKMATRIX::Rotate(const GEKVECTOR2 &kVector) { return GEKVECTOR3(((kVector.x * _11) + (kVector.y * _21)), ((kVector.x * _12) + (kVector.y * _22)), ((kVector.x * _13) + (kVector.y * _23))); }

    GEKVECTOR3 GEKMATRIX::Rotate(const GEKVECTOR3 &kVector) { return GEKVECTOR3(((kVector.x * _11) + (kVector.y * _21) + (kVector.z * _31)), ((kVector.x * _12) + (kVector.y * _22) + (kVector.z * _32)), ((kVector.x * _13) + (kVector.y * _23) + (kVector.z * _33))); }

    GEKVECTOR3 GEKMATRIX::Rotate(const GEKVECTOR4 &kVector) { return GEKVECTOR3(((kVector.x * _11) + (kVector.y * _21) + (kVector.z * _31)), ((kVector.x * _12) + (kVector.y * _22) + (kVector.z * _32)), ((kVector.x * _13) + (kVector.y * _23) + (kVector.z * _33))); }

    GEKVECTOR3 GEKMATRIX::Rotate(const GEKVECTOR2 &kVector) const { return GEKVECTOR3(((kVector.x * _11) + (kVector.y * _21)), ((kVector.x * _12) + (kVector.y * _22)), ((kVector.x * _13) + (kVector.y * _23))); }

    GEKVECTOR3 GEKMATRIX::Rotate(const GEKVECTOR3 &kVector) const { return GEKVECTOR3(((kVector.x * _11) + (kVector.y * _21) + (kVector.z * _31)), ((kVector.x * _12) + (kVector.y * _22) + (kVector.z * _32)), ((kVector.x * _13) + (kVector.y * _23) + (kVector.z * _33))); }

    GEKVECTOR3 GEKMATRIX::Rotate(const GEKVECTOR4 &kVector) const { return GEKVECTOR3(((kVector.x * _11) + (kVector.y * _21) + (kVector.z * _31)), ((kVector.x * _12) + (kVector.y * _22) + (kVector.z * _32)), ((kVector.x * _13) + (kVector.y * _23) + (kVector.z * _33))); }

    GEKVECTOR4 GEKMATRIX::Transform(const GEKVECTOR2 &kVector) { return GEKVECTOR4(((kVector.x * _11) + (kVector.y * _21) + _41), ((kVector.x * _12) + (kVector.y * _22) + _42), ((kVector.x * _13) + (kVector.y * _23) + _43), ((kVector.x * _14) + (kVector.y * _24) + _44)); }

    GEKVECTOR4 GEKMATRIX::Transform(const GEKVECTOR3 &kVector) { return GEKVECTOR4(((kVector.x * _11) + (kVector.y * _21) + (kVector.z * _31) + _41), ((kVector.x * _12) + (kVector.y * _22) + (kVector.z * _32) + _42), ((kVector.x * _13) + (kVector.y * _23) + (kVector.z * _33) + _43), ((kVector.x * _14) + (kVector.y * _24) + (kVector.z * _34) + _44)); }

    GEKVECTOR4 GEKMATRIX::Transform(const GEKVECTOR4 &kVector) { return GEKVECTOR4(((kVector.x * _11) + (kVector.y * _21) + (kVector.z * _31) + (kVector.w * _41)), ((kVector.x * _12) + (kVector.y * _22) + (kVector.z * _32) + (kVector.w * _42)), ((kVector.x * _13) + (kVector.y * _23) + (kVector.z * _33) + (kVector.w * _43)), ((kVector.x * _14) + (kVector.y * _24) + (kVector.z * _34) + (kVector.w * _44))); }

    GEKVECTOR4 GEKMATRIX::Transform(const GEKVECTOR2 &kVector) const { return GEKVECTOR4(((kVector.x * _11) + (kVector.y * _21) + _41), ((kVector.x * _12) + (kVector.y * _22) + _42), ((kVector.x * _13) + (kVector.y * _23) + _43), ((kVector.x * _14) + (kVector.y * _24) + _44)); }

    GEKVECTOR4 GEKMATRIX::Transform(const GEKVECTOR3 &kVector) const { return GEKVECTOR4(((kVector.x * _11) + (kVector.y * _21) + (kVector.z * _31) + _41), ((kVector.x * _12) + (kVector.y * _22) + (kVector.z * _32) + _42), ((kVector.x * _13) + (kVector.y * _23) + (kVector.z * _33) + _43), ((kVector.x * _14) + (kVector.y * _24) + (kVector.z * _34) + _44)); }

    GEKVECTOR4 GEKMATRIX::Transform(const GEKVECTOR4 &kVector) const { return GEKVECTOR4(((kVector.x * _11) + (kVector.y * _21) + (kVector.z * _31) + (kVector.w * _41)), ((kVector.x * _12) + (kVector.y * _22) + (kVector.z * _32) + (kVector.w * _42)), ((kVector.x * _13) + (kVector.y * _23) + (kVector.z * _33) + (kVector.w * _43)), ((kVector.x * _14) + (kVector.y * _24) + (kVector.z * _34) + (kVector.w * _44))); }

    GEKMATRIX GEKMatrixOrtho(float fXMin, float fYMin, float fXMax, float fYMax, float fNear, float fFar) { float fX = (2.0f / (fXMax - fXMin)); float fY = (2.0f / (fYMax - fYMin)); float fZ = (-2.0f / (fFar - fNear)); float fTX = -((fXMax + fXMin) / (fXMax - fXMin)); float fTY = -((fYMax + fYMin) / (fYMax - fYMin)); float fTZ = -((fFar + fNear) / (fFar - fNear));

    return GEKMATRIX( fX, 0.0f, 0.0f, 0.0f, 0.0f, -fY, 0.0f, 0.0f, 0.0f, 0.0f, -fZ, 0.0f, fTX, -fTY, -fTZ, 1.0f); }

    GEKMATRIX GEKMatrixPerspective(float fFOV, float fAspect, float fNear, float fFar) { float fYMax = (fNear * (SIN(fFOV / 2.0f) / COS(fFOV / 2.0f))); float fXMax = (fYMax * fAspect); float fYMin = -fYMax; float fXMin = -fXMax;

    float fX = ((2.0f * fNear) / (fXMax - fXMin)); float fY = ((2.0f * fNear) / (fYMax - fYMin)); float fA = ((fXMax + fXMin) / (fXMax - fXMin)); float fB = ((fYMax + fYMin) / (fYMax - fYMin)); float fC = -((fFar + fNear) / (fFar - fNear)); float fD = -((2.0f * fFar * fNear) / (fFar - fNear));

    return GEKMATRIX( fX, 0.0f, fA, 0.0f, 0.0f, fY, fB, 0.0f, 0.0f, 0.0f, -fC, -fD, 0.0f, 0.0f,-1.0f, 0.0f); }

    GEKMATRIX GEKMatrixMirror(const GEKPLANE &kPlane) { float fX = kPlane.n.x; float fY = kPlane.n.y; float fZ = kPlane.n.z; float fD = kPlane.d;

    return GEKMATRIX((0.0f - (2.0f * fX * fX)), (0.0f - (2.0f * fX * fY)), (0.0f - (2.0f * fX * fZ)), 0.0f, (0.0f - (2.0f * fY * fX)), (0.0f - (2.0f * fY * fY)), (0.0f - (2.0f * fY * fZ)), 0.0f, (0.0f - (2.0f * fZ * fX)), (0.0f - (2.0f * fZ * fY)), (0.0f - (2.0f * fZ * fZ)), 0.0f, (0.0f - (2.0f * fD * fX)), (0.0f - (2.0f * fD * fY)), (0.0f - (2.0f * fD * fZ)), 1.0f); }

    GEKMATRIX GEKMatrixTarget(const GEKVECTOR3 &kOrigin, const GEKVECTOR3 &kTarget) { GEKVECTOR3 kZAxis(kTarget - kOrigin); kZAxis.Normalize();

    GEKVECTOR3 m_kYAxis(0.0f, 1.0f, 0.0f); GEKVECTOR3 kXAxis(m_kYAxis.Cross(kZAxis)); kXAxis.Normalize();

    GEKVECTOR3 kYAxis(kZAxis.Cross(kXAxis));

    return GEKMATRIX(kXAxis.x, kXAxis.y, kXAxis.z, -kXAxis.Dot(kOrigin), kYAxis.x, kYAxis.y, kYAxis.z, -kYAxis.Dot(kOrigin), kZAxis.x, kZAxis.y, kZAxis.z, -kZAxis.Dot(kOrigin), 0.0f, 0.0f, 0.0f, 1.0f); }

    Currently browsing [GEKxMath.zip] (24,025 bytes) - [GEKxMath/Source/GEKPlane.cpp] - (1,137 bytes)

    #include "..\GEKXMath.h"

    GEKVECTOR3 GEKNormalize(const GEKVECTOR3 &kVector1, const GEKVECTOR3 &kVector2, const GEKVECTOR3 &kVector3) { GEKVECTOR3 kVectorA(kVector2 - kVector1); GEKVECTOR3 kVectorB(kVector3 - kVector1); GEKVECTOR3 kCross(kVectorA.Cross(kVectorB)); return kCross.Normal(); }

    GEKPLANE::GEKPLANE(const GEKVECTOR3 &kVector1, const GEKVECTOR3 &kVector2, const GEKVECTOR3 &kVector3) { n = GEKNormalize(kVector1, kVector2, kVector3); d = -n.Dot(kVector1); }

    float GEKPLANE::Distance(const GEKVECTOR3 &kPoint) const { return (n.Dot(kPoint) + d); }

    bool GEKPLANE::Intersect(const GEKRAY &kRay) const { float fDistanceA = Distance(kRay.m_kOrigin); float fDistanceB = Distance(kRay.m_kOrigin + kRay.m_kVector); if((fDistanceA > 0.0f)&&(fDistanceB < 0.0f)) return true; if((fDistanceA < 0.0f)&&(fDistanceB > 0.0f)) return true; return false; }

    GEKVECTOR3 GEKPLANE::Intersection(const GEKRAY &kRay) const { float fNumer = n.Dot(kRay.m_kOrigin); float fDenom = n.Dot(kRay.m_kVector); float fInter = (d - (fNumer / fDenom)); return (kRay.m_kOrigin + (kRay.m_kVector * fInter)); }

    Currently browsing [GEKxMath.zip] (24,025 bytes) - [GEKxMath/Source/GEKQuaternion.cpp] - (4,447 bytes)

    #include "..\GEKXMath.h"

    GEKQUATERNION GEKQUATERNION::operator * (const GEKQUATERNION &kQuat) { GEKQUATERNION kResult; kResult.x = ((w * kQuat.x) + (x * kQuat.w) + (y * kQuat.z) - (z * kQuat.y)); kResult.y = ((w * kQuat.y) - (x * kQuat.z) + (y * kQuat.w) + (z * kQuat.x)); kResult.z = ((w * kQuat.z) + (x * kQuat.y) - (y * kQuat.x) + (z * kQuat.w)); kResult.w = ((w * kQuat.w) - (x * kQuat.x) - (y * kQuat.y) - (z * kQuat.z)); return kResult; }

    void GEKQUATERNION::operator *= (const GEKQUATERNION &kQuat) { GEKQUATERNION kResult; kResult.x = ((w * kQuat.x) + (x * kQuat.w) + (y * kQuat.z) - (z * kQuat.y)); kResult.y = ((w * kQuat.y) - (x * kQuat.z) + (y * kQuat.w) + (z * kQuat.x)); kResult.z = ((w * kQuat.z) + (x * kQuat.y) - (y * kQuat.x) + (z * kQuat.w)); kResult.w = ((w * kQuat.w) - (x * kQuat.x) - (y * kQuat.y) - (z * kQuat.z)); *this = kResult; }

    void GEKQUATERNION::SetEuler(const GEKVECTOR3 &kEuler) { float fSINx = SIN(kEuler.x * 0.5f); float fSINy = SIN(kEuler.y * 0.5f); float fSINz = SIN(kEuler.z * 0.5f); float fCOSx = COS(kEuler.x * 0.5f); float fCOSy = COS(kEuler.y * 0.5f); float fCOSz = COS(kEuler.z * 0.5f); float fSINyCOSz = (fSINy * fCOSz); float fSINySINz = (fSINy * fSINz); float fCOSyCOSz = (fCOSy * fCOSz); float fCOSySINz = (fCOSy * fSINz);

    x = ((fSINx * fCOSyCOSz) - (fCOSx * fSINySINz)); y = ((fSINx * fCOSySINz) + (fCOSx * fSINyCOSz)); z = ((fCOSx * fCOSySINz) - (fSINx * fSINyCOSz)); w = ((fCOSx * fCOSyCOSz) + (fSINx * fSINySINz)); }

    void GEKQUATERNION::SetRotation(const GEKVECTOR3 &kAxis, float fAngle) { GEKVECTOR3 kNormal(((GEKVECTOR3 &)kAxis).Normal()); float fSinA = SIN(fAngle * 0.5f);

    x = (kNormal.x * fSinA); y = (kNormal.y * fSinA); z = (kNormal.z * fSinA); w = COS(fAngle * 0.5f); }

    void GEKQUATERNION::SetMatrix(const GEKMATRIX &kMatrix) { static int aiNext[3] = {1, 2, 0}; float fDiagonal = (kMatrix._11 + kMatrix._22 + kMatrix._33); if(fDiagonal > 0.0f) { float fSQRT = SQRT(fDiagonal + 1.0f); w = (fSQRT / 2.0f);

    fSQRT = (0.5f / fSQRT); x = ((kMatrix._23 - kMatrix._32) * fSQRT); y = ((kMatrix._31 - kMatrix._13) * fSQRT); z = ((kMatrix._12 - kMatrix._21) * fSQRT); } else { int iCount = 0; if(kMatrix._22 > kMatrix._11) iCount = 1; if(kMatrix._33 > kMatrix.m[iCount][iCount]) iCount = 2;

    float afTemp[4]; int iTemp1 = aiNext[iCount]; int iTemp2 = aiNext[iTemp1];

    float fSQRT = SQRT((kMatrix.m[iCount][iCount] - (kMatrix.m[iTemp1][iTemp1] + kMatrix.m[iTemp2][iTemp2])) + 1.0f); afTemp[iCount] = (fSQRT * 0.5f);

    if(fSQRT != 0.0) fSQRT = (0.5f / fSQRT); afTemp[3] = ((kMatrix.m[iTemp1][iTemp2] - kMatrix.m[iTemp2][iTemp1]) * fSQRT); afTemp[iTemp1] = ((kMatrix.m[iCount][iTemp1] + kMatrix.m[iTemp1][iCount]) * fSQRT); afTemp[iTemp2] = ((kMatrix.m[iCount][iTemp2] + kMatrix.m[iTemp2][iCount]) * fSQRT); x = afTemp[0]; y = afTemp[1]; z = afTemp[2]; w = afTemp[3]; } }

    GEKQUATERNION GEKQUATERNION::Normal(void) { float fLength = Length(); return GEKQUATERNION((x / fLength), (y / fLength), (z / fLength), (w / fLength)); }

    void GEKQUATERNION::Normalize(void) { *this = Normal(); }

    #include <math.h> GEKQUATERNION GEKQUATERNION::Slerp(const GEKQUATERNION &kQuat, float fFactor) { float fDot = Dot(kQuat); float fSqrt = SQRT(ABS(1.0f - fDot * fDot)); if(ABS(fSqrt) < (_EPSILON * 100.0f)) return *this; if(fDot < 0.0f) fDot = -fDot;

    float fAngle = 0.0f; if(fDot != 0.0f) fAngle = (float)atanf(fSqrt / fDot); fSqrt = 1 / fSqrt; GEKQUATERNION kResult; kResult.x = ((x * fSqrt * SIN(fAngle * (1.0f - fFactor))) + (kQuat.x * fSqrt * SIN(fAngle * fFactor))); kResult.y = ((y * fSqrt * SIN(fAngle * (1.0f - fFactor))) + (kQuat.y * fSqrt * SIN(fAngle * fFactor))); kResult.z = ((z * fSqrt * SIN(fAngle * (1.0f - fFactor))) + (kQuat.z * fSqrt * SIN(fAngle * fFactor))); kResult.w = ((w * fSqrt * SIN(fAngle * (1.0f - fFactor))) + (kQuat.w * fSqrt * SIN(fAngle * fFactor))); return kResult; }

    GEKQUATERNION GEKQUATERNION::Lerp(const GEKQUATERNION &kQuat, float fFactor) { GEKQUATERNION kResult; kResult.x = (x + ((kQuat.x - x) * fFactor)); kResult.y = (y + ((kQuat.y - y) * fFactor)); kResult.z = (z + ((kQuat.z - z) * fFactor)); kResult.w = (w + ((kQuat.w - w) * fFactor)); return kResult; }

    Currently browsing [GEKxMath.zip] (24,025 bytes) - [GEKxMath/Source/GEKVector2.cpp] - (24 bytes)

    #include "..\GEKXMath.h" 

    Currently browsing [GEKxMath.zip] (24,025 bytes) - [GEKxMath/Source/GEKVector3.cpp] - (24 bytes)

    #include "..\GEKXMath.h" 

    Currently browsing [GEKxMath.zip] (24,025 bytes) - [GEKxMath/Source/GEKVector4.cpp] - (24 bytes)

    #include "..\GEKXMath.h" 

    Currently browsing [GEKxMath.zip] (24,025 bytes) - [GEKxMath/Source/GEKXMath.cpp] - (3,522 bytes)

    #include "..\GEKXMath.h"

    int NextSquare(int iValue) { long Squared = 1; while(Squared < iValue) Squared <<= 1; if(Squared == iValue) return Squared; else return (Squared >> 1); }

    __declspec(naked) int __fastcall F2ID(float fValue) { __asm { push ebp mov ebp,esp push ecx push ebx push esi mov eax,dword ptr [ebp+8] mov ebx,eax sar ebx,31 mov edx,eax and edx,2147483647 sar edx,23 add edx,-127 mov ecx,edx sar ecx,31 not ecx mov dword ptr [ebp-4],ecx mov ecx,dword ptr [ebp-4] and ecx,edx push ecx mov ecx,31 pop esi sub ecx,esi mov esi,1 shl esi,cl dec esi and eax,8388607 mov ecx,31 sub ecx,edx mov edx,eax or edx,8388608 shl edx,8 shr edx,cl and edx,dword ptr [ebp-4] xor edx,ebx shl eax,8 test eax,esi sete al and eax,1 and ebx,eax add edx,ebx mov eax,edx pop esi pop ebx pop ecx pop ebp ret 4 }; }

    __declspec(naked) int __fastcall F2IU(float fValue) { __asm { push ebp mov ebp,esp push ecx push ebx push esi mov eax,dword ptr [ebp+8] xor eax,-2147483648 mov edx,eax sar edx,31 mov dword ptr [ebp-4],edx mov edx,eax and edx,2147483647 sar edx,23 add edx,-127 mov ebx,edx sar ebx,31 not ebx mov ecx,ebx and ecx,edx push ecx mov ecx,31 pop esi sub ecx,esi mov esi,1 shl esi,cl dec esi and eax,8388607 mov ecx,31 sub ecx,edx mov edx,eax or edx,8388608 shl edx,8 shr edx,cl and edx,ebx xor edx,dword ptr [ebp-4] shl eax,8 test eax,esi sete al and eax,1 and eax,dword ptr [ebp-4] and ebx,eax add edx,ebx neg edx mov eax,edx pop esi pop ebx pop ecx pop ebp ret 4 }; }

    __declspec(naked) float __fastcall ABS(float fValue) { __asm { fld dword ptr[esp + 4] fabs ret 4 }; }

    __declspec(naked) float __fastcall SIN(float fValue) { __asm { fld dword ptr[esp + 4] fsin ret 4 }; }

    __declspec(naked) float __fastcall COS(float fValue) { __asm { fld dword ptr[esp + 4] fcos ret 4 }; }

    float TAN(float fValue) { return (SIN(fValue) / COS(fValue)); }

    float SQRT(float fValue) { return (fValue ? (fValue * ISQRT(fValue)) : 0.0f); }

    __declspec(naked) float __fastcall ISQRT(float fValue) { __asm { mov eax, 0be6eb508h mov dword ptr[esp-12],03fc00000h sub eax, dword ptr[esp + 4] sub dword ptr[esp+4], 800000h shr eax, 1 mov dword ptr[esp - 8], eax

    fld dword ptr[esp - 8] fmul st, st fld dword ptr[esp - 8] fxch st(1) fmul dword ptr[esp + 4] fld dword ptr[esp - 12] fld st(0) fsub st,st(2)

    fld st(1) fxch st(1) fmul st(3),st fmul st(3),st fmulp st(4),st fsub st,st(2)

    fmul st(2),st fmul st(3),st fmulp st(2),st fxch st(1) fsubp st(1),st

    fmulp st(1), st ret 4 } }

    The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

     

    Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
    Please read our Terms, Conditions, and Privacy information.