Skip to content

Commit dd6cffc

Browse files
authored
Added example on how to apply force to physics collision objects. (#147)
* Added example on how to apply force to physics collision objects. * Changed title of the example in game.project
1 parent cebab74 commit dd6cffc

File tree

14 files changed

+508
-0
lines changed

14 files changed

+508
-0
lines changed
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+
}
146 KB
Binary file not shown.
1.5 KB
Loading
822 Bytes
Loading
1.01 KB
Loading
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
images {
2+
image: "/assets/images/elementStone019.png"
3+
}
4+
images {
5+
image: "/assets/images/elementStone023.png"
6+
}
7+
images {
8+
image: "/assets/images/elementMetal001.png"
9+
}
10+
animations {
11+
id: "coin"
12+
playback: PLAYBACK_LOOP_FORWARD
13+
fps: 8
14+
}
15+
extrude_borders: 2
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
font: "/assets/SourceSansPro-Semibold.ttf"
2+
material: "/builtins/fonts/font.material"
3+
size: 48
4+
outline_alpha: 0.0
5+
outline_width: 0.0

physics/apply_force/example.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
tags: physics
3+
title: Apply force
4+
brief: This example demonstrates how to apply directional force to all dynamic blocks on touch/click and draws debug direction lines.
5+
thumbnail: thumbnail.png
6+
scripts: apply_force.script
7+
---
8+
9+
This example demonstrates how to apply directional force to all dynamic blocks on touch/click and draws debug direction lines.
10+
11+
## Setup
12+
13+
![setup](setup.png)
14+
15+
Scene consists of a few game objects:
16+
17+
- `controller`
18+
- Main object that contains `/example/apply_force.script` and a label with usage text.
19+
- `block1`, `block2`, `block3`, `block4`
20+
- Dynamic rigid bodies (sprite + dynamic collision object).
21+
- `walls`
22+
- Static boundary collision object around the screen.
23+
24+
Proposed settings regarding physics in the `game.project` file:
25+
26+
![gameproject](gameproject.png)
27+
28+
## Script flow
29+
30+
A single controller script handles input for the whole scene.
31+
When you touch/click, it loops over all dynamic blocks, computes a force vector for each one, applies the force, and draws a debug line that visualizes the direction.
32+
33+
1. acquires input focus in `init()`
34+
2. listens to `hash("touch")` in `on_input()`
35+
3. iterates over all block ids
36+
4. computes `force = (touch - center) * force_factor`
37+
5. posts `apply_force` to each block
38+
6. posts `@render: draw_line` for debug visualization
Lines changed: 312 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,312 @@
1+
name: "default"
2+
scale_along_z: 0
3+
embedded_instances {
4+
id: "walls"
5+
data: "embedded_components {\n"
6+
" id: \"collisionobject\"\n"
7+
" type: \"collisionobject\"\n"
8+
" data: \"type: COLLISION_OBJECT_TYPE_STATIC\\n"
9+
"mass: 0.0\\n"
10+
"friction: 0.9\\n"
11+
"restitution: 0.1\\n"
12+
"group: \\\"default\\\"\\n"
13+
"mask: \\\"default\\\"\\n"
14+
"embedded_collision_shape {\\n"
15+
" shapes {\\n"
16+
" shape_type: TYPE_BOX\\n"
17+
" position {\\n"
18+
" x: -50.0\\n"
19+
" y: 360.0\\n"
20+
" }\\n"
21+
" rotation {\\n"
22+
" }\\n"
23+
" index: 0\\n"
24+
" count: 3\\n"
25+
" }\\n"
26+
" shapes {\\n"
27+
" shape_type: TYPE_BOX\\n"
28+
" position {\\n"
29+
" x: 770.0\\n"
30+
" y: 360.0\\n"
31+
" }\\n"
32+
" rotation {\\n"
33+
" }\\n"
34+
" index: 3\\n"
35+
" count: 3\\n"
36+
" }\\n"
37+
" shapes {\\n"
38+
" shape_type: TYPE_BOX\\n"
39+
" position {\\n"
40+
" x: 360.0\\n"
41+
" y: 770.0\\n"
42+
" }\\n"
43+
" rotation {\\n"
44+
" }\\n"
45+
" index: 6\\n"
46+
" count: 3\\n"
47+
" }\\n"
48+
" shapes {\\n"
49+
" shape_type: TYPE_BOX\\n"
50+
" position {\\n"
51+
" x: 360.0\\n"
52+
" y: -50.0\\n"
53+
" }\\n"
54+
" rotation {\\n"
55+
" }\\n"
56+
" index: 9\\n"
57+
" count: 3\\n"
58+
" }\\n"
59+
" data: 50.0\\n"
60+
" data: 360.0\\n"
61+
" data: 10.0\\n"
62+
" data: 50.0\\n"
63+
" data: 360.0\\n"
64+
" data: 10.0\\n"
65+
" data: 460.0\\n"
66+
" data: 50.0\\n"
67+
" data: 10.0\\n"
68+
" data: 460.0\\n"
69+
" data: 50.0\\n"
70+
" data: 10.0\\n"
71+
"}\\n"
72+
"\"\n"
73+
"}\n"
74+
""
75+
}
76+
embedded_instances {
77+
id: "block1"
78+
data: "embedded_components {\n"
79+
" id: \"collisionobject\"\n"
80+
" type: \"collisionobject\"\n"
81+
" data: \"type: COLLISION_OBJECT_TYPE_DYNAMIC\\n"
82+
"mass: 2.0\\n"
83+
"friction: 0.5\\n"
84+
"restitution: 0.2\\n"
85+
"group: \\\"default\\\"\\n"
86+
"mask: \\\"default\\\"\\n"
87+
"embedded_collision_shape {\\n"
88+
" shapes {\\n"
89+
" shape_type: TYPE_BOX\\n"
90+
" position {\\n"
91+
" }\\n"
92+
" rotation {\\n"
93+
" }\\n"
94+
" index: 0\\n"
95+
" count: 3\\n"
96+
" }\\n"
97+
" data: 109.4645\\n"
98+
" data: 68.3975\\n"
99+
" data: 10.0\\n"
100+
"}\\n"
101+
"linear_damping: 0.8\\n"
102+
"angular_damping: 0.8\\n"
103+
"event_collision: false\\n"
104+
"event_contact: false\\n"
105+
"event_trigger: false\\n"
106+
"\"\n"
107+
"}\n"
108+
"embedded_components {\n"
109+
" id: \"sprite\"\n"
110+
" type: \"sprite\"\n"
111+
" data: \"default_animation: \\\"elementStone019\\\"\\n"
112+
"material: \\\"/builtins/materials/sprite.material\\\"\\n"
113+
"textures {\\n"
114+
" sampler: \\\"texture_sampler\\\"\\n"
115+
" texture: \\\"/assets/sprites.atlas\\\"\\n"
116+
"}\\n"
117+
"\"\n"
118+
"}\n"
119+
""
120+
position {
121+
x: 220.14
122+
y: 559.457
123+
}
124+
}
125+
embedded_instances {
126+
id: "block2"
127+
data: "embedded_components {\n"
128+
" id: \"sprite\"\n"
129+
" type: \"sprite\"\n"
130+
" data: \"default_animation: \\\"elementStone023\\\"\\n"
131+
"material: \\\"/builtins/materials/sprite.material\\\"\\n"
132+
"textures {\\n"
133+
" sampler: \\\"texture_sampler\\\"\\n"
134+
" texture: \\\"/assets/sprites.atlas\\\"\\n"
135+
"}\\n"
136+
"\"\n"
137+
"}\n"
138+
"embedded_components {\n"
139+
" id: \"collisionobject\"\n"
140+
" type: \"collisionobject\"\n"
141+
" data: \"type: COLLISION_OBJECT_TYPE_DYNAMIC\\n"
142+
"mass: 1.0\\n"
143+
"friction: 0.5\\n"
144+
"restitution: 0.2\\n"
145+
"group: \\\"default\\\"\\n"
146+
"mask: \\\"default\\\"\\n"
147+
"embedded_collision_shape {\\n"
148+
" shapes {\\n"
149+
" shape_type: TYPE_BOX\\n"
150+
" position {\\n"
151+
" }\\n"
152+
" rotation {\\n"
153+
" }\\n"
154+
" index: 0\\n"
155+
" count: 3\\n"
156+
" }\\n"
157+
" data: 68.0\\n"
158+
" data: 68.3975\\n"
159+
" data: 10.0\\n"
160+
"}\\n"
161+
"linear_damping: 0.8\\n"
162+
"angular_damping: 0.8\\n"
163+
"event_collision: false\\n"
164+
"event_contact: false\\n"
165+
"event_trigger: false\\n"
166+
"\"\n"
167+
"}\n"
168+
""
169+
position {
170+
x: 385.289
171+
y: 385.021
172+
}
173+
}
174+
embedded_instances {
175+
id: "block3"
176+
data: "embedded_components {\n"
177+
" id: \"sprite\"\n"
178+
" type: \"sprite\"\n"
179+
" data: \"default_animation: \\\"elementStone023\\\"\\n"
180+
"material: \\\"/builtins/materials/sprite.material\\\"\\n"
181+
"size {\\n"
182+
" x: 140.0\\n"
183+
" y: 140.0\\n"
184+
"}\\n"
185+
"textures {\\n"
186+
" sampler: \\\"texture_sampler\\\"\\n"
187+
" texture: \\\"/assets/sprites.atlas\\\"\\n"
188+
"}\\n"
189+
"\"\n"
190+
"}\n"
191+
"embedded_components {\n"
192+
" id: \"collisionobject\"\n"
193+
" type: \"collisionobject\"\n"
194+
" data: \"type: COLLISION_OBJECT_TYPE_DYNAMIC\\n"
195+
"mass: 1.0\\n"
196+
"friction: 0.5\\n"
197+
"restitution: 0.2\\n"
198+
"group: \\\"default\\\"\\n"
199+
"mask: \\\"default\\\"\\n"
200+
"embedded_collision_shape {\\n"
201+
" shapes {\\n"
202+
" shape_type: TYPE_BOX\\n"
203+
" position {\\n"
204+
" }\\n"
205+
" rotation {\\n"
206+
" }\\n"
207+
" index: 0\\n"
208+
" count: 3\\n"
209+
" }\\n"
210+
" data: 68.0\\n"
211+
" data: 68.3975\\n"
212+
" data: 10.0\\n"
213+
"}\\n"
214+
"linear_damping: 0.8\\n"
215+
"angular_damping: 0.8\\n"
216+
"event_collision: false\\n"
217+
"event_contact: false\\n"
218+
"event_trigger: false\\n"
219+
"\"\n"
220+
"}\n"
221+
""
222+
position {
223+
x: 508.289
224+
y: 547.021
225+
}
226+
}
227+
embedded_instances {
228+
id: "block4"
229+
data: "embedded_components {\n"
230+
" id: \"sprite\"\n"
231+
" type: \"sprite\"\n"
232+
" data: \"default_animation: \\\"elementStone023\\\"\\n"
233+
"material: \\\"/builtins/materials/sprite.material\\\"\\n"
234+
"size {\\n"
235+
" x: 140.0\\n"
236+
" y: 140.0\\n"
237+
"}\\n"
238+
"textures {\\n"
239+
" sampler: \\\"texture_sampler\\\"\\n"
240+
" texture: \\\"/assets/sprites.atlas\\\"\\n"
241+
"}\\n"
242+
"\"\n"
243+
"}\n"
244+
"embedded_components {\n"
245+
" id: \"collisionobject\"\n"
246+
" type: \"collisionobject\"\n"
247+
" data: \"type: COLLISION_OBJECT_TYPE_DYNAMIC\\n"
248+
"mass: 1.0\\n"
249+
"friction: 0.5\\n"
250+
"restitution: 0.2\\n"
251+
"group: \\\"default\\\"\\n"
252+
"mask: \\\"default\\\"\\n"
253+
"embedded_collision_shape {\\n"
254+
" shapes {\\n"
255+
" shape_type: TYPE_BOX\\n"
256+
" position {\\n"
257+
" }\\n"
258+
" rotation {\\n"
259+
" }\\n"
260+
" index: 0\\n"
261+
" count: 3\\n"
262+
" }\\n"
263+
" data: 68.0\\n"
264+
" data: 68.3975\\n"
265+
" data: 10.0\\n"
266+
"}\\n"
267+
"linear_damping: 0.8\\n"
268+
"angular_damping: 0.8\\n"
269+
"event_collision: false\\n"
270+
"event_contact: false\\n"
271+
"event_trigger: false\\n"
272+
"\"\n"
273+
"}\n"
274+
""
275+
position {
276+
x: 608.289
277+
y: 395.021
278+
}
279+
}
280+
embedded_instances {
281+
id: "controller"
282+
data: "components {\n"
283+
" id: \"apply_force\"\n"
284+
" component: \"/example/apply_force.script\"\n"
285+
"}\n"
286+
"embedded_components {\n"
287+
" id: \"label\"\n"
288+
" type: \"label\"\n"
289+
" data: \"size {\\n"
290+
" x: 700.0\\n"
291+
" y: 50.0\\n"
292+
"}\\n"
293+
"color {\\n"
294+
" x: 0.101960786\\n"
295+
" y: 0.2\\n"
296+
" z: 0.6\\n"
297+
"}\\n"
298+
"text: \\\"Click to apply force to all blocks\\\"\\n"
299+
"font: \\\"/assets/text48.font\\\"\\n"
300+
"material: \\\"/builtins/fonts/label.material\\\"\\n"
301+
"\"\n"
302+
" position {\n"
303+
" x: 360.0\n"
304+
" y: 680.0\n"
305+
" }\n"
306+
" scale {\n"
307+
" x: 0.75\n"
308+
" y: 0.75\n"
309+
" }\n"
310+
"}\n"
311+
""
312+
}

0 commit comments

Comments
 (0)