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_revolute_bridge/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_revolute_bridge/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_revolute_bridge/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: []
52 changes: 52 additions & 0 deletions physics/box2d_revolute_bridge/example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
tags: physics, box2d
title: Box2D Revolute Bridge
brief: Create a bridge from revolute joints between existing Defold collision objects.
author: Defold Foundation
scripts: box2d_revolute_bridge_v3.script, box2d_revolute_bridge_v2.script
thumbnail: thumbnail.webp
---

This example builds a simple rope bridge from rectangular dynamic bodies connected with Box2D revolute joints.
A ball drops onto the bridge so the hinge chain bends under load.

Click or tap the window to reset the ball.

## What You'll Learn

- How to get Box2D bodies from Defold collision objects with `b2d.get_body()`
- How to connect two bodies with a `revolute` type joint
- How local joint anchors define hinge points on each connected body

## Setup

The collection contains these game objects:

`controller`
: Contains both backend scripts, `box2d_revolute_bridge_v2.script` and `box2d_revolute_bridge_v3.script`, plus the title and hint labels. Each script checks the active Box2D version and only one script runs.

`left_anchor` and `right_anchor`
: Static collision objects used as the fixed bridge supports.

`segment_01` to `segment_10`
: Dynamic rectangular collision objects. The active script creates revolute joints between neighboring segments.

`ball`
: A dynamic circle collision object that falls onto the bridge to show how the joints move.

`floor`, `left_wall`, and `right_wall`
: Static collision objects that keep the simulation in view after the ball falls through or off the bridge.

The project uses `box2d_v3.appmanifest` by default, so it runs with Box2D V3.
To use the older legacy V2 backend, switch the native extension app manifest in `game.project` to `/box2d_v2.appmanifest`.

![setup](setup.png)

## How It Works

Both scripts use the same collection-authored bodies. `b2d.get_body()` returns the Box2D body owned by each collision object, and `b2d.joint.create_revolute()` creates the hinge constraints at runtime.

Each bridge segment is 48 pixels wide. The script connects the right edge of one body to the left edge of the next body by using local anchors: `(24, 0, 0)` on the previous segment and `(-24, 0, 0)` on the current segment.
The first and last joints connect to static anchors at the bridge supports.

On reset, the script moves the ball up and drops it onto the bridge again. The joints are created once in `init()` and destroyed in `final()`.
41 changes: 41 additions & 0 deletions physics/box2d_revolute_bridge/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: 22.0\n"
" y: 22.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.4\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: 11.0\n"
" data: 11.0\n"
" data: 10.0\n"
"}\n"
""
}
42 changes: 42 additions & 0 deletions physics/box2d_revolute_bridge/example/ball.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
embedded_components {
id: "sprite"
type: "sprite"
data: "default_animation: \"ball\"\n"
"material: \"/builtins/materials/sprite.material\"\n"
"size {\n"
" x: 254.0\n"
" y: 254.0\n"
"}\n"
"textures {\n"
" sampler: \"texture_sampler\"\n"
" texture: \"/textures/textures.atlas\"\n"
"}\n"
""
scale {
x: 0.5
y: 0.5
}
}
embedded_components {
id: "collisionobject"
type: "collisionobject"
data: "type: COLLISION_OBJECT_TYPE_DYNAMIC\n"
"mass: 1.0\n"
"friction: 0.35\n"
"restitution: 0.2\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