My current challenge involves creating a car game here. The issue is that although the wheels are rotating, they are not moving in sync with the car itself. Here is the code snippet from my car.js file:
export function createCar(x, y, z, color) {
const geometry = new THREE.BoxGeometry(0, 0, 0);
const material = new THREE.MeshBasicMaterial();
const carCenter = new THREE.Mesh(geometry, material);
carCenter.position.set(x + 2.5, y, z + 1.25)
let objects = [carCenter];
function createWheel(x, y, z) {
createTorus(0.5, 0.25, "#36353a", x, y, z);
createTorus(0.25, 0.25, "gray", x, y, z);
}
function createBody() {
createRectangle(5.5, 3, 1, color, x + 2.5, y + 1, z + 1.25);
createRectangle(2.5, 3, 1, "#e1ebe3", x + 1.75, y + 2, z + 1.25)
}
function createTorus(radius, innerRadius, color, x, y, z) {
const geometry = new THREE.TorusGeometry(radius, innerRadius, 16, 100);
const material = new THREE.MeshLambertMaterial({ color: color });
const torus = new THREE.Mesh(geometry, material);
torus.position.set(x, y, z);
objects.push(torus);
return torus;
}
function createRectangle(length, width, height, color, x, y, z) {
const geometry = new THREE.BoxGeometry(length, height, width);
const material = new THREE.MeshLambertMaterial({ color: color });
const rect = new THREE.Mesh(geometry, material);
rect.position.set(x, y, z);
objects.push(rect);
return rect;
}
function createCylinder(radius, height, color, x, y, z) {
const geometry = new THREE.CylinderGeometry(radius, radius, height, 32);
const material = new THREE.MeshLambertMaterial({color: color});
const cylinder = new THREE.Mesh(geometry, material);
cylinder.position.set(x, y, z);
objects.push(cylinder);
return cylinder
}
createWheel(x, y, z);
createWheel(x + 5, y, z);
createWheel(x, y, z + 2.5);
createWheel(x + 5, y, z + 2.5);
createBody();
function turnRight() {
objects.forEach(part => {
part.rotation.y += 0.1;
})
}
return {
objects,
turnRight,
}
}
I have experimented with using sin, cosine, and combining meshes without success. Despite spending hours on the problem, I have been unable to make the wheels move harmoniously with the car. I have also tried determining the position in the corner of a box to set the wheel position accordingly. Are there alternative approaches I could explore?