update
This commit is contained in:
8
physics_pick/README.md
Normal file
8
physics_pick/README.md
Normal 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
|
||||
|
||||
36
physics_pick/Sources/lnx/PickEvent.hx
Normal file
36
physics_pick/Sources/lnx/PickEvent.hx
Normal 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) { }
|
||||
}
|
||||
26
physics_pick/Sources/lnx/PickTrait.hx
Normal file
26
physics_pick/Sources/lnx/PickTrait.hx
Normal 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();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
BIN
physics_pick/physics_pick.blend
Normal file
BIN
physics_pick/physics_pick.blend
Normal file
Binary file not shown.
Reference in New Issue
Block a user