Claire's Game Engine
Full C++ Engine using OpenGL
Loading...
Searching...
No Matches
Vector3 Class Reference

Vector3 is a 3D vector utility class used for position, direction, and math operations in 3D space. More...

#include <Vector3.h>

Public Member Functions

 Vector3 ()
 Default constructor, initializes to (0,0,0).
 
 Vector3 (float x)
 Initializes all components to the same scalar value.
 
 Vector3 (float x, float y, float z)
 Initializes vector with individual float components.
 
 Vector3 (Vector2 vec2)
 Constructs a Vector3 from a Vector2, setting z to 0.
 
float Magnitude () const
 Computes the magnitude (length) of the vector.
 
float MagnitudeSqr () const
 Computes the squared magnitude.
 
float Magnitude2D () const
 Computes 2D magnitude using x and z components only.
 
float Magnitude2DSqr () const
 Computes squared 2D magnitude using x and z components.
 
void Normalize ()
 Normalizes the vector to unit length.
 
Vector3 Normalized ()
 Returns a normalized copy of the vector.
 
void Normalize2D ()
 Normalizes x and z components to unit length (2D).
 
Vector3 Normalized2D ()
 Returns a copy normalized in the XZ plane.
 
Vector3 Cross (const Vector3 &vector) const
 Computes the cross product between two vectors.
 
std::string ToString () const
 Converts the vector to a string representation.
 
const float * GetAsFloatPtr () const
 Gets a pointer to the vector's float data.
 
Vector3 operator- () const
 
void operator+= (const Vector3 &vector)
 
void operator-= (const Vector3 &vector)
 
void operator*= (const float value)
 
void operator/= (const float value)
 

Static Public Member Functions

static Vector3 Normalize (const Vector3 &vec)
 Static method to normalize a vector.
 
static float Dot (const Vector3 &left, const Vector3 &right)
 Computes the dot product between two vectors.
 
static float Distance (Vector3 &a, Vector3 &b)
 Computes the distance between two vectors.
 
static Vector3 Cross (const Vector3 &left, const Vector3 &right)
 Computes the cross product with another vector.
 
static Vector3 Lerp (const Vector3 &a, const Vector3 &b, float f)
 Linearly interpolates between two vectors.
 
static Vector3 Reflect (const Vector3 &v, const Vector3 &n)
 Reflects a vector across a normal.
 
static Vector3 Transform (Vector3 &vec, class Matrix4 &mat4, float w=1.0f)
 Transforms the vector by a 4x4 matrix (ignores perspective divide).
 
static Vector3 TransformWithPerspDiv (Vector3 &vec, class Matrix4 &mat4, float w=1.0f)
 Transforms the vector by a matrix and applies perspective divide.
 
static Vector3 Transform (const Vector3 &v, const class Quaternion &q)
 Transforms the vector by a quaternion rotation.
 

Public Attributes

float x = 0
 
float y = 0
 
float z = 0
 

Static Public Attributes

static const Vector3 Zero
 
static const Vector3 One
 
static const Vector3 Forward
 
static const Vector3 Up
 
static const Vector3 Right
 
static const Vector3 Backward
 
static const Vector3 Down
 
static const Vector3 Left
 
static const Vector3 Infinity
 
static const Vector3 MinusInfinity
 

Friends

bool operator== (const Vector3 &left, const Vector3 &right)
 
Vector3 operator+ (const Vector3 &left, const Vector3 &right)
 
Vector3 operator- (const Vector3 &left, const Vector3 &right)
 
Vector3 operator/ (const Vector3 &vector, const float value)
 
Vector3 operator* (const Vector3 &left, const Vector3 &right)
 
Vector3 operator* (const Vector3 &vector, const float value)
 
Vector3 operator* (const float value, const Vector3 &vector)
 

Detailed Description

Vector3 is a 3D vector utility class used for position, direction, and math operations in 3D space.

Constructor & Destructor Documentation

◆ Vector3() [1/4]

Vector3::Vector3 ( )

Default constructor, initializes to (0,0,0).

◆ Vector3() [2/4]

Vector3::Vector3 ( float x)

Initializes all components to the same scalar value.

Parameters
xScalar value to assign to x, y, and z.

◆ Vector3() [3/4]

Vector3::Vector3 ( float x,
float y,
float z )

Initializes vector with individual float components.

Parameters
xX component.
yY component.
zZ component.

◆ Vector3() [4/4]

Vector3::Vector3 ( Vector2 vec2)

Constructs a Vector3 from a Vector2, setting z to 0.

Parameters
vec22D vector to use for x and y.

Member Function Documentation

◆ Cross() [1/2]

Vector3 Vector3::Cross ( const Vector3 & left,
const Vector3 & right )
static

Computes the cross product with another vector.

Parameters
vectorOther vector.
Returns
Result of the cross product.

◆ Cross() [2/2]

Vector3 Vector3::Cross ( const Vector3 & vector) const

Computes the cross product between two vectors.

Parameters
leftFirst vector.
rightSecond vector.
Returns
Cross product result.

◆ Distance()

float Vector3::Distance ( Vector3 & a,
Vector3 & b )
static

Computes the distance between two vectors.

Parameters
aFirst point.
bSecond point.
Returns
Euclidean distance between a and b.

◆ Dot()

float Vector3::Dot ( const Vector3 & left,
const Vector3 & right )
static

