logo
Spin

movement · physics-puzzle

Spin

Free

Install with the CLI:

bjs download Spin

Spin

Turns a MeshPrimitive at a steady rate: a ring on a fixed axis, or a rock tumbling on an axis picked for it.

What it does

Spin: {} turns the mesh half a radian a second about Y. rate sets radians per second about x, y and z. Set tumble above 0 and rate is ignored: the System picks three rates for the entity, pointing in a random direction and tumble long in total.

That pick is easy to get wrong. Roll each axis with Math.random() and the rocks turn at different speeds and favour the diagonals. tumbleRate picks a direction evenly over the whole sphere and scales it to tumble, so no axis is favoured and every rock turns at about the same speed:

TypeScript
tumbleRate('rock-3', 1, 2.4); // [2.01, 0.97, -0.89], length 2.4
tumbleRate('rock-3', 2, 2.4); // [0.28, -2.15, 1.02]: the same rock, next time it enters play
tumbleRate('rock-4', 1, 2.4); // [1.07, 1.57, -1.46]: the slot next door

The pick hashes the entity id with a count of how often it has entered play. A pooled rock gets a new axis each time it is handed out, so a stream never tumbles in lockstep.

The angle lives in MeshPrimitive.rotation, so a rock parked mid-turn comes back where it stopped.

Use it in a scene

The SpaceGolf plant turns on two axes, and its rocks tumble:

A scene is just data — a list of entities, each with its components. At startup the SceneLoader turns this JSON into a live world; edit the file and reload to rebuild it.

Build it from scratch with the bjs CLI:

  1. 1Scaffold a project
    npm create @babylonjsmarket/arcade@latest my-game

    World, renderer, and dev server — ready to run.

  2. 2Install dependencies
    cd my-game && npm install
  3. 3Add Spin
    bjs download Spin

    Copies its source into src/ so the scene resolves.

    First time? Run bjs login once.

  4. 4Paste the scene into src/scenes/arcade-room.ts and run
    npm run dev

    SceneLoader builds the world from the JSON; reload to rebuild.

JSON
{
  "Plant": {
    "components": {
      "MeshPrimitive": { "primitive": "torus", "diameter": 4.6, "thickness": 0.5, "tessellation": 48 },
      "Spin": { "rate": [0.55, 0.35, 0] }
    }
  },
  "Rock": {
    "components": {
      "MeshPrimitive": { "primitive": "sphere", "diameter": 1, "segments": 8 },
      "Spin": { "tumble": 2.4 }
    }
  }
}

Props

  • rate ([x, y, z], default [0, 0.5, 0]) — radians per second about x, y and z. A live edit takes effect the next frame.
  • tumble (number, default 0) — above 0, ignore rate and tumble at this many radians per second, on an axis picked again each time the entity enters play.

Events

None either way.

Dependencies

  • MeshPrimitive — holds the angle and the mesh. Spin does nothing without one.

Notes

  • tumble is read when the entity enters play. A live change waits for the next entry, except a drop to 0, which switches to rate the next frame.
  • On the frame an entity enters play, Spin moves the angle but leaves the mesh write to MeshPrimitive's restore, so the two never write the same mesh in one frame.
  • The rates add straight onto the Euler angles. With two or three axes turning, that is a wobbling tumble rather than a turn about one fixed line, and Babylon (YXZ order) and Three.js (XYZ) draw it a little differently. A spin on one axis looks the same in both.

More like this

MeshPrimitive
AiDriver
AiPilot
Animation
ArcCamera
Asteroid
AxisViewCamera
BlackHole
BoatMode
Bullet
CameraFollow
CaptureZone
CarDrive
DirectionalLight
EnemySpawner
EnvironmentTexture
FreighterCar
FreighterChain
FreighterHead
GameConfig
GravityBody
GravityLens
GravityMass
Hand
Health
HemisphericLight
Jump
KeyboardInput
KeyboardMover
Lifetime
LineOfSight
MachineGun
MassDriver
Mesh
Missile
MissileLauncher
MissionDirector
MotionTrail
MouseInput
Movement
Obstacle
ObstacleField
OilBlob
OrbitMover
Parts
Physics
PlayerInput
PlayerWalkAnimator
PointerDrag
RespawnTimer
RoundReset
Score
Shadow
ShipFlight
ShipLoadout
SkeletonAnimator
SpaceGolfDirector
SpaceGolfHUD
SpaceRock
SpaceShooterBullet
SpaceShooterHealth
SpaceShooterScore
TrafficConvoy
TrajectoryPreview
Transform6DOF
TwinStickEnemy
Velocity6DOF
WaveDirector
Waypoint
WaypointTrack
WhiteHole
WorldOriginAnchor
Wormhole

Was this page helpful?

We read every note — tell us what's working and what isn't.

↑↓ NavigateEnter SelectEsc CloseCtrl+K Open Search