protected static Transform toBulletTransform(org.terasology.math.Transform transform) { return new Transform( new javax.vecmath.Matrix4f(VecMath.to(transform.rotation), VecMath.to(transform.origin), transform.scale) ); } }
@Override public BulletSweepCallback sweep(org.terasology.math.geom.Vector3f startPos, org.terasology.math.geom.Vector3f endPos, float allowedPenetration, float slopeFactor) { Transform startTransform = new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), VecMath.to(startPos), 1.0f)); Transform endTransform = new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), VecMath.to(endPos), 1.0f)); BulletSweepCallback callback = new BulletSweepCallback(collider, new org.terasology.math.geom.Vector3f(0, 1, 0), slopeFactor); callback.collisionFilterGroup = collider.getBroadphaseHandle().collisionFilterGroup; callback.collisionFilterMask = collider.getBroadphaseHandle().collisionFilterMask; callback.collisionFilterMask = (short) (callback.collisionFilterMask & (~StandardCollisionGroup.SENSOR.getFlag())); collider.convexSweepTest((ConvexShape) (collider.getCollisionShape()), startTransform, endTransform, callback, allowedPenetration); return callback; } }
private PairCachingGhostObject createCollider(Vector3f pos, ConvexShape shape, short groups, short filters, int collisionFlags) { Transform startTransform = new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), pos, 1.0f)); PairCachingGhostObject result = new PairCachingGhostObject(); result.setWorldTransform(startTransform); result.setCollisionShape(shape); result.setCollisionFlags(collisionFlags); discreteDynamicsWorld.addCollisionObject(result, groups, filters); return result; }
@Override public CollisionShape rotate(Quat4f rot) { CompoundShape newShape = new CompoundShape(); for (BulletCompoundShapeChild child : childList) { CollisionShape rotatedChild = child.childShape.rotate(rot); javax.vecmath.Vector3f offset = com.bulletphysics.linearmath.QuaternionUtil.quatRotate(VecMath.to(rot), child.transform.origin, new javax.vecmath.Vector3f()); newShape.addChildShape(new Transform(new javax.vecmath.Matrix4f(VecMath.to(Rotation.none().getQuat4f()), offset, 1.0f)), ((BulletCollisionShape) rotatedChild).underlyingShape); } return new BulletCompoundShape(newShape); }
@Override public Transform getWorldTransform(Transform transform) { LocationComponent loc = entity.getComponent(LocationComponent.class); if (loc != null) { // NOTE: JBullet ignores scale anyway transform.set(new javax.vecmath.Matrix4f(VecMath.to(loc.getWorldRotation()), VecMath.to(loc.getWorldPosition()), 1)); } return transform; }
@Override //TODO: update if detectGroups changed public boolean updateTrigger(EntityRef entity) { LocationComponent location = entity.getComponent(LocationComponent.class); PairCachingGhostObject triggerObj = entityTriggers.get(entity); if (location == null) { logger.warn("Trying to update or create trigger of entity that has no LocationComponent?! Entity: {}", entity); return false; } if (triggerObj != null) { float scale = location.getWorldScale(); if (Math.abs(triggerObj.getCollisionShape().getLocalScaling(new Vector3f()).x - scale) > BulletGlobals.SIMD_EPSILON) { discreteDynamicsWorld.removeCollisionObject(triggerObj); newTrigger(entity); } else { Quat4f worldRotation = VecMath.to(location.getWorldRotation()); Vector3f worldPosition = VecMath.to(location.getWorldPosition()); triggerObj.setWorldTransform(new Transform(new Matrix4f(worldRotation, worldPosition, 1.0f))); } return true; } else { newTrigger(entity); return false; } }
toWorld.y -= 0.2f; CollisionWorld.ClosestRayResultCallback rayResult = new CollisionWorld.ClosestRayResultCallback(fromWorld, toWorld); Transform from = new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), fromWorld, 1.0f)); Transform to = new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), toWorld, 1.0f)); Transform targetTransform = this.hitCollisionObject.getWorldTransform(new Transform()); CollisionWorld.rayTestSingle(from, to, this.hitCollisionObject, this.hitCollisionObject.getCollisionShape(), targetTransform, rayResult); toWorld.add(secondTraceOffset); rayResult = new CollisionWorld.ClosestRayResultCallback(fromWorld, toWorld); from = new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), fromWorld, 1.0f)); to = new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), toWorld, 1.0f)); targetTransform = this.hitCollisionObject.getWorldTransform(new Transform()); CollisionWorld.rayTestSingle(from, to, this.hitCollisionObject, this.hitCollisionObject.getCollisionShape(), targetTransform, rayResult);
toWorld.add(lookAheadOffset); CollisionWorld.ClosestRayResultCallback rayResult = new CollisionWorld.ClosestRayResultCallback(fromWorld, toWorld); Transform transformFrom = new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), fromWorld, 1.0f)); Transform transformTo = new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), toWorld, 1.0f)); Transform targetTransform = this.hitCollisionObject.getWorldTransform(new Transform()); CollisionWorld.rayTestSingle(transformFrom, transformTo, this.hitCollisionObject, this.hitCollisionObject.getCollisionShape(), targetTransform, rayResult); toWorld.add(lookAheadOffset); rayResult = new CollisionWorld.ClosestRayResultCallback(fromWorld, toWorld); transformFrom = new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), fromWorld, 1.0f)); transformTo = new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), toWorld, 1.0f)); targetTransform = this.hitCollisionObject.getWorldTransform(new Transform()); CollisionWorld.rayTestSingle(transformFrom, transformTo, this.hitCollisionObject, this.hitCollisionObject.getCollisionShape(), targetTransform, rayResult);
public Matrix4f getMat() { return new Matrix4f( this.mat ); }
public BulletPhysics(WorldProvider world) { broadphase = new DbvtBroadphase(); broadphase.getOverlappingPairCache().setInternalGhostPairCallback(new GhostPairCallback()); CollisionConfiguration defaultCollisionConfiguration = new DefaultCollisionConfiguration(); dispatcher = new CollisionDispatcher(defaultCollisionConfiguration); SequentialImpulseConstraintSolver sequentialImpulseConstraintSolver = new SequentialImpulseConstraintSolver(); discreteDynamicsWorld = new DiscreteDynamicsWorld(dispatcher, broadphase, sequentialImpulseConstraintSolver, defaultCollisionConfiguration); discreteDynamicsWorld.setGravity(new Vector3f(0f, -15f, 0f)); blockEntityRegistry = CoreRegistry.get(BlockEntityRegistry.class); wrapper = new PhysicsWorldWrapper(world); VoxelWorldShape worldShape = new VoxelWorldShape(wrapper); liquidWrapper = new PhysicsLiquidWrapper(world); VoxelWorldShape liquidShape = new VoxelWorldShape(liquidWrapper); Matrix3f rot = new Matrix3f(); rot.setIdentity(); DefaultMotionState blockMotionState = new DefaultMotionState(new Transform(new Matrix4f(rot, new Vector3f(0, 0, 0), 1.0f))); RigidBodyConstructionInfo blockConsInf = new RigidBodyConstructionInfo(0, blockMotionState, worldShape, new Vector3f()); BulletRigidBody rigidBody = new BulletRigidBody(blockConsInf); rigidBody.rb.setCollisionFlags(CollisionFlags.STATIC_OBJECT | rigidBody.rb.getCollisionFlags()); short mask = (short) (~(CollisionFilterGroups.STATIC_FILTER | StandardCollisionGroup.LIQUID.getFlag())); discreteDynamicsWorld.addRigidBody(rigidBody.rb, combineGroups(StandardCollisionGroup.WORLD), mask); RigidBodyConstructionInfo liquidConsInfo = new RigidBodyConstructionInfo(0, blockMotionState, liquidShape, new Vector3f()); BulletRigidBody liquidBody = new BulletRigidBody(liquidConsInfo); liquidBody.rb.setCollisionFlags(CollisionFlags.STATIC_OBJECT | rigidBody.rb.getCollisionFlags()); discreteDynamicsWorld.addRigidBody(liquidBody.rb, combineGroups(StandardCollisionGroup.LIQUID), CollisionFilterGroups.SENSOR_TRIGGER); }
Matrix4f transform = new Matrix4f(); transform.setIdentity();
Matrix4f matrix = new Matrix4f(); matrix.setIdentity(); matrix.setRotation( new AxisAngle4f( 0, 1, 0, rotation ) );
public static Matrix4f generateRandomMatrix4f(Random random, float maxAbsolute) { Matrix4f ret = new Matrix4f(); return ret; }
public static void translateMatrix(Matrix4f mat, float x, float y) { Matrix4f transMat = new Matrix4f(1.0f, 0.0f, 0.0f, x, 0.0f, 1.0f, 0.0f, y, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f); mat.mul(transMat); }
public static void scaleMatrix(Matrix4f mat, float x, float y) { Matrix4f scaleMat = new Matrix4f(x, 0.0f, 0.0f, 0.0f, 0.0f, y, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f); mat.mul(scaleMat); }
public static Matrix4f createInverse(Matrix4f mat) { Matrix4f rv = new Matrix4f(); rv.invert(mat); return rv; }
public static void rotateMatrixX(Matrix4f mat, float thetaDegrees) { Matrix4f rotMat = new Matrix4f(); rotMat.rotX((float)(thetaDegrees * Math.PI/180)); mat.mul(rotMat); }
public static void rotateMatrixY(Matrix4f mat, float thetaDegrees) { Matrix4f rotMat = new Matrix4f(); rotMat.rotY((float)(thetaDegrees * Math.PI/180)); mat.mul(rotMat); }
public static void rotateMatrixZ(Matrix4f mat, float thetaDegrees) { Matrix4f rotMat = new Matrix4f(); rotMat.rotZ((float)(thetaDegrees * Math.PI/180)); mat.mul(rotMat); }