xxxxxxxxxx
95
class Particle {
constructor() {
this.pos = createVector(width / 2, height / 2)
this.dir = createVector(1, 0)
this.vel = 0
this.maxVel = 5
this.angleVel = 0
this.maxAngleVel = 0.1
this.directions = { up: false, down: false, right: false, left: false }
this.rays = []
let rays = 1000
let angle = 80
for (let a = -angle/2; a < angle/2; a += 360 / rays) {
this.rays.push(new Ray(this.pos, this.dir, radians(a)))
}
this.shape = []
}
go(direction, boolean) { this.directions[direction] = boolean }
update() {
// Get vel
if (this.directions.up) { this.vel = this.maxVel }
else if (this.directions.down) { this.vel = - this.maxVel }
else { this.vel = 0 }
// Get angleVel
if (this.directions.right) { this.angleVel = this.maxAngleVel }
else if (this.directions.left) { this.angleVel = - this.maxAngleVel }
else { this.angleVel = 0 }
// Update pos and dir
this.dir.rotate(this.angleVel)
const step = this.dir.copy().mult(this.vel)
this.pos.add(step)
for (let ray of this.rays) { ray.update() }
}
look(walls) {
this.shape = []
for (let ray of this.rays) {
let closest = null
let record = Infinity
let wallHitId = null
for (let wall of walls) {
let pt = ray.cast(wall)
if (pt) {
const d = p5.Vector.dist(this.pos, pt)
if (d < record) {
closest = pt
wallHitId = wall.id
record = d
}
}
}
if (closest) {
this.shape.push({createVector(closest.x, closest.y)})
}
}
}
show() {
// Draw the vision
noStroke()
fill(100)
beginShape()
vertex(this.pos.x, this.pos.y)
for (let v of this.shape) { vertex(v.x, v.y) }
vertex(this.pos.x, this.pos.y)
endShape()
// Draw the end of the rays
// stroke(255)
// noFill()
// beginShape()
// for (let v of this.shape) { vertex(v.x, v.y) }
// endShape()
// Draw the particle
noStroke()
fill(255)
ellipse(this.pos.x, this.pos.y, 16)
}
}