Skip to content

Commit 4dc095f

Browse files
authored
212 box2d dynamic scale (#221)
* Added more comments in the weld joints example. * Added dynamic scale example.
1 parent 8fc1c2b commit 4dc095f

17 files changed

Lines changed: 674 additions & 10 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: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
tags: physics, box2d
3+
title: Box2D dynamic shape scale and mass
4+
brief: Resize dynamic Box2D collision shapes at runtime and recalculate body mass.
5+
author: Defold Foundation
6+
scripts: box2d_dynamic_scale_v3.script, box2d_dynamic_scale_v2.script
7+
thumbnail: thumbnail.webp
8+
---
9+
10+
This example shows how to change bodies shapes and mass in runtime.
11+
12+
It includes 4 independent dynamic Box2D bodies.
13+
14+
Click or tap to give every square body a new random size,
15+
update its collision shape, recalculate its mass, and apply a random impulse.
16+
17+
## What You'll Learn
18+
19+
- How to get a Box2D body from a Defold collision object
20+
- How to detect the active Box2D version with `b2d.get_version()`
21+
- How to resize collision geometry through the V2 fixture API
22+
- How to resize collision geometry through the V3 shape API
23+
- How `update_mass = true` makes body mass follow the new collision size
24+
- How to apply a dynamic impulse to the body
25+
26+
## Setup
27+
28+
The collection contains four instances of `block.go` game object.
29+
The `block.go` prototype contains:
30+
31+
- `script_v3`, which runs only with the Box2D V3 backend
32+
- `script_v2`, which runs only with the Box2D V2 backend
33+
- a square sprite
34+
- a dynamic box collision object with base half-extents of `40.0`
35+
36+
The collection also contains `walls` game object with:
37+
38+
- one static collision object with four box shapes around the play area
39+
- a label that explains the click or tap interaction.
40+
41+
![setup](setup.png)
42+
43+
The `game.project` of this example is configured to build with `/box2d_v3.appmanifest` by default.
44+
To test V2 locally after downloading the example, change `Native Extensions -> App Manifest` in `game.project` to `/box2d_v2.appmanifest`.
45+
46+
## How It Works
47+
48+
Both scripts read `b2d.get_version()` in `init()` and are only active if a matching version is used -
49+
`box2d_dynamic_scale_v2.script` only continues when the major version is 2,
50+
while `box2d_dynamic_scale_v3.script` only continues when the major version is 3.
51+
52+
The active script acquires input focus and initialized a random number generator.
53+
54+
When the built-in `touch` action is pressed, each block picks a random uniform scale from `0.5` to `1.5`.
55+
The script multiplies the base sprite size and base collision half-extent by that scale,
56+
so the visible square and physics box stay aligned.
57+
58+
The mass changes because `update_mass = true` makes Box2D recalculate the body mass from the resized shape.
59+
60+
In Box2D V2, collision geometry is attached to a body through fixtures.
61+
The V2 script reads the first fixture with `b2d.body.get_fixtures()`
62+
and resizes it with `b2d.fixture.set_shape(body, fixture.index, shape, true)`.
63+
64+
In Box2D V3, collision geometry is attached through shapes.
65+
The V3 script reads the first shape with `b2d.body.get_shapes()`
66+
and resizes it with `b2d.shape.set_shape(shape.shape_id, shape, true)`.
67+
68+
After resizing, the active script applies a random linear impulse at the body's world center.
69+
Since each body has just recalculated its mass from its new collision size,
70+
the resulting movement makes the size and mass changes visible.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
components {
2+
id: "script_v3"
3+
component: "/example/box2d_dynamic_scale_v3.script"
4+
}
5+
components {
6+
id: "script_v2"
7+
component: "/example/box2d_dynamic_scale_v2.script"
8+
}
9+
embedded_components {
10+
id: "sprite"
11+
type: "sprite"
12+
data: "default_animation: \"body_soft\"\n"
13+
"material: \"/builtins/materials/sprite.material\"\n"
14+
"size {\n"
15+
" x: 80.0\n"
16+
" y: 80.0\n"
17+
"}\n"
18+
"size_mode: SIZE_MODE_MANUAL\n"
19+
"textures {\n"
20+
" sampler: \"texture_sampler\"\n"
21+
" texture: \"/textures/textures.atlas\"\n"
22+
"}\n"
23+
""
24+
}
25+
embedded_components {
26+
id: "collisionobject"
27+
type: "collisionobject"
28+
data: "type: COLLISION_OBJECT_TYPE_DYNAMIC\n"
29+
"mass: 1.0\n"
30+
"friction: 0.2\n"
31+
"restitution: 0.2\n"
32+
"group: \"block\"\n"
33+
"mask: \"block\"\n"
34+
"mask: \"wall\"\n"
35+
"embedded_collision_shape {\n"
36+
" shapes {\n"
37+
" shape_type: TYPE_BOX\n"
38+
" position {\n"
39+
" }\n"
40+
" rotation {\n"
41+
" }\n"
42+
" index: 0\n"
43+
" count: 3\n"
44+
" id: \"box\"\n"
45+
" }\n"
46+
" data: 40.0\n"
47+
" data: 40.0\n"
48+
" data: 10.0\n"
49+
"}\n"
50+
"linear_damping: 0.2\n"
51+
"angular_damping: 0.1\n"
52+
""
53+
}

0 commit comments

Comments
 (0)