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_motor_joint/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_motor_joint/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_motor_joint/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: []
52 changes: 52 additions & 0 deletions physics/box2d_motor_joint/example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
tags: physics, box2d
title: Box2D Motor Joint
brief: Create and control a motorized Box2D joint from script using Box2D V2 and V3.
author: Defold Foundation
scripts: box2d_motor_joint_v3.script, box2d_motor_joint_v2.script
thumbnail: thumbnail.webp
---

This example creates a motorized Box2D joint at runtime. It works with both Box2D V2 and V3 by attaching one script for each backend. Each script checks `b2d.get_version()` during `init()` and becomes a no-op when the other backend is active.

Click or tap the window to reverse the motor direction.

## What You'll Learn

- How to get Box2D body handles from Defold collision objects
- How to detect the active Box2D version with `b2d.get_version()`
- How to create a pivoted motor joint with `b2d.joint.create_revolute()`
- How to control joint motor speed with `b2d.joint.set_motor_speed()`
- How to tune the Box2D V3 joint solver with `b2d.world.set_joint_tuning()`

## Setup

The collection contains a static `pivot` game object, one dynamic `arm` game object with a particle effect, and a controller game object.

The `pivot` object marks the world point the arm should rotate around. The `arm` object is the dynamic body driven by the joint. The example uses a revolute joint with its motor enabled, because the revolute joint provides a stable hinge pivot while the motor drives the rotation.

The controller has both backend scripts attached. Each script checks `b2d.get_version()` and only runs when the selected app manifest matches its Box2D backend.

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

![game_project](game_project.png)

## How It Works

Both scripts read `b2d.get_version()` once. `box2d_motor_joint_v2.script` only continues when the major version is 2, while `box2d_motor_joint_v3.script` only continues when the major version is 3.

`b2d.get_body()` returns the Box2D bodies owned by the `pivot` and `arm` collision objects. The active script then creates a revolute joint between those bodies with `b2d.joint.create_revolute()`.

The joint definition uses:

- `local_anchor_a` on the pivot body
- `local_anchor_b` on the arm body
- `enable_motor` to turn on the joint motor
- `max_motor_torque` to limit how strongly the motor can rotate the arm
- `motor_speed` to set the current motor direction and speed

The important part is the arm anchor. The pivot is placed in the collection as a visible reference point. The scripts convert that world position into the arm's local space, so the arm rotates around the same visible pivot instead of rotating around its center.

Click or tap to reverse the motor with `b2d.joint.set_motor_speed()`. The V3 script also calls `b2d.world.set_joint_tuning()` to adjust the joint solver used by the Box2D V3 backend.
55 changes: 55 additions & 0 deletions physics/box2d_motor_joint/example/arm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
components {
id: "trail"
component: "/particlefx/trail.particlefx"
position {
x: 90.0
z: -0.1
}
}
components {
id: "play"
component: "/particlefx/play.script"
}
embedded_components {
id: "sprite"
type: "sprite"
data: "default_animation: \"pixel_blue\"\n"
"material: \"/builtins/materials/sprite.material\"\n"
"size {\n"
" x: 200.0\n"
" y: 40.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: 3.0\n"
"friction: 0.4\n"
"restitution: 0.0\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: 100.0\n"
" data: 20.0\n"
" data: 10.0\n"
"}\n"
"linear_damping: 0.05\n"
"angular_damping: 0.2\n"
""
}
47 changes: 47 additions & 0 deletions physics/box2d_motor_joint/example/box2d_motor_joint.collection
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: "box2d_motor_joint"
instances {
id: "pivot"
prototype: "/example/pivot.go"
position {
x: 360.0
y: 360.0
z: 0.5
}
}
instances {
id: "arm"
prototype: "/example/arm.go"
position {
x: 480.0
y: 360.0
}
}
scale_along_z: 0
embedded_instances {
id: "controller"
data: "components {\n"
" id: \"box2d_v2\"\n"
" component: \"/example/box2d_motor_joint_v2.script\"\n"
"}\n"
"components {\n"
" id: \"box2d_v3\"\n"
" component: \"/example/box2d_motor_joint_v3.script\"\n"
"}\n"
"embedded_components {\n"
" id: \"label\"\n"
" type: \"label\"\n"
" data: \"size {\\n"
" x: 640.0\\n"
" y: 96.0\\n"
"}\\n"
"text: \\\"Box2D motor joint\\\"\\n"
"font: \\\"/fonts/text28.font\\\"\\n"
"material: \\\"/builtins/fonts/label-df.material\\\"\\n"
"\"\n"
" position {\n"
" x: 360.0\n"
" y: 60.0\n"
" }\n"
"}\n"
""
}
Loading
Loading