sketch.js: replace spheres with actual models + in-place rotation
This commit is contained in:
parent
10206177c7
commit
6fd82fd0d7
28
sketch.js
28
sketch.js
|
|
@ -15,9 +15,9 @@ class RotatingOnion {
|
|||
}
|
||||
|
||||
// Add an orbiting object to the onion
|
||||
addOrbitingObject(orbitRadius, speed, scale, tiltAngleX, tiltAngleY) {
|
||||
addOrbitingObject(model, orbitRadius, speed, scale, tiltAngleX, tiltAngleY) {
|
||||
const tiltAngle = { x: tiltAngleX, y: tiltAngleY }; // Pass both X and Y tilt angles
|
||||
this.orbitingObjects.push(new OrbitingObject(this, orbitRadius, speed, scale, tiltAngle));
|
||||
this.orbitingObjects.push(new OrbitingObject(this, model, orbitRadius, speed, scale, tiltAngle));
|
||||
}
|
||||
|
||||
update() {
|
||||
|
|
@ -70,11 +70,18 @@ class RotatingOnion {
|
|||
}
|
||||
|
||||
class OrbitingObject {
|
||||
constructor(parent, orbitRadius, speed, scale, tiltAngle) {
|
||||
constructor(parent, model, orbitRadius, speed, scale, tiltAngle) {
|
||||
this.parent = parent; // The parent object (Onion)
|
||||
this.model = model
|
||||
this.orbitRadius = orbitRadius;
|
||||
this.speed = speed; // Orbiting speed (angular speed)
|
||||
this.scale = scale;
|
||||
this.rotationAngle_x = random(360);
|
||||
this.rotationAngle_y = random(360);
|
||||
this.rotationAngle_z = random(360);
|
||||
this.rotationSpeed_x = random(0.5, 2);
|
||||
this.rotationSpeed_y = random(0.5, 2);
|
||||
this.rotationSpeed_z = random(0.5, 2);
|
||||
|
||||
this.angle = random(360); // Start at a random position in the orbit
|
||||
this.tiltAngle = tiltAngle;
|
||||
|
|
@ -84,6 +91,11 @@ class OrbitingObject {
|
|||
// Update the angle to simulate rotation around the parent
|
||||
this.angle += this.speed;
|
||||
|
||||
// Rotate in place
|
||||
this.rotationAngle_x += this.rotationSpeed_x;
|
||||
this.rotationAngle_y += this.rotationSpeed_y;
|
||||
this.rotationAngle_z += this.rotationSpeed_z;
|
||||
|
||||
// Calculate the position of the orbiting object in the un-tilted plane
|
||||
const x = this.orbitRadius * cos(this.angle);
|
||||
const z = this.orbitRadius * sin(this.angle);
|
||||
|
|
@ -109,7 +121,14 @@ class OrbitingObject {
|
|||
display() {
|
||||
push();
|
||||
translate(this.x, this.y, this.z); // Move to the correct orbit position
|
||||
sphere(this.scale); // Display the object (could be another model or shape)
|
||||
|
||||
// Apply in-place rotation
|
||||
rotateX(this.rotationAngle_x)
|
||||
rotateY(this.rotationAngle_y);
|
||||
rotateZ(this.rotationAngle_z)
|
||||
|
||||
scale(this.scale);
|
||||
model(this.model);
|
||||
pop();
|
||||
}
|
||||
}
|
||||
|
|
@ -134,6 +153,7 @@ function setup() {
|
|||
// Add orbiting objects to the onion
|
||||
for (let i = 1.8; i <= 4.8; i += 0.6) {
|
||||
onionObj.addOrbitingObject(
|
||||
onion_fill,
|
||||
i,
|
||||
random(0.1, 0.8),
|
||||
globalScaleVar * 0.0001 * random(1, 6),
|
||||
|
|
|
|||
Loading…
Reference in a new issue