What you'll learn
- The three settings every 3D object carries: position (where), rotation (which way), and scale (how big).
- Why three.js measures turns in radians, how many make a quarter, half, and full turn, and the one line that converts from degrees.
- How THREE.Group lets objects ride along with a parent, so a moon can orbit a planet that orbits a sun in three lines.
- The difference between an object's position inside its parent and its position in the world, which is behind most "why is it over there?" bugs.
Three settings, one toy on a table
Everything you have built so far sat at the center of the scene. That is fine for a demo and useless for a game. A game is objects moving, turning, and growing: a car rolling down a road, a door swinging open, a player crouching before a jump. In three.js, all of that comes down to three settings every object carries.
Picture a toy on a table. You can slide it somewhere else: that is position. You can turn it to face a different way: that is rotation. You can swap it for a bigger or smaller copy: that is scale. Every mesh, light, camera, and group has all three, because they all inherit from one class called Object3D. You have already used it: camera.position.z = 5 in Part 1 was this.
Each of the three has an .x, a .y, and a .z, one per axis from Part 2: x is right, y is up, z is toward you. Set all three at once with .set(x, y, z) or change one with cube.position.x = 2. There is no "apply" step and no move function. You change a number, and the next frame draws whatever the numbers say.
Now watch the hero stage. The moon traces a loopy path that would be a nightmare to compute by hand, and the code behind it is three lines that each add a small number to one rotation.y. That works because of the second idea on this page: objects can be placed inside other objects, and then they move, turn, and grow with them. Put the planet in a THREE.Group, spin the group, and the planet orbits.
Simple definition: position, rotation, and scale are an object's three transforms: where it is, which way it faces, and how big it is. A parent is an object that others have been added to; those children are drawn relative to it, so transforming the parent transforms everything inside it.
Move, turn, resize, then nest
We keep the setup from Part 1 (scene, camera, renderer, animation loop) and borrow two lights from Part 5. First one mint cube goes through all three transforms. Then we build the solar system, where the transforms start to feel like a superpower.
1. Position: where it is
Position is the easy one. The units are whatever you decide; most games say 1 unit is 1 meter. cube.position.set(2, 0.5, -1) puts the cube 2 units right, half a unit up, and 1 unit farther from you. Why half a unit up? Position is the center of the object, so a cube 1 unit tall sits at y = 0.5 for its bottom to touch the ground. Movement is just position changing over time: cube.position.x += 0.05 in the animation loop slides the cube a little right every frame. (Faster screens slide it faster; Part 7 fixes that with delta time.)
2. Rotation: which way, in radians
This lesson will save you more debugging time than any other in the series. Three.js measures angles in radians, not degrees. A radian is the angle you get by walking one radius-length along a circle's edge, and a circle's edge is 2π radius-lengths long, so a full turn is 2 * Math.PI radians, about 6.28. A half turn is Math.PI (3.14), a quarter turn Math.PI / 2 (1.57). If you think in degrees, convert: degrees * Math.PI / 180, or let three.js do it with THREE.MathUtils.degToRad(90).
A rotation is always around an axis, and the axis is the one thing that does not move.
rotation.y
a record on a tableThe axis points up, so the object turns flat, like a record or a character turning to face a new direction. The rotation games use most.
rotation.x
tips forward and backThe axis points right, so the object nods: its top leans toward or away from you. A ramp, a flipping coin, a see-saw.
rotation.z
a steering wheelThe axis points at you, so the object rolls like a steering wheel or a plane banking. Handy for 2D-style games viewed head-on.
When you set two or three at once, three.js applies them in a fixed order, x then y then z by default (rotation.order is 'XYZ'). You will rarely notice, but if a combined rotation ever looks "off", that order is why.
3. Scale: how big
Scale starts at 1, not 0, because 1 means "normal size" and 0 would make the object vanish. cube.scale.set(2, 2, 2) doubles it in every direction. The three numbers do not have to match: cube.scale.y = 0.5 squashes only the height, so a cube becomes a slab. A negative scale mirrors the object, usually by accident. Uneven scale matters more than it sounds: in Part 19 a jumping player will squash before takeoff and stretch in the air, the "squash and stretch" that makes cartoon motion feel alive.
// Every mesh (every Object3D) carries position, rotation, and scale const cube = new THREE.Mesh( new THREE.BoxGeometry(1, 1, 1), new THREE.MeshStandardMaterial({ color: 0x34d399 }) ); scene.add(cube); // WHERE: 2 right, half a unit up (so it sits on the ground), 1 away from you cube.position.set(2, 0.5, -1); cube.position.x += 0.05; // one small step to the right // WHICH WAY: rotations are in RADIANS, not degrees cube.rotation.y = Math.PI / 2; // a quarter turn (90°) around the up axis cube.rotation.x = Math.PI; // a half turn (180°): tipped upside down // Degrees to radians, two ways function degToRad(degrees) { return degrees * Math.PI / 180; } cube.rotation.z = degToRad(45); cube.rotation.z = THREE.MathUtils.degToRad(45); // same thing, built in // HOW BIG: 1 is normal size cube.scale.set(2, 2, 2); // twice as big every way cube.scale.y = 0.5; // then squash only the height // Draw the cube's OWN axes: a child, so they turn and squash with it cube.add(new THREE.AxesHelper(1.2));
"One object" mode below runs these lines with each number on a slider. The AxesHelper is the red, green, blue arrows from Part 2 (x, y, z), attached to the cube instead of the world: your first taste of parenting.
4. Groups: the unlock
add() works on any object, not just the scene. cube.add(helper) made the helper a child of the cube, and a child follows its parent: when the cube moves, turns, or scales, the helper goes with it. The child's own position is measured from the parent, not from the world.
A THREE.Group is an object with no shape at all, made to be a parent. Think of it as an invisible turntable: place it, put things on it, spin it. That is the hero stage. A group called planetOrbit sits at the sun's center with the planet as its child at x = 4; spin the group and the planet circles the sun. A second group, moonOrbit, is also a child of planetOrbit, parked at the same x = 4 so it rides along, and the moon is its child at x = 1.2. Spin moonOrbit and the moon circles the planet wherever it happens to be. Nothing here computes an orbit; each object knows one small number, and the nesting does the rest.
The same pattern is everywhere in games. A car is a group holding a body and four wheels: move the group and the wheels come along, yet each wheel can still spin. A sword is a child of the arm, so swinging the arm swings the sword.
// The sun: the root of everything. It glows on its own (emissive, Part 4) // and carries a PointLight (Part 5) so it lights the planet and moon. const sun = new THREE.Mesh( new THREE.SphereGeometry(1, 48, 32), new THREE.MeshStandardMaterial({ color: 0xfbbf24, emissive: 0xfbbf24, emissiveIntensity: 1.4 }) ); const sunLight = new THREE.PointLight(0xffd27a, 60, 0, 2); sun.add(sunLight); // a child of the sun, so it stays inside it scene.add(sun, new THREE.AmbientLight(0xffffff, 0.2)); // An invisible turntable at the sun's center. Spin it and everything on it swings. const planetOrbit = new THREE.Group(); scene.add(planetOrbit); const planet = new THREE.Mesh( new THREE.IcosahedronGeometry(0.45, 1), new THREE.MeshStandardMaterial({ color: 0x34d399, flatShading: true }) ); planet.position.x = 4; // 4 units from the TURNTABLE's center planetOrbit.add(planet); // A second turntable, parked at the planet's spot, INSIDE the first one const moonOrbit = new THREE.Group(); moonOrbit.position.x = 4; // same spot as the planet, so it rides along planetOrbit.add(moonOrbit); const moon = new THREE.Mesh( new THREE.IcosahedronGeometry(0.16, 1), new THREE.MeshStandardMaterial({ color: 0xcbd5e1, flatShading: true }) ); moon.position.x = 1.2; // 1.2 units from the PLANET, not from the sun moonOrbit.add(moon);
This is exactly what the hero stage is running. The faint orbit rings are a THREE.LineLoop of points around a circle; the moon's ring is added to moonOrbit so it travels with the planet. Notice moonOrbit is a sibling of the planet, not its child. That matters in Common Mistakes.
One more idea, in one sentence: moon.position is the moon's local position, always (1.2, 0, 0) relative to its parent, and moon.getWorldPosition(target) fills a Vector3 with where the moon really is in the scene, which collision checks in Part 15 will need.
Now the payoff: the whole solar system is three lines in the animation loop, each adding a small number to one rotation.y. Bigger number, faster orbit.
function animate() { planetOrbit.rotation.y += 0.01; // the year: swings the planet AND moonOrbit moonOrbit.rotation.y += 0.04; // the month: swings only the moon planet.rotation.y += 0.02; // the day: the planet spins in place renderer.render(scene, camera); } renderer.setAnimationLoop(animate);
The first two numbers are the speed sliders in "Solar system" mode. The planet is a low-poly icosahedron on purpose: a perfectly smooth ball looks the same at every angle, so you could never tell it was spinning.
One cube, then a solar system
One stage, two modes. One object is the cube from transforms.js with its own axes riding on it; the rotation sliders are in degrees, and the live code shows the radians you would actually type. Solar system is the hero scene with every number from solar-system.js and animate.js on a slider, plus a "Show groups" button that draws axes at each turntable's center. Controls that do not apply right now are dimmed.
Loading 3D preview…
rotation.y = 0.00 rad (0°)cube.position.set(0.0, 0.5, 0); cube.rotation.y = 0.00; // 0° = 0 * Math.PI / 180 cube.rotation.x = 0.00; // 0° cube.scale.set(1.00, 1.00, 1.00); cube.scale.y = 1.00; // same as the others: no squash
planetOrbit.rotation.y += 0.010; // the year moonOrbit.rotation.y += 0.040; // the month planet.position.x = 4.0; moonOrbit.position.x = 4.0; // both sit on planetOrbit moon.position.x = 1.20; // measured from the PLANET planet.scale.set(1.00, 1.00, 1.00); // the mesh only, so the moon stays put planetAxes.visible = false; moonAxes.visible = false; // new THREE.AxesHelper() on each group renderer.setAnimationLoop(animate);
- In One object, drag rotation.y to 360°. The cube looks exactly like it did at 0°, arrows and all, because 6.28 radians is one complete lap. Now drag it to 180°: the red and blue arrows have swapped ends. Without the arrows you could not tell a plain cube at 90° from one at 0°, which is why rotation bugs are sneaky.
- Set scale to 2.00, then squash to 0.25. The live code reads scale.set(2, 2, 2) and then scale.y = 0.5, and the cube is a wide pancake. Even the green arrow shrank, because the helper is the cube's child. That is the "squash" half of squash-and-stretch; slide squash to 2.5 for the "stretch" half.
- Switch to Solar system, turn on Show groups, then push Moon distance to 2 and Moon orbit speed to the max. The moon whips around but never leaves the planet, because it is a child of moonOrbit, which is parked exactly where the planet is. The small red arrow always points at the moon, because the moon sits on that group's own x axis. Now slide Planet distance and watch the whole moon system slide with it.
Common mistakes
Transform bugs rarely throw errors. They show up as an object spinning like a blender, sitting somewhere you did not put it, or a moon that wanders off when you resize its planet.
You wrote cube.rotation.y = 90 for a quarter turn, and the cube faces some random direction. In the animation loop, += 1 turns it into a blur.
Fix: radians. 90 radians is about 14.3 full turns, so the cube points wherever the leftover third of a turn lands. Write Math.PI / 2 or THREE.MathUtils.degToRad(90). A rotation number bigger than 7 is probably degrees by accident.
You tried cube.position = new THREE.Vector3(2, 0, 0) to move the cube. In a module you get "Cannot assign to read only property"; in older code it silently does nothing.
Fix: position, rotation, and scale are locked in place on purpose, because three.js watches them. Change what is inside them: cube.position.set(2, 0, 0), or cube.position.copy(someVector).
You set a child's position to where you wanted it in the world, and it showed up somewhere else entirely.
Fix: a child's position is measured from its parent. The moon really is 1.2 from moonOrbit; it is moonOrbit that is out at x = 4. For the world spot, use moon.getWorldPosition(new THREE.Vector3()), and turn on Show groups to see where each parent's center is.
You made the moon a child of the planet, scaled the planet up to 2, and the moon jumped twice as far out and doubled in size too.
Fix: scale is inherited, and it scales a child's position as well as its size, so a moon at 1.2 inside a planet scaled by 2 is really 2.4 away. That is why the demo makes moonOrbit a sibling of the planet, not its child. To resize one thing alone, make sure nothing you care about is inside it.
Check your understanding
You want a helicopter's rotor to spin while the whole helicopter flies forward. How would you structure the objects, and which rotation or position would you change each frame?
Your answer:
Sample answer: make a helicopter group with two children: the body mesh and a rotor mesh on top at rotor.position.y = 1. Each frame, helicopter.position.x += speed moves the whole thing forward and rotor.rotation.y += 0.5 spins the blades. The rotor rides along because it is a child of the group, and its spin stays centered on the helicopter because its position is measured from the group, not the world.
Quick quiz
1. How many radians is a half turn, the rotation that leaves an object facing exactly backward?
2. A moon mesh is a child of a group, and you change the group's position.x from 4 to 6. What happens to the moon?
3. What does player.scale.set(1, 0.5, 1) do to the player?
Keep learning
You can now put an object exactly where you want it, turn it by the right number of radians, resize or squash it, and nest objects in groups so they move together. Next you look inside the animation loop itself: why the same code runs faster on a faster screen, and how delta time keeps your game at one speed everywhere.
Part of the Games and Animations with Three.js tutorial series.