Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>

<head>
</head>

<body>
</body>

</html>
              
            
!

CSS

              
                body {
    padding: 0;
    margin: 0;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    justify-content: center;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    background: black;
}

canvas {
    position: fixed;
    top: 50%;
    left: 50%;
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
}

              
            
!

JS

              
                console.clear();

// 2D Ray Casting
// https://editor.p5js.org/codingtrain/sketches/Nqsq3DFv-
class Boundary {
    constructor(x1, y1, x2, y2) {
        this.a = createVector(x1, y1);
        this.b = createVector(x2, y2);
    }

    show() {
        stroke(255);
        line(this.a.x, this.a.y, this.b.x, this.b.y);
    }
}

class Terrain {
    constructor() {
        console.log("new terrain");
        this.points = [];
    }

    show() {
        this.walls.forEach((w) => w.show());
    }
}

// 2D Ray Casting
// https://editor.p5js.org/codingtrain/sketches/Nqsq3DFv-
class Ray {
    constructor(pos, angle) {
        this.pos = pos;
        this.dir = p5.Vector.fromAngle(angle);
    }

    show() {
        stroke(255);
        push();
        translate(this.pos.x, this.pos.y);
        line(0, 0, this.dir.x * 10, this.dir.y * 10);
        pop();
    }

    cast(wall) {
        const x1 = wall.a.x;
        const y1 = wall.a.y;
        const x2 = wall.b.x;
        const y2 = wall.b.y;

        const x3 = this.pos.x;
        const y3 = this.pos.y;
        const x4 = this.pos.x + this.dir.x;
        const y4 = this.pos.y + this.dir.y;

        const den = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
        if (den == 0) {
            return;
        }

        const t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / den;
        const u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / den;
        if (t > 0 && t < 1 && u > 0) {
            const pt = createVector();
            pt.x = x1 + t * (x2 - x1);
            pt.y = y1 + t * (y2 - y1);
            return pt;
        } else {
            return;
        }
    }
}

// 2D Ray Casting
// https://editor.p5js.org/codingtrain/sketches/Nqsq3DFv-
class Particle {
    constructor() {
        this.pos = createVector(width / 2, height / 2);
        this.speed = createVector(1, 0);
        this.rays = [];
        this.crashed = false;
        for (let a = -45; a < 45; a += 10) {
            this.rays.push(new Ray(this.pos, radians(a)));
        }
    }

    move() {
        if (this.speed.mag() > 0.1) {
            this.pos.add(this.speed);
        }
        this.wrap();
    }

    wrap() {
        if (this.pos.x < 0) {
            this.pos.x = width;
        } else if (this.pos.x > width) {
            this.pos.x = 0;
        }
        if (this.pos.y < 0) {
            this.pos.y = height;
        } else if (this.pos.y > height) {
            this.pos.y = 0;
        }
    }

    update(x, y) {
        this.pos.set(x, y);
    }

    rotate(angle) {
        for (const ray of this.rays) {
            ray.dir.rotate(radians(angle));
        }
        this.speed.rotate(radians(angle));
    }

    accelerate() {
        this.speed.mult(1.1);
    }
    deccelerate() {
        this.speed.mult(0.9);
    }

    look(walls) {
        let distances = [];
        for (let i = 0; i < this.rays.length; i++) {
            const ray = this.rays[i];
            let closest = null;
            let record = Infinity;
            for (let wall of walls) {
                const pt = ray.cast(wall);
                if (pt) {
                    const d = p5.Vector.dist(this.pos, pt);
                    if (d < record) {
                        record = d;
                        closest = pt;
                    }
                }
            }
            if (closest) {
                // colorMode(HSB);
                // stroke((i + frameCount * 2) % 360, 255, 255, 50);
                stroke(255, 100);
                line(this.pos.x, this.pos.y, closest.x, closest.y);
                distances.push(
                    dist(this.pos.x, this.pos.y, closest.x, closest.y)
                );
            } else {
                distances.push(-1.0);
            }
        }

        if (distances.find((d) => d < 5)) {
            this.crashed = true;
            this.speed.setMag(0.01);
        } else {
            this.crashed = false;
        }
        const distText = distances
            .map((v) => v.toFixed(0).padStart(" ", 4))
            .join("\t");
        text(distText, 0, height - 20);
    }

    show() {
        fill(255);
        if (this.crashed) {
            fill(250, 0, 0);
        }
        ellipse(this.pos.x, this.pos.y, 4);
        for (let ray of this.rays) {
            ray.show();
        }
    }

    showDebug() {}
}

let walls = [];
let particle;
let terrain;
function setup() {
    createCanvas(700, 700);
    for (let i = 0; i < 5; i++) {
        let x1 = random(width);
        let x2 = random(width);
        let y1 = random(height);
        let y2 = random(height);
        walls[i] = new Boundary(x1, y1, x2, y2);
    }
    walls.push(new Boundary(0, 0, width, 0));
    walls.push(new Boundary(width, 0, width, height));
    walls.push(new Boundary(width, height, 0, height));
    walls.push(new Boundary(0, height, 0, 0));
    particle = new Particle();
    particle.update(width / 2, height / 2);

    terrain = new Terrain();
}

function draw() {
    background(10);
    walls.forEach((w) => w.show());
    particle.show();
    //particle.update(mouseX, mouseY);
    particle.move();
    particle.look(walls);

    const Q = 81;
    const A = 65;
    const W = 87;
    const Z = 90;
    const S = 83;
    const D = 68;
    if (keyIsDown(LEFT_ARROW) || keyIsDown(Q) || keyIsDown(A)) {
        particle.rotate(-5);
    }
    if (keyIsDown(RIGHT_ARROW) || keyIsDown(D)) {
        particle.rotate(5);
    }
    if (keyIsDown(UP_ARROW) || keyIsDown(W) || keyIsDown(Z)) {
        particle.accelerate();
    }
    if (keyIsDown(DOWN_ARROW) || keyIsDown(S)) {
        particle.deccelerate();
    }
}

function keyPressed() {
    console.log(keyCode);
}

              
            
!
999px

Console