public void transform(Matrix4f mat, boolean updateLocal) { activate(); Transform t = new Transform(); t.set(mat); Vector3f v = new Vector3f(); for (int i = 0; i < 3; ++i) { t.basis.getColumn(i, v); v.normalize(); t.basis.setColumn(i, v); } body.setWorldTransform(t); // required for static objects: body.getMotionState().setWorldTransform(t); if (body.isInWorld() && body.isStaticOrKinematicObject()) { scene.world.updateSingleAabb(body); for (GameObject g : touchingObjects) g.activate(); } // updateChildTransforms(); if (parent != null && updateLocal) { updateLocalTransform(); } }
public void scale(float x, float y, float z, boolean updateLocal) { activate(); // Set unit scale Matrix4 t = modelInstance.transform; Matrix4 mat_scale = new Matrix4(); Vector3 s = new Vector3(); t.getScale(s); mat_scale.scl(1 / s.x, 1 / s.y, 1 / s.z); t.mul(mat_scale); // Set target scale mat_scale.idt(); mat_scale.scl(x, y, z); t.mul(mat_scale); // Relevant bullet body update CollisionShape cs = body.getCollisionShape(); cs.setLocalScaling(new Vector3f(x, y, z)); if (body.isInWorld() && body.isStaticOrKinematicObject()) scene.world.updateSingleAabb(body); // Child propagation Vector3f ps = scale(); Matrix4f pt = transform(); Matrix4f ct = new Matrix4f(); Matrix4f ms = new Matrix4f(); ms.setIdentity(); ms.m00 = ps.x; ms.m11 = ps.y; ms.m22 = ps.z; pt.mul(ms); for (GameObject c : children) { c.scale(scale().mul(c.localScale), false); ct.mul(pt, c.localTransform); c.transform(ct, false); } if (parent != null && updateLocal) { updateLocalScale(); } }
public void bodyType(String s) { int flags = body.getCollisionFlags(); if (body.isInWorld()) scene.world.removeRigidBody(body); if (s.equals("NO_COLLISION")) { for (GameObject g : touchingObjects) g.activate(); flags &= ~CollisionFlags.KINEMATIC_OBJECT; } else { if (s.equals("STATIC")) { flags |= CollisionFlags.KINEMATIC_OBJECT; } else if (s.equals("SENSOR")) { flags |= CollisionFlags.KINEMATIC_OBJECT; flags |= CollisionFlags.NO_CONTACT_RESPONSE; } else { // NO_COLLISION -> DYNAMIC or RIGID_BODY hack if (currBodyType.equals("NO_COLLISION")) { body.clearForces(); body.setLinearVelocity(new Vector3f()); } // kinematic initialization hack if (mass() == Float.POSITIVE_INFINITY) { mass(1); // Blender default flags &= ~CollisionFlags.KINEMATIC_OBJECT; body.setCollisionFlags(flags); } flags &= ~CollisionFlags.KINEMATIC_OBJECT; if (s.equals("DYNAMIC")) { body.setAngularVelocity(new Vector3f()); body.setAngularFactor(0); } else if (s.equals("RIGID_BODY")) { body.setAngularFactor(1); } else { throw new RuntimeException(s + " is no valid bodyType name."); } } scene.world.addRigidBody(body); activate(); } body.setCollisionFlags(flags); currBodyType = s; }
public boolean dynamics() { return body.isInWorld() && !body.isKinematicObject(); }
public void replaceModel(String modelName, boolean updateVisual, boolean updatePhysics) { if (modelName.equals(modelInstance.model.meshParts.get(0).id)) return; Model model = null; JsonValue mOrigin = null; JsonValue mDimNoScale = null; for (Scene sce : Bdx.scenes) { if (sce.models.containsKey(modelName)) { model = sce.models.get(modelName); mOrigin = sce.json.get("origins").get(modelName); mDimNoScale = sce.json.get("dimensions").get(modelName); break; } } if (model == null) { throw new RuntimeException("No model found with name: '" + modelName + "'"); } origin = mOrigin == null ? new Vector3f() : new Vector3f(mOrigin.asFloatArray()); dimensionsNoScale = mDimNoScale == null ? new Vector3f(1, 1, 1) : new Vector3f(mDimNoScale.asFloatArray()); Matrix4 trans = modelInstance.transform; if (updateVisual) { ModelInstance mi = new ModelInstance(model); mi.transform.set(trans); modelInstance = mi; } if (updatePhysics) { GameObject compParent = parent != null && parent.body.getCollisionShape().isCompound() ? parent : null; boolean isCompChild = compParent != null && !(currBodyType.equals("NO_COLLISION") || currBodyType.equals("SENSOR")); if (isCompChild) { parent(null); } Matrix4f transform = transform(); Vector3f scale = scale(); String boundsType = json.get("physics").get("bounds_type").asString(); float margin = json.get("physics").get("margin").asFloat(); boolean compound = json.get("physics").get("compound").asBoolean(); body.setCollisionShape(Bullet.makeShape(model.meshes.first(), boundsType, margin, compound)); if (boundsType.equals("CONVEX_HULL")) { Transform startTransform = new Transform(); body.getMotionState().getWorldTransform(startTransform); Matrix4f originMatrix = new Matrix4f(); originMatrix.set(origin); Transform centerOfMassTransform = new Transform(); centerOfMassTransform.set(originMatrix); centerOfMassTransform.mul(startTransform); body.setCenterOfMassTransform(centerOfMassTransform); } transform(transform); scale(scale); if (body.isInWorld()) { scene.world.updateSingleAabb(body); } else { // update Aabb hack for when not in world scene.world.addRigidBody(body); scene.world.updateSingleAabb(body); scene.world.removeRigidBody(body); } if (isCompChild) { parent(compParent); } } }