Upgrading from 1.x

This version makes vectors immutable, drops BigNumber and renames the namespace.

Summary of changes#

This version is a breaking release. These are the changes that affect your code.

Step by step#

Follow these steps to move an existing project from 1.x.

1. Update the namespace#

Change the imports. The class names Vector, Vector2 and Vector3 stay the same.

// before
use PHP\Math\Vector\Vector3;

// after
use PHPMath\Vector\Vector3;

2. Replace Tuple and array access#

Tuple was based on ArrayObject. It is gone, and so is array access such as $vector[0]. Use getElement() to read a component, toArray() for an array and foreach to loop.

// before
$value = $vector[0];

// after
$value = $vector->getElement(0);

Code that added or removed components with addElement(), removeElement() or removeElementIndex() must build a new Vector from an array.

3. Replace mutable code#

In 1.x, add(), subtract(), scale(), negate() and normalize() changed the vector and returned $this. Now they return a new vector and leave the original alone. Assign the result.

// before
$vector->add($other);

// after
$vector = $vector->add($other);

The same applies to setters.

// before
$vector->setX(5);

// after
$vector = $vector->withX(5);

4. Replace VectorUtils#

The static helpers are now methods. VectorUtils::add() and VectorUtils::subtract() return a new vector, just like the methods do.

// before
$sum = VectorUtils::add($a, $b);
$angle = VectorUtils::angleBetween($a, $b);

// after
$sum = $a->add($b);
$angle = $a->angleBetween($b);

angleBetween() now divides the dot product by both lengths. In 1.x it only took the acos() of the dot product, which is only correct for unit vectors. Results change for vectors that are not normalised.

5. Replace BigNumber values#

getLength(), getLengthSquared(), dotProduct(), getElement() and the other getters used to return BigNumber objects. They now return plain floats, and the constructors take ints or floats.

// before
$length = $vector->getLength()->getValue();

// after
$length = $vector->getLength();

Floats have limited precision, while BigNumber could be exact. If you relied on exact decimal results, compare floats with a small tolerance instead of ==.

6. Check the class hierarchy#

Vector3 used to extend Vector2. Both are now final and extend Vector directly, so an instanceof Vector2 check is false for a Vector3. Use Vector as the type when you accept both.