Skip to content

Commit bafef5f

Browse files
committed
Added prismatic joint example.
1 parent f2531f8 commit bafef5f

16 files changed

Lines changed: 571 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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
tags: physics, box2d
3+
title: Box2D Prismatic Joint
4+
brief: Create a motorized Box2D prismatic joint with translation limits from script using Box2D.
5+
author: Defold Foundation
6+
scripts: box2d_prismatic_joint.script
7+
thumbnail: thumbnail.webp
8+
---
9+
10+
This example creates a Box2D prismatic joint at runtime.
11+
The blue dynamic body (slider) is constrained to a diagonal rail,
12+
moves between two translation limits, and reverses its motor when it reaches either end.
13+
14+
Click or tap the window to reverse the motor manually.
15+
16+
There are no differences in scripting between Box2D V2 and V3 in this example.
17+
18+
## What You'll Learn
19+
20+
- How to get Box2D body handles from Defold collision objects
21+
- How to create a prismatic joint with `b2d.joint.create_prismatic()`
22+
- How `local_axis_a`, `lower_translation`, and `upper_translation` define the slide rail
23+
- How to control a prismatic joint motor with `b2d.joint.set_motor_speed()`
24+
25+
## Setup
26+
27+
The collection contains:
28+
29+
- `controller` game object with the main script and labels with informations
30+
- `rail` game object with static collision object for frame reference for the prismatic joint and a sprite rotated by 45 degrees around Z axis to indicate the "rail"
31+
- `slider` game object with a dynamic collision object and a sprite to indicate where the slider is
32+
33+
The `rail` and `slider` bodies start at the same world position in the middle.
34+
35+
![setup](setup.png)
36+
37+
The `game.project` of this example is configured to build with `/box2d_v3.appmanifest` by default.
38+
To test V2 locally after downloading the example, change `Native Extensions -> App Manifest` in `game.project` to `/box2d_v2.appmanifest`.
39+
40+
## How It Works
41+
42+
The script uses `b2d.get_body()` to fetch the Box2D bodies owned by the `rail` and `slider` collision objects.
43+
Then calls `b2d.joint.create_prismatic()` which creates the prismatic joint between them,
44+
with `local_axis_a` set to the same diagonal direction as the visible rail (rotated 45 degrees areound Z axis),
45+
enables limits from `-110` to `110` project units, and a motor, sets a maximum motor force, and starts the slider moving along the rail.
46+
47+
The prismatic joint is a constraint solver - only defines *how* two bodies are allowed to move, not *why* they should move.
48+
So normally, the slider would fall in the gravity direction, but would be moving constrained like a slider along a rail.
49+
We enable the motor in the example to move in both direction along the rail.
50+
Motor applied on a prismatic joint moves the object along the joint axis at the given linear speed.
51+
52+
During `update()`, the script reads `b2d.joint.get_joint_translation()`, which returns the position alongside the constraint,
53+
and reverses the motor when the translation reaches either limit.
54+
55+
On input (touch or mouse click) the direction of the motor that applies force to the slider changes too.
56+

0 commit comments

Comments
 (0)