- glm::mat4 transform = Transform(chunk_offset);
- glm::vec4 from = transform * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
- from /= from.w;
- glm::vec4 to = transform * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f);
- to /= to.w;
- return Ray{ glm::vec3(from), glm::normalize(glm::vec3(to - from)) };
+ glm::mat4 transform = ViewTransform(chunk_offset);
+ return Ray{ glm::vec3(transform[3]), -glm::vec3(transform[2]) };
+}
+
+void Entity::UpdateModel() noexcept {
+ state.AdjustHeading();
+ state.orient = glm::quat(glm::vec3(0.0f, state.yaw, 0.0f));
+ if (model) {
+ model.EyesState().orientation = glm::quat(glm::vec3(state.pitch, 0.0f, 0.0f));
+ }
+}
+
+void Entity::Update(float dt) {
+ UpdateTransforms();
+ UpdateHeading();
+ if (HasController()) {
+ GetController().Update(*this, dt);
+ }
+}
+
+void Entity::UpdateTransforms() noexcept {
+ // model transform is the one given by current state
+ model_transform = state.Transform(state.chunk_pos);
+ // view transform is either the model's eyes transform or,
+ // should the entity have no model, the pitch (yaw already is
+ // in model transform)
+ if (model) {
+ view_transform = model.EyesTransform();
+ } else {
+ view_transform = glm::eulerAngleX(state.pitch);
+ }
+}
+
+void Entity::UpdateHeading() noexcept {
+ speed = length(Velocity());
+ if (speed > std::numeric_limits<float>::epsilon()) {
+ heading = Velocity() / speed;
+ } else {
+ speed = 0.0f;
+ // use -Z (forward axis) of local view transform
+ heading = -glm::vec3(view_transform[2]);
+ }
+}
+
+
+EntityController::~EntityController() {
+
+}
+
+bool EntityController::MaxOutForce(
+ glm::vec3 &out,
+ const glm::vec3 &add,
+ float max
+) noexcept {
+ if (iszero(add) || any(isnan(add))) {
+ return false;
+ }
+ float current = iszero(out) ? 0.0f : length(out);
+ float remain = max - current;
+ if (remain <= 0.0f) {
+ return true;
+ }
+ float additional = length(add);
+ if (additional > remain) {
+ out += normalize(add) * remain;
+ return true;
+ } else {
+ out += add;
+ return false;
+ }