Search in sources :

Example 96 with Vector3d

use of org.joml.Vector3d in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class MixinNetHandlerPlayServer method preProcessPlayer.

/**
 * This mixin fixes "Player Moved Wrongly" errors.
 *
 * @param packetPlayer The packet the player sent us
 * @param info We can use this to cancel the invocation
 */
@Inject(method = "processPlayer", at = @At("HEAD"))
private void preProcessPlayer(final CPacketPlayer packetPlayer, final CallbackInfo info) {
    // Don't run any of this code on the network thread!
    if (this.player.getServerWorld().isCallingFromMinecraftThread()) {
        // This fixes players dying of fall damage when changing dimensions
        if (this.player.isInvulnerableDimensionChange()) {
            return;
        }
        final PlayerMovementData addedPlayerMovementData = IHasPlayerMovementData.class.cast(packetPlayer).getPlayerMovementData();
        final World world = player.world;
        final UUID lastTouchedShipId = addedPlayerMovementData.getLastTouchedShipId();
        final int ticksSinceTouchedLastShip = addedPlayerMovementData.getTicksSinceTouchedLastShip();
        if (ticksSinceTouchedLastShip > 40) {
            // If the player hasn't touched the ship in over 40 ticks, then ignore its coordinates relative to that ship.
            final IDraggable playerAsDraggable = IDraggable.class.cast(this.player);
            playerAsDraggable.setEntityShipMovementData(playerAsDraggable.getEntityShipMovementData().withLastTouchedShip(null).withAddedLinearVelocity(new Vector3d()).withAddedYawVelocity(0).withTicksPartOfGround(addedPlayerMovementData.getTicksPartOfGround()).withTicksSinceTouchedShip(ticksSinceTouchedLastShip));
            return;
        }
        final int ticksPartOfGround = addedPlayerMovementData.getTicksPartOfGround();
        final Vector3d playerPosInShip = new Vector3d(addedPlayerMovementData.getPlayerPosInShip());
        final Vector3d playerLookInShip = new Vector3d(addedPlayerMovementData.getPlayerLookInShip());
        ShipData lastTouchedShip = null;
        if (lastTouchedShipId != null) {
            final QueryableShipData queryableShipData = QueryableShipData.get(world);
            final Optional<ShipData> shipDataOptional = queryableShipData.getShip(lastTouchedShipId);
            if (shipDataOptional.isPresent()) {
                lastTouchedShip = shipDataOptional.get();
                final PhysicsObject shipObject = ValkyrienUtils.getServerShipManager(world).getPhysObjectFromUUID(lastTouchedShip.getUuid());
                if (shipObject != null) {
                    if (shipObject.getTicksSinceShipTeleport() > PhysicsObject.TICKS_SINCE_TELEPORT_TO_START_DRAGGING) {
                        final ShipTransform shipTransform = lastTouchedShip.getShipTransform();
                        shipTransform.transformPosition(playerPosInShip, TransformType.SUBSPACE_TO_GLOBAL);
                        shipTransform.transformDirection(playerLookInShip, TransformType.SUBSPACE_TO_GLOBAL);
                    } else {
                        // Don't move the player relative to the ship until the TicksSinceShipTeleport timer expires.
                        playerPosInShip.set(player.posX, player.posY, player.posZ);
                    }
                }
            } else {
                // info.cancel();
                return;
            }
        }
        // Get the player pitch/yaw from the look vector
        final Tuple<Double, Double> pitchYawTuple = VSMath.getPitchYawFromVector(playerLookInShip);
        final double playerPitchInGlobal = pitchYawTuple.getFirst();
        final double playerYawInGlobal = pitchYawTuple.getSecond();
        // Idk if this is needed, but I'm too bothered to change it
        packetPlayer.moving = true;
        // Then update the packet values to match the ones above.
        packetPlayer.x = playerPosInShip.x();
        packetPlayer.y = playerPosInShip.y();
        packetPlayer.z = playerPosInShip.z();
        packetPlayer.yaw = (float) playerYawInGlobal;
        packetPlayer.pitch = (float) playerPitchInGlobal;
        // Set the player motion values to tell the NetHandlerPlayServer that the player is allowed to move this fast.
        this.player.motionX = packetPlayer.x - this.firstGoodX;
        this.player.motionY = packetPlayer.y - this.firstGoodY;
        this.player.motionZ = packetPlayer.z - this.firstGoodZ;
        // Update the player draggable
        final IDraggable playerAsDraggable = IDraggable.class.cast(this.player);
        playerAsDraggable.setEntityShipMovementData(playerAsDraggable.getEntityShipMovementData().withLastTouchedShip(lastTouchedShip).withAddedLinearVelocity(new Vector3d()).withAddedYawVelocity(0).withTicksPartOfGround(ticksPartOfGround).withTicksSinceTouchedShip(ticksSinceTouchedLastShip));
    }
}
Also used : QueryableShipData(org.valkyrienskies.mod.common.ships.QueryableShipData) ShipData(org.valkyrienskies.mod.common.ships.ShipData) IHasPlayerMovementData(org.valkyrienskies.mod.common.network.IHasPlayerMovementData) PlayerMovementData(org.valkyrienskies.mod.common.network.PlayerMovementData) World(net.minecraft.world.World) QueryableShipData(org.valkyrienskies.mod.common.ships.QueryableShipData) PhysicsObject(org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject) Vector3d(org.joml.Vector3d) ShipTransform(org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform) IDraggable(org.valkyrienskies.mod.common.ships.entity_interaction.IDraggable) IHasPlayerMovementData(org.valkyrienskies.mod.common.network.IHasPlayerMovementData)

