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_dynamic_scale/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_dynamic_scale/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_dynamic_scale/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: []
70 changes: 70 additions & 0 deletions physics/box2d_dynamic_scale/example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
tags: physics, box2d
title: Box2D dynamic shape scale and mass
brief: Resize dynamic Box2D collision shapes at runtime and recalculate body mass.
author: Defold Foundation
scripts: box2d_dynamic_scale_v3.script, box2d_dynamic_scale_v2.script
thumbnail: thumbnail.webp
---

This example shows how to change bodies shapes and mass in runtime.

It includes 4 independent dynamic Box2D bodies.

Click or tap to give every square body a new random size,
update its collision shape, recalculate its mass, and apply a random impulse.

## What You'll Learn

- How to get a Box2D body from a Defold collision object
- How to detect the active Box2D version with `b2d.get_version()`
- How to resize collision geometry through the V2 fixture API
- How to resize collision geometry through the V3 shape API
- How `update_mass = true` makes body mass follow the new collision size
- How to apply a dynamic impulse to the body

## Setup

The collection contains four instances of `block.go` game object.
The `block.go` prototype contains:

- `script_v3`, which runs only with the Box2D V3 backend
- `script_v2`, which runs only with the Box2D V2 backend
- a square sprite
- a dynamic box collision object with base half-extents of `40.0`

The collection also contains `walls` game object with:

- one static collision object with four box shapes around the play area
- a label that explains the click or tap interaction.

![setup](setup.png)

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`.

## How It Works

Both scripts read `b2d.get_version()` in `init()` and are only active if a matching version is used -
`box2d_dynamic_scale_v2.script` only continues when the major version is 2,
while `box2d_dynamic_scale_v3.script` only continues when the major version is 3.

The active script acquires input focus and initialized a random number generator.

When the built-in `touch` action is pressed, each block picks a random uniform scale from `0.5` to `1.5`.
The script multiplies the base sprite size and base collision half-extent by that scale,
so the visible square and physics box stay aligned.

The mass changes because `update_mass = true` makes Box2D recalculate the body mass from the resized shape.

In Box2D V2, collision geometry is attached to a body through fixtures.
The V2 script reads the first fixture with `b2d.body.get_fixtures()`
and resizes it with `b2d.fixture.set_shape(body, fixture.index, shape, true)`.

In Box2D V3, collision geometry is attached through shapes.
The V3 script reads the first shape with `b2d.body.get_shapes()`
and resizes it with `b2d.shape.set_shape(shape.shape_id, shape, true)`.

After resizing, the active script applies a random linear impulse at the body's world center.
Since each body has just recalculated its mass from its new collision size,
the resulting movement makes the size and mass changes visible.
53 changes: 53 additions & 0 deletions physics/box2d_dynamic_scale/example/block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
components {
id: "script_v3"
component: "/example/box2d_dynamic_scale_v3.script"
}
components {
id: "script_v2"
component: "/example/box2d_dynamic_scale_v2.script"
}
embedded_components {
id: "sprite"
type: "sprite"
data: "default_animation: \"body_soft\"\n"
"material: \"/builtins/materials/sprite.material\"\n"
"size {\n"
" x: 80.0\n"
" y: 80.0\n"
"}\n"
"size_mode: SIZE_MODE_MANUAL\n"
"textures {\n"
" sampler: \"texture_sampler\"\n"
" texture: \"/textures/textures.atlas\"\n"
"}\n"
""
}
embedded_components {
id: "collisionobject"
type: "collisionobject"
data: "type: COLLISION_OBJECT_TYPE_DYNAMIC\n"
"mass: 1.0\n"
"friction: 0.2\n"
"restitution: 0.2\n"
"group: \"block\"\n"
"mask: \"block\"\n"
"mask: \"wall\"\n"
"embedded_collision_shape {\n"
" shapes {\n"
" shape_type: TYPE_BOX\n"
" position {\n"
" }\n"
" rotation {\n"
" }\n"
" index: 0\n"
" count: 3\n"
" id: \"box\"\n"
" }\n"
" data: 40.0\n"
" data: 40.0\n"
" data: 10.0\n"
"}\n"
"linear_damping: 0.2\n"
"angular_damping: 0.1\n"
""
}
Loading
Loading