armory/Sources/armory/trait/physics/bullet/RigidBody.hx

363 lines
11 KiB
Haxe
Raw Normal View History

2017-09-30 00:32:06 +02:00
package armory.trait.physics.bullet;
2015-11-26 22:30:19 +01:00
2017-09-30 02:15:21 +02:00
#if arm_bullet
2015-11-26 22:30:19 +01:00
import haxebullet.Bullet;
2016-07-10 00:51:39 +02:00
import iron.Trait;
import iron.math.Vec4;
2016-08-25 00:26:01 +02:00
import iron.object.Transform;
import iron.object.MeshObject;
2015-11-26 22:30:19 +01:00
class RigidBody extends Trait {
2016-07-19 19:42:46 +02:00
var shape:Shape;
2016-10-23 15:32:26 +02:00
var _motionState:BtMotionStatePointer;
2017-12-12 13:39:15 +01:00
var _shape:BtCollisionShapePointer;
var _shapeConvex:BtConvexHullShapePointer;
var isConvex = false;
2015-11-26 22:30:19 +01:00
public var physics:PhysicsWorld;
2016-10-09 16:06:18 +02:00
public var transform:Transform = null;
2015-11-26 22:30:19 +01:00
public var mass:Float;
public var friction:Float;
2017-05-14 11:59:12 +02:00
public var restitution:Float;
2016-05-06 23:46:04 +02:00
public var collisionMargin:Float;
2016-12-08 14:30:39 +01:00
public var linearDamping:Float;
public var angularDamping:Float;
2017-11-13 10:18:37 +01:00
public var animated:Bool;
2017-11-06 13:01:08 +01:00
var linearFactors:Array<Float>;
var angularFactors:Array<Float>;
var deactivationParams:Array<Float>;
2017-10-29 19:34:10 +01:00
public var group = 1;
public var trigger = false;
2017-12-12 13:39:15 +01:00
var bodyScaleX:Float; // Transform scale at creation time
var bodyScaleY:Float;
var bodyScaleZ:Float;
var currentScaleX:Float;
var currentScaleY:Float;
var currentScaleZ:Float;
2015-11-26 22:30:19 +01:00
2016-01-21 02:37:36 +01:00
public var body:BtRigidBodyPointer = null;
2016-12-07 02:01:42 +01:00
public var ready = false;
2015-11-26 22:30:19 +01:00
static var nextId = 0;
public var id = 0;
2016-10-02 19:52:40 +02:00
public var onReady:Void->Void = null;
2015-11-26 22:30:19 +01:00
2017-05-14 11:59:12 +02:00
public function new(mass = 1.0, shape = Shape.Box, friction = 0.5, restitution = 0.0, collisionMargin = 0.0,
2017-11-13 10:18:37 +01:00
linearDamping = 0.04, angularDamping = 0.1, animated = false,
2017-11-06 13:01:08 +01:00
linearFactors:Array<Float> = null, angularFactors:Array<Float> = null,
group = 1, trigger = false, deactivationParams:Array<Float> = null) {
2015-11-26 22:30:19 +01:00
super();
this.mass = mass;
this.shape = shape;
this.friction = friction;
2017-05-14 11:59:12 +02:00
this.restitution = restitution;
2016-05-06 23:46:04 +02:00
this.collisionMargin = collisionMargin;
2016-12-08 14:30:39 +01:00
this.linearDamping = linearDamping;
this.angularDamping = angularDamping;
2017-11-13 10:18:37 +01:00
this.animated = animated;
2017-11-06 13:01:08 +01:00
this.linearFactors = linearFactors;
this.angularFactors = angularFactors;
2017-10-29 19:34:10 +01:00
this.group = group;
this.trigger = trigger;
2017-11-06 13:01:08 +01:00
this.deactivationParams = deactivationParams;
2017-04-16 14:46:35 +02:00
notifyOnAdd(init);
notifyOnLateUpdate(lateUpdate);
notifyOnRemove(removeFromWorld);
2015-11-26 22:30:19 +01:00
}
2016-05-06 23:46:04 +02:00
inline function withMargin(f:Float) {
return f - f * collisionMargin;
}
2015-11-26 22:30:19 +01:00
2016-10-02 19:52:40 +02:00
public function notifyOnReady(f:Void->Void) {
onReady = f;
2017-04-20 11:13:33 +02:00
if (ready) onReady();
2016-10-02 19:52:40 +02:00
}
2015-11-26 22:30:19 +01:00
public function init() {
2016-12-07 02:01:42 +01:00
if (ready) return;
ready = true;
2017-08-21 12:17:04 +02:00
2017-04-02 13:13:43 +02:00
transform = object.transform;
2017-09-30 00:32:06 +02:00
physics = armory.trait.physics.PhysicsWorld.active;
2017-04-02 13:13:43 +02:00
2017-12-12 13:39:15 +01:00
_shape = null;
_shapeConvex = null;
isConvex = false;
2015-11-26 22:30:19 +01:00
2016-07-19 19:42:46 +02:00
if (shape == Shape.Box) {
2015-11-26 22:30:19 +01:00
_shape = BtBoxShape.create(BtVector3.create(
withMargin(transform.dim.x / 2),
withMargin(transform.dim.y / 2),
withMargin(transform.dim.z / 2)));
2015-11-26 22:30:19 +01:00
}
2016-07-19 19:42:46 +02:00
else if (shape == Shape.Sphere) {
_shape = BtSphereShape.create(withMargin(transform.dim.x / 2));
2015-11-26 22:30:19 +01:00
}
2017-12-04 15:10:20 +01:00
else if (shape == Shape.ConvexHull || (shape == Shape.Mesh && mass > 0)) {
2015-11-26 22:30:19 +01:00
_shapeConvex = BtConvexHullShape.create();
2017-12-04 15:10:20 +01:00
isConvex = true;
addPointsToConvexHull(_shapeConvex, transform.scale, collisionMargin);
2015-11-26 22:30:19 +01:00
}
2016-07-19 19:42:46 +02:00
else if (shape == Shape.Cone) {
2016-01-21 02:37:36 +01:00
_shape = BtConeShapeZ.create(
withMargin(transform.dim.x / 2), // Radius
withMargin(transform.dim.z)); // Height
2015-11-26 22:30:19 +01:00
}
2016-07-19 19:42:46 +02:00
else if (shape == Shape.Cylinder) {
2015-11-26 22:30:19 +01:00
_shape = BtCylinderShapeZ.create(BtVector3.create(
withMargin(transform.dim.x / 2),
withMargin(transform.dim.y / 2),
withMargin(transform.dim.z / 2)));
2015-11-26 22:30:19 +01:00
}
2016-07-19 19:42:46 +02:00
else if (shape == Shape.Capsule) {
var r = transform.dim.x / 2;
2016-01-21 02:37:36 +01:00
_shape = BtCapsuleShapeZ.create(
2016-12-08 14:30:39 +01:00
withMargin(r), // Radius
withMargin(transform.dim.z - r * 2)); // Height between 2 sphere centers
2015-11-26 22:30:19 +01:00
}
2017-12-05 23:45:40 +01:00
else if (shape == Shape.Mesh || shape == Shape.Terrain) { // Static
2015-11-26 22:30:19 +01:00
var meshInterface = BtTriangleMesh.create(true, true);
fillTriangleMesh(meshInterface, transform.scale);
2015-11-26 22:30:19 +01:00
_shape = BtBvhTriangleMeshShape.create(meshInterface, true, true);
}
2017-12-05 23:45:40 +01:00
//else if (shape == Shape.Terrain) {
2017-12-04 15:10:20 +01:00
// var data:Array<Dynamic> = [];
// _shape = BtHeightfieldTerrainShape.create(3, 3, data, 1, -10, 10, 2, 0, true);
//}
2015-11-26 22:30:19 +01:00
var _transform = BtTransform.create();
2017-02-26 15:09:36 +01:00
_transform.setIdentity();
2017-11-27 22:47:09 +01:00
_transform.setOrigin(BtVector3.create(transform.worldx(), transform.worldy(), transform.worldz()));
var rot = transform.world.getQuat();
_transform.setRotation(BtQuaternion.create(rot.x, rot.y, rot.z, rot.w));
2015-11-26 22:30:19 +01:00
var _centerOfMassOffset = BtTransform.create();
2017-02-26 15:09:36 +01:00
_centerOfMassOffset.setIdentity();
_motionState = BtDefaultMotionState.create(_transform, _centerOfMassOffset);
2015-11-26 22:30:19 +01:00
2017-12-04 15:10:20 +01:00
var _inertia = BtVector3.create(0, 0, 0);
if (!isConvex) {
if (mass > 0) _shape.calculateLocalInertia(mass, _inertia);
2017-02-26 15:09:36 +01:00
var _bodyCI = BtRigidBodyConstructionInfo.create(mass, _motionState, _shape, _inertia);
body = BtRigidBody.create(_bodyCI);
2015-11-26 22:30:19 +01:00
}
else {
2017-12-04 15:10:20 +01:00
if (mass > 0) _shapeConvex.calculateLocalInertia(mass, _inertia);
2017-02-26 15:09:36 +01:00
var _bodyCI = BtRigidBodyConstructionInfo.create(mass, _motionState, _shapeConvex, _inertia);
body = BtRigidBody.create(_bodyCI);
2015-11-26 22:30:19 +01:00
}
2017-04-02 13:13:43 +02:00
body.setFriction(friction);
body.setRollingFriction(friction);
2017-05-14 11:59:12 +02:00
body.setRestitution(restitution);
2015-11-26 22:30:19 +01:00
2017-11-06 13:01:08 +01:00
if (deactivationParams != null) {
setDeactivationParams(deactivationParams[0], deactivationParams[1], deactivationParams[2]);
}
else {
setActivationState(ActivationState.NoDeactivation);
}
2015-11-26 22:30:19 +01:00
2016-12-08 14:30:39 +01:00
if (linearDamping != 0.04 || angularDamping != 0.1) {
2017-02-26 15:09:36 +01:00
body.setDamping(linearDamping, angularDamping);
2016-12-08 14:30:39 +01:00
}
2017-11-06 13:01:08 +01:00
if (linearFactors != null) {
setLinearFactor(linearFactors[0], linearFactors[1], linearFactors[2]);
}
2017-11-06 13:01:08 +01:00
if (angularFactors != null) {
setAngularFactor(angularFactors[0], angularFactors[1], angularFactors[2]);
}
if (trigger) body.setCollisionFlags(body.getCollisionFlags() | BtCollisionObject.CF_NO_CONTACT_RESPONSE);
2017-11-02 13:32:21 +01:00
2017-12-12 13:39:15 +01:00
bodyScaleX = currentScaleX = transform.scale.x;
bodyScaleY = currentScaleY = transform.scale.y;
bodyScaleZ = currentScaleZ = transform.scale.z;
2017-11-06 13:01:08 +01:00
id = nextId;
nextId++;
2015-11-26 22:30:19 +01:00
#if js
//body.setUserIndex(nextId);
untyped body.userIndex = id;
#elseif cpp
2017-02-26 15:09:36 +01:00
body.setUserIndex(id);
2015-11-26 22:30:19 +01:00
#end
physics.addRigidBody(this);
2016-10-02 19:52:40 +02:00
if (onReady != null) onReady();
2015-11-26 22:30:19 +01:00
}
2016-03-28 23:02:42 +02:00
function lateUpdate() {
2016-12-07 02:01:42 +01:00
if (!ready) return;
2017-11-13 10:18:37 +01:00
if (object.animation != null || animated) {
2016-10-09 16:06:18 +02:00
syncTransform();
}
else {
2017-02-26 15:09:36 +01:00
var trans = body.getWorldTransform();
2016-10-09 16:06:18 +02:00
var p = trans.getOrigin();
var q = trans.getRotation();
transform.loc.set(p.x(), p.y(), p.z());
transform.rot.set(q.x(), q.y(), q.z(), q.w());
2017-08-23 00:53:26 +02:00
if (object.parent != null) {
var ptransform = object.parent.transform;
transform.loc.x -= ptransform.worldx();
transform.loc.y -= ptransform.worldy();
transform.loc.z -= ptransform.worldz();
}
2017-03-12 17:29:22 +01:00
transform.buildMatrix();
2016-10-09 16:06:18 +02:00
}
2015-11-26 22:30:19 +01:00
}
2016-04-27 10:29:32 +02:00
public function removeFromWorld() {
2016-09-14 11:49:32 +02:00
if (physics != null) physics.removeRigidBody(this);
2015-11-26 22:30:19 +01:00
}
2016-04-27 10:29:32 +02:00
public function activate() {
2017-02-26 15:09:36 +01:00
body.activate(false);
2015-11-26 22:30:19 +01:00
}
2016-04-27 10:29:32 +02:00
public function disableGravity() {
2015-11-26 22:30:19 +01:00
// TODO: use setGravity instead
setLinearFactor(0, 0, 0);
setAngularFactor(0, 0, 0);
}
2016-04-27 10:29:32 +02:00
/*public function setGravity(v:Vec4) {
2017-02-26 15:09:36 +01:00
body.setGravity(BtVector3.create(v.x, v.y, v.z));
2015-11-26 22:30:19 +01:00
}*/
2017-01-19 00:26:18 +01:00
public function setActivationState(newState:Int) {
2017-02-26 15:09:36 +01:00
body.setActivationState(newState);
2017-01-19 00:26:18 +01:00
}
2015-11-26 22:30:19 +01:00
2017-11-06 13:01:08 +01:00
public function setDeactivationParams(linearThreshold:Float, angularThreshold:Float, time:Float) {
body.setSleepingThresholds(linearThreshold, angularThreshold);
// body.setDeactivationTime(time); // not available in ammo
}
2016-08-25 00:26:01 +02:00
public function applyImpulse(impulse:Vec4, loc:Vec4 = null) {
2017-08-07 10:47:29 +02:00
activate();
2016-08-25 00:26:01 +02:00
if (loc == null) {
2017-02-26 15:09:36 +01:00
body.applyCentralImpulse(BtVector3.create(impulse.x, impulse.y, impulse.z));
2015-11-26 22:30:19 +01:00
}
else {
2017-02-26 15:09:36 +01:00
body.applyImpulse(BtVector3.create(impulse.x, impulse.y, impulse.z),
BtVector3.create(loc.x, loc.y, loc.z));
2015-11-26 22:30:19 +01:00
}
}
2016-04-27 10:29:32 +02:00
public function setLinearFactor(x:Float, y:Float, z:Float) {
2017-02-26 15:09:36 +01:00
body.setLinearFactor(BtVector3.create(x, y, z));
2015-11-26 22:30:19 +01:00
}
2016-04-27 10:29:32 +02:00
public function setAngularFactor(x:Float, y:Float, z:Float) {
2017-02-26 15:09:36 +01:00
body.setAngularFactor(BtVector3.create(x, y, z));
2015-11-26 22:30:19 +01:00
}
2016-05-05 19:50:03 +02:00
public function getLinearVelocity():BtVector3 {
2017-02-26 15:09:36 +01:00
return body.getLinearVelocity(); // Unable to compile in cpp
2016-05-05 19:50:03 +02:00
}
2015-11-26 22:30:19 +01:00
2016-04-27 10:29:32 +02:00
public function setLinearVelocity(x:Float, y:Float, z:Float) {
2017-02-26 15:09:36 +01:00
body.setLinearVelocity(BtVector3.create(x, y, z));
2015-11-26 22:30:19 +01:00
}
2016-04-27 10:29:32 +02:00
// public function getAngularVelocity():BtVector3 {
2017-02-26 15:09:36 +01:00
// return body.getAngularVelocity();
2015-11-26 22:30:19 +01:00
// }
2016-04-27 10:29:32 +02:00
public function setAngularVelocity(x:Float, y:Float, z:Float) {
2017-02-26 15:09:36 +01:00
body.setAngularVelocity(BtVector3.create(x, y, z));
2015-11-26 22:30:19 +01:00
}
public function setFriction(f:Float) {
body.setFriction(f);
body.setRollingFriction(f);
this.friction = f;
}
2015-11-26 22:30:19 +01:00
public function syncTransform() {
2017-12-12 13:39:15 +01:00
var t = transform;
t.buildMatrix();
2015-11-26 22:30:19 +01:00
var trans = BtTransform.create();
2017-12-12 13:39:15 +01:00
trans.setOrigin(BtVector3.create(t.worldx(), t.worldy(), t.worldz()));
var rot = t.world.getQuat();
2017-11-27 22:47:09 +01:00
trans.setRotation(BtQuaternion.create(rot.x, rot.y, rot.z, rot.w));
2017-02-26 15:09:36 +01:00
body.setCenterOfMassTransform(trans);
2016-10-09 16:06:18 +02:00
// _motionState.getWorldTransform(trans);
2017-12-12 13:39:15 +01:00
// trans.setOrigin(BtVector3.create(t.loc.x, t.loc.y, t.loc.z));
2016-10-09 16:06:18 +02:00
// _motionState.setWorldTransform(trans);
2017-12-12 13:39:15 +01:00
if (currentScaleX != t.scale.x || currentScaleY != t.scale.y || currentScaleZ != t.scale.z) setScale(t.scale);
2017-07-01 13:12:22 +02:00
activate();
2015-11-26 22:30:19 +01:00
}
2017-12-12 13:39:15 +01:00
function setScale(v:Vec4) {
currentScaleX = v.x;
currentScaleY = v.y;
currentScaleZ = v.z;
2017-12-14 12:23:56 +01:00
if (isConvex) _shapeConvex.setLocalScaling(BtVector3.create(bodyScaleX * v.x, bodyScaleY * v.y, bodyScaleZ * v.z));
else _shape.setLocalScaling(BtVector3.create(bodyScaleX * v.x, bodyScaleY * v.y, bodyScaleZ * v.z));
physics.world.updateSingleAabb(body);
2017-12-12 13:39:15 +01:00
}
function addPointsToConvexHull(shape:BtConvexHullShapePointer, scale:Vec4, margin:Float) {
2017-05-14 09:27:47 +02:00
var positions = cast(object, MeshObject).data.geom.positions;
2015-11-26 22:30:19 +01:00
var sx = scale.x * (1.0 - margin);
var sy = scale.y * (1.0 - margin);
var sz = scale.z * (1.0 - margin);
2015-11-26 22:30:19 +01:00
for (i in 0...Std.int(positions.length / 3)) {
2017-02-26 15:09:36 +01:00
shape.addPoint(BtVector3.create(positions[i * 3] * sx, positions[i * 3 + 1] * sy, positions[i * 3 + 2] * sz), true);
2015-11-26 22:30:19 +01:00
}
}
function fillTriangleMesh(triangleMesh:BtTriangleMeshPointer, scale:Vec4) {
2017-05-14 09:27:47 +02:00
var positions = cast(object, MeshObject).data.geom.positions;
2017-06-02 16:41:36 +02:00
var indices = cast(object, MeshObject).data.geom.indices;
2015-11-26 22:30:19 +01:00
for (i in 0...Std.int(indices[0].length / 3)) {
2017-02-26 15:09:36 +01:00
triangleMesh.addTriangle(
BtVector3.create(positions[indices[0][i * 3 + 0] * 3 + 0] * scale.x,
positions[indices[0][i * 3 + 0] * 3 + 1] * scale.y,
2017-02-26 15:09:36 +01:00
positions[indices[0][i * 3 + 0] * 3 + 2] * scale.z),
BtVector3.create(positions[indices[0][i * 3 + 1] * 3 + 0] * scale.x,
positions[indices[0][i * 3 + 1] * 3 + 1] * scale.y,
2017-02-26 15:09:36 +01:00
positions[indices[0][i * 3 + 1] * 3 + 2] * scale.z),
BtVector3.create(positions[indices[0][i * 3 + 2] * 3 + 0] * scale.x,
positions[indices[0][i * 3 + 2] * 3 + 1] * scale.y,
2017-02-26 15:09:36 +01:00
positions[indices[0][i * 3 + 2] * 3 + 2] * scale.z)
2015-11-26 22:30:19 +01:00
);
}
}
}
2016-07-19 19:42:46 +02:00
2017-11-06 13:01:08 +01:00
@:enum abstract Shape(Int) from Int to Int {
2016-07-19 19:42:46 +02:00
var Box = 0;
var Sphere = 1;
var ConvexHull = 2;
var Mesh = 3;
var Cone = 4;
var Cylinder = 5;
var Capsule = 6;
var Terrain = 7;
}
2017-01-19 00:26:18 +01:00
2017-11-06 13:01:08 +01:00
@:enum abstract ActivationState(Int) from Int to Int {
2017-01-19 00:26:18 +01:00
var Active = 1;
var NoDeactivation = 4;
var NoSimulation = 5;
}
2017-09-30 02:15:21 +02:00
#end