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_sensor_toggle/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_sensor_toggle/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_sensor_toggle/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: []
74 changes: 74 additions & 0 deletions physics/box2d_sensor_toggle/example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
tags: physics, box2d
title: Box2D Sensors
brief: Create Box2D sensors from static collision shapes.
author: Defold Foundation
scripts: sensor_box2d_v3.script, sensor_box2d_v2.script, spawn.script
thumbnail: thumbnail.webp
---

This example shows how to utilize the Box2D scripting API to operate with sensors (triggers).
It spawns dynamic physics balls from slightly different random positions near the top of the scene,
and gravity pulls each ball down, and these pass through two collision objects.

## What You'll Learn

- How to get a Box2D body from a collision object with `b2d.get_body()`.
- How to turn a Box2D V2 fixture into a sensor with `b2d.fixture.set_sensor()`.
- How to recreate a Box2D V3 shape as a sensor with `b2d.body.create_shape()`.
- How to count V2 sensor overlaps from `trigger_response` enter/exit messages.
- How to poll V3 sensor overlaps with `b2d.shape.get_sensor_overlaps()`.

## Setup

The collection contains one `controller` game object with `spawn.script` and a `ball_factory`.
The factory creates `ball.go` - a small prototype with one sprite and one dynamic circle collision object.

The two sensor game objects both contain:

- `sensor_box2d_v3.script` - for Box2D V3 example handling
- `sensor_box2d_v2.script` - for Box2D V2 example handling
- A sprite, label, and static collision object with component id `collisionobject`.

The collision objects are of type `Static` in the editor.

All sensors use the `sensor` collision group and mask the `ball` group.
The spawned ball belongs to the `ball` collision group and masks the `sensor` group,
so the sensor overlaps only include spawned balls.

![setup](setup.png)

The `game.project` currently uses `/box2d_v3.appmanifest`.
To test V2 locally, change `Native Extensions -> App Manifest` to `/box2d_v2.appmanifest`.
Gravity set in `game.project` causes the falling motion.

## How It Works

The controller spawns a ball repeateadly with `factory.create()`.
Each ball is spawned at a random X position near the center, and is deleted after a short delay.
The spawned balls pass through the sensor's collision object shapes.

Each sensor has two backend scripts (V2 and V3), but only one runs processing depending on a choosen Box2D version.
Each script checks `b2d.get_version().major` in `init()` and returns immediately when the active backend does not match.

At startup, the active Box2D script changes the static collision geometry into a sensor (trigger) through the `b2d` API.
Box2D V2 and V3 expose this through different scripting APIs:

- V2 uses fixtures and Defold trigger messages.
- V3 uses shapes and can poll a sensor shape's current overlaps directly.

In Box2D V2, `sensor_box2d_v2.script` gets the body with `b2d.get_body("#collisionobject")`,
reads the first fixture with `b2d.body.get_fixtures()`, and calls `b2d.fixture.set_sensor()`,
so the fixture no longer blocks the balls. V2 fixture sensors do not expose a `get_sensor_overlaps()` polling API.
But Defold still sends `trigger_response` messages when another collision object enters or exits the sensor,
and the script maintains a Lua table of current overlapping ball ids.

In Box2D V3, `sensor_box2d_v3.script` uses the shape API.
It reads the first editor-authored shape with `b2d.body.get_shapes()` and `b2d.shape.get_shape()`,
destroys that solid shape, and creates a replacement with `b2d.body.create_shape()` using the same geometry and `sensor = true`.
The V3 script then calls `b2d.shape.enable_sensor_events()` for the new sensor shape.
In `update()`, it polls the current overlaps with `b2d.shape.get_sensor_overlaps()`.
This returns shape info tables for the shapes currently overlapping the sensor.

Both scripts gives us the same result: each sensor sprite scales up while at least one ball overlaps it,
and the label shows the current overlap count reported by the active backend path.
42 changes: 42 additions & 0 deletions physics/box2d_sensor_toggle/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.2\n"
"restitution: 0.0\n"
"group: \"ball\"\n"
"mask: \"sensor\"\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