Skip to content

Commit 59c0e3e

Browse files
authored
Added motor joint example for Box2D v2 and v3. (#197)
* Added joints examples for Box2D v2 and v3. * Changed joints to motor joint.
1 parent ad7593b commit 59c0e3e

22 files changed

Lines changed: 841 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 Motor Joint
4+
brief: Create and control a motorized Box2D joint from script using Box2D V2 and V3.
5+
author: Defold Foundation
6+
scripts: box2d_motor_joint_v3.script, box2d_motor_joint_v2.script
7+
thumbnail: thumbnail.webp
8+
---
9+
10+
This example creates a motorized Box2D joint at runtime. It works with both Box2D V2 and V3 by attaching one script for each backend. Each script checks `b2d.get_version()` during `init()` and becomes a no-op when the other backend is active.
11+
12+
Click or tap the window to reverse the motor direction.
13+
14+
## What You'll Learn
15+
16+
- How to get Box2D body handles from Defold collision objects
17+
- How to detect the active Box2D version with `b2d.get_version()`
18+
- How to create a pivoted motor joint with `b2d.joint.create_revolute()`
19+
- How to control joint motor speed with `b2d.joint.set_motor_speed()`
20+
- How to tune the Box2D V3 joint solver with `b2d.world.set_joint_tuning()`
21+
22+
## Setup
23+
24+
The collection contains a static `pivot` game object, one dynamic `arm` game object with a particle effect, and a controller game object.
25+
26+
The `pivot` object marks the world point the arm should rotate around. The `arm` object is the dynamic body driven by the joint. The example uses a revolute joint with its motor enabled, because the revolute joint provides a stable hinge pivot while the motor drives the rotation.
27+
28+
The controller has both backend scripts attached. Each script checks `b2d.get_version()` and only runs when the selected app manifest matches its Box2D backend.
29+
30+
![setup](setup.png)
31+
32+
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`.
33+
34+
![game_project](game_project.png)
35+
36+
## How It Works
37+
38+
Both scripts read `b2d.get_version()` once. `box2d_motor_joint_v2.script` only continues when the major version is 2, while `box2d_motor_joint_v3.script` only continues when the major version is 3.
39+
40+
`b2d.get_body()` returns the Box2D bodies owned by the `pivot` and `arm` collision objects. The active script then creates a revolute joint between those bodies with `b2d.joint.create_revolute()`.
41+
42+
The joint definition uses:
43+
44+
- `local_anchor_a` on the pivot body
45+
- `local_anchor_b` on the arm body
46+
- `enable_motor` to turn on the joint motor
47+
- `max_motor_torque` to limit how strongly the motor can rotate the arm
48+
- `motor_speed` to set the current motor direction and speed
49+
50+
The important part is the arm anchor. The pivot is placed in the collection as a visible reference point. The scripts convert that world position into the arm's local space, so the arm rotates around the same visible pivot instead of rotating around its center.
51+
52+
Click or tap to reverse the motor with `b2d.joint.set_motor_speed()`. The V3 script also calls `b2d.world.set_joint_tuning()` to adjust the joint solver used by the Box2D V3 backend.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
components {
2+
id: "trail"
3+
component: "/particlefx/trail.particlefx"
4+
position {
5+
x: 90.0
6+
z: -0.1
7+
}
8+
}
9+
components {
10+
id: "play"
11+
component: "/particlefx/play.script"
12+
}
13+
embedded_components {
14+
id: "sprite"
15+
type: "sprite"
16+
data: "default_animation: \"pixel_blue\"\n"
17+
"material: \"/builtins/materials/sprite.material\"\n"
18+
"size {\n"
19+
" x: 200.0\n"
20+
" y: 40.0\n"
21+
"}\n"
22+
"size_mode: SIZE_MODE_MANUAL\n"
23+
"textures {\n"
24+
" sampler: \"texture_sampler\"\n"
25+
" texture: \"/textures/textures.atlas\"\n"
26+
"}\n"
27+
""
28+
}
29+
embedded_components {
30+
id: "collisionobject"
31+
type: "collisionobject"
32+
data: "type: COLLISION_OBJECT_TYPE_DYNAMIC\n"
33+
"mass: 3.0\n"
34+
"friction: 0.4\n"
35+
"restitution: 0.0\n"
36+
"group: \"default\"\n"
37+
"mask: \"default\"\n"
38+
"embedded_collision_shape {\n"
39+
" shapes {\n"
40+
" shape_type: TYPE_BOX\n"
41+
" position {\n"
42+
" }\n"
43+
" rotation {\n"
44+
" }\n"
45+
" index: 0\n"
46+
" count: 3\n"
47+
" }\n"
48+
" data: 100.0\n"
49+
" data: 20.0\n"
50+
" data: 10.0\n"
51+
"}\n"
52+
"linear_damping: 0.05\n"
53+
"angular_damping: 0.2\n"
54+
""
55+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: "box2d_motor_joint"
2+
instances {
3+
id: "pivot"
4+
prototype: "/example/pivot.go"
5+
position {
6+
x: 360.0
7+
y: 360.0
8+
z: 0.5
9+
}
10+
}
11+
instances {
12+
id: "arm"
13+
prototype: "/example/arm.go"
14+
position {
15+
x: 480.0
16+
y: 360.0
17+
}
18+
}
19+
scale_along_z: 0
20+
embedded_instances {
21+
id: "controller"
22+
data: "components {\n"
23+
" id: \"box2d_v2\"\n"
24+
" component: \"/example/box2d_motor_joint_v2.script\"\n"
25+
"}\n"
26+
"components {\n"
27+
" id: \"box2d_v3\"\n"
28+
" component: \"/example/box2d_motor_joint_v3.script\"\n"
29+
"}\n"
30+
"embedded_components {\n"
31+
" id: \"label\"\n"
32+
" type: \"label\"\n"
33+
" data: \"size {\\n"
34+
" x: 640.0\\n"
35+
" y: 96.0\\n"
36+
"}\\n"
37+
"text: \\\"Box2D motor joint\\\"\\n"
38+
"font: \\\"/fonts/text28.font\\\"\\n"
39+
"material: \\\"/builtins/fonts/label-df.material\\\"\\n"
40+
"\"\n"
41+
" position {\n"
42+
" x: 360.0\n"
43+
" y: 60.0\n"
44+
" }\n"
45+
"}\n"
46+
""
47+
}

0 commit comments

Comments
 (0)