public static void setToInterpolateState( EntityRef entity, CharacterStateEvent a, CharacterStateEvent b, long time) { float t = (float) (time - a.getTime()) / (b.getTime() - a.getTime()); Vector3f newPos = new Vector3f(); newPos.interpolate(a.getPosition(), b.getPosition(), t); Quat4f newRot = new Quat4f(); newRot.interpolate(a.getRotation(), b.getRotation(), t); LocationComponent location = entity.getComponent(LocationComponent.class); location.setWorldPosition(newPos); location.setWorldRotation(newRot); entity.saveComponent(location); CharacterMovementComponent movementComponent = entity.getComponent(CharacterMovementComponent.class); movementComponent.mode = a.getMode(); movementComponent.setVelocity(a.getVelocity()); movementComponent.grounded = a.isGrounded(); if (b.getFootstepDelta() < a.getFootstepDelta()) { movementComponent.footstepDelta = t * (1 + b.getFootstepDelta() - a.getFootstepDelta()) + a.getFootstepDelta(); if (movementComponent.footstepDelta > 1) { movementComponent.footstepDelta -= 1; } } else { movementComponent.footstepDelta = t * (b.getFootstepDelta() - a.getFootstepDelta()) + a.getFootstepDelta(); } entity.saveComponent(movementComponent); CharacterComponent characterComponent = entity.getComponent(CharacterComponent.class); characterComponent.pitch = b.pitch; characterComponent.yaw = b.yaw; entity.saveComponent(characterComponent); setPhysicsLocation(entity, newPos); }
protected void moveOrientation() { if (Camera.host != owner || !PerfIO.holdMouse) return; AxisAngle4f tmp = new AxisAngle4f(Camera.right, -PerfIO.dy * 0.001f); Quat4f rot = new Quat4f(); rot.set(tmp); owner.getOrientWritable().mul(rot, owner.getOrientation()); owner.getOrientWritable().normalize(); tmp.set(0, Camera.up.y > 0 ? 1 : -1, 0, PerfIO.dx * 0.001f); rot.set(tmp); owner.getOrientWritable().mul(rot, owner.getOrientWritable()); owner.flushChanges(); }
// Note we set teh locations values up, but process does the work, in case teh world changes // without us moving (a door opens) public void locationUpdated(Quat4f rot, Vector3f trans) { // only update if new things have happened if (!prevRot.epsilonEquals(rot, 0.0001f) || !prevTrans.epsilonEquals(trans, 0.005f)) { // set head point currentAvartarHeadPoint.set(trans); currentAvartarHeadPoint.y += avatarCollisionInfo.getCameraAbovePelvisHeight(); tempRotTransform.set(rot); currentCameraMaxVector.set(0, 0, 1); // use a normal tempRotTransform.transform(currentCameraMaxVector); // set max camera vector currentCameraMaxVector.scale(TRAIL_MAX_DIST); // recall for next iter prevRot.set(rot); prevTrans.set(trans); } }
/** * Figures out the rotation needed to get from one vector to another, then applies that rotation * to a third vector and normalizes the result. * * @param from The vector from which we are finding the transform. * @param to The vector to which we are finding the transform. * @param vec The vector to be transformed. * @return A new vector that represents the rotated and normalized input {@code vec}. */ protected Vector3f transformAndNormalize(Vector3f from, Vector3f to, Vector3f vec) { // find the transform between the first two vectors Quat4f rot = MathUtil.getRotation(from, to); Transform trans = new Transform(); trans.setIdentity(); if (!rot.equals(new Quat4f())) { trans.setRotation(rot); } // apply the transform to the third vector and normalize Vector3f temp = new Vector3f(vec); trans.transform(temp); temp.normalize(); return temp; }