Computes the dot product between two vectors.

Parameters
leftFirst vector.
rightSecond vector.
Returns
Dot product result.

◆ GetAsFloatPtr()

const float * Vector3::GetAsFloatPtr ( ) const

Gets a pointer to the vector's float data.

Returns
Pointer to x (useful for OpenGL).

◆ Lerp()

Vector3 Vector3::Lerp ( const Vector3 & a,
const Vector3 & b,
float f )
static

Linearly interpolates between two vectors.

Parameters
aStarting vector.
bEnding vector.
fInterpolation factor (0.0 to 1.0).
Returns
Interpolated vector.

◆ Magnitude()

float Vector3::Magnitude ( ) const

Computes the magnitude (length) of the vector.

Returns
Length of the vector.

◆ Magnitude2D()

float Vector3::Magnitude2D ( ) const

Computes 2D magnitude using x and z components only.

Returns
Length in the XZ plane.

◆ Magnitude2DSqr()

float Vector3::Magnitude2DSqr ( ) const

Computes squared 2D magnitude using x and z components.

Returns
Squared length in the XZ plane.

◆ MagnitudeSqr()

float Vector3::MagnitudeSqr ( ) const

Computes the squared magnitude.

Returns
Squared length of the vector.

◆ Normalize() [1/2]

void Vector3::Normalize ( )

Normalizes the vector to unit length.

◆ Normalize() [2/2]

Vector3 Vector3::Normalize ( const Vector3 & vec)
static

Static method to normalize a vector.

Parameters
vecVector to normalize.
Returns
Normalized vector.

◆ Normalize2D()

void Vector3::Normalize2D ( )

Normalizes x and z components to unit length (2D).

◆ Normalized()

Vector3 Vector3::Normalized ( )

Returns a normalized copy of the vector.

Returns
Unit-length vector in same direction.

◆ Normalized2D()

Vector3 Vector3::Normalized2D ( )

Returns a copy normalized in the XZ plane.

Returns
2D-normalized vector (y = 0).

◆ operator*=()

void Vector3::operator*= ( const float value)

◆ operator+=()

void Vector3::operator+= ( const Vector3 & vector)

◆ operator-()

Vector3 Vector3::operator- ( ) const

◆ operator-=()

void Vector3::operator-= ( const Vector3 & vector)

◆ operator/=()

void Vector3::operator/= ( const float value)

◆ Reflect()

Vector3 Vector3::Reflect ( const Vector3 & v,
const Vector3 & n )
static

Reflects a vector across a normal.

Parameters
vIncoming vector.
nNormal vector (should be normalized).
Returns
Reflected vector.

◆ ToString()

std::string Vector3::ToString ( ) const

Converts the vector to a string representation.

Returns
String in format (x , y , z).

◆ Transform() [1/2]

static Vector3 Vector3::Transform ( const Vector3 & v,
const class Quaternion & q )
static

Transforms the vector by a quaternion rotation.

Parameters
vVector to rotate.
qQuaternion to apply.
Returns
Rotated vector.

◆ Transform() [2/2]

Vector3 Vector3::Transform ( Vector3 & vec,
class Matrix4 & mat4,
float w = 1.0f )
static

Transforms the vector by a 4x4 matrix (ignores perspective divide).

Parameters
vecVector to transform.
mat4Matrix to use.
wHomogeneous coordinate (default: 1.0f).
Returns
Transformed vector.

◆ TransformWithPerspDiv()

Vector3 Vector3::TransformWithPerspDiv ( Vector3 & vec,
class Matrix4 & mat4,
float w = 1.0f )
static

Transforms the vector by a matrix and applies perspective divide.

Parameters
vecVector to transform.
mat4Matrix to use.
wHomogeneous coordinate (default: 1.0f).
Returns
Transformed and perspective-divided vector.

Friends And Related Symbol Documentation

◆ operator* [1/3]

Vector3 operator* ( const float value,
const Vector3 & vector )
friend

◆ operator* [2/3]

Vector3 operator* ( const Vector3 & left,
const Vector3 & right )
friend

◆ operator* [3/3]

Vector3 operator* ( const Vector3 & vector,
const float value )
friend

◆ operator+

Vector3 operator+ ( const Vector3 & left,
const Vector3 & right )
friend

◆ operator-

Vector3 operator- ( const Vector3 & left,
const Vector3 & right )
friend

◆ operator/

Vector3 operator/ ( const Vector3 & vector,
const float value )
friend

◆ operator==

bool operator== ( const Vector3 & left,
const Vector3 & right )
friend

Member Data Documentation

◆ Backward

const Vector3 Vector3::Backward
static

◆ Down

const Vector3 Vector3::Down
static

◆ Forward

const Vector3 Vector3::Forward
static

◆ Infinity

const Vector3 Vector3::Infinity
static

◆ Left

const Vector3 Vector3::Left
static

◆ MinusInfinity

const Vector3 Vector3::MinusInfinity
static

◆ One

const Vector3 Vector3::One
static

◆ Right

const Vector3 Vector3::Right
static

◆ Up

const Vector3 Vector3::Up
static

◆ x

float Vector3::x = 0

◆ y

float Vector3::y = 0

◆ z

float Vector3::z = 0

◆ Zero

const Vector3 Vector3::Zero
static

The documentation for this class was generated from the following files: