Search in sources :

Example 36 with Vector3dc

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

the class MixinEntityRenderer method orientCamera.

@Inject(method = "orientCamera", at = @At("HEAD"), cancellable = true)
private void orientCamera(float partialTicks, CallbackInfo ci) {
    EntityShipMountData mountData = ValkyrienUtils.getMountedShipAndPos(mc.getRenderViewEntity());
    if (mountData.getMountedShip() == null) {
        // Do nothing. We don't want to mess with camera code unless we have to.
        return;
    } else {
        // Take over the camera orientation entirely. Don't let anything else touch it.
        ci.cancel();
    }
    Entity entity = this.mc.getRenderViewEntity();
    Vector3d eyeVector = new Vector3d(0, entity.getEyeHeight(), 0);
    if (entity instanceof EntityLivingBase && ((EntityLivingBase) entity).isPlayerSleeping()) {
        eyeVector.y += .7D;
    }
    double d0 = entity.prevPosX + (entity.posX - entity.prevPosX) * partialTicks;
    double d1 = entity.prevPosY + (entity.posY - entity.prevPosY) * partialTicks;
    double d2 = entity.prevPosZ + (entity.posZ - entity.prevPosZ) * partialTicks;
    // Probably overkill, but this should 100% fix the crash in issue #78
    if (mountData.isMounted() && mountData.getMountedShip().getShipRenderer().offsetPos != null) {
        final ShipTransform renderTransform = mountData.getMountedShip().getShipTransformationManager().getRenderTransform();
        renderTransform.transformDirection(eyeVector, TransformType.SUBSPACE_TO_GLOBAL);
        Vector3d playerPosition = JOML.convert(mountData.getMountPos());
        renderTransform.transformPosition(playerPosition, TransformType.SUBSPACE_TO_GLOBAL);
        d0 = playerPosition.x;
        d1 = playerPosition.y;
        d2 = playerPosition.z;
    }
    d0 += eyeVector.x;
    d1 += eyeVector.y;
    d2 += eyeVector.z;
    if (entity instanceof EntityLivingBase && ((EntityLivingBase) entity).isPlayerSleeping()) {
        if (!this.mc.gameSettings.debugCamEnable) {
            // VS code starts here
            if (mountData.isMounted()) {
                Vector3d playerPosInLocal = JOML.convert(mountData.getMountPos());
                playerPosInLocal.sub(.5D, .6875, .5);
                playerPosInLocal.round();
                BlockPos bedPos = new BlockPos(playerPosInLocal.x, playerPosInLocal.y, playerPosInLocal.z);
                IBlockState state = this.mc.world.getBlockState(bedPos);
                Block block = state.getBlock();
                float angleYaw = 0;
                if (block != null && block.isBed(state, entity.world, bedPos, entity)) {
                    angleYaw = block.getBedDirection(state, entity.world, bedPos).getHorizontalIndex() * 90;
                    angleYaw += 180;
                }
                entity.rotationYaw = entity.prevRotationYaw = angleYaw;
                entity.rotationPitch = entity.prevRotationPitch = 0;
            } else {
                BlockPos blockpos = new BlockPos(entity);
                IBlockState iblockstate = this.mc.world.getBlockState(blockpos);
                net.minecraftforge.client.ForgeHooksClient.orientBedCamera(this.mc.world, blockpos, iblockstate, entity);
                GlStateManager.rotate(entity.prevRotationYaw + (entity.rotationYaw - entity.prevRotationYaw) * partialTicks + 180.0F, 0.0F, -1.0F, 0.0F);
                GlStateManager.rotate(entity.prevRotationPitch + (entity.rotationPitch - entity.prevRotationPitch) * partialTicks, -1.0F, 0.0F, 0.0F);
            }
        }
    } else if (this.mc.gameSettings.thirdPersonView > 0) {
        double d3 = this.thirdPersonDistancePrev + (4.0F - this.thirdPersonDistancePrev) * partialTicks;
        IShipPilot shipPilot = (IShipPilot) Minecraft.getMinecraft().player;
        if (shipPilot.isPilotingShip()) {
            // TODO: Make this number scale with the Ship
            d3 = 15D;
        }
        if (this.mc.gameSettings.debugCamEnable) {
            GlStateManager.translate(0.0F, 0.0F, (float) (-d3));
        } else {
            float f1 = entity.rotationYaw;
            float f2 = entity.rotationPitch;
            if (this.mc.gameSettings.thirdPersonView == 2) {
                f2 += 180.0F;
            }
            double d4 = -MathHelper.sin(f1 * 0.017453292F) * MathHelper.cos(f2 * 0.017453292F) * d3;
            double d5 = MathHelper.cos(f1 * 0.017453292F) * MathHelper.cos(f2 * 0.017453292F) * d3;
            double d6 = (-MathHelper.sin(f2 * 0.017453292F)) * d3;
            for (int i = 0; i < 8; ++i) {
                float f3 = (i & 1) * 2 - 1;
                float f4 = (i >> 1 & 1) * 2 - 1;
                float f5 = (i >> 2 & 1) * 2 - 1;
                f3 = f3 * 0.1F;
                f4 = f4 * 0.1F;
                f5 = f5 * 0.1F;
                IShipPilot pilot = (IShipPilot) Minecraft.getMinecraft().player;
                ((IWorldVS) this.mc.world).excludeShipFromRayTracer(((IShipPilot) this.mc.player).getPilotedShip());
                // RayTraceResult raytraceresult = EntityMoveInjectionMethods.rayTraceBlocksIgnoreShip(Minecraft.getMinecraft().world, new Vec3d(d0 + f3, d1 + f4, d2 + f5), new Vec3d(d0 - d4 + f3 + f5, d1 - d6 + f4, d2 - d5 + f5), false, false, false, pilot.getPilotedShip());
                RayTraceResult raytraceresult = mc.world.rayTraceBlocks(new Vec3d(d0 + (double) f3, d1 + (double) f4, d2 + (double) f5), new Vec3d(d0 - d4 + (double) f3 + (double) f5, d1 - d6 + (double) f4, d2 - d5 + (double) f5));
                ((IWorldVS) this.mc.world).unexcludeShipFromRayTracer(((IShipPilot) this.mc.player).getPilotedShip());
                if (raytraceresult != null) {
                    double d7 = raytraceresult.hitVec.distanceTo(new Vec3d(d0, d1, d2));
                    if (d7 < d3) {
                        d3 = d7;
                    }
                }
            }
            if (this.mc.gameSettings.thirdPersonView == 2) {
                GlStateManager.rotate(180.0F, 0.0F, 1.0F, 0.0F);
            }
            GlStateManager.rotate(entity.rotationPitch - f2, 1.0F, 0.0F, 0.0F);
            GlStateManager.rotate(entity.rotationYaw - f1, 0.0F, 1.0F, 0.0F);
            GlStateManager.translate(0.0F, 0.0F, (float) (-d3));
            GlStateManager.rotate(f1 - entity.rotationYaw, 0.0F, 1.0F, 0.0F);
            GlStateManager.rotate(f2 - entity.rotationPitch, 1.0F, 0.0F, 0.0F);
        }
    } else {
        GlStateManager.translate(0.0F, 0.0F, 0.05F);
    }
    if (!this.mc.gameSettings.debugCamEnable) {
        float yaw = entity.prevRotationYaw + (entity.rotationYaw - entity.prevRotationYaw) * partialTicks + 180.0F;
        float pitch = entity.prevRotationPitch + (entity.rotationPitch - entity.prevRotationPitch) * partialTicks;
        float roll = 0.0F;
        if (entity instanceof EntityAnimal) {
            EntityAnimal entityanimal = (EntityAnimal) entity;
            yaw = entityanimal.prevRotationYawHead + (entityanimal.rotationYawHead - entityanimal.prevRotationYawHead) * partialTicks + 180.0F;
        }
        IBlockState state = ActiveRenderInfo.getBlockStateAtEntityViewpoint(this.mc.world, entity, partialTicks);
        net.minecraftforge.client.event.EntityViewRenderEvent.CameraSetup event = new net.minecraftforge.client.event.EntityViewRenderEvent.CameraSetup(EntityRenderer.class.cast(this), entity, state, partialTicks, yaw, pitch, roll);
        net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(event);
        GlStateManager.rotate(event.getRoll(), 0.0F, 0.0F, 1.0F);
        GlStateManager.rotate(event.getPitch(), 1.0F, 0.0F, 0.0F);
        GlStateManager.rotate(event.getYaw(), 0.0F, 1.0F, 0.0F);
    }
    if (mountData.isMounted() && mountData.getMountedShip().getShipRenderer().offsetPos != null) {
        final ShipTransform renderTransform = mountData.getMountedShip().getShipTransformationManager().getRenderTransform();
        Quaterniond orientationQuat = renderTransform.rotationQuaternion(TransformType.SUBSPACE_TO_GLOBAL);
        Vector3dc radians = orientationQuat.getEulerAnglesXYZ(new Vector3d());
        float moddedPitch = (float) Math.toDegrees(radians.x());
        float moddedYaw = (float) Math.toDegrees(radians.y());
        float moddedRoll = (float) Math.toDegrees(radians.z());
        GlStateManager.rotate(-moddedRoll, 0.0F, 0.0F, 1.0F);
        GlStateManager.rotate(-moddedYaw, 0.0F, 1.0F, 0.0F);
        GlStateManager.rotate(-moddedPitch, 1.0F, 0.0F, 0.0F);
    }
    GlStateManager.translate(-eyeVector.x, -eyeVector.y, -eyeVector.z);
    d0 = entity.prevPosX + (entity.posX - entity.prevPosX) * partialTicks + eyeVector.x;
    d1 = entity.prevPosY + (entity.posY - entity.prevPosY) * partialTicks + eyeVector.y;
    d2 = entity.prevPosZ + (entity.posZ - entity.prevPosZ) * partialTicks + eyeVector.z;
    this.cloudFog = this.mc.renderGlobal.hasCloudFog(d0, d1, d2, partialTicks);
}
Also used : Entity(net.minecraft.entity.Entity) EntityShipMountData(org.valkyrienskies.mod.common.ships.entity_interaction.EntityShipMountData) IBlockState(net.minecraft.block.state.IBlockState) Quaterniond(org.joml.Quaterniond) RayTraceResult(net.minecraft.util.math.RayTraceResult) Vec3d(net.minecraft.util.math.Vec3d) Vector3dc(org.joml.Vector3dc) IShipPilot(org.valkyrienskies.mod.common.piloting.IShipPilot) Vector3d(org.joml.Vector3d) ShipTransform(org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform) EntityLivingBase(net.minecraft.entity.EntityLivingBase) Block(net.minecraft.block.Block) BlockPos(net.minecraft.util.math.BlockPos) EntityAnimal(net.minecraft.entity.passive.EntityAnimal) EntityRenderer(net.minecraft.client.renderer.EntityRenderer)

Example 37 with Vector3dc

use of org.joml.Vector3dc 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 38 with Vector3dc

use of org.joml.Vector3dc 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 39 with Vector3dc

use of org.joml.Vector3dc 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 40 with Vector3dc

use of org.joml.Vector3dc 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

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