Example 97 with Vector3d

use of org.joml.Vector3d in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class IBlockForceProvider method getBlockForceInWorldSpace.

/**
 * The World space version of the force vector, calculated by default from the Ship space
 * vector, do not override unless you have a good reason to.
 */
@Nullable
default Vector3dc getBlockForceInWorldSpace(World world, BlockPos pos, IBlockState state, PhysicsObject physicsObject, double secondsToApply) {
    Vector3dc toReturn = getBlockForceInShipSpace(world, pos, state, physicsObject, secondsToApply);
    if (toReturn == null) {
        return null;
    }
    if (shouldLocalForceBeRotated(world, pos, state, secondsToApply)) {
        ShipTransform shipTransform = physicsObject.getShipTransformationManager().getCurrentTickTransform();
        Vector3d rotated = new Vector3d(toReturn);
        shipTransform.transformDirection(rotated, TransformType.SUBSPACE_TO_GLOBAL);
        return rotated;
    }
    return toReturn;
}
Also used : Vector3dc(org.joml.Vector3dc) ShipTransform(org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform) Vector3d(org.joml.Vector3d) Nullable(javax.annotation.Nullable)

Example 98 with Vector3d

use of org.joml.Vector3d in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class BlockBoatChair method onBlockActivated.

@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
    if (!worldIn.isRemote) {
        Optional<PhysicsObject> physicsObject = ValkyrienUtils.getPhysoManagingBlock(worldIn, pos);
        if (physicsObject.isPresent()) {
            TileEntity tileEntity = worldIn.getTileEntity(pos);
            if (tileEntity instanceof TileEntityBoatChair) {
                Vector3d playerPos = new Vector3d(playerIn.posX, playerIn.posY, playerIn.posZ);
                physicsObject.get().getShipTransformationManager().getCurrentTickTransform().transformPosition(playerPos, TransformType.SUBSPACE_TO_GLOBAL);
                playerIn.posX = playerPos.x;
                playerIn.posY = playerPos.y;
                playerIn.posZ = playerPos.z;
                // Only mount the player if they're standing on the ship.
                final EntityShipMovementData entityShipMovementData = ValkyrienUtils.getEntityShipMovementDataFor(playerIn);
                if (entityShipMovementData.getTicksSinceTouchedShip() == 0 && (entityShipMovementData.getLastTouchedShip() == physicsObject.get().getShipData())) {
                    Vector3dc localMountPos = getPlayerMountOffset(state, pos);
                    ValkyrienUtils.fixEntityToShip(playerIn, localMountPos, physicsObject.get());
                }
                ((TileEntityBoatChair) tileEntity).setPilotEntity(playerIn);
                physicsObject.get().getShipTransformationManager().getCurrentTickTransform().transformPosition(playerPos, TransformType.GLOBAL_TO_SUBSPACE);
                playerIn.posX = playerPos.x;
                playerIn.posY = playerPos.y;
                playerIn.posZ = playerPos.z;
            }
        }
    }
    return true;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) EntityShipMovementData(org.valkyrienskies.mod.common.entity.EntityShipMovementData) Vector3dc(org.joml.Vector3dc) Vector3d(org.joml.Vector3d) TileEntityBoatChair(org.valkyrienskies.mod.common.tileentity.TileEntityBoatChair) PhysicsObject(org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject)

Example 99 with Vector3d

use of org.joml.Vector3d in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class PhysObjectRenderManager method renderDebugInfo.

