Vector3 — Aspose.3D FOSS for Java

Overview

Vector3 is a double-precision 3-component vector with x, y, and z components. It is used throughout Aspose.3D for positions, directions, normals, and scale values. All arithmetic operations return new Vector3 instances; the original is not mutated.

import com.aspose.threed.*;

// Construct from components
Vector3 v = new Vector3(1.0, 2.0, 3.0);

// Copy constructor
Vector3 v2 = new Vector3(v);

// Default (zero vector)
Vector3 zero = new Vector3();

Constructor

SignatureDescription
Vector3()Constructs the zero vector (0, 0, 0)
Vector3(double x, double y, double z)Constructs from three components
Vector3(Vector3 other)Copy constructor

Public Fields

Vector3 uses public fields for component access (not getter/setter methods):

FieldTypeAccessDescription
xdoublev.xX component
ydoublev.yY component
zdoublev.zZ component

Computed Properties

NameTypeGetterDescription
lengthdoublegetLength()Euclidean length (sqrt(x^2+y^2+z^2)), read-only
length2doublegetLength2()Squared length, read-only; cheaper than getLength() when only comparison is needed

Static Fields

FieldTypeDescription
ZEROVector3Vector3(0, 0, 0)
ONEVector3Vector3(1, 1, 1)
UNIT_XVector3Vector3(1, 0, 0)
UNIT_YVector3Vector3(0, 1, 0)
UNIT_ZVector3Vector3(0, 0, 1)

Methods

MethodReturn TypeDescription
set(double x, double y, double z)voidSets all three components in place
dot(Vector3 rhs)doubleDot product with another Vector3
cross(Vector3 rhs)Vector3Cross product – returns a vector perpendicular to both operands
normalize()Vector3Returns a unit-length copy; returns zero vector if length is zero
angleBetween(Vector3 dir)doubleAngle in radians between this vector and dir
angleBetween(Vector3 dir, Vector3 up)doubleSigned angle in radians projected onto the plane defined by up
sin()Vector3Component-wise sine
cos()Vector3Component-wise cosine
compareTo(Vector3 other)intLexicographic comparison: returns -1, 0, or 1
Vector3.parse(String s)Vector3Static. Parses "x y z" string representation

Example

import com.aspose.threed.*;
import com.aspose.threed.Scene;

// Basic arithmetic
Vector3 a = new Vector3(1.0, 0.0, 0.0);
Vector3 b = new Vector3(0.0, 1.0, 0.0);

// Dot and cross products
double dot = a.dot(b);           // 0.0
Vector3 perp = a.cross(b);      // Vector3(0, 0, 1)

// Normalize
Vector3 direction = new Vector3(3.0, 4.0, 0.0);
Vector3 unit = direction.normalize();   // Vector3(0.6, 0.8, 0.0)
System.out.println(unit.getLength());    // 1.0

// Angle between vectors
double angle = a.angleBetween(b);
System.out.println(Math.toDegrees(angle));   // 90.0

// Use with a scene node transform
Scene scene = new Scene();
Node node = scene.getRootNode().createChildNode("box");
node.getTransform().setTranslation(new Vector3(10.0, 0.0, 0.0));

See Also