Search in sources :

Example 6 with ShipTransform

use of org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class ShipPilot method getTorqueInGlobal.

public Vector3dc getTorqueInGlobal(PhysicsCalculations physicsCalculations) {
    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)

Example 7 with ShipTransform

use of org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class ItemShipTracker method onItemRightClick.

public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer player, EnumHand hand) {
    if (!worldIn.isRemote) {
        final ItemStack heldItemStack = player.getHeldItem(hand);
        final NBTTagCompound stackTagCompound;
        if (heldItemStack.hasTagCompound()) {
            stackTagCompound = heldItemStack.stackTagCompound;
        } else {
            stackTagCompound = new NBTTagCompound();
        }
        if (stackTagCompound.hasKey(NBT_DATA_KEY)) {
            // Tell the player the ship location
            final UUID shipUUID = UUID.fromString(stackTagCompound.getString(NBT_DATA_KEY));
            final Optional<ShipData> shipDataOptional = ValkyrienUtils.getQueryableData(worldIn).getShip(shipUUID);
            if (shipDataOptional.isPresent()) {
                final ShipData shipData = shipDataOptional.get();
                final ShipTransform currentTransform = shipData.getShipTransform();
                final Vector3d shipPosition = new Vector3d(currentTransform.getPosX(), currentTransform.getPosY(), currentTransform.getPosZ());
                // Only print up to 2 digits after the decimal place
                final String shipPositionString = shipPosition.toString(new DecimalFormat("############.##"));
                player.sendMessage(new TextComponentString(String.format("The ship %s is currently at %s.", shipData.getName(), shipPositionString)));
            } else {
                player.sendMessage(new TextComponentString(String.format("No ship with UUID %s found! Maybe it was destroyed.", shipUUID.toString())));
            }
        } else {
            player.sendMessage(new TextComponentString("Not tracking any ships. Right click on a ship to track it."));
        }
    }
    return new ActionResult<>(EnumActionResult.PASS, player.getHeldItem(hand));
}
Also used : ActionResult(net.minecraft.util.ActionResult) EnumActionResult(net.minecraft.util.EnumActionResult) ShipTransform(org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform) Vector3d(org.joml.Vector3d) ShipData(org.valkyrienskies.mod.common.ships.ShipData) DecimalFormat(java.text.DecimalFormat) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) TextComponentString(net.minecraft.util.text.TextComponentString) ItemStack(net.minecraft.item.ItemStack) UUID(java.util.UUID) TextComponentString(net.minecraft.util.text.TextComponentString)

Example 8 with ShipTransform

use of org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class PlayerMovementDataGenerator method generatePlayerMovementDataForClient.

/**
 * Only works on the client.
 */
public static PlayerMovementData generatePlayerMovementDataForClient() {
    final EntityPlayerSP entityPlayer = Minecraft.getMinecraft().player;
    final EntityShipMovementData entityShipMovementData = ValkyrienUtils.getEntityShipMovementDataFor(entityPlayer);
    final ShipData lastTouchedShip = entityShipMovementData.getLastTouchedShip();
    final UUID lastTouchedShipId = lastTouchedShip != null ? lastTouchedShip.getUuid() : null;
    final Vector3d playerPosInLocal = new Vector3d(entityPlayer.posX, entityPlayer.posY, entityPlayer.posZ);
    final Vector3d playerLookInLocal = JOML.convert(entityPlayer.getLook(1));
    final boolean onGround = entityPlayer.onGround;
    if (lastTouchedShip != null) {
        final ShipTransform shipTransform = lastTouchedShip.getShipTransform();
        shipTransform.transformPosition(playerPosInLocal, TransformType.GLOBAL_TO_SUBSPACE);
        shipTransform.transformDirection(playerLookInLocal, TransformType.GLOBAL_TO_SUBSPACE);
    }
    return new PlayerMovementData(lastTouchedShipId, entityShipMovementData.getTicksSinceTouchedShip(), entityShipMovementData.getTicksPartOfGround(), playerPosInLocal, playerLookInLocal, onGround);
}
Also used : EntityShipMovementData(org.valkyrienskies.mod.common.entity.EntityShipMovementData) Vector3d(org.joml.Vector3d) ShipTransform(org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform) ShipData(org.valkyrienskies.mod.common.ships.ShipData) EntityPlayerSP(net.minecraft.client.entity.EntityPlayerSP) UUID(java.util.UUID)

Example 9 with ShipTransform

use of org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class ShipTransformUpdateMessage method fromBytes.

