This commit is contained in:
2026-05-06 17:52:45 -07:00
parent 9fc3f35125
commit 1463c23334
402 changed files with 3758 additions and 0 deletions

8
physics_pick/README.md Normal file
View File

@ -0,0 +1,8 @@
Example on physics-based picking.
Object to be picked is required to have rigid body enabled.
Click on a cube to make it jump.
Select Camera object, go to `Properties - Object - Leenkx Traits` and enable one of the traits you wish to test:
- PickTrait - using scripting
- PickEvent - using scripting with mouse event
- PickTree - using logic nodes

View File

@ -0,0 +1,36 @@
package lnx;
import kha.input.Mouse;
import leenkx.trait.physics.PhysicsWorld;
// Using mouse events
class PickEvent extends iron.Trait {
public function new() {
super();
notifyOnInit(function() {
Mouse.get().notify(onMouseDown, onMouseUp, onMouseMove, onMouseWheel);
});
notifyOnRemove(function() {
// Trait or its object is removed, remove event listeners
Mouse.get().remove(onMouseDown, onMouseUp, onMouseMove, onMouseWheel);
});
}
function onMouseDown(button: Int, x: Int, y: Int) {
// Pick object at mouse coords
var rb = PhysicsWorld.active.pickClosest(x, y);
// Check if picked object is our Cube
if (rb != null && rb.object.name == 'Cube') {
rb.object.transform.translate(0, 0, 1);
rb.syncTransform();
}
}
function onMouseUp(button: Int, x: Int, y: Int) { }
function onMouseMove(x: Int, y: Int, movementX: Int, movementY: Int) { }
function onMouseWheel(delta: Int) { }
}

View File

@ -0,0 +1,26 @@
package lnx;
import iron.system.Input;
import leenkx.trait.physics.PhysicsWorld;
class PickTrait extends iron.Trait {
public function new() {
super();
notifyOnUpdate(function() {
// Check mouse button
var mouse = Input.getMouse();
if (!mouse.started()) return;
// Pick object at mouse coords
var rb = PhysicsWorld.active.pickClosest(mouse.x, mouse.y);
// Check if picked object is our Cube
if (rb != null && rb.object.name == 'Cube') {
rb.object.transform.translate(0, 0, 1);
rb.syncTransform();
}
});
}
}

Binary file not shown.