Last active
August 14, 2024 21:14
-
-
Save jeremyconkin/a3909b2d3276d1b6fbff02cefecd561a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SCNVector3+MathUtils.swift | |
// | |
// Created by Jeremy Conkin on 4/26/16. | |
// | |
import SceneKit | |
/** | |
Add two vectors | |
- parameter left: Addend 1 | |
- parameter right: Addend 2 | |
*/ | |
func +(left:SCNVector3, right:SCNVector3) -> SCNVector3 { | |
return SCNVector3(left.x + right.x, left.y + right.y, left.z + right.z) | |
} | |
/** | |
Subtract two vectors | |
- parameter left: Minuend | |
- parameter right: Subtrahend | |
*/ | |
func -(left:SCNVector3, right:SCNVector3) -> SCNVector3 { | |
return left + (right * -1.0) | |
} | |
/** | |
Add one vector to another | |
- parameter left: Vector to change | |
- parameter right: Vector to add | |
*/ | |
func +=(inout left: SCNVector3, right:SCNVector3) { | |
left = SCNVector3(left.x + right.x, left.y + right.y, left.z + right.z) | |
} | |
/** | |
Subtract one vector to another | |
- parameter left: Vector to change | |
- parameter right: Vector to subtract | |
*/ | |
func -=(inout left: SCNVector3, right:SCNVector3) { | |
left = SCNVector3(left.x - right.x, left.y - right.y, left.z - right.z) | |
} | |
/** | |
Multiply a vector times a constant | |
- parameter vector: Vector to modify | |
- parameter constant: Multiplier | |
*/ | |
func *(vector:SCNVector3, multiplier:SCNFloat) -> SCNVector3 { | |
return SCNVector3(vector.x * multiplier, vector.y * multiplier, vector.z * multiplier) | |
} | |
/** | |
Multiply a vector times a constant and update the vector inline | |
- parameter vector: Vector to modify | |
- parameter constant: Multiplier | |
*/ | |
func *=(inout vector: SCNVector3, multiplier:SCNFloat) { | |
vector = vector * multiplier | |
} | |
extension SCNVector3 { | |
/// Calculate the magnitude of this vector | |
var magnitude:SCNFloat { | |
get { | |
return sqrt(dotProduct(self)) | |
} | |
} | |
/// Vector in the same direction as this vector with a magnitude of 1 | |
var normalized:SCNVector3 { | |
get { | |
let localMagnitude = magnitude | |
let localX = x / localMagnitude | |
let localY = y / localMagnitude | |
let localZ = z / localMagnitude | |
return SCNVector3(localX, localY, localZ) | |
} | |
} | |
/** | |
Calculate the dot product of two vectors | |
- parameter vectorB: Other vector in the calculation | |
*/ | |
func dotProduct(_ vectorB:SCNVector3) -> SCNFloat { | |
return (x * vectorB.x) + (y * vectorB.y) + (z * vectorB.z) | |
} | |
/** | |
Calculate the dot product of two vectors | |
- parameter vectorB: Other vector in the calculation | |
*/ | |
func crossProduct(_ vectorB:SCNVector3) -> SCNVector3 { | |
let computedX = (y * vectorB.z) - (z * vectorB.y) | |
let computedY = (z * vectorB.x) - (x * vectorB.z) | |
let computedZ = (x * vectorB.y) - (y * vectorB.x) | |
return SCNVector3(computedX, computedY, computedZ) | |
} | |
/** | |
Calculate the angle between two vectors | |
- parameter vectorB: Other vector in the calculation | |
*/ | |
func angleBetweenVectors(_ vectorB:SCNVector3) -> SCNFloat { | |
//cos(angle) = (A.B)/(|A||B|) | |
let cosineAngle = (dotProduct(vectorB) / (magnitude * vectorB.magnitude)) | |
return SCNFloat(acos(cosineAngle)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment