forked from LeenkxTeam/Leenkx_Examples
update
This commit is contained in:
4
physics_raycast/README.md
Normal file
4
physics_raycast/README.md
Normal file
@ -0,0 +1,4 @@
|
||||
- Keyboard `L/R` Rotate cone
|
||||
- Mouse `L` Raycast in cone direction
|
||||
|
||||
https://github.com/leenkx3d/leenkx/wiki/physics#raycast
|
||||
52
physics_raycast/Sources/lnx/RayCastTrait.hx
Normal file
52
physics_raycast/Sources/lnx/RayCastTrait.hx
Normal file
@ -0,0 +1,52 @@
|
||||
package lnx;
|
||||
|
||||
import leenkx.trait.physics.PhysicsWorld;
|
||||
|
||||
class RayCastTrait extends iron.Trait {
|
||||
|
||||
var q = new iron.math.Quat();
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
|
||||
var mouse = iron.system.Input.getMouse();
|
||||
var keyboard = iron.system.Input.getKeyboard();
|
||||
|
||||
notifyOnUpdate(function() {
|
||||
|
||||
if (mouse.down()) {
|
||||
var physics = PhysicsWorld.active;
|
||||
|
||||
// Start from cone location
|
||||
var from = object.transform.world.getLoc();
|
||||
|
||||
// Cast ray in the direction cone points to
|
||||
var to = object.transform.look();
|
||||
|
||||
// 1000 units long
|
||||
to.mult(1000);
|
||||
|
||||
// End position
|
||||
to.add(from);
|
||||
|
||||
var hit = physics.rayCast(from, to);
|
||||
var rb = (hit != null) ? hit.rb : null;
|
||||
var info = '';
|
||||
if( rb != null ) info += ' ${rb.object.name}';
|
||||
trace(info);
|
||||
}
|
||||
|
||||
if (keyboard.down("left")) {
|
||||
q.fromEuler(0, 0, 0.1);
|
||||
object.transform.rot.mult(q);
|
||||
object.transform.buildMatrix();
|
||||
}
|
||||
|
||||
if (keyboard.down("right")) {
|
||||
q.fromEuler(0, 0, -0.1);
|
||||
object.transform.rot.mult(q);
|
||||
object.transform.buildMatrix();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
BIN
physics_raycast/physics_raycast.blend
Normal file
BIN
physics_raycast/physics_raycast.blend
Normal file
Binary file not shown.
Reference in New Issue
Block a user