Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions physics/box2d_distance_joint/all.texture_profiles
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
path_settings {
path: "**"
profile: "Default"
}
profiles {
name: "Default"
platforms {
os: OS_ID_GENERIC
formats {
format: TEXTURE_FORMAT_RGBA
compression_level: BEST
compression_type: COMPRESSION_TYPE_DEFAULT
}
mipmaps: false
max_texture_size: 0
premultiply_alpha: true
}
}
95 changes: 95 additions & 0 deletions physics/box2d_distance_joint/box2d_v2.appmanifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
platforms:
arm64-ios:
context:
excludeLibs: []
excludeSymbols: []
symbols: []
libs: []
frameworks: []
linkFlags: []
x86_64-ios:
context:
excludeLibs: []
excludeSymbols: []
symbols: []
libs: []
frameworks: []
linkFlags: []
armv7-android:
context:
excludeLibs: []
excludeJars: []
excludeSymbols: []
symbols: []
libs: []
linkFlags: []
jetifier: true
arm64-android:
context:
excludeLibs: []
excludeJars: []
excludeSymbols: []
symbols: []
libs: []
linkFlags: []
jetifier: true
arm64-osx:
context:
excludeLibs: []
excludeSymbols: []
symbols: []
libs: []
frameworks: []
linkFlags: []
x86_64-osx:
context:
excludeLibs: []
excludeSymbols: []
symbols: []
libs: []
frameworks: []
linkFlags: []
x86_64-linux:
context:
excludeLibs: []
excludeSymbols: []
symbols: []
libs: []
linkFlags: []
arm64-linux:
context:
excludeLibs: []
excludeSymbols: []
symbols: []
libs: []
linkFlags: []
x86-win32:
context:
excludeLibs: []
excludeSymbols: []
symbols: []
libs: []
linkFlags: []
x86_64-win32:
context:
excludeLibs: []
excludeSymbols: []
symbols: []
libs: []
linkFlags: []
wasm-web:
context:
excludeLibs: []
excludeJsLibs: []
excludeSymbols: []
symbols: []
libs: []
linkFlags: []
wasm_pthread-web:
context:
excludeLibs: []
excludeJsLibs: []
excludeSymbols: []
symbols: []
libs: []
linkFlags: []
100 changes: 100 additions & 0 deletions physics/box2d_distance_joint/box2d_v3.appmanifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
platforms:
armv7-ios:
context:
excludeLibs: [physics, box2d_defold, script_box2d_defold]
excludeSymbols: []
libs: [physics_2d, box2d, script_box2d, physics_3d]
arm64-ios:
context:
excludeLibs: [physics, box2d_defold, script_box2d_defold]
excludeSymbols: []
symbols: []
libs: [physics_2d, box2d, script_box2d, physics_3d]
frameworks: []
linkFlags: []
x86_64-ios:
context:
excludeLibs: [physics, box2d_defold, script_box2d_defold]
excludeSymbols: []
symbols: []
libs: [physics_2d, box2d, script_box2d, physics_3d]
frameworks: []
linkFlags: []
armv7-android:
context:
excludeLibs: [physics, box2d_defold, script_box2d_defold]
excludeJars: []
excludeSymbols: []
symbols: []
libs: [physics_2d, box2d, script_box2d, physics_3d]
linkFlags: []
jetifier: true
arm64-android:
context:
excludeLibs: [physics, box2d_defold, script_box2d_defold]
excludeJars: []
excludeSymbols: []
symbols: []
libs: [physics_2d, box2d, script_box2d, physics_3d]
linkFlags: []
jetifier: true
arm64-osx:
context:
excludeLibs: [physics, box2d_defold, script_box2d_defold]
excludeSymbols: []
symbols: []
libs: [physics_2d, box2d, script_box2d, physics_3d]
frameworks: []
linkFlags: []
x86_64-osx:
context:
excludeLibs: [physics, box2d_defold, script_box2d_defold]
excludeSymbols: []
symbols: []
libs: [physics_2d, box2d, script_box2d, physics_3d]
frameworks: []
linkFlags: []
x86_64-linux:
context:
excludeLibs: [physics, box2d_defold, script_box2d_defold]
excludeSymbols: []
symbols: []
libs: [physics_2d, box2d, script_box2d, physics_3d]
linkFlags: []
arm64-linux:
context:
excludeLibs: [physics, box2d_defold, script_box2d_defold]
excludeSymbols: []
symbols: []
libs: [physics_2d, box2d, script_box2d, physics_3d]
linkFlags: []
x86-win32:
context:
excludeLibs: [libphysics, libbox2d_defold, script_box2d_defold]
excludeSymbols: []
symbols: []
libs: [libphysics_2d.lib, libbox2d.lib, libscript_box2d.lib, libphysics_3d.lib]
linkFlags: []
x86_64-win32:
context:
excludeLibs: [libphysics, libbox2d_defold, script_box2d_defold]
excludeSymbols: []
symbols: []
libs: [libphysics_2d.lib, libbox2d.lib, libscript_box2d.lib, libphysics_3d.lib]
linkFlags: []
wasm-web:
context:
excludeLibs: [physics, box2d_defold, script_box2d_defold]
excludeJsLibs: []
excludeSymbols: []
symbols: []
libs: [physics_2d, box2d, script_box2d, physics_3d]
linkFlags: []
wasm_pthread-web:
context:
excludeLibs: [physics, box2d_defold, script_box2d_defold]
excludeJsLibs: []
excludeSymbols: []
symbols: []
libs: [physics_2d, box2d, script_box2d, physics_3d]
linkFlags: []
58 changes: 58 additions & 0 deletions physics/box2d_distance_joint/example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
tags: physics, box2d
title: Box2D Distance Joint
brief: Compare a rigid distance joint with the same joint configured as a damped spring.
author_ids:
- defold-foundation
scripts: box2d_distance_joint.script
thumbnail: thumbnail.webp
---

