Browse Source

Add stub for the javascript version
(unlikely to work as is)

Godzil 1 year ago
parent
commit
4fca72ce34
4 changed files with 176 additions and 0 deletions
  1. 11 0
      javascript/index.html
  2. 2 0
      javascript/p5.js
  3. 15 0
      javascript/styles.css
  4. 148 0
      javascript/vectors.js

+ 11 - 0
javascript/index.html

@@ -0,0 +1,11 @@
+<!doctype html>
+<html lang="en">
+<head>
+    <title>Simple Javascript Raycaster</title>
+    <script src="p5.js"></script>
+    <link rel="stylesheet" href="styles.css" />
+    <script src="raycast.js"></script>
+</head>
+<body>
+</body>
+</html>

File diff suppressed because it is too large
+ 2 - 0
javascript/p5.js


+ 15 - 0
javascript/styles.css

@@ -0,0 +1,15 @@
+html, body
+{
+    margin: 0;
+    padding: 0;
+    background: #555;
+}
+
+canvas
+{
+    display: block;
+    margin: 0 auto;
+    border-style: solid;
+    border-width: 8;
+    border-color: #fff;
+}

+ 148 - 0
javascript/vectors.js

@@ -0,0 +1,148 @@
+class vec3
+{
+    constructor(x, y, z)
+    {
+        this.x = x;
+        this.y = y;
+        this.z = z;
+    }
+    mag()
+    {
+        return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z));
+    }
+    add(v)
+    {
+        this.x += v.x;
+        this.y += v.y;
+        this.z += v.z;
+    }
+    static add(v1, v2)
+    {
+        let result = new vec3(0, 0, 0);
+        result.x = v1.x + v2.x;
+        result.y = v1.y + v2.y;
+        result.z = v1.z + v2.z;
+        return result;
+    }
+    sub(v)
+    {
+        this.x -= v.x;
+        this.y -= v.y;
+        this.z -= v.z;
+    }
+    static sub(v1, v2)
+    {
+        return new vec3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
+    }
+    scale(n)
+    {
+        this.x *= n;
+        this.y *= n;
+        this.z *= n;
+    }
+    translate(n) { this.add(n); }
+    rotateX(n) { /* TODO */ }
+    rotateY(n) { /* TODO */ }
+    rotateZ(n) { /* TODO */ }
+    dot(v)
+    {
+        return (this.x * v.x) + (this.y + v.y) + (this.y * v.z)
+    }
+    cross(v)
+    {
+        return new vec3(this.y * v.z - this.z * v.y,
+            this.z * v.x - this.x * v.z,
+            this.x * v.y - this.y * v.x);
+    }
+    norm()
+    {
+        let m = this.mag();
+        this.x /= m;
+        this.y /= m;
+        this.z /= m;
+    }
+    draw(colour)
+    {
+        fill(colour);
+        circle(this.x, this.y, 10);
+    }
+}
+
+class vec2
+{
+    constructor(x, y)
+    {
+        this.x = x;
+        this.y = y;
+    }
+    mag()
+    {
+        return Math.sqrt((this.x * this.x) + (this.y * this.y));
+    }
+    add(v)
+    {
+        this.x += v.x;
+        this.y += v.y;
+    }
+    static add(v1, v2)
+    {
+        let result = new vec2(0, 0);
+        result.x = v1.x + v2.x;
+        result.y = v1.y + v2.y;
+        return result;
+    }
+    sub(v)
+    {
+        this.x -= v.x;
+        this.y -= v.y;
+    }
+    static sub(v1, v2)
+    {
+        return new vec2(v1.x - v2.x, v1.y - v2.y);
+    }
+    scale(n)
+    {
+        this.x *= n;
+        this.y *= n;
+    }
+    translate(n) { this.add(n); }
+    rotate(angle)
+    {
+        return new vec2(x * Math.cos(angle) - y * Math.sin(angle),
+                        this.y = x * Math.sin(angle) + y * Math.cos(angle));
+    }
+    dot(v)
+    {
+        return (this.x * v.x) + (this.y + v.y)
+    }
+    perp()
+    {
+        return new vec2(this.y, -this.x);
+    }
+    draw(colour)
+    {
+        fill(colour);
+        circle(this.x, this.y, 10);
+    }
+    norm()
+    {
+        let m = this.mag();
+        this.x /= m;
+        this.y /= m;
+    }
+}
+
+let position = new vec2(100, 100);
+let velocity = new vec2(1, 2);
+
+function setup()
+{
+    createCanvas(windowWidth, windowHeight);
+}
+
+function draw()
+{
+    background("black");
+    position = position.rotate(0.1);
+    position.draw("red");
+}

Some files were not shown because too many files changed in this diff