Search in sources :

Example 51 with PhysicsObject

use of org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject 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 52 with PhysicsObject

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

the class FixAccurateRain method getRainPosFromShips.

public static BlockPos getRainPosFromShips(final World world, final BlockPos originalHeight) {
    if (VSConfig.accurateRain && Minecraft.getMinecraft().player != null) {
        final AxisAlignedBB boundingBox = new AxisAlignedBB(originalHeight.getX() - .5, 0, originalHeight.getZ() - .5, originalHeight.getX() + .5, 255, originalHeight.getZ() + .5);
        final List<PhysicsObject> physicsObjectList = ValkyrienUtils.getPhysObjWorld(world).getPhysObjectsInAABB(boundingBox);
        final Vec3d traceStart = new Vec3d(originalHeight.getX() + .5, Minecraft.getMinecraft().player.posY + 50, originalHeight.getZ() + .5);
        final Vec3d traceEnd = new Vec3d(originalHeight.getX() + .5, originalHeight.getY() + .5, originalHeight.getZ() + .5);
        if (traceStart.y < traceEnd.y) {
            return originalHeight;
        }
        for (final PhysicsObject physicsObject : physicsObjectList) {
            final RayTraceResult result = ((IWorldVS) world).rayTraceBlocksInShip(traceStart, traceEnd, true, true, false, physicsObject);
            // noinspection ConstantConditions
            if (result != null && result.getBlockPos() != null && result.typeOfHit != RayTraceResult.Type.MISS) {
                Vector3d blockPosVector = JOML.convertDouble(result.getBlockPos()).add(.5, .5, .5);
                physicsObject.getShipTransformationManager().getCurrentTickTransform().getSubspaceToGlobal().transformPosition(blockPosVector);
                return new BlockPos(originalHeight.getX(), blockPosVector.y(), originalHeight.getZ());
            }
        }
    }
    return originalHeight;
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) IWorldVS(org.valkyrienskies.mod.common.ships.ship_world.IWorldVS) Vector3d(org.joml.Vector3d) RayTraceResult(net.minecraft.util.math.RayTraceResult) BlockPos(net.minecraft.util.math.BlockPos) PhysicsObject(org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject) Vec3d(net.minecraft.util.math.Vec3d)

Example 53 with PhysicsObject

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

the class VSDarknessLibAPILightProvider method apply.

@Override
public Integer apply(final EntityPlayer entityPlayer) {
    final World world = entityPlayer.world;
    final AxisAlignedBB searchBB = new AxisAlignedBB(entityPlayer.posX, entityPlayer.posY, entityPlayer.posZ, entityPlayer.posX, entityPlayer.posY, entityPlayer.posZ).grow(8);
    final List<PhysicsObject> nearbyShips = ValkyrienUtils.getPhysObjWorld(world).getPhysObjectsInAABB(searchBB);
    final Vector3d temp0 = new Vector3d();
    final BlockPos.MutableBlockPos temp1 = new BlockPos.MutableBlockPos();
    int maxLight = 0;
    for (final PhysicsObject physicsObject : nearbyShips) {
        final Vector3dc positionInShip = physicsObject.getShipTransform().transformPositionNew(temp0.set(entityPlayer.posX, entityPlayer.posY, entityPlayer.posZ), TransformType.GLOBAL_TO_SUBSPACE);
        final BlockPos blockPosInShip = temp1.setPos(positionInShip.x(), positionInShip.y(), positionInShip.z());
        final int blockLight = getLightFromNeighborsFor(world, EnumSkyBlock.BLOCK, blockPosInShip);
        maxLight = Math.max(blockLight, maxLight);
    }
    return maxLight;
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) Vector3dc(org.joml.Vector3dc) Vector3d(org.joml.Vector3d) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) PhysicsObject(org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject)

Example 54 with PhysicsObject

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

the class VSWorldPhysicsLoop method physicsTick.

private void physicsTick(double delta) {
    // Update the immutable ship list.
    immutableShipsList = ((IHasShipManager) hostWorld).getManager().getAllLoadedThreadSafe();
    // Run tasks queued to run on physics thread
    recurringTasks.forEach(t -> t.runTask(delta));
    taskQueue.forEach(Runnable::run);
    taskQueue.clear();
    // Make a sublist of physics objects to process physics on.
    List<PhysicsObject> physicsEntitiesToDoPhysics = new ArrayList<>();
    for (PhysicsObject physicsObject : immutableShipsList) {
        if (physicsObject.isPhysicsReady() && physicsObject.isPhysicsEnabled() && physicsObject.getCachedSurroundingChunks() != null) {
            physicsEntitiesToDoPhysics.add(physicsObject);
        }
    }
    // Finally, actually process the physics tick
    tickThePhysicsAndCollision(physicsEntitiesToDoPhysics, delta);
    // Send ship position update packets around 20 times a second
    final long currentTimeMillis = System.currentTimeMillis();
    final double secondsSinceLastPacket = (currentTimeMillis - lastPacketSendTime) / 1000.0;
    // Use .04 to guarantee we're always sending at least 20 packets per second
    if (secondsSinceLastPacket > .04) {
        // Update the last update time
        lastPacketSendTime = currentTimeMillis;
        try {
            // At the end, send the transform update packets
            final ShipTransformUpdateMessage shipTransformUpdateMessage = new ShipTransformUpdateMessage();
            final int dimensionID = hostWorld.provider.getDimension();
            shipTransformUpdateMessage.setDimensionID(dimensionID);
            for (final PhysicsObject physicsObject : immutableShipsList) {
                final UUID shipUUID = physicsObject.getUuid();
                final ShipTransform shipTransform = physicsObject.getShipTransformationManager().getCurrentPhysicsTransform();
                final AxisAlignedBB shipBB = physicsObject.getPhysicsTransformAABB();
                shipTransformUpdateMessage.addData(shipUUID, shipTransform, shipBB);
            }
            ValkyrienSkiesMod.physWrapperTransformUpdateNetwork.sendToDimension(shipTransformUpdateMessage, shipTransformUpdateMessage.getDimensionID());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) ShipTransformUpdateMessage(org.valkyrienskies.mod.common.network.ShipTransformUpdateMessage) PhysicsObject(org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject) ShipTransform(org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform) IHasShipManager(org.valkyrienskies.mod.common.ships.ship_world.IHasShipManager)

Aggregations

PhysicsObject (org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject)54 World (net.minecraft.world.World)21 Vector3d (org.joml.Vector3d)21 BlockPos (net.minecraft.util.math.BlockPos)20 ShipTransform (org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform)10 IPhysObjectWorld (org.valkyrienskies.mod.common.ships.ship_world.IPhysObjectWorld)10 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)9 Vector3dc (org.joml.Vector3dc)9 TileEntity (net.minecraft.tileentity.TileEntity)8 Inject (org.spongepowered.asm.mixin.injection.Inject)8 Polygon (org.valkyrienskies.mod.common.collision.Polygon)8 ShipPolygon (org.valkyrienskies.mod.common.collision.ShipPolygon)8 Entity (net.minecraft.entity.Entity)7 EntityPlayer (net.minecraft.entity.player.EntityPlayer)7 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)7 IBlockState (net.minecraft.block.state.IBlockState)5 Overwrite (org.spongepowered.asm.mixin.Overwrite)5 ArrayList (java.util.ArrayList)4 IThreadListener (net.minecraft.util.IThreadListener)4 Vec3d (net.minecraft.util.math.Vec3d)4