A distance joint keeps two body anchor points at a specified distance. It can also behave like a damped spring when spring behavior, hertz, and damping ratio are configured.

This example suspends two platforms from pairs of distance joints. Identical balls drop onto both at the same time: the rigid platform preserves both joint lengths, while the spring platform stretches, rebounds, oscillates, and settles.

The balls are dropped automatically every 5.5 seconds.

## What You'll Learn

- How to create distance joints with `b2d.joint.create_distance()`
- How local anchors connect a platform at two distinct attachment points
- How `enable_spring`, `hertz`, and `damping_ratio` configure rigid and spring behavior
- How to read current world-space joint anchors for visualization
- How to reset existing physics bodies for a repeated demonstration
- How to repeat an action with `timer.delay()`

## Setup

The collection contains two arrangements separated by a static wall divider:

`rigid_left_anchor`, `rigid_right_anchor`, and `rigid_platform`
: Two static supports and one dynamic platform connected by rigid distance joints.

`spring_left_anchor`, `spring_right_anchor`, and `spring_platform`
: An identical arrangement whose distance joints use a spring frequency of 1.35 Hz and a damping ratio of 0.25.

`rigid_ball` and `spring_ball`
: Identical dynamic balls that are reset at the same height to drop on the platforms in the scene.

`controller`
: Contains the shared script and the labels with information, like title, hint, `RIGID`, and `SPRING`.

`wall`
: A static vertical divider between the two arrangements.

![setup](setup.png)

The `game.project` of this example is configured to build with `/box2d_v3.appmanifest` by default.
To test V2 locally after downloading the example, change `Native Extensions -> App Manifest` in `game.project` to `/box2d_v2.appmanifest`.

## How It Works

Each platform is connected at two points near its ends. A distance joint constrains only the separation between its two anchors, so the platform remains free to swing, tilt, and rotate. This differs from a revolute joint, which brings two local anchors together at one shared hinge point while allowing relative rotation.

For the rigid setup, both joints have a fixed length and spring behavior is disabled. The upper anchors are closer together than the platform attachment points, which lets an off-center impact visibly couple sideways swing with platform rotation. For the spring setup, the same length is used as the spring rest length, with a given frequency and a damping ratio. Hertz controls how quickly the spring responds and therefore how stiff it feels. Damping ratio controls how quickly oscillation is removed.

Every frame, the script calls `b2d.joint.get_anchor_a()` and `b2d.joint.get_anchor_b()` and draws a line between those current world-space positions. The lower endpoints therefore stay attached to the correct points as each platform moves and rotates.

The same two authored ball bodies are moved back above the platforms every few seconds by a repeating `timer.delay()`. Their linear and angular motion is reset, so the comparison repeats without factories or accumulating objects.
41 changes: 41 additions & 0 deletions physics/box2d_distance_joint/example/anchor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
embedded_components {
id: "sprite"
type: "sprite"
data: "default_animation: \"pixel_gold\"\n"
"material: \"/builtins/materials/sprite.material\"\n"
"size {\n"
" x: 24.0\n"
" y: 24.0\n"
"}\n"
"size_mode: SIZE_MODE_MANUAL\n"
"textures {\n"
" sampler: \"texture_sampler\"\n"
" texture: \"/textures/textures.atlas\"\n"
"}\n"
""
}
embedded_components {
id: "collisionobject"
type: "collisionobject"
data: "type: COLLISION_OBJECT_TYPE_STATIC\n"
"mass: 0.0\n"
"friction: 0.5\n"
"restitution: 0.0\n"
"group: \"default\"\n"
"mask: \"default\"\n"
"embedded_collision_shape {\n"
" shapes {\n"
" shape_type: TYPE_BOX\n"
" position {\n"
" }\n"
" rotation {\n"
" }\n"
" index: 0\n"
" count: 3\n"
" }\n"
" data: 12.0\n"
" data: 12.0\n"
" data: 10.0\n"
"}\n"
""
}
39 changes: 39 additions & 0 deletions physics/box2d_distance_joint/example/ball.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
embedded_components {
id: "sprite"
type: "sprite"
data: "default_animation: \"ball\"\n"
"material: \"/builtins/materials/sprite.material\"\n"
"size {\n"
" x: 64.0\n"
" y: 64.0\n"
"}\n"
"size_mode: SIZE_MODE_MANUAL\n"
"textures {\n"
" sampler: \"texture_sampler\"\n"
" texture: \"/textures/textures.atlas\"\n"
"}\n"
""
}
embedded_components {
id: "collisionobject"
type: "collisionobject"
data: "type: COLLISION_OBJECT_TYPE_DYNAMIC\n"
"mass: 2.0\n"
"friction: 0.5\n"
"restitution: 0.15\n"
"group: \"default\"\n"
"mask: \"default\"\n"
"embedded_collision_shape {\n"
" shapes {\n"
" shape_type: TYPE_SPHERE\n"
" position {\n"
" }\n"
" rotation {\n"
" }\n"
" index: 0\n"
" count: 1\n"
" }\n"
" data: 32.0\n"
"}\n"
""
}
Loading
Loading