-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |