armory/Sources/armory/trait/PhysicsDrag.hx

133 lines
4 KiB
Haxe
Raw Permalink 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;
2021-08-04 22:09:54 +02:00
import iron.math.Vec3;
2017-07-14 14:18:38 +02:00
import iron.math.Vec4;
2018-02-10 13:12:20 +01:00
import iron.math.Mat4;
2017-07-14 14:18:38 +02:00
import iron.math.RayCaster;
2017-09-30 00:32:06 +02:00
import armory.trait.physics.RigidBody;
import armory.trait.physics.PhysicsWorld;
2016-02-19 11:41:54 +01:00
class PhysicsDrag extends Trait {
2017-09-30 02:15:21 +02:00
#if (!arm_bullet)
2016-02-19 11:41:54 +01:00
public function new() { super(); }
#else
2021-08-04 22:09:54 +02:00
@prop public var linearLowerLimit = new Vec3(0,0,0);
@prop public var linearUpperLimit = new Vec3(0,0,0);
@prop public var angularLowerLimit = new Vec3(-10,-10,-10);
@prop public var angularUpperLimit = new Vec3(10,10,10);
2019-12-19 23:54:08 +01:00
var pickConstraint: bullet.Bt.Generic6DofConstraint = null;
var pickDist: Float;
var pickedBody: RigidBody = null;
2016-02-19 11:41:54 +01:00
2019-12-19 23:54:08 +01:00
var rayFrom: bullet.Bt.Vector3;
var rayTo: bullet.Bt.Vector3;
2016-02-19 11:41:54 +01:00
2018-02-10 13:12:20 +01:00
static var v = new Vec4();
static var m = Mat4.identity();
static var first = true;
2016-02-19 11:41:54 +01:00
public function new() {
super();
2018-02-10 13:12:20 +01:00
if (first) {
first = false;
notifyOnUpdate(update);
}
2016-02-19 11:41:54 +01:00
}
function update() {
2018-02-10 13:12:20 +01:00
var physics = PhysicsWorld.active;
if (pickedBody != null) pickedBody.activate();
2016-02-19 11:41:54 +01:00
2017-04-10 21:17:17 +02:00
var mouse = Input.getMouse();
if (mouse.started()) {
2019-12-19 23:54:08 +01:00
2017-04-10 21:17:17 +02:00
var b = physics.pickClosest(mouse.x, mouse.y);
2017-02-26 15:09:36 +01:00
if (b != null && b.mass > 0 && !b.body.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;
2018-02-10 13:12:20 +01:00
m.getInverse(b.object.transform.world);
2017-07-14 14:18:38 +02:00
var hit = physics.hitPointWorld;
2018-02-10 13:12:20 +01:00
v.setFrom(hit);
v.applymat4(m);
var localPivot = new bullet.Bt.Vector3(v.x, v.y, v.z);
var tr = new bullet.Bt.Transform();
2017-02-26 15:09:36 +01:00
tr.setIdentity();
tr.setOrigin(localPivot);
2016-02-19 11:41:54 +01:00
pickConstraint = new bullet.Bt.Generic6DofConstraint(b.body, tr, false);
2021-08-04 22:09:54 +02:00
pickConstraint.setLinearLowerLimit(new bullet.Bt.Vector3(linearLowerLimit.x, linearLowerLimit.y, linearLowerLimit.z));
pickConstraint.setLinearUpperLimit(new bullet.Bt.Vector3(linearUpperLimit.x, linearUpperLimit.y, linearUpperLimit.z));
pickConstraint.setAngularLowerLimit(new bullet.Bt.Vector3(angularLowerLimit.x, angularLowerLimit.y, angularLowerLimit.z));
pickConstraint.setAngularUpperLimit(new bullet.Bt.Vector3(angularUpperLimit.x, angularUpperLimit.y, angularUpperLimit.z));
2017-02-26 15:09:36 +01:00
physics.world.addConstraint(pickConstraint, false);
2016-02-19 11:41:54 +01:00
2017-02-26 15:09:36 +01:00
/*pickConstraint.setParam(4, 0.8, 0);
pickConstraint.setParam(4, 0.8, 1);
pickConstraint.setParam(4, 0.8, 2);
pickConstraint.setParam(4, 0.8, 3);
pickConstraint.setParam(4, 0.8, 4);
pickConstraint.setParam(4, 0.8, 5);
2016-02-19 11:41:54 +01:00
2017-02-26 15:09:36 +01:00
pickConstraint.setParam(1, 0.1, 0);
pickConstraint.setParam(1, 0.1, 1);
pickConstraint.setParam(1, 0.1, 2);
pickConstraint.setParam(1, 0.1, 3);
pickConstraint.setParam(1, 0.1, 4);
pickConstraint.setParam(1, 0.1, 5);*/
2016-02-19 11:41:54 +01:00
2018-02-10 13:12:20 +01:00
pickDist = v.set(hit.x - rayFrom.x(), hit.y - rayFrom.y(), hit.z - rayFrom.z()).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
}
}
2017-04-10 21:17:17 +02:00
else if (mouse.released()) {
2016-02-19 11:41:54 +01:00
if (pickConstraint != null) {
2017-02-26 15:09:36 +01:00
physics.world.removeConstraint(pickConstraint);
2016-02-19 11:41:54 +01:00
pickConstraint = null;
pickedBody = null;
2016-10-15 20:19:09 +02:00
}
Input.occupied = false;
2016-02-19 11:41:54 +01:00
}
2017-04-10 21:17:17 +02:00
else if (mouse.down()) {
2016-10-15 20:19:09 +02:00
if (pickConstraint != null) {
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 dir = new bullet.Bt.Vector3(rayTo.x() - rayFrom.x(), rayTo.y() - rayFrom.y(), rayTo.z() - rayFrom.z());
2018-02-10 13:12:20 +01:00
dir.normalize();
dir.setX(dir.x() * pickDist);
dir.setY(dir.y() * pickDist);
dir.setZ(dir.z() * pickDist);
var newPivotB = new bullet.Bt.Vector3(rayFrom.x() + dir.x(), rayFrom.y() + dir.y(), rayFrom.z() + dir.z());
2016-02-19 11:41:54 +01:00
#if (js || hl)
2017-02-26 15:09:36 +01:00
pickConstraint.getFrameOffsetA().setOrigin(newPivotB);
2016-02-19 11:41:54 +01:00
#elseif cpp
2017-02-26 15:09:36 +01:00
pickConstraint.setFrameOffsetAOrigin(newPivotB);
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
}
}
2018-02-10 13:12:20 +01:00
static var start = new Vec4();
static var end = new Vec4();
2016-04-12 14:44:21 +02:00
inline function setRays() {
2017-04-10 21:17:17 +02:00
var mouse = Input.getMouse();
2017-07-14 14:18:38 +02:00
var camera = iron.Scene.active.camera;
2017-07-14 18:07:30 +02:00
var v = camera.transform.world.getLoc();
rayFrom = new bullet.Bt.Vector3(v.x, v.y, v.z);
2017-07-14 14:18:38 +02:00
RayCaster.getDirection(start, end, mouse.x, mouse.y, camera);
rayTo = new bullet.Bt.Vector3(end.x, end.y, end.z);
2016-02-19 11:41:54 +01:00
}
#end
}