Skip to content

Commit

Permalink
Create Cube.js
Browse files Browse the repository at this point in the history
  • Loading branch information
MOQN committed Oct 14, 2024
1 parent 0ecbea2 commit 4917625
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions _empty-project-three-p5/js/Cube.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
class Cube {
constructor() {
this.pos = createVector();
this.vel = createVector();
this.acc = createVector();
this.scl = createVector(1, 1, 1);
this.mass = 1;
//this.setMass(); // feel free to use this method; it arbitrarily defines the mass based on the scale.
this.rot = createVector();
this.rotVel = createVector();
this.rotAcc = createVector();
this.mesh = getBox();
scene.add(this.mesh); // don't forget to add to scene
}
setPosition(x, y, z) {
this.pos = createVector(x, y, z);
return this;
}
setTranslation(x, y, z) {
this.mesh.geometry.translate(x, y, z);
return this;
}
setVelocity(x, y, z) {
this.vel = createVector(x, y, z);
return this;
}
setRotationAngle(x, y, z) {
this.rot = createVector(x, y, z);
return this;
}
setRotationVelocity(x, y, z) {
this.rotVel = createVector(x, y, z);
return this;
}
setScale(w, h = w, d = w) {
// or
//h = (h === undefined) ? w : h;
//d = (d === undefined) ? w : d;
const minScale = 0.01;
if (w < minScale) w = minScale;
if (h < minScale) h = minScale;
if (d < minScale) d = minScale;
this.scl = createVector(w, h, d);
return this;
}
setMass(mass) {
if (mass) {
this.mass = mass;
} else {
this.mass = 1 + (this.scl.x * this.scl.y * this.scl.z) * 0.000001; // arbitrary
}
return this;
}
move() {
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.mult(0);
}
rotate() {
this.rotVel.add(this.rotAcc);
this.rot.add(this.rotVel);
this.rotAcc.mult(0);
}
applyForce(f) {
let force = f.copy();
if (this.mass > 0) {
force.div(this.mass);
}
this.acc.add(force);
}
update() {
this.mesh.position.set(this.pos.x, this.pos.y, this.pos.z);
this.mesh.rotation.set(this.rot.x, this.rot.y, this.rot.z);
this.mesh.scale.set(this.scl.x, this.scl.y, this.scl.z);
}
}

0 comments on commit 4917625

Please sign in to comment.