armory/Sources/armory/logicnode/CastPhysicsRayNode.hx

44 lines
1 KiB
Haxe
Raw Normal View History

2017-04-08 20:05:35 +02:00
package armory.logicnode;
2017-11-20 15:16:52 +01:00
import iron.math.Vec4;
2017-04-08 20:05:35 +02:00
class CastPhysicsRayNode extends LogicNode {
var v = new Vec4();
public function new(tree:LogicTree) {
super(tree);
}
override function get(from:Int):Dynamic {
var vfrom:Vec4 = inputs[0].get();
var vto:Vec4 = inputs[1].get();
2018-06-14 14:52:05 +02:00
if (vfrom == null || vto == null) return null;
2017-04-08 20:05:35 +02:00
#if arm_physics
2017-09-30 00:32:06 +02:00
var physics = armory.trait.physics.PhysicsWorld.active;
var hit = physics.rayCast(vfrom, vto);
var rb = (hit != null) ? hit.rb : null;
2017-07-14 14:18:38 +02:00
if (from == 0) { // Object
if (rb != null) return rb.object;
}
else if (from == 1) { // Hit
2017-07-14 14:18:38 +02:00
var hitPointWorld:Vec4 = rb != null ? physics.hitPointWorld : null;
if (hitPointWorld != null) {
v.set(hitPointWorld.x, hitPointWorld.y, hitPointWorld.z, 1);
return v;
}
} else { // Normal
var hitNormalWorld:Vec4 = rb != null ? physics.hitNormalWorld : null;
if (hitNormalWorld != null) {
v.set(hitNormalWorld.x, hitNormalWorld.y, hitNormalWorld.z, 0);
return v;
}
2017-04-08 20:05:35 +02:00
}
#end
return null;
}
}