use of org.terasology.engine.physics.HitResult in project Terasology by MovingBlocks.
the class LocalPlayer method activateTargetOrOwnedEntity.
/**
* @param usedOwnedEntity if it does not exist it is not an item usage.
* @return true if an activation request got sent. Returns always true if usedItem exists.
*/
private boolean activateTargetOrOwnedEntity(EntityRef usedOwnedEntity) {
EntityRef character = getCharacterEntity();
CharacterComponent characterComponent = character.getComponent(CharacterComponent.class);
Vector3f direction = getViewDirection(new Vector3f());
Vector3f originPos = getViewPosition(new Vector3f());
if (recordAndReplayCurrentStatus.getStatus() == RecordAndReplayStatus.RECORDING) {
this.directionAndOriginPosRecorderList.getTargetOrOwnedEntityDirectionAndOriginPosRecorder().add(direction, originPos);
} else if (recordAndReplayCurrentStatus.getStatus() == RecordAndReplayStatus.REPLAYING) {
Vector3f[] data = this.directionAndOriginPosRecorderList.getTargetOrOwnedEntityDirectionAndOriginPosRecorder().poll();
direction = data[0];
originPos = data[1];
}
boolean ownedEntityUsage = usedOwnedEntity.exists();
int activationId = nextActivationId++;
Physics physics = CoreRegistry.get(Physics.class);
// FIXME This is the same code as in CharacterSystem#isPredictionOfEventCorrect to derive the actual interaction range from the
// player's character component and the used item's range component...
float interactionRange;
if (ownedEntityUsage && usedOwnedEntity.hasComponent(RangeComponent.class)) {
interactionRange = Math.max(usedOwnedEntity.getComponent(RangeComponent.class).range, characterComponent.interactionRange);
} else {
interactionRange = characterComponent.interactionRange;
}
HitResult result = physics.rayTrace(originPos, direction, interactionRange, Sets.newHashSet(character), CharacterSystem.DEFAULTPHYSICSFILTER);
boolean eventWithTarget = result.isHit();
if (ownedEntityUsage || eventWithTarget) {
EntityRef activatedObject = usedOwnedEntity.exists() ? usedOwnedEntity : result.getEntity();
activatedObject.send(new ActivationPredicted(character, result.getEntity(), originPos, direction, result.getHitPoint(), result.getHitNormal(), activationId));
character.send(new ActivationRequest(character, ownedEntityUsage, usedOwnedEntity, eventWithTarget, result.getEntity(), originPos, direction, result.getHitPoint(), result.getHitNormal(), activationId));
return true;
}
return false;
}
use of org.terasology.engine.physics.HitResult in project Terasology by MovingBlocks.
the class CameraTargetSystem method update.
public void update(float delta) {
// Repair lost target
// TODO: Improvements to temporary chunk handling will remove the need for this
boolean lostTarget = false;
updateTarget();
if (!target.exists()) {
targetBlockPos = null;
lostTarget = true;
}
HitResult hitInfo = physics.rayTrace(localPlayer.getViewPosition(new Vector3f()), localPlayer.getViewDirection(new Vector3f()), targetDistance, filter);
updateFocalDistance(hitInfo, delta);
Vector3i newBlockPos = null;
EntityRef newTarget = EntityRef.NULL;
if (hitInfo.isHit()) {
newTarget = hitInfo.getEntity();
hitPosition = hitInfo.getHitPoint();
hitNormal = hitInfo.getHitNormal();
if (hitInfo.isWorldHit()) {
newBlockPos = new Vector3i(hitInfo.getBlockPosition());
}
}
if (!Objects.equal(target, newTarget) || lostTarget) {
EntityRef oldTarget = target;
oldTarget.send(new CameraOutEvent());
newTarget.send(new CameraOverEvent());
localPlayer.getCharacterEntity().send(new CameraTargetChangedEvent(oldTarget, newTarget));
// Set isBlock to false if the hit-entity does not have a BlockComponent
isBlock = !(isTargetAvailable() && !newTarget.hasComponent(BlockComponent.class));
}
target = newTarget;
targetBlockPos = newBlockPos;
}
use of org.terasology.engine.physics.HitResult in project Terasology by MovingBlocks.
the class TargetSystem method updateTarget.
public boolean updateTarget(Vector3f pos, Vector3f dir, float maxDist) {
if (targetBlockPos != null && !target.exists()) {
target = blockRegistry.getEntityAt(targetBlockPos);
}
HitResult hitInfo = physics.rayTrace(pos, dir, maxDist, filter);
EntityRef newTarget = hitInfo.getEntity();
if (hitInfo.isWorldHit()) {
if (targetBlockPos != null) {
if (targetBlockPos.equals(hitInfo.getBlockPosition())) {
return false;
}
}
targetBlockPos = hitInfo.getBlockPosition();
} else {
if (target.equals(newTarget)) {
return false;
}
targetBlockPos = null;
}
prevTarget = target;
target = newTarget;
LocationComponent location = target.getComponent(LocationComponent.class);
if (location != null && targetBlockPos != null) {
location.setLocalPosition(new Vector3f(targetBlockPos));
}
return true;
}
use of org.terasology.engine.physics.HitResult in project Terasology by MovingBlocks.
the class ParticleUpdaterImpl method checkCollision.
private void checkCollision(final ParticlePool pool, final int offset) {
final Vector3f vel = new Vector3f();
final Vector3f halfVelDir = new Vector3f();
final Vector3f curr = new Vector3f();
for (int i = offset; i < pool.livingParticles(); i += PHYSICS_SKIP_NR) {
int i3 = i * 3;
curr.set(pool.position[i3 + 0], pool.position[i3 + 1], pool.position[i3 + 2]);
vel.set(pool.velocity[i3 + 0], pool.velocity[i3 + 1], pool.velocity[i3 + 2]);
halfVelDir.set(0).add(vel).normalize().mul(0.5f);
curr.sub(halfVelDir);
float dist = (vel.length() + 0.5f) * movingAvgDelta * PHYSICS_SKIP_NR * 1.5f;
vel.normalize();
HitResult hitResult = physics.rayTrace(curr, vel, dist, StandardCollisionGroup.WORLD);
if (hitResult.isHit()) {
pool.energy[i] = 0;
}
}
}
Aggregations