@Override
public void fromBytes(ByteBuf buf) {
    PacketBuffer packetBuffer = new PacketBuffer(buf);
    int numberOfShips = packetBuffer.readInt();
    for (int i = 0; i < numberOfShips; i++) {
        UUID shipID = null;
        ShipTransform shipTransform = null;
        AxisAlignedBB axisAlignedBB = null;
        // Read the UUID
        {
            int bytesSize = packetBuffer.readInt();
            byte[] bytes = new byte[bytesSize];
            packetBuffer.readBytes(bytes);
            try {
                shipID = serializer.readValue(bytes, UUID.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // Read the ship transform
        {
            int bytesSize = packetBuffer.readInt();
            byte[] bytes = new byte[bytesSize];
            packetBuffer.readBytes(bytes);
            try {
                shipTransform = serializer.readValue(bytes, ShipTransform.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // Read the ship aabb
        {
            int bytesSize = packetBuffer.readInt();
            byte[] bytes = new byte[bytesSize];
            packetBuffer.readBytes(bytes);
            try {
                axisAlignedBB = serializer.readValue(bytes, AxisAlignedBB.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (shipID == null || shipTransform == null || axisAlignedBB == null) {
            // corrupt packet
            shipTransforms.clear();
            return;
        }
        shipTransforms.put(shipID, new Tuple<>(shipTransform, axisAlignedBB));
    }
    dimensionID = packetBuffer.readInt();
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) ShipTransform(org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform) IOException(java.io.IOException) PacketBuffer(net.minecraft.network.PacketBuffer)

Example 10 with ShipTransform

use of org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class ShipTransformUpdateMessageHandler method onMessage.

@Override
@SuppressWarnings("Convert2Lambda")
public // errors. DON'T USE A LAMBDA
IMessage onMessage(final ShipTransformUpdateMessage message, final MessageContext ctx) {
    IThreadListener mainThread = Minecraft.getMinecraft();
    mainThread.addScheduledTask(new Runnable() {

        @Override
        public void run() {
            World world = Minecraft.getMinecraft().world;
            IPhysObjectWorld physObjectWorld = ValkyrienUtils.getPhysObjWorld(world);
            QueryableShipData worldData = QueryableShipData.get(world);
            for (Map.Entry<UUID, Tuple<ShipTransform, AxisAlignedBB>> transformUpdate : message.shipTransforms.entrySet()) {
                final UUID shipID = transformUpdate.getKey();
                final ShipTransform shipTransform = transformUpdate.getValue().getFirst();
                final AxisAlignedBB shipBB = transformUpdate.getValue().getSecond();
                final PhysicsObject physicsObject = ValkyrienUtils.getPhysObjWorld(world).getPhysObjectFromUUID(shipID);
                if (physicsObject != null) {
                    // Do not update the transform in ShipData, that will be done by PhysicsObject.tick()
                    ITransformInterpolator interpolator = physicsObject.getTransformInterpolator();
                    interpolator.onNewTransformPacket(shipTransform, shipBB);
                }
            }
        }
    });
    return null;
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) ShipTransform(org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform) ITransformInterpolator(org.valkyrienskies.mod.common.ships.interpolation.ITransformInterpolator) IThreadListener(net.minecraft.util.IThreadListener) World(net.minecraft.world.World) IPhysObjectWorld(org.valkyrienskies.mod.common.ships.ship_world.IPhysObjectWorld) UUID(java.util.UUID) IPhysObjectWorld(org.valkyrienskies.mod.common.ships.ship_world.IPhysObjectWorld) QueryableShipData(org.valkyrienskies.mod.common.ships.QueryableShipData) PhysicsObject(org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject)

Aggregations

ShipTransform (org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform)32 Vector3d (org.joml.Vector3d)18 Vector3dc (org.joml.Vector3dc)15 PhysicsObject (org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject)9 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)7 World (net.minecraft.world.World)7 ShipData (org.valkyrienskies.mod.common.ships.ShipData)6 UUID (java.util.UUID)4 Nullable (javax.annotation.Nullable)4 BlockPos (net.minecraft.util.math.BlockPos)4 Vec3d (net.minecraft.util.math.Vec3d)4 IPhysObjectWorld (org.valkyrienskies.mod.common.ships.ship_world.IPhysObjectWorld)4 IBlockState (net.minecraft.block.state.IBlockState)3 Entity (net.minecraft.entity.Entity)3 Chunk (net.minecraft.world.chunk.Chunk)3 EntityShipMovementData (org.valkyrienskies.mod.common.entity.EntityShipMovementData)3 DecimalFormat (java.text.DecimalFormat)2 EntityLivingBase (net.minecraft.entity.EntityLivingBase)2 ChunkPos (net.minecraft.util.math.ChunkPos)2 ExtendedBlockStorage (net.minecraft.world.chunk.storage.ExtendedBlockStorage)2