Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Springy Boxes and Diff Rotational Speeds #37

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ let pixR = window.devicePixelRatio ? window.devicePixelRatio : 1;
let cubes = [];
let sceneOffsetTarget = {x: 0, y: 0};
let sceneOffset = {x: 0, y: 0};
const springConstant = 0.01;
const dampingConstant = 0.95;

let today = new Date();
today.setHours(0);
Expand Down Expand Up @@ -132,6 +134,19 @@ else
cube.position.x = win.shape.x + (win.shape.w * .5);
cube.position.y = win.shape.y + (win.shape.h * .5);

// Check if rotation speeds exist in localStorage
if (localStorage.getItem(`cube${i}RotationSpeedX`) && localStorage.getItem(`cube${i}RotationSpeedY`)) {
cube.rotationSpeedX = parseFloat(localStorage.getItem(`cube${i}RotationSpeedX`));
cube.rotationSpeedY = parseFloat(localStorage.getItem(`cube${i}RotationSpeedY`));
} else {
cube.rotationSpeedX = 0.01 + Math.random() * 0.01; // Initialize rotation speed
cube.rotationSpeedY = 0.02 + Math.random() * 0.01; // Initialize rotation speed
// Save rotation speeds to localStorage
localStorage.setItem(`cube${i}RotationSpeedX`, cube.rotationSpeedX);
localStorage.setItem(`cube${i}RotationSpeedY`, cube.rotationSpeedY);
}
cube.velocity = new THREE.Vector3(0, 0, 0);

world.add(cube);
cubes.push(cube);
}
Expand Down Expand Up @@ -163,21 +178,32 @@ else

let wins = windowManager.getWindows();


// loop through all our cubes and update their positions based on current window positions
for (let i = 0; i < cubes.length; i++)
{
for (let i = 0; i < cubes.length; i++) {
let cube = cubes[i];
let win = wins[i];
let _t = t;// + i * .2;

let posTarget = {x: win.shape.x + (win.shape.w * .5), y: win.shape.y + (win.shape.h * .5)}

cube.position.x = cube.position.x + (posTarget.x - cube.position.x) * falloff;
cube.position.y = cube.position.y + (posTarget.y - cube.position.y) * falloff;
cube.rotation.x = _t * .5;
cube.rotation.y = _t * .3;
};
// Calculate the spring force
let springForceX = (posTarget.x - cube.position.x) * springConstant;
let springForceY = (posTarget.y - cube.position.y) * springConstant;

// Apply the spring force to the velocity
cube.velocity.x += springForceX;
cube.velocity.y += springForceY;

// Apply damping to the velocity
cube.velocity.x *= dampingConstant;
cube.velocity.y *= dampingConstant;

// Update the position based on the velocity
cube.position.x += cube.velocity.x;
cube.position.y += cube.velocity.y;

cube.rotation.x += cube.rotationSpeedX; // Use the previously initialized speed
cube.rotation.y += cube.rotationSpeedY; // Use the previously initialized speed
}

renderer.render(scene, camera);
requestAnimationFrame(render);
Expand Down