Gravity Attractors

Point gravity

Standard gravity pulls everything down. Attractors pull objects toward a point — like planets, magnets, or black holes.


How it works

Place an attractor anywhere on the canvas. Every dynamic physics body feels a force pulling it toward that point. The force follows inverse-square law — stronger when close, weaker when far.

Force = strength / distance^2
Direction = toward the attractor

title: "Orbital motion"
description: "Objects orbiting a central attractor"
nodes:
  - type: ELLIPSE, name: "Sun", x: 200, y: 200, w: 40, h: 40, fill: "#F39C12"
  - type: ELLIPSE, name: "Planet1", x: 300, y: 200, w: 16, h: 16, fill: "#3498DB"
  - type: ELLIPSE, name: "Planet2", x: 200, y: 100, w: 12, h: 12, fill: "#2ECC71"
modules:
  - node: "Sun", module: "physics", config: { bodyType: "static" }
  - node: "Planet1", module: "physics", config: { bodyType: "dynamic", gravityScale: 0 }
  - node: "Planet2", module: "physics", config: { bodyType: "dynamic", gravityScale: 0 }
attractors:
  - x: 200, y: 200, strength: 5000
tools_visible: [select, play]
ui_visible: [properties, modules]

Properties

PropertyTypeDefaultDescription
xnumber0Attractor X position
ynumber0Attractor Y position
strengthnumber1000Pull force (negative = repel)

Set strength to negative for a repulsor — objects push away instead of pulling in.


Use cases

Planetary orbits

Set world gravity to zero (gravityScale: 0 on all bodies). Place an attractor at the center. Give planets initial perpendicular velocity. They orbit.

Magnetic fields

Attach an attractor to a node's position using formulas. Move the node — the magnetic field moves with it. Dynamic bodies follow.

Black hole effect

High strength + small attractor. Objects spiral inward and accelerate.

Repulsion zones

Negative strength pushes objects away. Use for force fields, shields, explosion effects.


Formula integration

Attractor properties are formula-bindable:

attractor.x = mouse.x // attractor follows cursor
attractor.strength = Slider.value * 100 // slider controls pull force


Multiple attractors

Place as many attractors as you need. Each dynamic body sums forces from all attractors plus world gravity. Bodies can orbit between attractors, creating Lagrange-point-like stable positions.


Implementation

No physics engine has built-in point gravity. The physics system implements attractors as a pre-step pass — before each simulation step, it calculates the force from each attractor on each dynamic body and applies it with applyForce().

for each dynamic body:
for each attractor:
dx = attractor.x - body.x
dy = attractor.y - body.y
distSq = dx*dx + dy*dy
if distSq < 1: skip // avoid singularity
force = strength / distSq
dist = sqrt(distSq)
applyForce(force * dx/dist, force * dy/dist)

Static and kinematic bodies are unaffected.


Back to physicsBodiesJoints

← НазадPhysics BodiesДальше →Built-In Physics