public void mouseDragged(MouseEvent e) { oldMoveVector = new Vector3f(fixOldX, oldY, oldZ); oldWorldZMoveVector = new Vector3f(oldX, oldY, oldZ); // get x,y newX = e.getX() / (adjustToScreenSize / 2) - 1; // used to be (float)jframe.getWidth() newY = 1 - e.getY() / (adjustToScreenSize / 2); // used to be (float)jframe.getWidth() newZ = calculateZ(newX, newY); newMoveVector = new Vector3f(fixNewX, newY, newZ); newWorldZMoveVector = new Vector3f(newX, newY, newZ); // now calculate the Camera movement Vector3f moveAxis = new Vector3f(); // moving around this axis moveAxis.cross( oldMoveVector, newMoveVector); // TODO identify, if the axis points down or upwards => adjust the angle float moveAngle = oldMoveVector.angle(newMoveVector); // moving by this angle // special Vector including the angle AxisAngle4f moveAxisAngle = new AxisAngle4f(); moveAxisAngle.set(moveAxis, moveAngle); // calculate turning axis mouseTurn = new Matrix4f(); mouseTurn.setIdentity(); mouseTurn.set(moveAxisAngle); // the new Multiplication-Matrix // now calculate the World movement Vector3f worldMoveAxis = new Vector3f(); // moving around this axis worldMoveAxis.x = 0; worldMoveAxis.y = 0; worldMoveAxis.z = 1; float worldMoveAngle = oldWorldZMoveVector.angle(newWorldZMoveVector); // moving by this angle // adjust world Angle for turning back Vector3f worldMoveAxisTemp = new Vector3f(); worldMoveAxisTemp.cross(oldWorldZMoveVector, newWorldZMoveVector); if (worldMoveAxisTemp.z < 0) { worldMoveAngle = -worldMoveAngle; } // special Vector including the angle AxisAngle4f worldMoveAxisAngle = new AxisAngle4f(); worldMoveAxisAngle.set(worldMoveAxis, worldMoveAngle); // calculate turning axis mouseWorldTurn = new Matrix4f(); mouseWorldTurn.setIdentity(); mouseWorldTurn.set(worldMoveAxisAngle); // the new Multiplication-Matrix // override the initial starting values fixOldX = fixNewX; oldX = newX; oldY = newY; oldZ = newZ; }
void setDirectionVector(ServerElement element, Float x, Float y, Float z) { RigidBody rb = elementToRigidBody.get(element); rb.getOrientation(oriantation); rot.set(oriantation); directionVector.set(x, y, z); rot.transform(directionVector); }
/** Calculate transforms needed to handle VRML semantics formula: T x C x R x SR x S x -SR x -C */ private void updateTransform() { // System.out.println(this); tempVec.x = -vfCenter[0]; tempVec.y = -vfCenter[1]; tempVec.z = -vfCenter[2]; matrix.setIdentity(); matrix.setTranslation(tempVec); float scaleVal = 1.0f; if (floatEq(vfScale[0], vfScale[1]) && floatEq(vfScale[0], vfScale[2])) { scaleVal = vfScale[0]; tempMtx1.set(scaleVal); // System.out.println("S" + tempMtx1); } else { // non-uniform scale // System.out.println("Non Uniform Scale"); tempAxis.x = vfScaleOrientation[0]; tempAxis.y = vfScaleOrientation[1]; tempAxis.z = vfScaleOrientation[2]; tempAxis.angle = -vfScaleOrientation[3]; double tempAxisNormalizer = 1 / Math.sqrt( tempAxis.x * tempAxis.x + tempAxis.y * tempAxis.y + tempAxis.z * tempAxis.z); tempAxis.x *= tempAxisNormalizer; tempAxis.y *= tempAxisNormalizer; tempAxis.z *= tempAxisNormalizer; tempMtx1.set(tempAxis); tempMtx2.mul(tempMtx1, matrix); // Set the scale by individually setting each element tempMtx1.setIdentity(); tempMtx1.m00 = vfScale[0]; tempMtx1.m11 = vfScale[1]; tempMtx1.m22 = vfScale[2]; matrix.mul(tempMtx1, tempMtx2); tempAxis.x = vfScaleOrientation[0]; tempAxis.y = vfScaleOrientation[1]; tempAxis.z = vfScaleOrientation[2]; tempAxis.angle = vfScaleOrientation[3]; tempMtx1.set(tempAxis); } tempMtx2.mul(tempMtx1, matrix); // System.out.println("Sx-C" + tempMtx2); float magSq = vfRotation[0] * vfRotation[0] + vfRotation[1] * vfRotation[1] + vfRotation[2] * vfRotation[2]; if (magSq < ZEROEPS) { tempAxis.x = 0; tempAxis.y = 0; tempAxis.z = 1; tempAxis.angle = 0; } else { if ((magSq > 1.01) || (magSq < 0.99)) { float mag = (float) (1 / Math.sqrt(magSq)); tempAxis.x = vfRotation[0] * mag; tempAxis.y = vfRotation[1] * mag; tempAxis.z = vfRotation[2] * mag; } else { tempAxis.x = vfRotation[0]; tempAxis.y = vfRotation[1]; tempAxis.z = vfRotation[2]; } tempAxis.angle = vfRotation[3]; } tempMtx1.set(tempAxis); // System.out.println("R" + tempMtx1); matrix.mul(tempMtx1, tempMtx2); // System.out.println("RxSx-C" + matrix); tempVec.x = vfCenter[0]; tempVec.y = vfCenter[1]; tempVec.z = vfCenter[2]; tempMtx1.setIdentity(); tempMtx1.setTranslation(tempVec); // System.out.println("C" + tempMtx1); tempMtx2.mul(tempMtx1, matrix); // System.out.println("CxRxSx-C" + tempMtx2); tempVec.x = vfTranslation[0]; tempVec.y = vfTranslation[1]; tempVec.z = vfTranslation[2]; tempMtx1.setIdentity(); tempMtx1.setTranslation(tempVec); matrix.mul(tempMtx1, tempMtx2); transform.set(matrix); implTG.setTransform(transform); }
public void updatePrevViewProjectionMatrix() { prevViewProjectionMatrix.set(viewProjectionMatrix); }
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); } } }
/** @param transform La {@code Matrix4f} asignada. */ public void setTransform(Matrix4f transform) { transformMatrix.set(transform); }