/**
 * Renders bounding boxes for this ship's AABB and center of mass relative to the offset
 * (probably the negative player's position). Used in F3+D
 */
public void renderDebugInfo(double offsetX, double offsetY, double offsetZ) {
    GlStateManager.pushMatrix();
    AxisAlignedBB shipBB = parent.getShipBB().offset(offsetX, offsetY, offsetZ);
    ShipTransform renderTransform = parent.getShipTransformationManager().getRenderTransform();
    Vector3dc centerOfMass = parent.getShipData().getInertiaData().getGameTickCenterOfMass();
    Vector3d centerOfMassPos = new Vector3d(centerOfMass);
    parent.getShipTransformationManager().getRenderTransform().transformPosition(centerOfMassPos, TransformType.SUBSPACE_TO_GLOBAL);
    AxisAlignedBB centerOfMassBB = new AxisAlignedBB(JOML.toMinecraft(centerOfMassPos), JOML.toMinecraft(centerOfMassPos)).grow(.1).offset(offsetX, offsetY, offsetZ);
    GlStateManager.depthMask(false);
    GlStateManager.disableTexture2D();
    GlStateManager.disableLighting();
    GlStateManager.disableCull();
    GlStateManager.disableBlend();
    // Draw the bounding box for the ship.
    RenderGlobal.drawSelectionBoundingBox(shipBB, 1.0F, 1.0F, 1.0F, 1.0F);
    // region Render claimed chunks
    if (VSConfig.renderShipChunkClaimsInDebug) {
        GlStateManager.pushMatrix();
        GlStateManager.translate((float) renderTransform.getPosX() + offsetX, (float) renderTransform.getPosY() + offsetY, (float) renderTransform.getPosZ() + offsetZ);
        Vector3dc angles = renderTransform.getSubspaceToGlobal().getEulerAnglesZYX(new Vector3d());
        GlStateManager.rotate((float) Math.toDegrees(angles.z()), 0, 0, 1);
        GlStateManager.rotate((float) Math.toDegrees(angles.y()), 0, 1, 0);
        GlStateManager.rotate((float) Math.toDegrees(angles.x()), 1, 0, 0);
        for (ChunkPos claimedChunk : parent.getChunkClaim()) {
            AxisAlignedBB claimBB = new AxisAlignedBB(claimedChunk.getXStart() + .1, renderTransform.getCenterCoord().y() - 8 + 0.1, claimedChunk.getZStart() + .1, claimedChunk.getXEnd() + .9, renderTransform.getCenterCoord().y() + 8 - .1, claimedChunk.getZEnd() + .9);
            claimBB = claimBB.offset(-renderTransform.getCenterCoord().x(), -renderTransform.getCenterCoord().y(), -renderTransform.getCenterCoord().z());
            RenderGlobal.drawSelectionBoundingBox(claimBB, 0, 1.0F, 0, 1.0F);
        }
        GlStateManager.popMatrix();
    }
    // endregion
    // Draw the center of mass bounding box.
    GlStateManager.disableDepth();
    RenderGlobal.drawSelectionBoundingBox(centerOfMassBB, 0, 0, 1.0F, 1.0F);
    GlStateManager.enableDepth();
    GlStateManager.enableTexture2D();
    GlStateManager.enableLighting();
    GlStateManager.enableCull();
    GlStateManager.disableBlend();
    GlStateManager.depthMask(true);
    // Draw a text box that shows the numerical value of the center of mass.
    String centerOfMassStr = "Center of Mass: " + (new Vector3d(centerOfMass)).toString(new DecimalFormat("############.##"));
    renderTextBox(centerOfMassStr, renderTransform.getPosX(), renderTransform.getPosY() + .5, renderTransform.getPosZ(), offsetX, offsetY, offsetZ);
    String massStr = String.format("Mass: %.2f", parent.getShipData().getInertiaData().getGameTickMass());
    renderTextBox(massStr, renderTransform.getPosX(), renderTransform.getPosY() + 1, renderTransform.getPosZ(), offsetX, offsetY, offsetZ);
    GlStateManager.popMatrix();
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) Vector3dc(org.joml.Vector3dc) ShipTransform(org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform) Vector3d(org.joml.Vector3d) DecimalFormat(java.text.DecimalFormat) ChunkPos(net.minecraft.util.math.ChunkPos)

Example 100 with Vector3d

use of org.joml.Vector3d in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class WorldPhysicsCollider method addFrictionToNormalForce.

