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
16 changes: 16 additions & 0 deletions physics/apply_linear_impulse/example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
tags: physics
title: Apply Linear Impluse
brief: This example demonstrates how to apply linear impluse to a dynamic cube on touch/click
author: JuLongZhiLu(巨龙之路)
scripts: cube.script
thumbnail: thumbnail.png
---

The cube contains a dynamic collisionobject component.

![image](image.png)

Comment thread
paweljarosz marked this conversation as resolved.
The below script applies a vertical linear impulse to the dynamic collisionobject on click/touch using the `b2d` API functions.

Read more about the [Box2D API here](https://defold.com/ref/stable/b2d-lua/).
Binary file not shown.
5 changes: 5 additions & 0 deletions physics/apply_linear_impulse/fonts/text32.font
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
font: "/fonts/SourceSansPro-Semibold.ttf"
material: "/builtins/fonts/font-df.material"
size: 32
output_format: TYPE_DISTANCE_FIELD
characters: " !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
30 changes: 30 additions & 0 deletions physics/apply_linear_impulse/game.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[bootstrap]
main_collection = /scenes/main.collectionc

[script]
shared_state = 1

[display]
width = 960
height = 640
high_dpi = 1

[android]
input_method = HiddenInputField

[physics]
gravity_y = -1000.0
scale = 0.01
use_fixed_timestep = 1

[html5]
scale_mode = stretch

[project]
title = defold-apply_linear_impulse-example

[render]
clear_color_red = 0.160156
clear_color_green = 0.164063
clear_color_blue = 0.183594

Binary file added physics/apply_linear_impulse/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions physics/apply_linear_impulse/input/game.input_binding
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mouse_trigger {
input: MOUSE_BUTTON_1
action: "touch"
}
128 changes: 128 additions & 0 deletions physics/apply_linear_impulse/scenes/main.collection
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
name: "main"
scale_along_z: 0
embedded_instances {
id: "cube"
data: "components {\n"
" id: \"cube\"\n"
" component: \"/scripts/cube.script\"\n"
"}\n"
"embedded_components {\n"
" id: \"sprite\"\n"
" type: \"sprite\"\n"
" data: \"default_animation: \\\"elementStone011\\\"\\n"
"material: \\\"/builtins/materials/sprite.material\\\"\\n"
"size {\\n"
" x: 60.0\\n"
" y: 60.0\\n"
"}\\n"
"size_mode: SIZE_MODE_MANUAL\\n"
"textures {\\n"
" sampler: \\\"texture_sampler\\\"\\n"
" texture: \\\"/textures/textures.atlas\\\"\\n"
"}\\n"
"\"\n"
"}\n"
"embedded_components {\n"
" id: \"collisionobject\"\n"
" type: \"collisionobject\"\n"
" data: \"type: COLLISION_OBJECT_TYPE_DYNAMIC\\n"
"mass: 1.0\\n"
"friction: 0.1\\n"
"restitution: 0.5\\n"
"group: \\\"default\\\"\\n"
"mask: \\\"default\\\"\\n"
"embedded_collision_shape {\\n"
" shapes {\\n"
" shape_type: TYPE_BOX\\n"
" position {\\n"
" }\\n"
" rotation {\\n"
" }\\n"
" index: 0\\n"
" count: 3\\n"
" }\\n"
" data: 30.0\\n"
" data: 30.0\\n"
" data: 10.0\\n"
"}\\n"
"\"\n"
"}\n"
""
position {
x: 480.0
y: 351.0
}
}
embedded_instances {
id: "platform"
data: "embedded_components {\n"
" id: \"sprite\"\n"
" type: \"sprite\"\n"
" data: \"default_animation: \\\"elementStone013\\\"\\n"
"material: \\\"/builtins/materials/sprite.material\\\"\\n"
"slice9 {\\n"
" x: 50.0\\n"
" z: 50.0\\n"
"}\\n"
"size {\\n"
" x: 400.0\\n"
" y: 70.0\\n"
"}\\n"
"size_mode: SIZE_MODE_MANUAL\\n"
"textures {\\n"
" sampler: \\\"texture_sampler\\\"\\n"
" texture: \\\"/textures/textures.atlas\\\"\\n"
"}\\n"
"\"\n"
"}\n"
"embedded_components {\n"
" id: \"collisionobject\"\n"
" type: \"collisionobject\"\n"
" data: \"type: COLLISION_OBJECT_TYPE_STATIC\\n"
"mass: 0.0\\n"
"friction: 0.1\\n"
"restitution: 0.5\\n"
"group: \\\"default\\\"\\n"
"mask: \\\"default\\\"\\n"
"embedded_collision_shape {\\n"
" shapes {\\n"
" shape_type: TYPE_BOX\\n"
" position {\\n"
" }\\n"
" rotation {\\n"
" }\\n"
" index: 0\\n"
" count: 3\\n"
" }\\n"
" data: 200.0\\n"
" data: 35.0\\n"
" data: 10.0\\n"
"}\\n"
"\"\n"
"}\n"
""
position {
x: 480.0
y: 272.0
}
}
embedded_instances {
id: "label"
data: "embedded_components {\n"
" id: \"label\"\n"
" type: \"label\"\n"
" data: \"size {\\n"
" x: 128.0\\n"
" y: 32.0\\n"
"}\\n"
"text: \\\"Click to apply linear impulse to the cube\\\"\\n"
"font: \\\"/fonts/text32.font\\\"\\n"
"material: \\\"/builtins/fonts/label-df.material\\\"\\n"
"\"\n"
" position {\n"
" x: 480.0\n"
" y: 435.0\n"
" }\n"
"}\n"
""
}
16 changes: 16 additions & 0 deletions physics/apply_linear_impulse/scripts/cube.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function init(self)
msg.post(".", "acquire_input_focus")
end

function on_input(self, action_id, action)
if action_id == hash("touch") and action.pressed then
-- get the b2body object
local body = b2d.get_body("#collisionobject")

-- add an impluse
local pos = b2d.body.get_position(body)
b2d.body.apply_linear_impulse(body, vmath.vector3(0,600,0), pos)
end
end


Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added physics/apply_linear_impulse/textures/pixel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions physics/apply_linear_impulse/textures/textures.atlas
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
images {
image: "/textures/pixel.png"
}
images {
image: "/textures/elementStone011.png"
}
images {
image: "/textures/elementStone013.png"
}
extrude_borders: 2
Binary file added physics/apply_linear_impulse/thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.