Merge pull request #1056 from katharostech/bullet-physics-updates

Update Bullet Bindings for Physics Traits
This commit is contained in:
Lubos Lenco 2018-12-21 08:15:35 +01:00 committed by GitHub
commit eca85d6817
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 95 additions and 109 deletions

View file

@ -2,10 +2,6 @@ package armory.logicnode;
import iron.math.Vec4;
#if arm_physics
import haxebullet.Bullet;
#end
class CastPhysicsRayNode extends LogicNode {
var v = new Vec4();

View file

@ -7,9 +7,6 @@ import iron.math.Mat4;
import iron.math.RayCaster;
import armory.trait.physics.RigidBody;
import armory.trait.physics.PhysicsWorld;
#if arm_bullet
import haxebullet.Bullet;
#end
class PhysicsDrag extends Trait {
@ -17,12 +14,12 @@ class PhysicsDrag extends Trait {
public function new() { super(); }
#else
var pickConstraint:BtGeneric6DofConstraintPointer = null;
var pickConstraint:bullet.Bt.Generic6DofConstraint = null;
var pickDist:Float;
var pickedBody:RigidBody = null;
var rayFrom:BtVector3;
var rayTo:BtVector3;
var rayFrom:bullet.Bt.Vector3;
var rayTo:bullet.Bt.Vector3;
static var v = new Vec4();
static var m = Mat4.identity();
@ -53,16 +50,16 @@ class PhysicsDrag extends Trait {
var hit = physics.hitPointWorld;
v.setFrom(hit);
v.applymat4(m);
var localPivot = BtVector3.create(v.x, v.y, v.z);
var tr = BtTransform.create();
var localPivot = new bullet.Bt.Vector3(v.x, v.y, v.z);
var tr = new bullet.Bt.Transform();
tr.setIdentity();
tr.setOrigin(localPivot);
pickConstraint = BtGeneric6DofConstraint.create(b.body, tr, false);
pickConstraint.setLinearLowerLimit(BtVector3.create(0, 0, 0));
pickConstraint.setLinearUpperLimit(BtVector3.create(0, 0, 0));
pickConstraint.setAngularLowerLimit(BtVector3.create(-10, -10, -10));
pickConstraint.setAngularUpperLimit(BtVector3.create(10, 10, 10));
pickConstraint = new bullet.Bt.Generic6DofConstraint(b.body, tr, false);
pickConstraint.setLinearLowerLimit(new bullet.Bt.Vector3(0, 0, 0));
pickConstraint.setLinearUpperLimit(new bullet.Bt.Vector3(0, 0, 0));
pickConstraint.setAngularLowerLimit(new bullet.Bt.Vector3(-10, -10, -10));
pickConstraint.setAngularUpperLimit(new bullet.Bt.Vector3(10, 10, 10));
physics.world.addConstraint(pickConstraint, false);
/*pickConstraint.setParam(4, 0.8, 0);
@ -99,12 +96,12 @@ class PhysicsDrag extends Trait {
setRays();
// Keep it at the same picking distance
var dir = BtVector3.create(rayTo.x() - rayFrom.x(), rayTo.y() - rayFrom.y(), rayTo.z() - rayFrom.z());
var dir = new bullet.Bt.Vector3(rayTo.x() - rayFrom.x(), rayTo.y() - rayFrom.y(), rayTo.z() - rayFrom.z());
dir.normalize();
dir.setX(dir.x() * pickDist);
dir.setY(dir.y() * pickDist);
dir.setZ(dir.z() * pickDist);
var newPivotB = BtVector3.create(rayFrom.x() + dir.x(), rayFrom.y() + dir.y(), rayFrom.z() + dir.z());
var newPivotB = new bullet.Bt.Vector3(rayFrom.x() + dir.x(), rayFrom.y() + dir.y(), rayFrom.z() + dir.z());
#if js
pickConstraint.getFrameOffsetA().setOrigin(newPivotB);
@ -121,9 +118,9 @@ class PhysicsDrag extends Trait {
var mouse = Input.getMouse();
var camera = iron.Scene.active.camera;
var v = camera.transform.world.getLoc();
rayFrom = BtVector3.create(v.x, v.y, v.z);
rayFrom = new bullet.Bt.Vector3(v.x, v.y, v.z);
RayCaster.getDirection(start, end, mouse.x, mouse.y, camera);
rayTo = BtVector3.create(end.x, end.y, end.z);
rayTo = new bullet.Bt.Vector3(end.x, end.y, end.z);
}
#end
}

View file

@ -6,9 +6,6 @@ import iron.object.CameraObject;
import iron.object.Transform;
import iron.system.Time;
import armory.trait.physics.PhysicsWorld;
#if arm_bullet
import haxebullet.Bullet;
#end
class VehicleBody extends Trait {
@ -26,8 +23,8 @@ class VehicleBody extends Trait {
var camera:CameraObject;
var wheels:Array<Object> = [];
var vehicle:BtRaycastVehiclePointer = null;
var carChassis:BtRigidBodyPointer;
var vehicle:bullet.Bt.RaycastVehicle = null;
var carChassis:bullet.Bt.RigidBody;
var chassis_mass = 600.0;
var wheelFriction = 1000;
@ -58,31 +55,31 @@ class VehicleBody extends Trait {
wheels.push(iron.Scene.active.root.getChild(n));
}
var wheelDirectionCS0 = BtVector3.create(0, 0, -1);
var wheelAxleCS = BtVector3.create(1, 0, 0);
var wheelDirectionCS0 = new bullet.Bt.Vector3(0, 0, -1);
var wheelAxleCS = new bullet.Bt.Vector3(1, 0, 0);
var chassisShape = BtBoxShape.create(BtVector3.create(
var chassisShape = new bullet.Bt.BoxShape(new bullet.Bt.Vector3(
transform.dim.x / 2,
transform.dim.y / 2,
transform.dim.z / 2));
var compound = BtCompoundShape.create();
var compound = new bullet.Bt.CompoundShape();
var localTrans = BtTransform.create();
var localTrans = new bullet.Bt.Transform();
localTrans.setIdentity();
localTrans.setOrigin(BtVector3.create(0, 0, 1));
localTrans.setOrigin(new bullet.Bt.Vector3(0, 0, 1));
compound.addChildShape(localTrans, chassisShape);
carChassis = createRigidBody(chassis_mass, compound);
// Create vehicle
var tuning = BtVehicleTuning.create();
var vehicleRayCaster = BtDefaultVehicleRaycaster.create(physics.world);
vehicle = BtRaycastVehicle.create(tuning, carChassis, vehicleRayCaster);
var tuning = new bullet.Bt.VehicleTuning();
var vehicleRayCaster = new bullet.Bt.DefaultVehicleRaycaster(physics.world);
vehicle = new bullet.Bt.RaycastVehicle(tuning, carChassis, vehicleRayCaster);
// Never deactivate the vehicle
carChassis.setActivationState(BtCollisionObject.DISABLE_DEACTIVATION);
carChassis.setActivationState(bullet.Bt.CollisionObject.DISABLE_DEACTIVATION);
// Choose coordinate system
var rightIndex = 0;
@ -189,32 +186,32 @@ class VehicleBody extends Trait {
camera.buildMatrix();
}
function createRigidBody(mass:Float, shape:BtCompoundShapePointer):BtRigidBodyPointer {
function createRigidBody(mass:Float, shape:bullet.Bt.CompoundShape):bullet.Bt.RigidBody {
var localInertia = BtVector3.create(0, 0, 0);
var localInertia = new bullet.Bt.Vector3(0, 0, 0);
shape.calculateLocalInertia(mass, localInertia);
var centerOfMassOffset = BtTransform.create();
var centerOfMassOffset = new bullet.Bt.Transform();
centerOfMassOffset.setIdentity();
var startTransform = BtTransform.create();
var startTransform = new bullet.Bt.Transform();
startTransform.setIdentity();
startTransform.setOrigin(BtVector3.create(
startTransform.setOrigin(new bullet.Bt.Vector3(
transform.loc.x,
transform.loc.y,
transform.loc.z));
startTransform.setRotation(BtQuaternion.create(
startTransform.setRotation(new bullet.Bt.Quaternion(
transform.rot.x,
transform.rot.y,
transform.rot.z,
transform.rot.w));
var myMotionState = BtDefaultMotionState.create(startTransform, centerOfMassOffset);
var cInfo = BtRigidBodyConstructionInfo.create(mass, myMotionState, shape, localInertia);
var myMotionState = new bullet.Bt.DefaultMotionState(startTransform, centerOfMassOffset);
var cInfo = new bullet.Bt.RigidBodyConstructionInfo(mass, myMotionState, shape, localInertia);
var body = BtRigidBody.create(cInfo);
body.setLinearVelocity(BtVector3.create(0, 0, 0));
body.setAngularVelocity(BtVector3.create(0, 0, 0));
var body = new bullet.Bt.RigidBody(cInfo);
body.setLinearVelocity(new bullet.Bt.Vector3(0, 0, 0));
body.setAngularVelocity(new bullet.Bt.Vector3(0, 0, 0));
physics.world.addRigidBody(body);
return body;
@ -261,8 +258,8 @@ class VehicleWheel {
locZ = vehicleTransform.dim.z / 2 + transform.loc.z;
}
public function getConnectionPoint():BtVector3 {
return BtVector3.create(locX, locY, locZ);
public function getConnectionPoint():bullet.Bt.Vector3 {
return new bullet.Bt.Vector3(locX, locY, locZ);
}
#end
}

View file

@ -2,7 +2,6 @@ package armory.trait.physics.bullet;
#if arm_bullet
import haxebullet.Bullet;
import iron.Trait;
import iron.math.Vec4;
import iron.math.Quat;
@ -12,8 +11,8 @@ import iron.object.MeshObject;
class KinematicCharacterController extends Trait {
var shape:ControllerShape;
var shapeConvex:BtConvexShapePointer;
var shapeConvexHull:BtConvexHullShapePointer;
var shapeConvex:bullet.Bt.ConvexShape;
var shapeConvexHull:bullet.Bt.ConvexHullShape;
var isConvexHull = false;
public var physics:PhysicsWorld;
@ -32,17 +31,17 @@ class KinematicCharacterController extends Trait {
var currentScaleZ:Float;
var jumpSpeed:Float;
public var body:BtPairCachingGhostObjectPointer = null;
public var character:BtKinematicCharacterControllerPointer = null;
public var body:bullet.Bt.PairCachingGhostObject = null;
public var character:bullet.Bt.KinematicCharacterController = null;
public var ready = false;
static var nextId = 0;
public var id = 0;
public var onReady:Void->Void = null;
static var nullvec = true;
static var vec1:BtVector3;
static var quat1:BtQuaternion;
static var trans1:BtTransform;
static var vec1:bullet.Bt.Vector3;
static var quat1:bullet.Bt.Quaternion;
static var trans1:bullet.Bt.Transform;
static var quat = new Quat();
public function new(mass = 1.0, shape = ControllerShape.Capsule, jumpSpeed = 8.0, friction = 0.5, restitution = 0.0,
@ -51,9 +50,9 @@ class KinematicCharacterController extends Trait {
if (nullvec) {
nullvec = false;
vec1 = BtVector3.create(0, 0, 0);
quat1 = BtQuaternion.create(0, 0, 0, 0);
trans1 = BtTransform.create();
vec1 = new bullet.Bt.Vector3(0, 0, 0);
quat1 = new bullet.Bt.Quaternion(0, 0, 0, 0);
trans1 = new bullet.Bt.Transform();
}
this.mass = mass;
@ -94,21 +93,21 @@ class KinematicCharacterController extends Trait {
vec1.setX(withMargin(transform.dim.x / 2));
vec1.setY(withMargin(transform.dim.y / 2));
vec1.setZ(withMargin(transform.dim.z / 2));
shapeConvex = BtBoxShape.create(vec1);
shapeConvex = new bullet.Bt.BoxShape(vec1);
}
else if (shape == ControllerShape.Sphere) {
var width = transform.dim.x;
if(transform.dim.y > width) width = transform.dim.y;
if(transform.dim.z > width) width = transform.dim.z;
shapeConvex = BtSphereShape.create(withMargin(width / 2));
shapeConvex = new bullet.Bt.SphereShape(withMargin(width / 2));
}
else if (shape == ControllerShape.ConvexHull && mass > 0) {
shapeConvexHull = BtConvexHullShape.create();
shapeConvexHull = new bullet.Bt.ConvexHullShape();
isConvexHull = true;
addPointsToConvexHull(shapeConvexHull, transform.scale, collisionMargin);
}
else if (shape == ControllerShape.Cone) {
shapeConvex = BtConeShapeZ.create(
shapeConvex = new bullet.Bt.ConeShapeZ(
withMargin(transform.dim.x / 2), // Radius
withMargin(transform.dim.z)); // Height
}
@ -116,11 +115,11 @@ class KinematicCharacterController extends Trait {
vec1.setX(withMargin(transform.dim.x / 2));
vec1.setY(withMargin(transform.dim.y / 2));
vec1.setZ(withMargin(transform.dim.z / 2));
shapeConvex = BtCylinderShapeZ.create(vec1);
shapeConvex = new bullet.Bt.CylinderShapeZ(vec1);
}
else if (shape == ControllerShape.Capsule) {
var r = transform.dim.x / 2;
shapeConvex = BtCapsuleShapeZ.create(
shapeConvex = new bullet.Bt.CapsuleShapeZ(
withMargin(r), // Radius
withMargin(transform.dim.z - r * 2)); // Height between 2 sphere centers
}
@ -138,17 +137,17 @@ class KinematicCharacterController extends Trait {
quat1.setW(quat.w);
trans1.setRotation(quat1);
body = BtPairCachingGhostObject.create();
body = new bullet.Bt.PairCachingGhostObject();
body.setCollisionShape(isConvexHull ? shapeConvexHull : shapeConvex);
body.setCollisionFlags(BtCollisionObject.CF_CHARACTER_OBJECT);
body.setCollisionFlags(bullet.Bt.CollisionObject.CF_CHARACTER_OBJECT);
body.setWorldTransform(trans1);
body.setFriction(friction);
body.setRollingFriction(friction);
body.setRestitution(restitution);
#if js
character = BtKinematicCharacterController.create(body, isConvexHull ? shapeConvexHull : shapeConvex, 0.5, 2);
character = new bullet.Bt.KinematicCharacterController(body, isConvexHull ? shapeConvexHull : shapeConvex, 0.5, 2);
#elseif cpp
character = BtKinematicCharacterController.create(body, isConvexHull ? shapeConvexHull : shapeConvex, 0.5, BtVector3.create(0.0, 0.0, 1.0));
character = new bullet.Bt.KinematicCharacterController.create(body, isConvexHull ? shapeConvexHull : shapeConvex, 0.5, bullet.Bt.Vector3(0.0, 0.0, 1.0));
#end
character.setJumpSpeed(jumpSpeed);
character.setUseGhostSweepTest(true);
@ -168,7 +167,7 @@ class KinematicCharacterController extends Trait {
body.setUserIndex(id);
#end
physics.addKinematicCharacterController(this);
// physics.addKinematicCharacterController(this);
if (onReady != null) onReady();
}
@ -247,7 +246,7 @@ class KinematicCharacterController extends Trait {
#end
public function removeFromWorld() {
if (physics != null) physics.removeKinematicCharacterController(this);
// if (physics != null) physics.removeKinematicCharacterController(this);
}
public function activate() {
@ -329,7 +328,7 @@ class KinematicCharacterController extends Trait {
physics.world.updateSingleAabb(body);
}
function addPointsToConvexHull(shape:BtConvexHullShapePointer, scale:Vec4, margin:Float) {
function addPointsToConvexHull(shape:bullet.Bt.ConvexHullShape, scale:Vec4, margin:Float) {
var positions = cast(object, MeshObject).data.geom.positions;
var sx = scale.x * (1.0 - margin);

View file

@ -4,7 +4,6 @@ package armory.trait.physics.bullet;
import armory.trait.physics.RigidBody;
import armory.trait.physics.PhysicsWorld;
import haxebullet.Bullet;
class PhysicsConstraint extends iron.Trait {
@ -14,25 +13,25 @@ class PhysicsConstraint extends iron.Trait {
var disableCollisions:Bool;
var breakingThreshold:Float;
var limits:Array<Float>;
var con:BtTypedConstraintPointer = null;
var con:bullet.Bt.TypedConstraint = null;
static var nullvec = true;
static var vec1:BtVector3;
static var vec2:BtVector3;
static var vec3:BtVector3;
static var trans1:BtTransform;
static var trans2:BtTransform;
static var vec1:bullet.Bt.Vector3;
static var vec2:bullet.Bt.Vector3;
static var vec3:bullet.Bt.Vector3;
static var trans1:bullet.Bt.Transform;
static var trans2:bullet.Bt.Transform;
public function new(body1:String, body2:String, type:String, disableCollisions:Bool, breakingThreshold:Float, limits:Array<Float> = null) {
super();
if (nullvec) {
nullvec = false;
vec1 = BtVector3.create(0, 0, 0);
vec2 = BtVector3.create(0, 0, 0);
vec3 = BtVector3.create(0, 0, 0);
trans1 = BtTransform.create();
trans2 = BtTransform.create();
vec1 = new bullet.Bt.Vector3(0, 0, 0);
vec2 = new bullet.Bt.Vector3(0, 0, 0);
vec3 = new bullet.Bt.Vector3(0, 0, 0);
trans1 = new bullet.Bt.Transform();
trans2 = new bullet.Bt.Transform();
}
this.body1 = body1;
@ -70,7 +69,7 @@ class PhysicsConstraint extends iron.Trait {
trans2.setOrigin(vec2);
if (type == "GENERIC" || type == "FIXED" || type == "POINT") {
var c = BtGeneric6DofConstraint.create2(rb1.body, rb2.body, trans1, trans2, false);
var c = bullet.Bt.Generic6DofConstraint.new2(rb1.body, rb2.body, trans1, trans2, false);
if (type == "POINT") {
vec1.setX(0);
vec1.setY(0);
@ -113,7 +112,7 @@ class PhysicsConstraint extends iron.Trait {
axis.setX(0);
axis.setY(0);
axis.setZ(1);
var c = BtHingeConstraint.create(rb1.body, rb2.body, vec2, vec1, axis, axis);
var c = new bullet.Bt.HingeConstraint(rb1.body, rb2.body, vec2, vec1, axis, axis);
con = cast c;
}
// else if (type == "SLIDER") {}
@ -128,7 +127,7 @@ class PhysicsConstraint extends iron.Trait {
public function removeFromWorld() {
#if js
Ammo.destroy(con);
bullet.Bt.Ammo.destroy(con);
#end
}
}

View file

@ -13,7 +13,6 @@ import iron.data.MeshData;
import iron.data.SceneFormat;
import armory.trait.physics.RigidBody;
import armory.trait.physics.PhysicsWorld;
import haxebullet.Bullet;
class PhysicsHook extends Trait {
var target:Object;
@ -21,17 +20,17 @@ class PhysicsHook extends Trait {
var targetTransform:Transform;
var verts:Array<Float>;
var constraint:BtGeneric6DofConstraintPointer = null;
var constraint:bullet.Bt.Generic6DofConstraint = null;
#if arm_physics_soft
var hookRB:BtRigidBodyPointer = null;
var hookRB:bullet.Bt.RigidBody = null;
#end
static var nullvec = true;
static var vec1:BtVector3;
static var quat1:BtQuaternion;
static var trans1:BtTransform;
static var trans2:BtTransform;
static var vec1:bullet.Bt.Vector3;
static var quat1:bullet.Bt.Quaternion;
static var trans1:bullet.Bt.Transform;
static var trans2:bullet.Bt.Transform;
static var quat = new Quat();
public function new(targetName:String, verts:Array<Float>) {
@ -39,10 +38,10 @@ class PhysicsHook extends Trait {
if (nullvec) {
nullvec = false;
vec1 = BtVector3.create(0, 0, 0);
quat1 = BtQuaternion.create(0, 0, 0, 0);
trans1 = BtTransform.create();
trans2 = BtTransform.create();
vec1 = new bullet.Bt.Vector3(0, 0, 0);
quat1 = new bullet.Bt.Quaternion(0, 0, 0, 0);
trans1 = new bullet.Bt.Transform();
trans2 = new bullet.Bt.Transform();
}
this.targetName = targetName;
@ -80,15 +79,15 @@ class PhysicsHook extends Trait {
var centerOfMassOffset = trans2;
centerOfMassOffset.setIdentity();
var mass = 0.0;
var motionState = BtDefaultMotionState.create(trans1, centerOfMassOffset);
var motionState = new bullet.Bt.DefaultMotionState(trans1, centerOfMassOffset);
var inertia = vec1;
inertia.setX(0);
inertia.setY(0);
inertia.setZ(0);
var shape = BtSphereShape.create(0.01);
var shape = new bullet.Bt.SphereShape(0.01);
shape.calculateLocalInertia(mass, inertia);
var bodyCI = BtRigidBodyConstructionInfo.create(mass, motionState, shape, inertia);
hookRB = BtRigidBody.create(bodyCI);
var bodyCI = new bullet.Bt.RigidBodyConstructionInfo(mass, motionState, shape, inertia);
hookRB = new bullet.Bt.RigidBody(bodyCI);
#if js
var nodes = sb.body.get_m_nodes();
@ -131,7 +130,7 @@ class PhysicsHook extends Trait {
vec1.setY(targetTransform.worldy() - object.transform.worldy());
vec1.setZ(targetTransform.worldz() - object.transform.worldz());
trans1.setOrigin(vec1);
constraint = BtGeneric6DofConstraint.create(rb1.body, trans1, false);
constraint = new bullet.Bt.Generic6DofConstraint(rb1.body, trans1, false);
vec1.setX(0);
vec1.setY(0);
vec1.setZ(0);

View file

@ -11,7 +11,6 @@ import iron.data.SceneFormat;
#if arm_physics_soft
import armory.trait.physics.RigidBody;
import armory.trait.physics.PhysicsWorld;
import haxebullet.Bullet;
#end
class SoftBody extends Trait {
@ -31,11 +30,11 @@ class SoftBody extends Trait {
public var vertOffsetY = 0.0;
public var vertOffsetZ = 0.0;
public var body:BtSoftBodyPointer;
public var body:bullet.Bt.SoftBody;
static var helpers:BtSoftBodyHelpers;
static var helpers:bullet.Bt.SoftBodyHelpers;
static var helpersCreated = false;
static var worldInfo:BtSoftBodyWorldInfo;
static var worldInfo:bullet.Bt.SoftBodyWorldInfo;
public function new(shape = SoftShape.Cloth, bend = 0.5, mass = 1.0, margin = 0.04) {
super();
@ -115,7 +114,7 @@ class SoftBody extends Trait {
for (ar in geom.indices) numtri += Std.int(ar.length / 3);
if (!helpersCreated) {
helpers = BtSoftBodyHelpers.create();
helpers = new bullet.Bt.SoftBodyHelpers();
worldInfo = physics.world.getWorldInfo();
helpersCreated = true;
}