Skip to content

Commit 04c04b2

Browse files
authored
Added revolute joint example (#202)
1 parent fa8ec97 commit 04c04b2

22 files changed

Lines changed: 874 additions & 0 deletions
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
path_settings {
2+
path: "**"
3+
profile: "Default"
4+
}
5+
profiles {
6+
name: "Default"
7+
platforms {
8+
os: OS_ID_GENERIC
9+
formats {
10+
format: TEXTURE_FORMAT_RGBA
11+
compression_level: BEST
12+
compression_type: COMPRESSION_TYPE_DEFAULT
13+
}
14+
mipmaps: false
15+
max_texture_size: 0
16+
premultiply_alpha: true
17+
}
18+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
platforms:
2+
arm64-ios:
3+
context:
4+
excludeLibs: []
5+
excludeSymbols: []
6+
symbols: []
7+
libs: []
8+
frameworks: []
9+
linkFlags: []
10+
x86_64-ios:
11+
context:
12+
excludeLibs: []
13+
excludeSymbols: []
14+
symbols: []
15+
libs: []
16+
frameworks: []
17+
linkFlags: []
18+
armv7-android:
19+
context:
20+
excludeLibs: []
21+
excludeJars: []
22+
excludeSymbols: []
23+
symbols: []
24+
libs: []
25+
linkFlags: []
26+
jetifier: true
27+
arm64-android:
28+
context:
29+
excludeLibs: []
30+
excludeJars: []
31+
excludeSymbols: []
32+
symbols: []
33+
libs: []
34+
linkFlags: []
35+
jetifier: true
36+
arm64-osx:
37+
context:
38+
excludeLibs: []
39+
excludeSymbols: []
40+
symbols: []
41+
libs: []
42+
frameworks: []
43+
linkFlags: []
44+
x86_64-osx:
45+
context:
46+
excludeLibs: []
47+
excludeSymbols: []
48+
symbols: []
49+
libs: []
50+
frameworks: []
51+
linkFlags: []
52+
x86_64-linux:
53+
context:
54+
excludeLibs: []
55+
excludeSymbols: []
56+
symbols: []
57+
libs: []
58+
linkFlags: []
59+
arm64-linux:
60+
context:
61+
excludeLibs: []
62+
excludeSymbols: []
63+
symbols: []
64+
libs: []
65+
linkFlags: []
66+
x86-win32:
67+
context:
68+
excludeLibs: []
69+
excludeSymbols: []
70+
symbols: []
71+
libs: []
72+
linkFlags: []
73+
x86_64-win32:
74+
context:
75+
excludeLibs: []
76+
excludeSymbols: []
77+
symbols: []
78+
libs: []
79+
linkFlags: []
80+
wasm-web:
81+
context:
82+
excludeLibs: []
83+
excludeJsLibs: []
84+
excludeSymbols: []
85+
symbols: []
86+
libs: []
87+
linkFlags: []
88+
wasm_pthread-web:
89+
context:
90+
excludeLibs: []
91+
excludeJsLibs: []
92+
excludeSymbols: []
93+
symbols: []
94+
libs: []
95+
linkFlags: []
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
platforms:
2+
armv7-ios:
3+
context:
4+
excludeLibs: [physics, box2d_defold, script_box2d_defold]
5+
excludeSymbols: []
6+
libs: [physics_2d, box2d, script_box2d, physics_3d]
7+
arm64-ios:
8+
context:
9+
excludeLibs: [physics, box2d_defold, script_box2d_defold]
10+
excludeSymbols: []
11+
symbols: []
12+
libs: [physics_2d, box2d, script_box2d, physics_3d]
13+
frameworks: []
14+
linkFlags: []
15+
x86_64-ios:
16+
context:
17+
excludeLibs: [physics, box2d_defold, script_box2d_defold]
18+
excludeSymbols: []
19+
symbols: []
20+
libs: [physics_2d, box2d, script_box2d, physics_3d]
21+
frameworks: []
22+
linkFlags: []
23+
armv7-android:
24+
context:
25+
excludeLibs: [physics, box2d_defold, script_box2d_defold]
26+
excludeJars: []
27+
excludeSymbols: []
28+
symbols: []
29+
libs: [physics_2d, box2d, script_box2d, physics_3d]
30+
linkFlags: []
31+
jetifier: true
32+
arm64-android:
33+
context:
34+
excludeLibs: [physics, box2d_defold, script_box2d_defold]
35+
excludeJars: []
36+
excludeSymbols: []
37+
symbols: []
38+
libs: [physics_2d, box2d, script_box2d, physics_3d]
39+
linkFlags: []
40+
jetifier: true
41+
arm64-osx:
42+
context:
43+
excludeLibs: [physics, box2d_defold, script_box2d_defold]
44+
excludeSymbols: []
45+
symbols: []
46+
libs: [physics_2d, box2d, script_box2d, physics_3d]
47+
frameworks: []
48+
linkFlags: []
49+
x86_64-osx:
50+
context:
51+
excludeLibs: [physics, box2d_defold, script_box2d_defold]
52+
excludeSymbols: []
53+
symbols: []
54+
libs: [physics_2d, box2d, script_box2d, physics_3d]
55+
frameworks: []
56+
linkFlags: []
57+
x86_64-linux:
58+
context:
59+
excludeLibs: [physics, box2d_defold, script_box2d_defold]
60+
excludeSymbols: []
61+
symbols: []
62+
libs: [physics_2d, box2d, script_box2d, physics_3d]
63+
linkFlags: []
64+
arm64-linux:
65+
context:
66+
excludeLibs: [physics, box2d_defold, script_box2d_defold]
67+
excludeSymbols: []
68+
symbols: []
69+
libs: [physics_2d, box2d, script_box2d, physics_3d]
70+
linkFlags: []
71+
x86-win32:
72+
context:
73+
excludeLibs: [libphysics, libbox2d_defold, script_box2d_defold]
74+
excludeSymbols: []
75+
symbols: []
76+
libs: [libphysics_2d.lib, libbox2d.lib, libscript_box2d.lib, libphysics_3d.lib]
77+
linkFlags: []
78+
x86_64-win32:
79+
context:
80+
excludeLibs: [libphysics, libbox2d_defold, script_box2d_defold]
81+
excludeSymbols: []
82+
symbols: []
83+
libs: [libphysics_2d.lib, libbox2d.lib, libscript_box2d.lib, libphysics_3d.lib]
84+
linkFlags: []
85+
wasm-web:
86+
context:
87+
excludeLibs: [physics, box2d_defold, script_box2d_defold]
88+
excludeJsLibs: []
89+
excludeSymbols: []
90+
symbols: []
91+
libs: [physics_2d, box2d, script_box2d, physics_3d]
92+
linkFlags: []
93+
wasm_pthread-web:
94+
context:
95+
excludeLibs: [physics, box2d_defold, script_box2d_defold]
96+
excludeJsLibs: []
97+
excludeSymbols: []
98+
symbols: []
99+
libs: [physics_2d, box2d, script_box2d, physics_3d]
100+
linkFlags: []
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
tags: physics, box2d
3+
title: Box2D Revolute Bridge
4+
brief: Create a bridge from revolute joints between existing Defold collision objects.
5+
author: Defold Foundation
6+
scripts: box2d_revolute_bridge_v3.script, box2d_revolute_bridge_v2.script
7+
thumbnail: thumbnail.webp
8+
---
9+
10+
This example builds a simple rope bridge from rectangular dynamic bodies connected with Box2D revolute joints.
11+
A ball drops onto the bridge so the hinge chain bends under load.
12+
13+
Click or tap the window to reset the ball.
14+
15+
## What You'll Learn
16+
17+
- How to get Box2D bodies from Defold collision objects with `b2d.get_body()`
18+
- How to connect two bodies with a `revolute` type joint
19+
- How local joint anchors define hinge points on each connected body
20+
21+
## Setup
22+
23+
The collection contains these game objects:
24+
25+
`controller`
26+
: 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.
27+
28+
`left_anchor` and `right_anchor`
29+
: Static collision objects used as the fixed bridge supports.
30+
31+
`segment_01` to `segment_10`
32+
: Dynamic rectangular collision objects. The active script creates revolute joints between neighboring segments.
33+
34+
`ball`
35+
: A dynamic circle collision object that falls onto the bridge to show how the joints move.
36+
37+
`floor`, `left_wall`, and `right_wall`
38+
: Static collision objects that keep the simulation in view after the ball falls through or off the bridge.
39+
40+
The project uses `box2d_v3.appmanifest` by default, so it runs with Box2D V3.
41+
To use the older legacy V2 backend, switch the native extension app manifest in `game.project` to `/box2d_v2.appmanifest`.
42+
43+
![setup](setup.png)
44+
45+
## How It Works
46+
47+
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.
48+
49+
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.
50+
The first and last joints connect to static anchors at the bridge supports.
51+
52+
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()`.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
embedded_components {
2+
id: "sprite"
3+
type: "sprite"
4+
data: "default_animation: \"pixel_gold\"\n"
5+
"material: \"/builtins/materials/sprite.material\"\n"
6+
"size {\n"
7+
" x: 22.0\n"
8+
" y: 22.0\n"
9+
"}\n"
10+
"size_mode: SIZE_MODE_MANUAL\n"
11+
"textures {\n"
12+
" sampler: \"texture_sampler\"\n"
13+
" texture: \"/textures/textures.atlas\"\n"
14+
"}\n"
15+
""
16+
}
17+
embedded_components {
18+
id: "collisionobject"
19+
type: "collisionobject"
20+
data: "type: COLLISION_OBJECT_TYPE_STATIC\n"
21+
"mass: 0.0\n"
22+
"friction: 0.4\n"
23+
"restitution: 0.0\n"
24+
"group: \"default\"\n"
25+
"mask: \"default\"\n"
26+
"embedded_collision_shape {\n"
27+
" shapes {\n"
28+
" shape_type: TYPE_BOX\n"
29+
" position {\n"
30+
" }\n"
31+
" rotation {\n"
32+
" }\n"
33+
" index: 0\n"
34+
" count: 3\n"
35+
" }\n"
36+
" data: 11.0\n"
37+
" data: 11.0\n"
38+
" data: 10.0\n"
39+
"}\n"
40+
""
41+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
embedded_components {
2+
id: "sprite"
3+
type: "sprite"
4+
data: "default_animation: \"ball\"\n"
5+
"material: \"/builtins/materials/sprite.material\"\n"
6+
"size {\n"
7+
" x: 254.0\n"
8+
" y: 254.0\n"
9+
"}\n"
10+
"textures {\n"
11+
" sampler: \"texture_sampler\"\n"
12+
" texture: \"/textures/textures.atlas\"\n"
13+
"}\n"
14+
""
15+
scale {
16+
x: 0.5
17+
y: 0.5
18+
}
19+
}
20+
embedded_components {
21+
id: "collisionobject"
22+
type: "collisionobject"
23+
data: "type: COLLISION_OBJECT_TYPE_DYNAMIC\n"
24+
"mass: 1.0\n"
25+
"friction: 0.35\n"
26+
"restitution: 0.2\n"
27+
"group: \"default\"\n"
28+
"mask: \"default\"\n"
29+
"embedded_collision_shape {\n"
30+
" shapes {\n"
31+
" shape_type: TYPE_SPHERE\n"
32+
" position {\n"
33+
" }\n"
34+
" rotation {\n"
35+
" }\n"
36+
" index: 0\n"
37+
" count: 1\n"
38+
" }\n"
39+
" data: 32.0\n"
40+
"}\n"
41+
""
42+
}

0 commit comments

Comments
 (0)