// Applies the friction force generated by the collision.
// The magnitude of this vector must be adjusted to minimize energy
private void addFrictionToNormalForce(Vector3dc momentumAtPoint, Vector3d impulseVector, Vector3dc inBody) {
    Vector3d contactNormal = new Vector3d(impulseVector);
    contactNormal.normalize();
    Vector3d frictionVector = new Vector3d(momentumAtPoint);
    frictionVector.normalize();
    frictionVector.mul(impulseVector.length() * KINETIC_FRICTION_COEFFICIENT);
    if (frictionVector.dot(momentumAtPoint) > 0) {
        frictionVector.mul(-1D);
    }
    // Remove all friction components along the impulse vector
    double frictionImpulseDot = frictionVector.dot(contactNormal);
    Vector3d toRemove = contactNormal.mul(frictionImpulseDot, new Vector3d());
    frictionVector.sub(toRemove);
    double inertiaScalarAlongAxis = parent.getPhysicsCalculations().getInertiaAlongRotationAxis();
    // The change in velocity vector
    Vector3dc initialVelocity = parent.getPhysicsCalculations().getLinearVelocity();
    // Don't forget to multiply by delta t
    Vector3d deltaVelocity = new Vector3d(frictionVector);
    deltaVelocity.mul(parent.getPhysicsCalculations().getInvMass() * parent.getPhysicsCalculations().getDragForPhysTick());
    double A = initialVelocity.lengthSquared();
    double B = 2 * initialVelocity.dot(deltaVelocity);
    double C = deltaVelocity.lengthSquared();
    Vector3d initialAngularVelocity = new Vector3d(parent.getPhysicsCalculations().getAngularVelocity());
    Vector3d deltaAngularVelocity = inBody.cross(frictionVector, new Vector3d());
    // This might need to be 1 / inertiaScalarAlongAxis
    deltaAngularVelocity.mul(parent.getPhysicsCalculations().getDragForPhysTick() / inertiaScalarAlongAxis);
    double D = initialAngularVelocity.lengthSquared();
    double E = 2 * deltaAngularVelocity.dot(initialAngularVelocity);
    double F = deltaAngularVelocity.lengthSquared();
    // This is tied to PhysicsCalculations line 430
    if (initialAngularVelocity.lengthSquared() < .05 && initialVelocity.lengthSquared() < .05) {
        // Remove rotational friction if we are rotating slow enough
        D = E = F = 0;
    }
    // The coefficients of energy as a function of energyScaleFactor in the form (A
    // + B * k + c * k^2)
    double firstCoefficient = A * parent.getPhysicsCalculations().getMass() + D * inertiaScalarAlongAxis;
    double secondCoefficient = B * parent.getPhysicsCalculations().getMass() + E * inertiaScalarAlongAxis;
    double thirdCoefficient = C * parent.getPhysicsCalculations().getMass() + F * inertiaScalarAlongAxis;
    double scaleFactor = -secondCoefficient / (thirdCoefficient * 2);
    if (new Double(scaleFactor).isNaN()) {
        scaleFactor = 0;
    } else {
        scaleFactor = Math.max(0, Math.min(scaleFactor, 1));
        frictionVector.mul(scaleFactor);
    }
    // System.out.println(scaleFactor);
    // ===== Friction Scaling Code End =====
    impulseVector.add(frictionVector);
}
Also used : Vector3dc(org.joml.Vector3dc) Vector3d(org.joml.Vector3d)

Aggregations

Vector3d (org.joml.Vector3d)117 Vector3dc (org.joml.Vector3dc)33 PhysicsObject (org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject)20 BlockPos (net.minecraft.util.math.BlockPos)19 ShipTransform (org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform)18 Location (io.xol.chunkstories.api.Location)12 Entity (io.xol.chunkstories.api.entity.Entity)11 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)9 World (net.minecraft.world.World)9 ShipData (org.valkyrienskies.mod.common.ships.ShipData)9 WorldClient (io.xol.chunkstories.api.world.WorldClient)8 WorldMaster (io.xol.chunkstories.api.world.WorldMaster)8 EntityPlayer (net.minecraft.entity.player.EntityPlayer)8 EntityShipMovementData (org.valkyrienskies.mod.common.entity.EntityShipMovementData)7 EntityControllable (io.xol.chunkstories.api.entity.interfaces.EntityControllable)6 IBlockState (net.minecraft.block.state.IBlockState)6 Vec3d (net.minecraft.util.math.Vec3d)6 Vector3f (org.joml.Vector3f)6 EntityLiving (io.xol.chunkstories.api.entity.EntityLiving)5 CellData (io.xol.chunkstories.api.world.cell.CellData)5