Skip to content

Vector Utilities

Swifter edited this page Oct 9, 2024 · 6 revisions

Prerequisites

You should know about what vectors are mathematically. Here is a great resource where you can learn about them: https://www.youtube.com/watch?v=fNk_zzaMoSs&list=PLZHQObOWTQDPD3MizzM2xVFitgF8hE_ab

Utilities

"Vectors" are not a special class in ReMapper, they are just an array of values. ReMapper provides a couple types for vectors:

  • rm.Vec3 - [number, number, number]
  • rm.Vec4 - [number, number, number, number]

  • rm.areVectorsEqual will simply check 2 vectors of the same length against each other to see if their elements are all equal.
rm.areVectorsEqual([1, 2, 3], [1, 2, 3]) // true
rm.areVectorsEqual([1, 2, 3], [0, 0, 0]) // false

You may also provide a third parameter leniency which will describe the maximum difference between numbers that are allowed for them to still be considered "equal".

rm.areVectorsEqual([2, 3], [2, 3.5], 0.5) // true
rm.areVectorsEqual([2, 3], [2, 3.5], 0.25) // false
  • rm.magnitude will calculate the magnitude of a vector.
rm.magnitude([1, 0]) // 1
rm.magnitude([1, 1]) // 1.414..
  • rm.distance will take in two vectors A and B and return the magnitude of (B - A).
rm.distance([1, 0], [3, 2]) // 2.828...
  • rm.normalize will take in a vector and return a vector in the same direction with a magnitude of 1.
rm.normalize([3, 1, 4]) // ~ [0.588, 0.196, 0.784]
  • rm.dotProduct will take in 2 vectors and give the resulting dot product.
rm.dotProduct([1, 0], [0, 1]) // 0
  • rm.crossProduct will take in 2 three-dimensional vectors and give the resulting cross product.
rm.crossProduct([0, 0, 1], [1, 0, 0]) // [0, 1, 0]

It is worth noting that the cross product operates using the right-hand rule, while unity is in left-hand space. https://en.wikipedia.org/wiki/Right-hand_rule#/media/File:Right-hand_rule_for_cross_product.png

Clone this wiki locally