2004-05-26 23:18:29 +02:00
|
|
|
// Vector2D.h
|
|
|
|
//
|
|
|
|
// 2-dimensional vector class, primarily for use by simulation code.
|
|
|
|
|
2007-05-07 18:33:24 +02:00
|
|
|
#ifndef INCLUDED_VECTOR2D
|
|
|
|
#define INCLUDED_VECTOR2D
|
2004-05-26 23:18:29 +02:00
|
|
|
|
2006-06-02 03:01:39 +02:00
|
|
|
#include <math.h>
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "maths/Vector3D.h"
|
2004-05-26 23:18:29 +02:00
|
|
|
|
|
|
|
class CVector2D
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
float x;
|
|
|
|
float y;
|
2004-08-03 01:14:54 +02:00
|
|
|
inline CVector2D() { x = 0.0f; y = 0.0f; }
|
|
|
|
inline CVector2D( float _x, float _y )
|
2004-05-26 23:18:29 +02:00
|
|
|
{
|
|
|
|
x = _x; y = _y;
|
|
|
|
}
|
2004-08-03 01:14:54 +02:00
|
|
|
inline CVector2D( const CVector3D& v3 ) // This is done an awful lot.
|
|
|
|
{
|
|
|
|
x = v3.X; y = v3.Z;
|
|
|
|
}
|
2004-11-15 23:30:01 +01:00
|
|
|
inline operator CVector3D() const
|
|
|
|
{
|
|
|
|
return( CVector3D( x, 0, y ) );
|
|
|
|
}
|
2006-04-20 03:33:57 +02:00
|
|
|
inline bool operator==( const CVector2D& rhs ) const
|
|
|
|
{
|
|
|
|
return( x == rhs.x && y == rhs.y );
|
|
|
|
}
|
2007-05-02 14:07:08 +02:00
|
|
|
static inline float Dot( const CVector2D& u, const CVector2D& v )
|
2004-05-26 23:18:29 +02:00
|
|
|
{
|
|
|
|
return( u.x * v.x + u.y * v.y );
|
|
|
|
}
|
2004-05-29 05:32:33 +02:00
|
|
|
static inline float betadot( const CVector2D& u, const CVector2D& v )
|
|
|
|
{
|
|
|
|
// Beta-dot product. I have no idea if that's its correct name
|
|
|
|
// but use of it tends to simplify collision formulae.
|
2005-09-30 02:59:42 +02:00
|
|
|
// At the moment I think all of my code uses separate vectors
|
2004-05-29 05:32:33 +02:00
|
|
|
// and dots them together, though.
|
|
|
|
return( u.x * v.y - u.y * v.x );
|
|
|
|
}
|
|
|
|
inline CVector2D beta() const
|
|
|
|
{
|
|
|
|
return( CVector2D( y, -x ) );
|
|
|
|
}
|
2007-05-02 14:07:08 +02:00
|
|
|
inline float Dot( const CVector2D& u ) const
|
2004-05-26 23:18:29 +02:00
|
|
|
{
|
2007-05-02 14:07:08 +02:00
|
|
|
return( Dot( *this, u ) );
|
2004-05-26 23:18:29 +02:00
|
|
|
}
|
2004-05-29 05:32:33 +02:00
|
|
|
inline float betadot( const CVector2D& u ) const
|
|
|
|
{
|
|
|
|
return( betadot( *this, u ) );
|
|
|
|
}
|
2004-05-26 23:18:29 +02:00
|
|
|
inline CVector2D operator+( const CVector2D& u ) const
|
|
|
|
{
|
|
|
|
return( CVector2D( x + u.x, y + u.y ) );
|
|
|
|
}
|
|
|
|
inline CVector2D operator-( const CVector2D& u ) const
|
|
|
|
{
|
|
|
|
return( CVector2D( x - u.x, y - u.y ) );
|
|
|
|
}
|
|
|
|
inline CVector2D& operator+=( const CVector2D& u )
|
|
|
|
{
|
|
|
|
x += u.x; y += u.y;
|
|
|
|
return( *this );
|
|
|
|
}
|
|
|
|
inline CVector2D& operator-=( const CVector2D& u )
|
|
|
|
{
|
|
|
|
x -= u.x; y -= u.y;
|
|
|
|
return( *this );
|
|
|
|
}
|
|
|
|
inline CVector2D operator*( const float scale ) const
|
|
|
|
{
|
|
|
|
return( CVector2D( x * scale, y * scale ) );
|
|
|
|
}
|
2004-05-29 05:32:33 +02:00
|
|
|
inline CVector2D operator/( const float scale ) const
|
|
|
|
{
|
|
|
|
return( CVector2D( x / scale, y / scale ) );
|
|
|
|
}
|
2004-05-26 23:18:29 +02:00
|
|
|
inline CVector2D& operator*=( const float scale )
|
|
|
|
{
|
|
|
|
x *= scale; y *= scale;
|
|
|
|
return( *this );
|
|
|
|
}
|
2004-05-29 05:32:33 +02:00
|
|
|
inline CVector2D& operator/=( const float scale )
|
|
|
|
{
|
|
|
|
x /= scale; y /= scale;
|
|
|
|
return( *this );
|
|
|
|
}
|
2007-05-02 14:07:08 +02:00
|
|
|
inline float Length() const
|
2004-05-26 23:18:29 +02:00
|
|
|
{
|
|
|
|
return( sqrt( x * x + y * y ) );
|
|
|
|
}
|
2006-07-09 00:40:01 +02:00
|
|
|
inline float length2() const
|
|
|
|
{
|
|
|
|
return( x * x + y * y );
|
|
|
|
}
|
2007-05-02 14:07:08 +02:00
|
|
|
CVector2D Normalize() const
|
2004-05-26 23:18:29 +02:00
|
|
|
{
|
2007-05-02 14:07:08 +02:00
|
|
|
float l = Length();
|
2004-05-26 23:18:29 +02:00
|
|
|
if( l < 0.00001 ) return( CVector2D( 1.0f, 0.0f ) );
|
|
|
|
l = 1 / l;
|
|
|
|
return( CVector2D( x * l, y * l ) );
|
|
|
|
}
|
|
|
|
inline bool within( const float dist ) const
|
|
|
|
{
|
|
|
|
return( ( x * x + y * y ) <= ( dist * dist ) );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2004-06-02 17:31:55 +02:00
|
|
|
#endif
|