use of org.joml.Vector3dc in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.
the class EntityDraggable method addEntityVelocityFromShipBelow.
/**
* Adds the ship below velocity to entity.
*/
private static void addEntityVelocityFromShipBelow(final Entity entity) {
final IDraggable draggable = EntityDraggable.getDraggableFromEntity(entity);
final EntityShipMountData mountData = ValkyrienUtils.getMountedShipAndPos(entity);
final EntityShipMovementData oldEntityShipMovementData = draggable.getEntityShipMovementData();
final ShipData lastShipTouchedPlayer = oldEntityShipMovementData.getLastTouchedShip();
final int oldTicksSinceTouchedShip = oldEntityShipMovementData.getTicksSinceTouchedShip();
final Vector3dc oldVelocityAdded = oldEntityShipMovementData.getAddedLinearVelocity();
final double oldYawVelocityAdded = oldEntityShipMovementData.getAddedYawVelocity();
if (lastShipTouchedPlayer == null || oldTicksSinceTouchedShip >= VSConfig.ticksToStickToShip) {
if (entity.onGround) {
// Player is on ground and not on a ship, therefore set their added velocity to 0.
draggable.setEntityShipMovementData(oldEntityShipMovementData.withAddedLinearVelocity(new Vector3d()).withAddedYawVelocity(0));
} else {
if (entity instanceof EntityPlayer) {
EntityPlayer player = (EntityPlayer) entity;
if (player.isCreative() && player.capabilities.isFlying) {
// If the player is flying, then slow down their added velocity significantly every tick
final Vector3dc newVelocityAdded = oldVelocityAdded.mul(.95, new Vector3d());
final double newYawVelocityAdded = oldYawVelocityAdded * .95 * .95;
final EntityShipMovementData newMovementData = oldEntityShipMovementData.withAddedLinearVelocity(newVelocityAdded).withAddedYawVelocity(newYawVelocityAdded);
draggable.setEntityShipMovementData(newMovementData);
} else {
// Otherwise only slow down their added velocity slightly every tick
final Vector3dc newVelocityAdded = oldVelocityAdded.mul(.99, new Vector3d());
final double newYawVelocityAdded = oldYawVelocityAdded * .95;
final EntityShipMovementData newMovementData = oldEntityShipMovementData.withAddedLinearVelocity(newVelocityAdded).withAddedYawVelocity(newYawVelocityAdded);
draggable.setEntityShipMovementData(newMovementData);
}
}
}
} else {
final float rotYaw = entity.rotationYaw;
final float rotPitch = entity.rotationPitch;
final float prevYaw = entity.prevRotationYaw;
final float prevPitch = entity.prevRotationPitch;
final Vector3dc oldPos = new Vector3d(entity.posX, entity.posY, entity.posZ);
final Matrix4d betweenTransform = ShipTransform.createTransform(lastShipTouchedPlayer.getPrevTickShipTransform(), lastShipTouchedPlayer.getShipTransform());
ValkyrienUtils.transformEntity(betweenTransform, entity, false);
final Vector3dc newPos = new Vector3d(entity.posX, entity.posY, entity.posZ);
// Move the entity back to its old position, the added velocity will be used
// afterwards
entity.setPosition(oldPos.x(), oldPos.y(), oldPos.z());
final Vector3dc addedVel = newPos.sub(oldPos, new Vector3d());
// Now compute the added yaw velocity
entity.rotationYaw = rotYaw;
entity.rotationPitch = rotPitch;
entity.prevRotationYaw = prevYaw;
entity.prevRotationPitch = prevPitch;
// Ignore the pitch, calculate the look vector using only the yaw
final Vector3d newLookYawVec;
if (entity instanceof EntityLivingBase && !(entity instanceof EntityPlayer)) {
newLookYawVec = new Vector3d(-MathHelper.sin(-entity.getRotationYawHead() * 0.017453292F - (float) Math.PI), 0, -MathHelper.cos(-entity.getRotationYawHead() * 0.017453292F - (float) Math.PI));
} else {
newLookYawVec = new Vector3d(-MathHelper.sin(-entity.rotationYaw * 0.017453292F - (float) Math.PI), 0, -MathHelper.cos(-entity.rotationYaw * 0.017453292F - (float) Math.PI));
}
// Transform the player look vector
betweenTransform.transformDirection(newLookYawVec);
// Calculate the yaw of the transformed player look vector
final Tuple<Double, Double> newPlayerLookYawOnly = VSMath.getPitchYawFromVector(newLookYawVec);
final double wrappedYaw = MathHelper.wrapDegrees(newPlayerLookYawOnly.getSecond());
final double wrappedRotYaw;
// [Changed because EntityPlayerSP is a 'client' class]
if (entity instanceof EntityLivingBase && !(entity instanceof EntityPlayer)) {
wrappedRotYaw = MathHelper.wrapDegrees(entity.getRotationYawHead());
} else {
wrappedRotYaw = MathHelper.wrapDegrees(entity.rotationYaw);
}
double yawDif = wrappedYaw - wrappedRotYaw;
if (Math.abs(yawDif) > 180D) {
if (yawDif < 0) {
yawDif += 360D;
} else {
yawDif -= 360D;
}
}
yawDif %= 360D;
final double threshold = .1D;
if (Math.abs(yawDif) < threshold) {
yawDif = 0D;
}
draggable.setEntityShipMovementData(oldEntityShipMovementData.withAddedLinearVelocity(addedVel.mul(1, new Vector3d())).withAddedYawVelocity(yawDif));
}
final EntityShipMovementData newEntityShipMovementData = draggable.getEntityShipMovementData();
// it unless we have to.
if (newEntityShipMovementData.getAddedLinearVelocity().lengthSquared() > 0) {
// Now that we've determined the added velocity, move the entity forward by that amount
final boolean originallySneaking = entity.isSneaking();
entity.setSneaking(false);
// The added velocity vector of the player, except we have made sure that it won't push the player inside of a
// solid block.
final Vector3dc addedVelocityNoNoClip = applyAddedVelocity(newEntityShipMovementData.getAddedLinearVelocity(), entity);
draggable.setEntityShipMovementData(oldEntityShipMovementData.withAddedLinearVelocity(addedVelocityNoNoClip));
entity.setSneaking(originallySneaking);
}
// Add the yaw velocity to the player as well, because its possible for addedVelocity=0 and yawVel != 0
final double addedYawVelocity = newEntityShipMovementData.getAddedYawVelocity();
if (!mountData.isMounted() && addedYawVelocity != 0) {
entity.setRotationYawHead((float) (entity.getRotationYawHead() + addedYawVelocity));
entity.rotationYaw += addedYawVelocity;
}
}
use of org.joml.Vector3dc in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.
the class TileEntityBoatChair method getTorqueInGlobal.
@Nullable
public Vector3dc getTorqueInGlobal(PhysicsCalculations physicsCalculations) {
// Don't add force if theres no pilot
if (getPilotEntity() == null) {
return null;
}
final PhysicsObject physicsObject = physicsCalculations.getParent();
final ShipTransform shipTransform = physicsObject.getShipTransformationManager().getCurrentPhysicsTransform();
final Vector3dc idealAngularVelocity = shipTransform.transformDirectionNew(new Vector3d(targetAngularVelocity), TransformType.SUBSPACE_TO_GLOBAL);
final Vector3dc currentAngularVelocity = physicsCalculations.getAngularVelocity();
final Vector3dc velocityDifference = idealAngularVelocity.sub(currentAngularVelocity, new Vector3d());
final Vector3d resultingTorque = physicsCalculations.getPhysMOITensor().transform(velocityDifference, new Vector3d());
resultingTorque.mul(physicsCalculations.getPhysicsTimeDeltaPerPhysTick());
resultingTorque.mul(ANGULAR_EMA_FILTER_CONSTANT);
// Only effect y axis
resultingTorque.x = 0;
resultingTorque.z = 0;
// Add a stabilization torque
final Vector3dc shipUp = shipTransform.transformDirectionNew(new Vector3d(0, 1, 0), TransformType.SUBSPACE_TO_GLOBAL);
final Vector3dc idealUp = new Vector3d(0, 1, 0);
final double angleBetween = shipUp.angle(idealUp);
if (angleBetween > .01) {
final Vector3dc stabilizationRotationAxisNormalized = shipUp.cross(idealUp, new Vector3d()).normalize();
final Vector3d stabilizationTorque = physicsCalculations.getPhysMOITensor().transform(stabilizationRotationAxisNormalized.mul(angleBetween, new Vector3d()));
stabilizationTorque.mul(physicsCalculations.getPhysicsTimeDeltaPerPhysTick());
stabilizationTorque.mul(STABILIZATION_TORQUE_CONSTANT);
resultingTorque.add(stabilizationTorque);
}
return resultingTorque;
}
use of org.joml.Vector3dc in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.
the class ShipTransformationManager method updateRenderTransform.
public void updateRenderTransform(double partialTick) {
if (partialTick == 0) {
renderTransform = prevTickTransform;
return;
} else if (partialTick == 1) {
renderTransform = currentTickTransform;
return;
}
ShipTransform prev = prevTickTransform;
ShipTransform cur = currentTickTransform;
Vector3dc shipCenter = parent.getCenterCoord();
Vector3d prevPos = new Vector3d(shipCenter);
Vector3d curPos = new Vector3d(shipCenter);
prev.transformPosition(prevPos, TransformType.SUBSPACE_TO_GLOBAL);
cur.transformPosition(curPos, TransformType.SUBSPACE_TO_GLOBAL);
Vector3d deltaPos = curPos.sub(prevPos, new Vector3d());
deltaPos.mul(partialTick);
Vector3d partialPos = new Vector3d(prevPos);
// Now partialPos is complete
partialPos.add(deltaPos);
Quaterniondc prevRot = prev.rotationQuaternion(TransformType.SUBSPACE_TO_GLOBAL);
Quaterniondc curRot = cur.rotationQuaternion(TransformType.SUBSPACE_TO_GLOBAL);
Quaterniondc partialRot = prevRot.slerp(curRot, partialTick, new Quaterniond()).normalize();
// Put it all together to get the render transform.
renderTransform = new ShipTransform(partialPos.x, partialPos.y, partialPos.z, partialRot, parent.getCenterCoord());
}
use of org.joml.Vector3dc in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.
the class PhysicsObject method destroyShip.
void destroyShip() {
// Then tell the game to stop tracking/loading the chunks
List<EntityPlayerMP> watchersCopy = new ArrayList<>(getWatchingPlayers());
for (ChunkPos chunkPos : getChunkClaim()) {
SPacketUnloadChunk unloadPacket = new SPacketUnloadChunk(chunkPos.x, chunkPos.z);
for (EntityPlayerMP wachingPlayer : watchersCopy) {
wachingPlayer.connection.sendPacket(unloadPacket);
}
// NOTICE: This method isnt being called to avoid the
// watchingPlayers.remove(player) call, which is a waste of CPU time
// onPlayerUntracking(wachingPlayer);
}
getWatchingPlayers().clear();
// Finally, copy all the blocks from the ship to the world
if (!getBlockPositions().isEmpty()) {
if (deconstructState.copyBlocks) {
MutableBlockPos newPos = new MutableBlockPos();
ShipTransform currentTransform = getShipTransformationManager().getCurrentTickTransform();
Vector3dc position = new Vector3d(currentTransform.getPosX(), currentTransform.getPosY(), currentTransform.getPosZ());
BlockPos centerDifference = new BlockPos(Math.round(getCenterCoord().x() - position.x()), Math.round(getCenterCoord().y() - position.y()), Math.round(getCenterCoord().z() - position.z()));
for (BlockPos oldPos : this.getBlockPositions()) {
newPos.setPos(oldPos.getX() - centerDifference.getX(), oldPos.getY() - centerDifference.getY(), oldPos.getZ() - centerDifference.getZ());
MoveBlocks.copyBlockToPos(getWorld(), oldPos, newPos, null);
}
// Then relight the chunks we just copied the blocks to
{
Set<Long> chunksRelit = new HashSet<>();
for (BlockPos changedPos : this.getBlockPositions()) {
int changedChunkX = (changedPos.getX() - centerDifference.getX()) >> 4;
int changedChunkZ = (changedPos.getZ() - centerDifference.getZ()) >> 4;
long changedChunkPos = ChunkPos.asLong(changedChunkX, changedChunkZ);
if (chunksRelit.contains(changedChunkPos)) {
continue;
}
final Chunk chunk = world.getChunk(changedChunkX, changedChunkZ);
chunk.generateSkylightMap();
chunk.checkLight();
chunk.markDirty();
chunksRelit.add(changedChunkPos);
}
}
}
// Just delete the tile entities in ship to prevent any dupe bugs.
for (BlockPos oldPos : this.getBlockPositions()) {
getWorld().removeTileEntity(oldPos);
}
}
// Delete all the old ship chunks
getClaimedChunkCache().deleteShipChunksFromWorld();
}
use of org.joml.Vector3dc in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.
the class ShipPilot method getBlockForceInShipSpace.
public Vector3dc getBlockForceInShipSpace(PhysicsObject physicsObject, double secondsToApply) {
final ShipTransform shipTransform = physicsObject.getShipTransformationManager().getCurrentPhysicsTransform();
final Vector3dc idealLinearVelocity = shipTransform.transformDirectionNew(new Vector3d(targetLinearVelocity), TransformType.SUBSPACE_TO_GLOBAL);
final Vector3dc currentLinearVelocity = physicsObject.getPhysicsCalculations().getLinearVelocity();
final Vector3dc velocityDifference = idealLinearVelocity.sub(currentLinearVelocity, new Vector3d());
final Vector3d resultingBlockForce = new Vector3d(velocityDifference);
resultingBlockForce.mul(physicsObject.getInertiaData().getGameTickMass());
resultingBlockForce.mul(secondsToApply);
resultingBlockForce.mul(LINEAR_EMA_FILTER_CONSTANT);
// Do not affect y axis
resultingBlockForce.y = 0;
return resultingBlockForce;
}
Aggregations