armory/Sources/armory/trait/PhysicsDrag.hx

159 lines
4.3 KiB
Haxe
Raw Normal View History

2016-07-10 00:51:39 +02:00
package armory.trait;
2016-02-19 11:41:54 +01:00
2016-07-10 00:51:39 +02:00
import iron.Trait;
2016-09-23 00:34:42 +02:00
import iron.system.Input;
2016-07-10 00:51:39 +02:00
import armory.trait.internal.RigidBody;
import armory.trait.internal.PhysicsWorld;
2016-10-12 18:26:56 +02:00
#if arm_physics
2016-02-19 11:41:54 +01:00
import haxebullet.Bullet;
#end
2016-10-12 17:52:27 +02:00
@:keep
2016-02-19 11:41:54 +01:00
class PhysicsDrag extends Trait {
2016-10-12 18:26:56 +02:00
#if (!arm_physics)
2016-02-19 11:41:54 +01:00
public function new() { super(); }
#else
var physics:PhysicsWorld;
var pickConstraint:BtGeneric6DofConstraintPointer = null;
var pickDist:Float;
var pickedBody:RigidBody = null;
var rayFrom:BtVector3Pointer;
var rayTo:BtVector3Pointer;
public function new() {
super();
2016-07-10 00:51:39 +02:00
notifyOnInit(init);
2016-02-19 11:41:54 +01:00
}
function init() {
2016-08-29 09:56:34 +02:00
physics = armory.trait.internal.PhysicsWorld.active;
2016-10-02 19:52:40 +02:00
notifyOnUpdate(update);
2016-02-19 11:41:54 +01:00
}
function update() {
if (pickedBody != null) {
pickedBody.activate();
}
if (Input.started) {
var b = physics.pickClosest(Input.x, Input.y);
2016-09-23 00:34:42 +02:00
if (b != null && b.mass > 0 && !b.body.ptr.isKinematicObject() && b.object.getTrait(PhysicsDrag) != null) {
2016-02-19 11:41:54 +01:00
2016-04-12 14:44:21 +02:00
setRays();
2016-02-19 11:41:54 +01:00
pickedBody = b;
#if js
var pickPos:BtVector3 = physics.rayCallback.get_m_hitPointWorld();
#elseif cpp
var pickPos:BtVector3 = physics.rayCallback.value.m_hitPointWorld;
#end
2016-09-23 00:34:42 +02:00
// var ct = b.object.transform.matrix;
// var inv = iron.math.Mat4.identity();
// inv.getInverse(ct);
// var localPivotVec = new iron.math.Vec4(pickPos.x(), pickPos.y(), pickPos.z());
// localPivotVec.applyMat4(inv);
// var localPivot:BtVector3 = BtVector3.create(localPivotVec.x, localPivotVec.y, localPivotVec.z);
2016-10-15 20:19:09 +02:00
var ct = b.body.ptr.getCenterOfMassTransform();
var inv = ct.inverse();
2016-02-19 11:41:54 +01:00
#if js
2016-10-15 20:19:09 +02:00
var localPivot:BtVector3 = inv.mulVec(pickPos);
2016-02-19 11:41:54 +01:00
#elseif cpp
2016-10-15 20:19:09 +02:00
var localPivot:BtVector3 = untyped __cpp__("inv.value * pickPos.value"); // Operator overload
#end
2016-02-19 11:41:54 +01:00
2016-10-15 20:19:09 +02:00
var tr = BtTransform.create();
tr.value.setIdentity();
tr.value.setOrigin(localPivot);
2016-02-19 11:41:54 +01:00
2016-10-15 20:19:09 +02:00
pickConstraint = BtGeneric6DofConstraint.create(b.body.value, tr.value, false);
2016-04-27 10:29:32 +02:00
pickConstraint.value.setLinearLowerLimit(BtVector3.create(0, 0, 0).value);
2016-10-15 20:19:09 +02:00
pickConstraint.value.setLinearUpperLimit(BtVector3.create(0, 0, 0).value);
pickConstraint.value.setAngularLowerLimit(BtVector3.create(-10, -10, -10).value);
pickConstraint.value.setAngularUpperLimit(BtVector3.create(10, 10, 10).value);
2016-02-19 11:41:54 +01:00
2016-10-15 20:19:09 +02:00
physics.world.ptr.addConstraint(pickConstraint, false);
2016-02-19 11:41:54 +01:00
2016-10-15 20:19:09 +02:00
/*pickConstraint.value.setParam(4, 0.8, 0);
pickConstraint.value.setParam(4, 0.8, 1);
pickConstraint.value.setParam(4, 0.8, 2);
pickConstraint.value.setParam(4, 0.8, 3);
pickConstraint.value.setParam(4, 0.8, 4);
pickConstraint.value.setParam(4, 0.8, 5);
2016-02-19 11:41:54 +01:00
2016-10-15 20:19:09 +02:00
pickConstraint.value.setParam(1, 0.1, 0);
pickConstraint.value.setParam(1, 0.1, 1);
pickConstraint.value.setParam(1, 0.1, 2);
pickConstraint.value.setParam(1, 0.1, 3);
pickConstraint.value.setParam(1, 0.1, 4);
pickConstraint.value.setParam(1, 0.1, 5);*/
2016-02-19 11:41:54 +01:00
2016-10-15 20:19:09 +02:00
var v = BtVector3.create(pickPos.x() - rayFrom.value.x(),
pickPos.y() - rayFrom.value.y(),
pickPos.z() - rayFrom.value.z());
2016-02-19 11:41:54 +01:00
2016-10-15 20:19:09 +02:00
pickDist = v.value.length();
2016-09-23 00:34:42 +02:00
2016-10-15 20:19:09 +02:00
Input.occupied = true;
2016-02-19 11:41:54 +01:00
}
}
else if (Input.released) {
if (pickConstraint != null) {
2016-04-27 10:29:32 +02:00
physics.world.ptr.removeConstraint(pickConstraint);
2016-02-19 11:41:54 +01:00
pickConstraint = null;
pickedBody = null;
2016-10-15 20:19:09 +02:00
}
2016-09-23 00:34:42 +02:00
2016-10-15 20:19:09 +02:00
Input.occupied = false;
2016-02-19 11:41:54 +01:00
}
else if (Input.touch) {
2016-10-15 20:19:09 +02:00
if (pickConstraint != null) {
2016-02-19 11:41:54 +01:00
2016-10-15 20:19:09 +02:00
setRays();
2016-02-19 11:41:54 +01:00
2016-10-15 20:19:09 +02:00
// Keep it at the same picking distance
var btRayTo = BtVector3.create(rayTo.value.x(), rayTo.value.y(), rayTo.value.z());
var btRayFrom = BtVector3.create(rayFrom.value.x(), rayFrom.value.y(), rayFrom.value.z());
2016-02-19 11:41:54 +01:00
2016-10-15 20:19:09 +02:00
var dir = BtVector3.create(btRayTo.value.x() - btRayFrom.value.x(),
btRayTo.value.y() - btRayFrom.value.y(),
btRayTo.value.z() - btRayFrom.value.z());
2016-02-19 11:41:54 +01:00
2016-10-15 20:19:09 +02:00
var bt = dir.value.normalize();
bt.setX(bt.x() * pickDist);
bt.setY(bt.y() * pickDist);
bt.setZ(bt.z() * pickDist);
2016-02-19 11:41:54 +01:00
2016-10-15 20:19:09 +02:00
var newPivotB = BtVector3.create(btRayFrom.value.x() + bt.x(),
btRayFrom.value.y() + bt.y(),
btRayFrom.value.z() + bt.z());
2016-02-19 11:41:54 +01:00
#if js
2016-10-15 20:19:09 +02:00
pickConstraint.value.getFrameOffsetA().setOrigin(newPivotB.value);
2016-02-19 11:41:54 +01:00
#elseif cpp
2016-10-15 20:19:09 +02:00
pickConstraint.value.setFrameOffsetAOrigin(newPivotB.value);
2016-02-19 11:41:54 +01:00
#end
2016-10-15 20:19:09 +02:00
}
2016-02-19 11:41:54 +01:00
}
}
2016-04-12 14:44:21 +02:00
inline function setRays() {
2016-02-19 11:41:54 +01:00
rayFrom = physics.getRayFrom();
rayTo = physics.getRayTo(Input.x, Input.y);
}
#end
}