-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHolisticMath.cs
More file actions
77 lines (63 loc) · 2.46 KB
/
HolisticMath.cs
File metadata and controls
77 lines (63 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HolisticMath
{
static public float Square(float value)
{
return value * value;
}
static public float Distance(Coords point1, Coords point2)
{
float diffSquared = Square(point1.x - point2.x) +
Square(point1.y - point2.y) +
Square(point1.z - point2.z);
float squareRoot = Mathf.Sqrt(diffSquared);
//if you are interested in how the Sqrt is calculated see
//https://www.codeproject.com/Articles/570700/SquareplusRootplusalgorithmplusforplusC
return squareRoot;
}
static public Coords Lerp(Coords A, Coords B, float t)
{
t = Mathf.Clamp(t, 0, 1);
Coords v = new Coords(B.x - A.x, B.y - A.y, B.z - A.z);
float xt = A.x + v.x * t;
float yt = A.y + v.y * t;
float zt = A.z + v.z * t;
return new Coords(xt, yt, zt);
}
static public Coords GetNormal(Coords vector)
{
float length = Distance(new Coords(0, 0, 0), vector);
vector.x /= length;
vector.y /= length;
vector.z /= length;
return vector;
}
static public float Dot(Coords vector1, Coords vector2)
{
return (vector1.x * vector2.x + vector1.y * vector2.y + vector1.z * vector2.z);
}
static public float Angle(Coords vector1, Coords vector2)
{
float dotDivide = Dot(vector1, vector2) / (Distance(new Coords(0, 0, 0), vector1) * Distance(new Coords(0, 0, 0), vector2));
return Mathf.Acos(dotDivide); //radians. For degrees * 180/Mathf.PI;
}
static public Coords Cross(Coords vector1, Coords vector2)
{
float iMult = vector1.y * vector2.z - vector1.z * vector2.y;
float jMult = vector1.z * vector2.x - vector1.x * vector2.z;
float kMult = vector1.x * vector2.y - vector1.y * vector2.x;
Coords crossProd = new Coords(iMult, jMult, kMult);
return crossProd;
}
static public Coords Rotate(Coords vector, float angle, bool clockwise)
//angle in radians please
{
if (clockwise)
angle = 2*Mathf.PI - angle;
float xVal = vector.x * Mathf.Cos(angle) - vector.y * Mathf.Sin(angle);
float yVal = vector.x * Mathf.Sin(angle) + vector.y * Mathf.Cos(angle);
return new Coords(xVal, yVal, 0);
}
}