Skip to content

Commit

Permalink
feat(JOML): migrate character package events and systems (MovingBlo…
Browse files Browse the repository at this point in the history
  • Loading branch information
jdrueckert authored Dec 12, 2020
1 parent 00856a1 commit e4d9a62
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package org.terasology.logic.characters;

import org.terasology.entitySystem.event.Event;
import org.terasology.math.geom.Vector3f;
import org.joml.Vector3f;

public class CharacterImpulseEvent implements Event {
Vector3f direction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ public void setToState(EntityRef entity, CharacterStateEvent state) {
LocationComponent location = entity.getComponent(LocationComponent.class);
CharacterMovementComponent movementComp = entity.getComponent(CharacterMovementComponent.class);

if (location == null || Float.isNaN(location.getWorldPosition().x) || movementComp == null) {
if (location == null || !location.getWorldPosition(new Vector3f()).isFinite() || movementComp == null) {
return;
}
location.setWorldPosition(state.getPosition());
location.setWorldRotation(state.getRotation());
entity.saveComponent(location);

movementComp.mode = state.getMode();
movementComp.setVelocity(JomlUtil.from(state.getVelocity()));
movementComp.setVelocity(state.getVelocity());
movementComp.grounded = state.isGrounded();
movementComp.footstepDelta = state.getFootstepDelta();
entity.saveComponent(movementComp);

setPhysicsLocation(entity, JomlUtil.from(state.getPosition()));
setPhysicsLocation(entity, state.getPosition());

// set the pitch to the character's gaze entity
Quaternionf rotation = new Quaternionf().rotationX(TeraMath.DEG_TO_RAD * state.getPitch());
Expand All @@ -66,9 +66,9 @@ public void setToState(EntityRef entity, CharacterStateEvent state) {

public void setToInterpolateState(EntityRef entity, CharacterStateEvent a, CharacterStateEvent b, long time) {
float t = (float) (time - a.getTime()) / (b.getTime() - a.getTime());
Vector3f newPos = JomlUtil.from(a.getPosition()).lerp(JomlUtil.from(b.getPosition()),t);
Quaternionf newRot = JomlUtil.from(a.getRotation()).nlerp(JomlUtil.from(b.getRotation()),t);

Vector3f newPos = a.getPosition().lerp(b.getPosition(),t);
Quaternionf newRot = a.getRotation().nlerp(b.getRotation(),t);
entity.updateComponent(LocationComponent.class, location -> {
location.setWorldPosition(JomlUtil.from(newPos));
location.setWorldRotation(JomlUtil.from(newRot));
Expand All @@ -77,7 +77,7 @@ public void setToInterpolateState(EntityRef entity, CharacterStateEvent a, Chara

entity.updateComponent(CharacterMovementComponent.class, movementComponent -> {
movementComponent.mode = a.getMode();
movementComponent.setVelocity(JomlUtil.from(a.getVelocity()));
movementComponent.setVelocity(a.getVelocity());
movementComponent.grounded = a.isGrounded();
if (b.getFootstepDelta() < a.getFootstepDelta()) {
movementComponent.footstepDelta = t * (1 + b.getFootstepDelta() - a.getFootstepDelta()) + a.getFootstepDelta();
Expand All @@ -99,9 +99,9 @@ public void setToInterpolateState(EntityRef entity, CharacterStateEvent a, Chara

public void setToExtrapolateState(EntityRef entity, CharacterStateEvent state, long time) {
float t = (time - state.getTime()) * 0.0001f;
Vector3f newPos = new Vector3f(JomlUtil.from(state.getVelocity()));
Vector3f newPos = new Vector3f(state.getVelocity());
newPos.mul(t);
newPos.add(JomlUtil.from(state.getPosition()));
newPos.add(state.getPosition());
extrapolateLocationComponent(entity, state, newPos);

extrapolateCharacterMovementComponent(entity, state);
Expand All @@ -119,7 +119,7 @@ private void extrapolateLocationComponent(EntityRef entity, CharacterStateEvent
private void extrapolateCharacterMovementComponent(EntityRef entity, CharacterStateEvent state) {
CharacterMovementComponent movementComponent = entity.getComponent(CharacterMovementComponent.class);
movementComponent.mode = state.getMode();
movementComponent.setVelocity(JomlUtil.from(state.getVelocity()));
movementComponent.setVelocity(state.getVelocity());
movementComponent.grounded = state.isGrounded();
entity.saveComponent(movementComponent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void onJump(JumpEvent event, EntityRef entity, CharacterSoundComponent ch

@ReceiveEvent
public void onLanded(VerticalCollisionEvent event, EntityRef entity, CharacterSoundComponent characterSounds) {
Vector3f velocity = JomlUtil.from(event.getVelocity());
Vector3f velocity = event.getVelocity();
float soundVolumeModifier = (velocity.y * -1 - LANDING_VELOCITY_THRESHOLD) * LANDING_VOLUME_MODIFIER;

if (soundVolumeModifier <= 0f) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package org.terasology.logic.characters;

import org.terasology.math.geom.Quat4f;
import org.terasology.math.geom.Vector3f;
import org.terasology.math.geom.Vector3i;
import org.joml.Quaternionf;
import org.joml.Vector3f;
import org.joml.Vector3i;
import org.terasology.network.BroadcastEvent;
import org.terasology.network.NetworkEvent;

Expand All @@ -27,7 +27,7 @@ public class CharacterStateEvent extends NetworkEvent {
private long time;
private int sequenceNumber;
private Vector3f position = new Vector3f();
private Quat4f rotation = new Quat4f(0, 0, 0, 1);
private Quaternionf rotation = new Quaternionf(0, 0, 0, 1);
private MovementMode mode = MovementMode.WALKING;
private boolean grounded;
private Vector3f velocity = new Vector3f();
Expand Down Expand Up @@ -57,7 +57,7 @@ public CharacterStateEvent(
long time,
int sequenceNumber,
Vector3f position,
Quat4f rotation,
Quaternionf rotation,
Vector3f velocity,
float yaw,
float pitch,
Expand All @@ -82,7 +82,7 @@ public Vector3f getPosition() {
return position;
}

public Quat4f getRotation() {
public Quaternionf getRotation() {
return rotation;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

import com.google.common.collect.Maps;
import com.google.common.collect.Queues;

import org.joml.Quaternionf;
import org.joml.Vector3f;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.engine.Time;
Expand All @@ -27,15 +28,14 @@
import org.terasology.entitySystem.entity.lifecycleEvents.OnActivatedComponent;
import org.terasology.entitySystem.event.ReceiveEvent;
import org.terasology.entitySystem.systems.BaseComponentSystem;
import org.terasology.registry.In;
import org.terasology.entitySystem.systems.RegisterMode;
import org.terasology.entitySystem.systems.RegisterSystem;
import org.terasology.entitySystem.systems.UpdateSubscriberSystem;
import org.terasology.logic.location.LocationComponent;
import org.terasology.logic.players.LocalPlayer;
import org.terasology.math.geom.Vector3f;
import org.terasology.network.ClientComponent;
import org.terasology.physics.engine.PhysicsEngine;
import org.terasology.registry.In;
import org.terasology.utilities.collection.CircularBuffer;
import org.terasology.world.WorldProvider;

Expand Down Expand Up @@ -140,7 +140,7 @@ public void onPlayerInput(CharacterMoveInputEvent input, EntityRef entity) {

private CharacterStateEvent createInitialState(EntityRef entity) {
LocationComponent location = entity.getComponent(LocationComponent.class);
return new CharacterStateEvent(time.getGameTimeInMs(), 0, location.getWorldPosition(), location.getWorldRotation(), new Vector3f(), 0, 0, MovementMode.WALKING, false);
return new CharacterStateEvent(time.getGameTimeInMs(), 0, location.getWorldPosition(new org.joml.Vector3f()), location.getWorldRotation(new Quaternionf()), new Vector3f(), 0, 0, MovementMode.WALKING, false);
}

private CharacterStateEvent stepState(CharacterMoveInputEvent input, CharacterStateEvent lastState, EntityRef entity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ public CharacterStateEvent step(CharacterStateEvent initial, CharacterMoveInputE
// The CharacterMovementComponent also has a 'radius' which may be used here.
// Question: Is this connected with _shapes_ or bounding boxes in some way?
checkBlockEntry(entity,
new Vector3i(JomlUtil.from(initial.getPosition()), org.joml.RoundingMode.HALF_UP),
new Vector3i(JomlUtil.from(result.getPosition()), org.joml.RoundingMode.HALF_UP),
new Vector3i(initial.getPosition(), org.joml.RoundingMode.HALF_UP),
new Vector3i(result.getPosition(), org.joml.RoundingMode.HALF_UP),
characterMovementComponent.height);
}
if (result.getMode() != MovementMode.GHOSTING && result.getMode() != MovementMode.NONE) {
Expand Down Expand Up @@ -175,7 +175,7 @@ private void checkMode(final CharacterMovementComponent movementComp, final Char
if (!state.getMode().respondToEnvironment) {
return;
}
Vector3f worldPos = JomlUtil.from(state.getPosition());
Vector3f worldPos = state.getPosition();
Vector3f top = new Vector3f(worldPos);
Vector3f bottom = new Vector3f(worldPos);
top.y += 0.5f * movementComp.height;
Expand All @@ -193,7 +193,7 @@ private void checkMode(final CharacterMovementComponent movementComp, final Char
finalDir = findClimbable(movementComp, worldPos, newSwimming, newDiving);
if (finalDir != null) {
newClimbing = true;
state.setClimbDirection(JomlUtil.from(finalDir));
state.setClimbDirection(finalDir);
}
}

Expand Down Expand Up @@ -338,10 +338,10 @@ private Vector3f extractResidualMovement(Vector3f hitNormal, Vector3f direction,
private void followToParent(final CharacterStateEvent state, EntityRef entity) {
LocationComponent locationComponent = entity.getComponent(LocationComponent.class);
if (!locationComponent.getParent().equals(EntityRef.NULL)) {
Vector3f velocity = new Vector3f(JomlUtil.from(locationComponent.getWorldPosition()));
velocity.sub(JomlUtil.from(state.getPosition()));
state.getVelocity().set(JomlUtil.from(velocity));
state.getPosition().set(locationComponent.getWorldPosition());
Vector3f velocity = new Vector3f(locationComponent.getWorldPosition(new Vector3f()));
velocity.sub(state.getPosition());
state.getVelocity().set(velocity);
state.getPosition().set(locationComponent.getWorldPosition(new Vector3f()));
}
}

Expand Down Expand Up @@ -576,9 +576,9 @@ private void updateRotation(CharacterMovementComponent movementComp, CharacterSt
CharacterMoveInputEvent input) {
if (movementComp.faceMovementDirection && result.getVelocity().lengthSquared() > 0.01f) {
float yaw = (float) Math.atan2(result.getVelocity().x, result.getVelocity().z);
result.getRotation().set(JomlUtil.from(new Vector3f(0, 1, 0)), yaw);
result.getRotation().set(0, 1, 0, yaw);
} else {
result.getRotation().set(new Quat4f(TeraMath.DEG_TO_RAD * input.getYaw(), 0, 0));
result.getRotation().set(new Quaternionf().rotationYXZ(org.joml.Math.toRadians(input.getYaw()), 0, 0));
}
}

Expand Down Expand Up @@ -622,9 +622,9 @@ private void walk(final CharacterMovementComponent movementComp, final Character

// Modify velocity towards desired, up to the maximum rate determined by friction
Vector3f velocityDiff = new Vector3f(desiredVelocity);
velocityDiff.sub(JomlUtil.from(state.getVelocity()));
velocityDiff.sub(state.getVelocity());
velocityDiff.mul(Math.min(movementComp.mode.scaleInertia * input.getDelta(), 1.0f));
Vector3f endVelocity = new Vector3f(JomlUtil.from(state.getVelocity()));
Vector3f endVelocity = new Vector3f(state.getVelocity());
endVelocity.x += velocityDiff.x;
endVelocity.z += velocityDiff.z;
if (movementComp.mode.scaleGravity == 0) {
Expand All @@ -638,14 +638,14 @@ private void walk(final CharacterMovementComponent movementComp, final Character
Vector3f moveDelta = new Vector3f(endVelocity);
moveDelta.mul(input.getDelta());
CharacterCollider collider = movementComp.mode.useCollision ? physics.getCharacterCollider(entity) : null;
MoveResult moveResult = move(JomlUtil.from(state.getPosition()), moveDelta,
MoveResult moveResult = move(state.getPosition(), moveDelta,
(state.getMode() != MovementMode.CLIMBING && state.isGrounded() && movementComp.mode.canBeGrounded) ? movementComp.stepHeight : 0,
movementComp.slopeFactor, collider);
Vector3f distanceMoved = new Vector3f(moveResult.getFinalPosition());
distanceMoved.sub(JomlUtil.from(state.getPosition()));
state.getPosition().set(JomlUtil.from(moveResult.getFinalPosition()));
distanceMoved.sub(state.getPosition());
state.getPosition().set(moveResult.getFinalPosition());
if (input.isFirstRun() && distanceMoved.length() > 0) {
entity.send(new MovedEvent(new ImmutableVector3f(JomlUtil.from(distanceMoved)), new ImmutableVector3f(state.getPosition())));
entity.send(new MovedEvent(new Vector3f(distanceMoved), new Vector3f(state.getPosition())));
}

// Upon hitting solid ground, reset the number of jumps back to the maximum value.
Expand All @@ -656,9 +656,9 @@ private void walk(final CharacterMovementComponent movementComp, final Character
if (moveResult.isBottomHit()) {
if (!state.isGrounded() && movementComp.mode.canBeGrounded) {
if (input.isFirstRun()) {
Vector3f landVelocity = new Vector3f(JomlUtil.from(state.getVelocity()));
Vector3f landVelocity = new Vector3f(state.getVelocity());
landVelocity.y += (distanceMoved.y / moveDelta.y) * (endVelocity.y - state.getVelocity().y);
entity.send(new VerticalCollisionEvent(state.getPosition(), JomlUtil.from(landVelocity)));
entity.send(new VerticalCollisionEvent(state.getPosition(), landVelocity));
}
state.setGrounded(true);
movementComp.numberOfJumpsLeft = movementComp.numberOfJumpsMax;
Expand Down Expand Up @@ -688,9 +688,9 @@ private void walk(final CharacterMovementComponent movementComp, final Character
} else {
if (moveResult.isTopHit() && endVelocity.y > 0) {
if (input.isFirstRun()) {
Vector3f hitVelocity = new Vector3f(JomlUtil.from(state.getVelocity()));
Vector3f hitVelocity = new Vector3f(state.getVelocity());
hitVelocity.y += (distanceMoved.y / moveDelta.y) * (endVelocity.y - state.getVelocity().y);
entity.send(new VerticalCollisionEvent(state.getPosition(), JomlUtil.from(hitVelocity)));
entity.send(new VerticalCollisionEvent(state.getPosition(), hitVelocity));
}
endVelocity.y = -0.0f * endVelocity.y;
}
Expand Down Expand Up @@ -721,12 +721,12 @@ private void walk(final CharacterMovementComponent movementComp, final Character
}
}
if (input.isFirstRun() && moveResult.isHorizontalHit()) {
Vector3f hitVelocity = new Vector3f(JomlUtil.from(state.getVelocity()));
Vector3f hitVelocity = new Vector3f(state.getVelocity());
hitVelocity.x += (distanceMoved.x / moveDelta.x) * (endVelocity.x - state.getVelocity().x);
hitVelocity.z += (distanceMoved.z / moveDelta.z) * (endVelocity.z - state.getVelocity().z);
entity.send(new HorizontalCollisionEvent(state.getPosition(), JomlUtil.from(hitVelocity)));
entity.send(new HorizontalCollisionEvent(state.getPosition(), hitVelocity));
}
state.getVelocity().set(JomlUtil.from(endVelocity));
state.getVelocity().set(endVelocity);
if (state.isGrounded() || movementComp.mode == MovementMode.SWIMMING || movementComp.mode == MovementMode.DIVING) {
state.setFootstepDelta(
state.getFootstepDelta() + distanceMoved.length() / movementComp.distanceBetweenFootsteps);
Expand Down Expand Up @@ -759,7 +759,7 @@ private void climb(final CharacterStateEvent state, CharacterMoveInputEvent inpu
}
Vector3f tmp;

Vector3i climbDir3i = JomlUtil.from(state.getClimbDirection());
Vector3i climbDir3i = state.getClimbDirection();
Vector3f climbDir3f = new Vector3f(climbDir3i);

Quaternionf rotation = new Quaternionf().rotationYXZ(TeraMath.DEG_TO_RAD * state.getYaw(), 0, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.joml.Quaternionf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.engine.Time;
Expand All @@ -34,7 +35,7 @@
import org.terasology.logic.location.LocationComponent;
import org.terasology.logic.players.LocalPlayer;
import org.terasology.math.JomlUtil;
import org.terasology.math.geom.Vector3f;
import org.joml.Vector3f;
import org.terasology.network.NetworkSystem;
import org.terasology.physics.engine.CharacterCollider;
import org.terasology.physics.engine.PhysicsEngine;
Expand Down Expand Up @@ -147,7 +148,7 @@ public void onPlayerInput(CharacterMoveInputEvent input, EntityRef entity) {
if (recordAndReplayCurrentStatus.getStatus() == RecordAndReplayStatus.REPLAYING) {
characterStateEventPositionMap.updateCharacterStateEvent(newState);
} else if (recordAndReplayCurrentStatus.getStatus() == RecordAndReplayStatus.RECORDING) {
characterStateEventPositionMap.add(newState.getSequenceNumber(), newState.getPosition(), newState.getVelocity());
characterStateEventPositionMap.add(newState.getSequenceNumber(), JomlUtil.from(newState.getPosition()), JomlUtil.from(newState.getVelocity()));
}

characterMovementSystemUtility.setToState(entity, newState);
Expand All @@ -162,7 +163,7 @@ public void onTeleport(CharacterTeleportEvent event, EntityRef entity) {
CircularBuffer<CharacterStateEvent> stateBuffer = characterStates.get(entity);
CharacterStateEvent lastState = stateBuffer.getLast();
CharacterStateEvent newState = new CharacterStateEvent(lastState);
newState.setPosition(new Vector3f(JomlUtil.from(event.getTargetPosition())));
newState.setPosition(new Vector3f(event.getTargetPosition()));
newState.setTime(time.getGameTimeInMs());
stateBuffer.add(newState);
characterMovementSystemUtility.setToState(entity, newState);
Expand All @@ -185,7 +186,7 @@ public void onImpulse(CharacterImpulseEvent event, EntityRef entity) {

private CharacterStateEvent createInitialState(EntityRef entity) {
LocationComponent location = entity.getComponent(LocationComponent.class);
return new CharacterStateEvent(time.getGameTimeInMs(), 0, location.getWorldPosition(), location.getWorldRotation(), new Vector3f(), 0, 0, MovementMode.WALKING, false);
return new CharacterStateEvent(time.getGameTimeInMs(), 0, location.getWorldPosition(new Vector3f()), location.getWorldRotation(new Quaternionf()), new Vector3f(), 0, 0, MovementMode.WALKING, false);
}

private CharacterStateEvent stepState(CharacterMoveInputEvent input, CharacterStateEvent lastState, EntityRef entity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package org.terasology.logic.characters.events;

import org.terasology.entitySystem.event.Event;
import org.terasology.math.geom.Vector3f;
import org.joml.Vector3f;

/**
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package org.terasology.logic.characters.events;

import org.terasology.math.geom.Vector3f;
import org.joml.Vector3f;

/**
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package org.terasology.logic.characters.events;

import org.terasology.math.geom.Vector3f;
import org.joml.Vector3f;

/**
*/
Expand Down
Loading

0 comments on commit e4d9a62

Please sign in to comment.