private void manage(Entity e, float elapsedTime) { Model model = e.get(Model.class); if (!model.isCreated()) return; PlanarStance stance = e.get(PlanarStance.class); Spatial s = SpatialPool.models.get(e.getId()); // translation s.setLocalTranslation(TranslateUtil.toVector3f(stance.getCoord().get3D(stance.getElevation()))); // rotation Quaternion r = new Quaternion(); Point3D pu = stance.getUpVector(); Point3D pv = stance.getPlanarVector(); Vector3f u = TranslateUtil.toVector3f(pu).normalize(); Vector3f v = TranslateUtil.toVector3f(pv).normalize(); r.lookAt(v, u); // we correct the pitch of the unit because the direction is always flatten // this is only to follow the terrain relief double angle = Math.acos(pu.getDotProduct(pv) / (pu.getNorm() * pv.getNorm())); r = r.mult( new Quaternion() .fromAngles( (float) (-angle + AngleUtil.RIGHT + model.getPitchFix()), (float) (model.getRollFix()), (float) (model.getYawFix()))); s.setLocalRotation(r); }
@Override public void update(float tpf) { if (forward ^ backward) { // float yCoord = charControl.getRigidBody().getLinearVelocity().getY(); Vector3f charLocation = charControl.getPhysicsLocation(); Vector3f walkDir = charLocation .subtract(app.getCamera().getLocation().clone().setY(charLocation.getY())) .normalizeLocal(); if (backward) { walkDir.negateLocal(); } charControl.setWalkDirection(walkDir); charControl.setMove(true); } else { charControl.setMove(false); } if (jump) { charControl.setJump(); jump = false; } Vector3f camdir = app.getCamera().getDirection().clone(); Quaternion newRot = new Quaternion(); newRot.lookAt(camdir.setY(0), Vector3f.UNIT_Y); charControl.setRotationInUpdate(newRot); }
protected void computeRandomSpawnPoint(Flight flight) { Vector3f position = new Vector3f( randomSpawnGenerator.nextInt(7000) - 3500, randomSpawnGenerator.nextInt(7000) - 3500, randomSpawnGenerator.nextInt(7000) - 3500); Quaternion rotation = new Quaternion(); rotation.lookAt(position.negate(), Vector3f.UNIT_Z); flight.setSpawn(new Spawn(position, rotation)); }
void lookAt(Vector3f pos) { Vector3f location = spatial.getLocalTranslation(); Vector3f target = pos.clone(); Vector3f distance = target.subtract(location); if (distance.length() < 0.3f) { return; } Vector3f newVel = distance.normalize(); Quaternion q = new Quaternion(); q.lookAt(newVel, Vector3f.UNIT_Y); // q.addLocal(playerInfo.initalRot); spatial.setLocalRotation(q); }
@Override protected void onEntityEachTick(Entity e) { Spatial s = Pool.models.get(e.getId()); if (s == null) return; PlanarStance stance = e.get(PlanarStance.class); // translation s.setLocalTranslation(TranslateUtil.toVector3f(stance.coord.get3D(stance.elevation))); // rotation Quaternion r = new Quaternion(); Point3D pu = stance.upVector; Point3D pv = Point3D.UNIT_X.getRotationAroundZ(stance.orientation.getValue()); Vector3f u = TranslateUtil.toVector3f(pu).normalize(); Vector3f v = TranslateUtil.toVector3f(pv).normalize(); r.lookAt(v, u); double angle = Math.acos(pu.getDotProduct(pv) / (pu.getNorm() * pv.getNorm())); r = r.mult(new Quaternion().fromAngles((float) (-angle), 0, 0)); s.setLocalRotation(r); }
/** * Method for rotating agent in direction of velocity of agent. * * @param tpf time per frame */ protected void rotateAgent(float tpf) { Quaternion q = new Quaternion(); q.lookAt(velocity, new Vector3f(0, 1, 0)); agent.getLocalRotation().slerp(q, agent.getRotationSpeed() * tpf); }