Skip to content

Commit 88e3497

Browse files
committed
2 parents 96366f1 + 3206c8d commit 88e3497

817 files changed

Lines changed: 5827 additions & 1000 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

_data/examplesindex.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,19 @@
11341134
"title": "Box2D Mouse Joint",
11351135
"twitter_image": "https://www.defold.com/examples/physics/box2d_mouse_joint/thumbnail.webp"
11361136
},
1137+
{
1138+
"author": "Defold Foundation",
1139+
"brief": "Create a motorized Box2D prismatic joint with translation limits from script using Box2D.",
1140+
"category": "physics",
1141+
"layout": "example",
1142+
"opengraph_image": "https://www.defold.com/examples/physics/box2d_prismatic_joint/thumbnail.webp",
1143+
"path": "physics/box2d_prismatic_joint",
1144+
"scripts": "box2d_prismatic_joint.script",
1145+
"tags": "physics, box2d",
1146+
"thumbnail": "thumbnail.webp",
1147+
"title": "Box2D Prismatic Joint",
1148+
"twitter_image": "https://www.defold.com/examples/physics/box2d_prismatic_joint/thumbnail.webp"
1149+
},
11371150
{
11381151
"author": "Defold Foundation",
11391152
"brief": "This example shows how to use Box2D world overlap and raycast queries.",
@@ -1160,6 +1173,19 @@
11601173
"title": "Box2D Revolute Bridge",
11611174
"twitter_image": "https://www.defold.com/examples/physics/box2d_revolute_bridge/thumbnail.webp"
11621175
},
1176+
{
1177+
"author": "Defold Foundation",
1178+
"brief": "Create Box2D sensors from static collision shapes.",
1179+
"category": "physics",
1180+
"layout": "example",
1181+
"opengraph_image": "https://www.defold.com/examples/physics/box2d_sensor_toggle/thumbnail.webp",
1182+
"path": "physics/box2d_sensor_toggle",
1183+
"scripts": "sensor_box2d_v3.script, sensor_box2d_v2.script, spawn.script",
1184+
"tags": "physics, box2d",
1185+
"thumbnail": "thumbnail.webp",
1186+
"title": "Box2D Sensors",
1187+
"twitter_image": "https://www.defold.com/examples/physics/box2d_sensor_toggle/thumbnail.webp"
1188+
},
11631189
{
11641190
"brief": "This example shows a simple setup with dynamic physics objects.",
11651191
"category": "physics",
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
-- Unit vector for a 45 degree rail. This is the same direction as the rail sprite.
2+
local AXIS = vmath.vector3(0.70710678, 0.70710678, 0)
3+
4+
-- Prismatic translation is measured along `local_axis_a`, in project units.
5+
-- Define farthest limits on both ends:
6+
local LOWER_TRANSLATION = -110
7+
local UPPER_TRANSLATION = 110
8+
9+
-- Define motor speed that will be used to move the slider
10+
local MOTOR_SPEED = 50
11+
12+
-- Some maximum motor force allows to apply a force on the slider to push it
13+
local MAX_MOTOR_FORCE = 1200
14+
15+
function init(self)
16+
-- This will be storing a current direction of the slider
17+
self.direction = 1
18+
19+
-- Acquire input focus to handle inputs
20+
msg.post(".", "acquire_input_focus")
21+
22+
-- `b2d.get_body()` returns the native Box2D body created by the given collision object.
23+
local slider = b2d.get_body("/slider#collisionobject")
24+
local anchor = b2d.get_body("/rail#collisionobject")
25+
26+
-- This creates a prismatic joint in runtime between the 2 bodies - an anchor and a slider.
27+
-- The joint axis matches the visible diagonal rail in the collection.
28+
self.joint = b2d.joint.create_prismatic(anchor, slider, {
29+
-- Both bodies start at the same position, so zero local anchors share the same joint origin.
30+
local_anchor_a = vmath.vector3(),
31+
local_anchor_b = vmath.vector3(),
32+
33+
-- The axis is local to body A, the static anchor.
34+
local_axis_a = AXIS,
35+
36+
-- Limits keep the dynamic body between the two ends of the rail.
37+
enable_limit = true,
38+
lower_translation = LOWER_TRANSLATION,
39+
upper_translation = UPPER_TRANSLATION,
40+
41+
-- The motor pushes the slider along the axis without extra forces in update().
42+
enable_motor = true,
43+
max_motor_force = MAX_MOTOR_FORCE,
44+
motor_speed = self.direction * MOTOR_SPEED,
45+
46+
-- The connected bodies do not need to collide with each other in this example.
47+
collide_connected = false,
48+
})
49+
end
50+
51+
-- Helper function to reverse the motor direction
52+
local function reverse_motor(self)
53+
-- Assign a negative direction to the current direction variable
54+
self.direction = -self.direction
55+
56+
-- Positive and negative speed move the slider in opposite directions along the axis.
57+
b2d.joint.set_motor_speed(self.joint, self.direction * MOTOR_SPEED)
58+
end
59+
60+
function update(self, dt)
61+
-- The function returns a translation on the defined joint in project units
62+
local translation = b2d.joint.get_joint_translation(self.joint)
63+
64+
-- Reverse automatically when the slider reaches either translation limit.
65+
if self.direction > 0 and translation > UPPER_TRANSLATION then
66+
reverse_motor(self)
67+
elseif self.direction < 0 and translation < LOWER_TRANSLATION then
68+
reverse_motor(self)
69+
end
70+
71+
-- Create a string describing the current direction
72+
local direction_string = self.direction > 0 and "up-right" or "down-left"
73+
74+
-- Update the information on the label
75+
label.set_text("#label", string.format("Motor: %s\nTranslation: %.0f", direction_string, translation))
76+
end
77+
78+
function on_input(self, action_id, action)
79+
-- The built-in touch action also covers mouse clicks in the built-in all.input_binding.
80+
if action_id == hash("touch") and action.pressed then
81+
-- When click or touch is pressed we reverse the motor direction
82+
reverse_motor(self)
83+
end
84+
end
85+
86+
function final(self)
87+
-- Joints created through b2d.joint should be explicitly destroyed.
88+
if self.joint then
89+
b2d.joint.destroy(self.joint)
90+
self.joint = nil
91+
end
92+
93+
-- Release input focus
94+
msg.post(".", "release_input_focus")
95+
end
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
-- Helper function to convert a collision object to a sensor (trigger) for scripting
2+
local function enable_sensor_fixture()
3+
-- `b2d.get_body()` returns the native Box2D body created by this collision object.
4+
local body = b2d.get_body("#collisionobject")
5+
6+
-- In Box2D V2, collision geometry and material data live in fixtures.
7+
-- The collection-authored fixture starts solid, so the ball would collide with it.
8+
local fixture = b2d.body.get_fixtures(body)[1]
9+
10+
-- Turning the fixture into a sensor disables physical collision response.
11+
-- Box2D still reports trigger-style enter/exit interactions for overlaps.
12+
b2d.fixture.set_sensor(body, fixture.index, true)
13+
end
14+
15+
function init(self)
16+
-- This script uses the Box2D V2 fixture API.
17+
self.active = b2d.get_version().major == 2
18+
19+
-- If Box2D V2 is not used, skip further processing
20+
if not self.active then
21+
return
22+
end
23+
24+
enable_sensor_fixture()
25+
self.overlaps = {}
26+
end
27+
28+
local NORMAL_SCALE = vmath.vector3(1.0)
29+
local LARGER_SCALE = vmath.vector3(1.1)
30+
31+
function on_message(self, message_id, message, sender)
32+
-- If Box2D V2 is not used, skip further processing
33+
if not self.active then
34+
return
35+
end
36+
37+
-- Defold sends `trigger_response` when a sensor fixture starts or stops
38+
-- overlapping another collision object allowed by the collision mask.
39+
if message_id == hash("trigger_response") then
40+
41+
-- Store overlaps in a table or clear those that exited the sensor
42+
if message.enter then
43+
self.overlaps[message.other_id] = true
44+
else
45+
self.overlaps[message.other_id] = nil
46+
end
47+
48+
-- Count overlaps
49+
local count = 0
50+
for i,v in pairs(self.overlaps) do count = count + 1 end
51+
52+
-- Set the sprite scale to indicate the overlapping visually
53+
if count > 0 then
54+
go.set("#sprite", "scale", LARGER_SCALE)
55+
else
56+
go.set("#sprite", "scale", NORMAL_SCALE)
57+
end
58+
59+
-- Update the label text to show the amount of overlaps
60+
label.set_text("#label", "Overlaps: " .. count)
61+
end
62+
end
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
-- Helper function to create a sensor from the collision object for scripting
2+
local function create_sensor_shape()
3+
-- `b2d.get_body()` returns the native Box2D body created by this collision object.
4+
local body = b2d.get_body("#collisionobject")
5+
6+
-- In Box2D V3, collision geometry is attached to a body as shapes.
7+
-- Read the first editor-authored shape so the runtime sensor keeps the same size.
8+
local shape = b2d.body.get_shapes(body)[1]
9+
local shape_definition = b2d.shape.get_shape(body, shape.index)
10+
11+
-- The editor-authored shape is solid, so it would block the falling ball.
12+
-- Remove it before creating the replacement sensor shape.
13+
b2d.body.destroy_shape(body, shape.index)
14+
15+
-- A sensor shape reports overlaps without producing collision response.
16+
-- The ball can pass through it, but Box2D still tracks which shapes overlap it.
17+
local sensor = b2d.body.create_shape(body, {
18+
shape = shape_definition,
19+
friction = 0.0,
20+
restitution = 0.0,
21+
sensor = true,
22+
})
23+
24+
-- V3 sensor overlap polling is opt-in. Without this call,
25+
-- `get_sensor_overlaps()` would not return the current overlap list.
26+
b2d.shape.enable_sensor_events(sensor.shape_id, true)
27+
return sensor.shape_id
28+
end
29+
30+
function init(self)
31+
-- This script uses the Box2D V3 shape API.
32+
self.active = b2d.get_version().major == 3
33+
34+
-- If Box2D V3 is not used, skip further processing
35+
if not self.active then
36+
return
37+
end
38+
39+
self.sensor_shape = create_sensor_shape()
40+
self.overlaps = {}
41+
end
42+
43+
local NORMAL_SCALE = vmath.vector3(1.0)
44+
local LARGER_SCALE = vmath.vector3(1.1)
45+
46+
function update(self, dt)
47+
-- If Box2D V3 is not used, skip further processing
48+
if not self.active then
49+
return
50+
end
51+
52+
-- V3 returns the shapes currently overlapping this sensor shape.
53+
self.overlaps = b2d.shape.get_sensor_overlaps(self.sensor_shape)
54+
55+
-- Set the sprite scale to indicate the overlapping visually
56+
if #self.overlaps > 0 then
57+
go.set("#sprite", "scale", LARGER_SCALE)
58+
else
59+
go.set("#sprite", "scale", NORMAL_SCALE)
60+
end
61+
62+
-- Update the label text to show the amount of overlaps
63+
label.set_text("#label", "Overlaps: " .. #self.overlaps)
64+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
-- Helper function to spawn a ball at random position on top and remove it after a moment
2+
local function spawn_ball()
3+
-- X position is random between 200-600
4+
local x = 400 + math.random(-200, 200)
5+
6+
-- Spawn the ball from the ball_factory, assign the random position
7+
-- and store the created instance's id
8+
local id = factory.create("#ball_factory", vmath.vector3(x, 600, 0.0))
9+
10+
-- After a moment remove the spawned ball
11+
timer.delay(1.2, false, function()
12+
go.delete(id)
13+
end)
14+
end
15+
16+
function init(self)
17+
-- Initialize random number generator
18+
math.randomseed(os.time())
19+
20+
-- Use the above helper function to spawn the ball every 0.5 second
21+
timer.delay(0.2, true, spawn_ball)
22+
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"content":[{"name":"game.projectc","size":4800,"pieces":[{"name":"game0.projectc","offset":0}]},{"name":"game.arci","size":3168,"pieces":[{"name":"game0.arci","offset":0}]},{"name":"game.arcd","size":1444544,"pieces":[{"name":"game0.arcd","offset":0}]},{"name":"game.dmanifest","size":3303,"pieces":[{"name":"game0.dmanifest","offset":0}]}],"total_size":1455815}
1+
{"content":[{"name":"game.projectc","size":4826,"pieces":[{"name":"game0.projectc","offset":0}]},{"name":"game.arci","size":3168,"pieces":[{"name":"game0.arci","offset":0}]},{"name":"game.arcd","size":1444544,"pieces":[{"name":"game0.arcd","offset":0}]},{"name":"game.dmanifest","size":3303,"pieces":[{"name":"game0.dmanifest","offset":0}]}],"total_size":1455841}
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

examples/animation/3d_animations/archive/game0.projectc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ write_log = 0
77
write_log_file = 0
88
minimum_log_level = 1
99
compress_archive = 1
10+
dependencies_metadata = 0
1011
title_as_file_name = Defoldexamples
1112

1213
[display]

0 commit comments

Comments
 (0)