‹›
Utility Scripts · Physical Controls

Your First Physical Control

Create a returning button and trigger a game action only when it enters the pressed state.

This example belongs to one demonstration object that owns the physics world. If your project already has a shared physics controller, keep jolt_start(), jolt_update() and jolt_stop() there and copy only control creation, reading and destruction. Read after simulation, for example in End Step when the controller updates physics in Step.

Create event#

GML
button = undefined;
was_pressed = false;
if (!jolt_start()) {
    show_debug_message(jolt_get_error().message);
    exit;
}
jolt_debug_enable(true);
button = jolt_control_button({
    position: {x: 0, y: 0, z: 1},
    travel: 0.02,
    auto_return: true,
    target: 0,
    drive: {max_force: 12, frequency: 12, damping: 1}
});
if (is_undefined(button)) {
    show_debug_message(jolt_get_error().message);
}

Step event#

GML
if (!is_undefined(button) && button.is_valid()) {
    // Test force while holding Space; replace with contact or a grabber later.
    if (keyboard_check(vk_space)) button.body.add_force(0, 0, -30);
}
jolt_update();
if (!is_undefined(button) && button.is_valid()) {
    var pressed = button.is_pressed();
    if (pressed && !was_pressed) {
        show_debug_message("Button pressed");
    }
    was_pressed = pressed;
}

Without arguments, jolt_update() uses delta_time and an internal fixed step. The control needs no additional update call. Holding Space applies enough force to overcome the return; releasing it lets the button return. This example applies force along world −Z because the control has identity orientation.

Draw End event#

GML
// Use your project's active 3D camera.
jolt_debug_draw(JOLT_DEBUG_SHAPES | JOLT_DEBUG_CONSTRAINTS);

Clean Up event#

GML
if (!is_undefined(button)) button.destroy();
jolt_stop();

Current project integrations#