xxxxxxxxxx
64
class Player {
constructor(sc) {
this.sc = sc;
this.thresh = 1e-7;
this.pos = createVector(1, 0, 0);
if (abs(this.pos.mag() - 1.0) > this.thresh) {
throw ("position vector no bueno.");
}
this.ori = createVector(0, 0, -1);
}
moveForw(stepSize) {
this.pos = this.RotAroundU(this.pos, this.ori, stepSize)
}
moveBack(stepSize) {
this.pos = this.RotAroundU(this.pos, this.ori.copy().mult(-1), stepSize)
}
show() {
push();
translate(width / 2, height / 2);
translate(this.sc * this.pos.x, this.sc * this.pos.y);
fill(0, 255, 0);
noStroke();
ellipse(0, 0, 10*(1-this.pos.z), 10*(1-this.pos.z));
pop();
}
rotLeft(stepSize) {
this.ori = this.RotAroundU(this.ori, this.pos, stepSize);
}
rotRight(stepSize) {
this.ori = this.RotAroundU(this.ori, this.pos, -stepSize);
}
flip() {
this.ori.mult(-1);
}
RotAroundU(yin, u, theta) {
let y = [yin.x, yin.y, yin.z];
let R = [
[cos(theta) + u.x * u.x * (1 - cos(theta)), u.x * u.y * (1 - cos(theta)) - u.z * sin(theta), u.x * u.z * (1 - cos(theta)) + u.y * sin(theta)],
[u.x * u.y * (1 - cos(theta)) + u.z * sin(theta), cos(theta) + u.y * u.y * (1 - cos(theta)), u.y * u.z * (1 - cos(theta)) - u.x * sin(theta)],
[u.x * u.z * (1 - cos(theta)) - u.y * sin(theta), u.y * u.z * (1 - cos(theta)) + u.x * sin(theta), cos(theta) + u.z * u.z * (1 - cos(theta))]
];
let z = [];
for (let mi = 0; mi < 3; mi++) {
let tmp = 0;
for (let ni = 0; ni < 3; ni++) {
tmp += R[mi][ni] * y[ni];
}
z.push(tmp);
}
return createVector(z[0], z[1], z[2]);
}
rayMarch(fov,objects) {
let d = objects[0].signedDistanceToObject(this.pos);
text(d.toFixed(3),width-100,20);
}
}