Search in sources :

Example 46 with Vector3dc

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;
    }
}
Also used : EntityShipMovementData(org.valkyrienskies.mod.common.entity.EntityShipMovementData) ShipData(org.valkyrienskies.mod.common.ships.ShipData) Vector3dc(org.joml.Vector3dc) Matrix4d(org.joml.Matrix4d) Vector3d(org.joml.Vector3d) EntityLivingBase(net.minecraft.entity.EntityLivingBase) EntityPlayer(net.minecraft.entity.player.EntityPlayer)

Example 47 with Vector3dc

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;
}
Also used : Vector3dc(org.joml.Vector3dc) ShipTransform(org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform) Vector3d(org.joml.Vector3d) PhysicsObject(org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject) Nullable(javax.annotation.Nullable)

Example 48 with Vector3dc

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());
}
Also used : Vector3dc(org.joml.Vector3dc) Quaterniondc(org.joml.Quaterniondc) Vector3d(org.joml.Vector3d) Quaterniond(org.joml.Quaterniond)

Example 49 with Vector3dc

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();
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) Chunk(net.minecraft.world.chunk.Chunk) SPacketUnloadChunk(net.minecraft.network.play.server.SPacketUnloadChunk) Vector3dc(org.joml.Vector3dc) SPacketUnloadChunk(net.minecraft.network.play.server.SPacketUnloadChunk) ShipTransform(org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform) Vector3d(org.joml.Vector3d) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) ChunkPos(net.minecraft.util.math.ChunkPos) MutableBlockPos(net.minecraft.util.math.BlockPos.MutableBlockPos) BlockPos(net.minecraft.util.math.BlockPos) MutableBlockPos(net.minecraft.util.math.BlockPos.MutableBlockPos)

Example 50 with Vector3dc

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;
}
Also used : Vector3dc(org.joml.Vector3dc) ShipTransform(org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform) Vector3d(org.joml.Vector3d)

Aggregations

Vector3dc (org.joml.Vector3dc)51 Vector3d (org.joml.Vector3d)33 ShipTransform (org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform)15 PhysicsObject (org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject)9 BlockPos (net.minecraft.util.math.BlockPos)8 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)7 Location (io.xol.chunkstories.api.Location)6 IBlockState (net.minecraft.block.state.IBlockState)6 World (net.minecraft.world.World)6 WorldMaster (io.xol.chunkstories.api.world.WorldMaster)5 EntityShipMovementData (org.valkyrienskies.mod.common.entity.EntityShipMovementData)5 Entity (io.xol.chunkstories.api.entity.Entity)4 CollisionBox (io.xol.chunkstories.api.physics.CollisionBox)4 WorldClient (io.xol.chunkstories.api.world.WorldClient)4 CellData (io.xol.chunkstories.api.world.cell.CellData)4 Nullable (javax.annotation.Nullable)4 EntityLiving (io.xol.chunkstories.api.entity.EntityLiving)3 HitBox (io.xol.chunkstories.api.entity.EntityLiving.HitBox)3 Voxel (io.xol.chunkstories.api.voxel.Voxel)3 Block (net.minecraft.block.Block)3