Trying to make this Rubik’s cube with Babylonjs and for more than 24 hrs, i can’t get to change the cube’s position using cube.position
.
The entire Rubik’s cube is made up of 27 smaller cubes(you know what way i’m headed already).
i can’t get to rotate some of these smaller cubes that make a face.
see how i’m trying to make this update:
updateCubePositions(cubes: Cube[], rotatedMeshes: BABYLON.Mesh[]) {
// Loop through each cube and update its position
for (let i = 0; i < cubes.length; i++) {
let cube = cubes[i].mesh;
let rotatedMesh = rotatedMeshes[i];
let newPosition = rotatedMesh.position;
// Update the position of the cube
cube.updatePosition(newPosition);
}
console.log(
"Final Cube Positions:",
cubes.map((cube) => cube.position)
);
console.log(
"Final Rotated Mesh Positions:",
rotatedMeshes.map((mesh) => mesh.position)
);
}
and this is my Cube class:
import * as BABYLON from "@babylonjs/core";
export class Cube {
position: BABYLON.Vector3;
mesh: BABYLON.Mesh;
materials: BABYLON.MultiMaterial;
constructor(position: BABYLON.Vector3, scene: BABYLON.Scene) {
this.position = position;
this.mesh = this.createCubeMesh(scene);
this.materials = this.createCubeMaterials(scene);
this.applyMaterials();
}
// Create cube mesh
createCubeMesh(scene: BABYLON.Scene): BABYLON.Mesh {
const cube = BABYLON.MeshBuilder.CreateBox("cube", { size: 0.5 }, scene);
cube.position = this.position;
cube.enableEdgesRendering();
cube.edgesColor = new BABYLON.Color4(0, 0, 0, 1);
cube.edgesWidth = 2;
// Apply different materials to each face of the box
cube.subMeshes = []; // Reset any default subMeshes
const vertices: BABYLON.Nullable<BABYLON.FloatArray> = cube.getVerticesData(
BABYLON.VertexBuffer.PositionKind
);
// Create sub-meshes for each face
for (let i = 0; i < 6; i++) {
new BABYLON.SubMesh(i, 0, vertices?.length! / 3, i * 6, 6, cube);
}
return cube;
}
// Create cube materials
createCubeMaterials(scene: BABYLON.Scene): BABYLON.MultiMaterial {
const redMaterial = new BABYLON.StandardMaterial("redMaterial", scene);
redMaterial.diffuseColor = BABYLON.Color3.Red();
const greenMaterial = new BABYLON.StandardMaterial("greenMaterial", scene);
greenMaterial.diffuseColor = BABYLON.Color3.Green();
const blueMaterial = new BABYLON.StandardMaterial("blueMaterial", scene);
blueMaterial.diffuseColor = BABYLON.Color3.Blue();
const yellowMaterial = new BABYLON.StandardMaterial(
"yellowMaterial",
scene
);
yellowMaterial.diffuseColor = BABYLON.Color3.Yellow();
const whiteMaterial = new BABYLON.StandardMaterial("whiteMaterial", scene);
whiteMaterial.diffuseColor = BABYLON.Color3.White();
const orangeMaterial = new BABYLON.StandardMaterial(
"orangeMaterial",
scene
);
orangeMaterial.diffuseColor = new BABYLON.Color3(1, 0.647, 0); // Orange
// Apply the materials to the faces of the cube
const multiMaterial = new BABYLON.MultiMaterial("multiMat", scene);
multiMaterial.subMaterials = [
blueMaterial, // Front face
greenMaterial, // Back face
redMaterial, // Left face
orangeMaterial, // Right face
whiteMaterial, // Top face
yellowMaterial, // Bottom face
];
return multiMaterial;
}
// Apply the materials to the cube's mesh
applyMaterials() {
this.mesh.material = this.materials;
}
// Update the cube's position
updatePosition(newPosition: BABYLON.Vector3) {
this.mesh.position = newPosition;
}
// Update materials based on new cube position (after rotating the face)
updateCubeMaterial(newMaterials: BABYLON.MultiMaterial) {
this.mesh.material = newMaterials;
}
}
Please what am i doing wrong, i’m not happy with myself i can’t get this to work, and i need help
PLAYGROUND: Babylon.js Playground