I made a bunch of 3d Math functions for your entertainment.
These include:
//increase or decrease the length of vector by size
Vector3 AddVectorLength(Vector3 vector, float size){
//create a vector of direction "vector" with length "size"
Vector3 SetVectorLength(Vector3 vector, float size){
//caclulate the rotational difference from A to B
Quaternion SubtractRotation(Quaternion B, Quaternion A){
//Find the line of intersection between two planes.
bool PlanePlaneIntersection(out Vector3 linePoint, out Vector3 lineVec, Vector3 plane1Normal, Vector3 plane1Position, Vector3 plane2Normal, Vector3 plane2Position){
//Get the coordinates of the intersection between a line and a plane
bool LinePlaneIntersection(out Vector3 intersection, Vector3 linePoint, Vector3 lineVec, Vector3 planeNormal, Vector3 planePoint){
//Calculate the instersection point of two lines. Returns true if lines intersect, otherwise false.
bool LineLineIntersection(out Vector3 intersection, Vector3 linePoint1, Vector3 lineVec1, Vector3 linePoint2, Vector3 lineVec2){
//Two non-parallel lines which may or may not touch each other have a point on each line which lays closest
//to each other. This function finds those two points.
bool ClosestPointsOnTwoLines(out Vector3 closestPointLine1, out Vector3 closestPointLine2, Vector3 linePoint1, Vector3 lineVec1, Vector3 linePoint2, Vector3 lineVec2){
//This function returns a point which is a projection from a point to a line.
Vector3 ProjectPointOnLine(Vector3 linePoint, Vector3 lineVec, Vector3 point){
//This function returns a point which is a projection from a point to a line segment.
Vector3 ProjectPointOnLineSegment(Vector3 linePoint1, Vector3 linePoint2, Vector3 point){
//This function returns a point which is a projection from a point to a plane.
Vector3 ProjectPointOnPlane(Vector3 planeNormal, Vector3 planePoint, Vector3 point){
//Projects a vector onto a plane. The output is not normalized.
Vector3 ProjectVectorOnPlane(Vector3 planeNormal, Vector3 vector){
//Get the shortest distance between a point and a plane. The output is signed so it holds information
//as to which side of the plane normal the point is.
float SignedDistancePlanePoint(Vector3 planeNormal, Vector3 planePoint, Vector3 point){
//This fuction calculates a signed (+ or - sign instead of being ambiguous) dot product. It is basically used
//to figure out whether a vector is positioned to the left or right of another vector.
float SignedDotProduct(Vector3 vectorA, Vector3 vectorB, Vector3 normal){
//Calculate the angle between a vector and a plane. The plane is made by a normal vector.
//Output is in radians.
float AngleVectorPlane(Vector3 vector, Vector3 normal){
//Calculate the dot product as an angle
float DotProductAngle(Vector3 vec1, Vector3 vec2){
//Convert a plane defined by 3 points to a plane defined by a vector and a point.
//The plane point is the middle of the triangle defined by the 3 points.
void PlaneFrom3Points(out Vector3 planeNormal, out Vector3 planePoint, Vector3 pointA, Vector3 pointB, Vector3 pointC){
//Returns the forward vector of a quaternion
Vector3 GetForwardVector(Quaternion q)
//Returns the up vector of a quaternion
Vector3 GetUpVector(Quaternion q)
//Returns the right vector of a quaternion
Vector3 GetRightVector(Quaternion q)
//Gets a quaternion from a matrix
Quaternion QuaternionFromMatrix(Matrix4x4 m)
//Gets a position from a matrix
Vector3 PositionFromMatrix(Matrix4x4 m)
//This function translates one object as if it was parented to the other.
//Before using this function, the Init() function must be called
void TransformWithParent(out Quaternion childRotation, out Vector3 childPosition, Quaternion parentRotation, Vector3 parentPosition, Quaternion startParentRotation, Vector3 startParentPosition, Quaternion startChildRotation, Vector3 startChildPosition)
//With this function you can align a triangle of an object with any transform.
void PreciseAlign(ref GameObject gameObjectInOut, Vector3 alignWithVector, Vector3 alignWithNormal, Vector3 alignWithPosition, Vector3 triangleForward, Vector3 triangleNormal, Vector3 trianglePosition)
//This is an alternative for Quaternion.LookRotation. Instead of aligning the forward and up vector of the game
//object with the input vectors, a custom direction can be used instead of the fixed forward and up vectors.
void LookRotationExtended(ref GameObject gameObjectInOut, Vector3 alignWithVector, Vector3 alignWithNormal, Vector3 customForward, Vector3 customUp)
//Convert a position, direction, and normal vector to a transform
void VectorsToTransform(ref GameObject gameObjectInOut, Vector3 positionVector, Vector3 directionVector, Vector3 normalVector){
//This function finds out on which side of a line segment the point is located.
int PointOnWhichSideOfLineSegment(Vector3 linePoint1, Vector3 linePoint2, Vector3 point){
//Returns true if a line segment (made up of linePoint1 and linePoint2) is fully or partially in a rectangle
bool IsLineInRectangle(Vector3 linePoint1, Vector3 linePoint2, Vector3 rectA, Vector3 rectB, Vector3 rectC, Vector3 rectD){
//Returns true if "point" is in a rectangle made up of RectA to RectD. The line point is assumed to be on the same
//plane as the rectangle. If the point is not on the plane, use ProjectPointOnPlane() first.
bool IsPointInRectangle(Vector3 point, Vector3 rectA, Vector3 rectC, Vector3 rectB, Vector3 rectD){
//Returns true if line segment made up of pointA1 and pointA2 is crossing line segment made up of
//pointB1 and pointB2. The two lines are assumed to be in the same plane.
bool AreLineSegmentsCrossing(Vector3 pointA1, Vector3 pointA2, Vector3 pointB1, Vector3 pointB2){
//Returns the pixel distance from the mouse pointer to a line.
//Alternative for HandleUtility.DistanceToLine(). Works both in Editor mode and Play mode.
float MouseDistanceToLine(Vector3 linePoint1, Vector3 linePoint2){
//Returns the pixel distance from the mouse pointer to a camera facing circle.
//Alternative for HandleUtility.DistanceToCircle(). Works both in Editor mode and Play mode.
float MouseDistanceToCircle(Vector3 point, float radius){
//This function calculates the acceleration vector in meter/second^2.
//Input: position.
bool LinearAcceleration(out Vector3 vector, Vector3 position, int samples){
//This function calculates angular acceleration in object space as deg/second^2, encoded as a vector.
bool AngularAcceleration(out Vector3 vector, Quaternion rotation, int samples){
//Get y from a linear function, with x as an input. The linear function goes through points
//0,0 on the left ,and Qxy on the right.
float LinearFunction2DBasic(float x, float Qx, float Qy){
//Get y from a linear function, with x as an input. The linear function goes through points
//Pxy on the left ,and Qxy on the right.
float LinearFunction2DFull(float x, float Px, float Py, float Qx, float Qy){
//Add rotation B to rotation A.
Quaternion AddRotation(Quaternion A, Quaternion B){
//Same as the build in TransformDirection(), but using a rotation instead of a transform
Vector3 TransformDirectionMath(Quaternion rotation, Vector3 vector){
//Same as the build in InverseTransformDirection(), but using a rotation instead of a transform
Vector3 InverseTransformDirectionMath(Quaternion rotation, Vector3 vector){
//Rotate a vector as if it is attached to an object with rotation "from", which is then rotated to rotation "to".
//Similar to TransformWithParent(), but rotating a vector instead of a transform.
Vector3 RotateVectorFromTo(Quaternion from, Quaternion to, Vector3 vector){
//Get a point on a Catmull-Rom spline.
Vector2 GetPointOnSpline(float percentage, Vector2[] cPoints) {
//Finds the intersection points between a straight line and a spline. Solves a Cubic polynomial equation.
float[] GetLineSplineIntersections(Vector2[] linePoints, Vector2[] cPoints) {
Full code here:
https://drive.google.com/file/d/1O0d_pqitpgbFYJG06JO6uAJjudI-QHwn/view?usp=sharing
Make sure all input vectors are normalized.