Vector3
Description
The Vector3 class provides a comprehensive object-oriented interface for working with 3D vectors. It encapsulates x, y, and z components and offers a wide range of vector operations including creation, manipulation, and mathematical functions.
Namespace
Universe.Core
Properties
| Property | Type | Description |
|---|---|---|
| x | float | The X component of the vector |
| y | float | The Y component of the vector |
| z | float | The Z component of the vector |
Methods
Vector3
void Vector3(float xV, float yV, float zV)
Constructor that initializes the vector with specific values.
Parameters
xV: Value for the X componentyV: Value for the Y componentzV: Value for the Z component
New
Vector3 New(float xV, float yV, float zV)
Sets the vector components to new values and returns the vector.
Parameters
xV: Value for the X componentyV: Value for the Y componentzV: Value for the Z component
Returns
- The vector itself (for method chaining)
Zero
Vector3 Zero()
Sets the vector to (0, 0, 0) and returns it.
Returns
- The vector itself (for method chaining)
Right
Vector3 Right()
Sets the vector to (1, 0, 0) and returns it.
Returns
- The vector itself (for method chaining)
Left
Vector3 Left()
Sets the vector to (-1, 0, 0) and returns it.
Returns
- The vector itself (for method chaining)
Up
Vector3 Up()
Sets the vector to (0, 1, 0) and returns it.
Returns
- The vector itself (for method chaining)
Down
Vector3 Down()
Sets the vector to (0, -1, 0) and returns it.
Returns
- The vector itself (for method chaining)
Forward
Vector3 Forward()
Sets the vector to (0, 0, 1) and returns it.
Returns
- The vector itself (for method chaining)
Backward
Vector3 Backward()
Sets the vector to (0, 0, -1) and returns it.
Returns
- The vector itself (for method chaining)
One
Vector3 One()
Sets the vector to (1, 1, 1) and returns it.
Returns
- The vector itself (for method chaining)
FromVector
Vector3 FromVector(vector3 vector)
Sets the vector components from an existing vector3 and returns it.
Parameters
vector: Source vector to copy from
Returns
- The vector itself (for method chaining)
Add
Vector3 Add(Vector3 vector)
Adds another vector to this one and returns the result.
Parameters
vector: Vector to add
Returns
- The vector itself (for method chaining)
ToVector
vector3 ToVector()
Converts the Vector3 object to a native vector3 value.
Returns
- A native vector3 value
Normalize
Vector3 Normalize()
Normalizes the vector (scales to unit length) and returns it.
Returns
- The vector itself (for method chaining)
Normalized
Vector3 Normalized()
Returns a new normalized copy of the vector without modifying the original.
Returns
- A new Vector3 with unit length
Magnitude
float Magnitude()
Calculates the length/magnitude of the vector.
Returns
- The length of the vector
Cross
Vector3 Cross(Vector3 vector)
Calculates the cross product with another vector.
Parameters
vector: The vector to cross with
Returns
- A new Vector3 representing the cross product
Project
Vector3 Project(Vector3 normal)
Projects this vector onto another vector.
Parameters
normal: The vector to project onto
Returns
- A new Vector3 representing the projection
Dot
float Dot(Vector3 vector)
Calculates the dot product with another vector.
Parameters
vector: The vector to dot with
Returns
- The scalar dot product
Lerp
Vector3 Lerp(Vector3 to, float phase)
Linearly interpolates to another vector by the specified amount.
Parameters
to: Target vectorphase: Interpolation amount (0-1)
Returns
- A new Vector3 with the interpolated values
SmoothLerp
Vector3 SmoothLerp(Vector3 to, float phase)
Smoothly interpolates to another vector using a smooth step function.
Parameters
to: Target vectorphase: Interpolation amount (0-1)
Returns
- A new Vector3 with the smoothly interpolated values
Example Usage
// Create vectors
Vector3 position = new Vector3(10, 5, 3);
Vector3 direction = new Vector3().Forward(); // (0, 0, 1)
// Basic operations
Vector3 targetPosition = new Vector3(15, 8, 12);
Vector3 moveDirection = new Vector3();
moveDirection.FromVector(targetPosition.ToVector() - position.ToVector());
moveDirection.Normalize(); // Convert to unit vector
// Calculate distance
float distance = position.ToVector() - targetPosition.ToVector()).Magnitude();
// Vector operations
Vector3 up = new Vector3().Up();
Vector3 right = new Vector3().Right();
Vector3 forward = right.Cross(up); // Cross product to find forward
// Dot product for angle calculation
float dotProduct = moveDirection.Dot(forward);
float angle = Math.Acos(dotProduct) * 57.2957795; // Convert to degrees
// Projection
Vector3 movementOnXZ = moveDirection.Project(new Vector3().New(1, 0, 1).Normalize());
// Interpolation for smooth movement
Vector3 newPosition = position.Lerp(targetPosition, 0.1); // Move 10% toward target
// Create a smooth path
Vector3 smoothPoint = position.SmoothLerp(targetPosition, 0.5);
Technical Details
- The
Vector3class is an object-oriented wrapper around the nativevector3type - Many methods return the instance itself to allow method chaining
- Methods that create new vectors (like
Normalized()) create newVector3instances - The class uses
VectorUtilfor many mathematical operations - Certain methods modify the vector in place (
Normalize(),Add(), etc.) - Other methods return new vectors without modifying the original (
Normalized(),Cross(), etc.) - The
ToVector()method converts to the nativevector3type for compatibility with other functions
Use Cases
- Representing positions in 3D space
- Defining directions and orientations
- Calculating movements and trajectories
- Performing geometric calculations
- Working with physics forces and velocities
- Defining offsets and displacements
- Interpolating between positions for smooth movement
Notes
- For best performance, reuse Vector3 instances when possible
- The convention follows a right-handed coordinate system
- Use the appropriate directional constants (Up, Forward, etc.) for standard directions
- Be careful not to normalize zero vectors, as this will result in NaN values
- The
SmoothLerpfunction provides a more natural-looking interpolation than linear interpolation - For frequent vector math, consider using the native
vector3type directly withVectorUtil
Related Components
- Used throughout the framework for positions, directions, and orientations
- Works with
Quaternion.csfor rotations - Compatible with most components that work with positions and directions
Dependencies
- Uses
VectorUtilfor vector operations - Uses
Mathfor mathematical functions like square root and trigonometry