Skip to content

Commit f2531f8

Browse files
authored
Added Box2D scripting sensors example (#217)
1 parent 8a84de4 commit f2531f8

18 files changed

Lines changed: 694 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: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
tags: physics, box2d
3+
title: Box2D Sensors
4+
brief: Create Box2D sensors from static collision shapes.
5+
author: Defold Foundation
6+
scripts: sensor_box2d_v3.script, sensor_box2d_v2.script, spawn.script
7+
thumbnail: thumbnail.webp
8+
---
9+
10+
This example shows how to utilize the Box2D scripting API to operate with sensors (triggers).
11+
It spawns dynamic physics balls from slightly different random positions near the top of the scene,
12+
and gravity pulls each ball down, and these pass through two collision objects.
13+
14+
## What You'll Learn
15+
16+
- How to get a Box2D body from a collision object with `b2d.get_body()`.
17+
- How to turn a Box2D V2 fixture into a sensor with `b2d.fixture.set_sensor()`.
18+
- How to recreate a Box2D V3 shape as a sensor with `b2d.body.create_shape()`.
19+
- How to count V2 sensor overlaps from `trigger_response` enter/exit messages.
20+
- How to poll V3 sensor overlaps with `b2d.shape.get_sensor_overlaps()`.
21+
22+
## Setup
23+
24+
The collection contains one `controller` game object with `spawn.script` and a `ball_factory`.
25+
The factory creates `ball.go` - a small prototype with one sprite and one dynamic circle collision object.
26+
27+
The two sensor game objects both contain:
28+
29+
- `sensor_box2d_v3.script` - for Box2D V3 example handling
30+
- `sensor_box2d_v2.script` - for Box2D V2 example handling
31+
- A sprite, label, and static collision object with component id `collisionobject`.
32+
33+
The collision objects are of type `Static` in the editor.
34+
35+
All sensors use the `sensor` collision group and mask the `ball` group.
36+
The spawned ball belongs to the `ball` collision group and masks the `sensor` group,
37+
so the sensor overlaps only include spawned balls.
38+
39+
![setup](setup.png)
40+
41+
The `game.project` currently uses `/box2d_v3.appmanifest`.
42+
To test V2 locally, change `Native Extensions -> App Manifest` to `/box2d_v2.appmanifest`.
43+
Gravity set in `game.project` causes the falling motion.
44+
45+
## How It Works
46+
47+
The controller spawns a ball repeateadly with `factory.create()`.
48+
Each ball is spawned at a random X position near the center, and is deleted after a short delay.
49+
The spawned balls pass through the sensor's collision object shapes.
50+
51+
Each sensor has two backend scripts (V2 and V3), but only one runs processing depending on a choosen Box2D version.
52+
Each script checks `b2d.get_version().major` in `init()` and returns immediately when the active backend does not match.
53+
54+
At startup, the active Box2D script changes the static collision geometry into a sensor (trigger) through the `b2d` API.
55+
Box2D V2 and V3 expose this through different scripting APIs:
56+
57+
- V2 uses fixtures and Defold trigger messages.
58+
- V3 uses shapes and can poll a sensor shape's current overlaps directly.
59+
60+
In Box2D V2, `sensor_box2d_v2.script` gets the body with `b2d.get_body("#collisionobject")`,
61+
reads the first fixture with `b2d.body.get_fixtures()`, and calls `b2d.fixture.set_sensor()`,
62+
so the fixture no longer blocks the balls. V2 fixture sensors do not expose a `get_sensor_overlaps()` polling API.
63+
But Defold still sends `trigger_response` messages when another collision object enters or exits the sensor,
64+
and the script maintains a Lua table of current overlapping ball ids.
65+
66+
In Box2D V3, `sensor_box2d_v3.script` uses the shape API.
67+
It reads the first editor-authored shape with `b2d.body.get_shapes()` and `b2d.shape.get_shape()`,
68+
destroys that solid shape, and creates a replacement with `b2d.body.create_shape()` using the same geometry and `sensor = true`.
69+
The V3 script then calls `b2d.shape.enable_sensor_events()` for the new sensor shape.
70+
In `update()`, it polls the current overlaps with `b2d.shape.get_sensor_overlaps()`.
71+
This returns shape info tables for the shapes currently overlapping the sensor.
72+
73+
Both scripts gives us the same result: each sensor sprite scales up while at least one ball overlaps it,
74+
and the label shows the current overlap count reported by the active backend path.
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.2\n"
26+
"restitution: 0.0\n"
27+
"group: \"ball\"\n"
28+
"mask: \"sensor\"\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)