summaryrefslogtreecommitdiff
path: root/frontend/_rigidbody/vec2.js
diff options
context:
space:
mode:
authorMatthew Kosarek <mattkae@protonmail.com>2021-02-06 18:39:48 -0500
committerMatthew Kosarek <mattkae@protonmail.com>2021-02-06 18:39:48 -0500
commit376e1a7f9207fffb1ec3027ac1e7f32db5de4922 (patch)
tree8bc6c381a10d8b62ff33cdcff6daa1d17cee0f9a /frontend/_rigidbody/vec2.js
Initial commit
Diffstat (limited to 'frontend/_rigidbody/vec2.js')
-rw-r--r--frontend/_rigidbody/vec2.js41
1 files changed, 41 insertions, 0 deletions
diff --git a/frontend/_rigidbody/vec2.js b/frontend/_rigidbody/vec2.js
new file mode 100644
index 0000000..3b24371
--- /dev/null
+++ b/frontend/_rigidbody/vec2.js
@@ -0,0 +1,41 @@
+function vec2(x = 0, y = 0) {
+ return { x: x, y: y};
+}
+
+function addVec2(v1, v2) {
+ return {
+ x: v1.x + v2.x,
+ y: v1.y + v2.y
+ };
+}
+
+function subVec2(v1, v2) {
+ return {
+ x: v1.x - v2.x,
+ y: v1.y - v2.y
+ };
+}
+
+function scaleVec2(v, s) {
+ return {
+ x: v.x * s,
+ y: v.y * s
+ };
+}
+
+function dot2(v1, v2) {
+ return v1.x * v2.x + v1.y * v2.y;
+}
+
+function length2(v) {
+ return Math.sqrt(v.x * v.x + v.y * v.y);
+}
+
+function normalize2(v) {
+ const lLength = 1.0 / length2(v);
+ return { x: v.x * lLength, y: v.y * lLength };
+}
+
+function vec2str(v) {
+ return `(${v.x.toFixed(2)}, ${v.y.toFixed(2)})`;
+} \ No newline at end of file