Search in sources :

Example 16 with Vector3dc

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

the class WorldPhysicsCollider method handleActualCollision.

// Takes the collision data along all axes generated prior, and creates the
// ideal value that is to be followed
private boolean handleActualCollision(BlockPos inWorldPos, BlockPos inLocalPos, IBlockState inWorldState, IBlockState inLocalState) {
    final ShipTransform parentTransform = parent.getShipTransformationManager().getCurrentPhysicsTransform();
    final boolean isXUpSolid = isBlockInWorldSolidFast(inWorldPos.getX() + 1, inWorldPos.getY(), inWorldPos.getZ());
    final boolean isXDownSolid = isBlockInWorldSolidFast(inWorldPos.getX() - 1, inWorldPos.getY(), inWorldPos.getZ());
    final boolean isYUpSolid = isBlockInWorldSolidFast(inWorldPos.getX(), inWorldPos.getY() + 1, inWorldPos.getZ());
    final boolean isYDownSolid = isBlockInWorldSolidFast(inWorldPos.getX(), inWorldPos.getY() - 1, inWorldPos.getZ());
    final boolean isZUpSolid = isBlockInWorldSolidFast(inWorldPos.getX(), inWorldPos.getY(), inWorldPos.getZ() + 1);
    final boolean isZDownSolid = isBlockInWorldSolidFast(inWorldPos.getX(), inWorldPos.getY(), inWorldPos.getZ() - 1);
    if (isXUpSolid && isXDownSolid && isYUpSolid && isYDownSolid && isZUpSolid && isZDownSolid) {
        // Interior block, skip
        return false;
    }
    for (int i = 0; i < 8; i++) {
        final int xAxis = combinationsOfOnes[i * 3];
        final int yAxis = combinationsOfOnes[i * 3 + 1];
        final int zAxis = combinationsOfOnes[i * 3 + 2];
        // Position of the World's block in global
        final Vector3dc shipBlockInLocal = temp0.set(inLocalPos.getX() + .5 + xAxis * SPHERE_RADIUS, inLocalPos.getY() + .5 + yAxis * SPHERE_RADIUS, inLocalPos.getZ() + .5 + zAxis * SPHERE_RADIUS);
        // Position of the World's block in local
        final Vector3dc shipBlockInGlobal = parentTransform.transformPositionNew(temp1.set(shipBlockInLocal), TransformType.SUBSPACE_TO_GLOBAL);
        double xUpOffset = .5 - (shipBlockInGlobal.x() - (inWorldPos.getX() + .5)) + SPHERE_RADIUS;
        double xDownOffset = .5 - ((inWorldPos.getX() + .5) - shipBlockInGlobal.x()) + SPHERE_RADIUS;
        double yUpOffset = .5 - (shipBlockInGlobal.y() - (inWorldPos.getY() + .5)) + SPHERE_RADIUS;
        double yDownOffset = .5 - ((inWorldPos.getY() + .5) - shipBlockInGlobal.y()) + SPHERE_RADIUS;
        double zUpOffset = .5 - (shipBlockInGlobal.z() - (inWorldPos.getZ() + .5)) + SPHERE_RADIUS;
        double zDownOffset = .5 - ((inWorldPos.getZ() + .5) - shipBlockInGlobal.z()) + SPHERE_RADIUS;
        if (isXUpSolid) {
            xUpOffset = 1;
        }
        if (isXDownSolid) {
            xDownOffset = 1;
        }
        if (isYUpSolid) {
            yUpOffset = 1;
        }
        if (isYDownSolid) {
            yDownOffset = 1;
        }
        if (isZUpSolid) {
            zUpOffset = 1;
        }
        if (isZDownSolid) {
            zDownOffset = 1;
        }
        if (xUpOffset > 0 && xDownOffset > 0 && yUpOffset > 0 && yDownOffset > 0 && zUpOffset > 0 && zDownOffset > 0) {
            // Intersection
            final Vector3dc axis;
            final Vector3dc response;
            // If X and Z collision have less magnitude than this, then we only consider the Y axis.
            final double THRESHOLD = .1;
            if (Math.abs(xUpOffset) < THRESHOLD && Math.abs(xDownOffset) < THRESHOLD && Math.abs(zUpOffset) < THRESHOLD && Math.abs(zDownOffset) < THRESHOLD && (!isYUpSolid || !isYDownSolid)) {
                // Ignore other axes just use Y
                if (yUpOffset < yDownOffset) {
                    // yUpOffset is minimum
                    axis = temp2.set(0, -1, 0);
                    response = axis.mul(yUpOffset, temp3);
                } else {
                    // yDownOffset is minimum
                    axis = temp2.set(0, 1, 0);
                    response = axis.mul(yDownOffset, temp3);
                }
            } else {
                // Use the minimum overlapping axis
                if (xUpOffset < xDownOffset && xUpOffset < yUpOffset && xUpOffset < yDownOffset && xUpOffset < zUpOffset && xUpOffset < zDownOffset) {
                    // xUpOffset is minimum
                    axis = temp2.set(-1, 0, 0);
                    response = axis.mul(xUpOffset, temp3);
                } else if (xDownOffset < yUpOffset && xDownOffset < yDownOffset && xDownOffset < zUpOffset && xDownOffset < zDownOffset) {
                    // xDownOffset is minimum
                    axis = temp2.set(1, 0, 0);
                    response = axis.mul(xDownOffset, temp3);
                } else if (yUpOffset < yDownOffset && yUpOffset < zUpOffset && yUpOffset < zDownOffset) {
                    // yUpOffset is minimum
                    axis = temp2.set(0, -1, 0);
                    response = axis.mul(yUpOffset, temp3);
                } else if (yDownOffset < zUpOffset && yDownOffset < zDownOffset) {
                    // yDownOffset is minimum
                    axis = temp2.set(0, 1, 0);
                    response = axis.mul(yDownOffset, temp3);
                } else if (zUpOffset < zDownOffset) {
                    // zUpOffset is minimum
                    axis = temp2.set(0, 0, -1);
                    response = axis.mul(zUpOffset, temp3);
                } else {
                    // zDownOffset is minimum
                    axis = temp2.set(0, 0, 1);
                    response = axis.mul(zDownOffset, temp3);
                }
            }
            handleCollision(shipBlockInGlobal, axis, response, 1);
        }
    }
    return false;
}
Also used : Vector3dc(org.joml.Vector3dc) ShipTransform(org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform)

Example 17 with Vector3dc

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

the class MixinClientWorld method preGetCombinedLight.

@SideOnly(Side.CLIENT)
@Inject(method = "getCombinedLight(Lnet/minecraft/util/math/BlockPos;I)I", at = @At("HEAD"), cancellable = true)
private void preGetCombinedLight(BlockPos pos, int lightValue, CallbackInfoReturnable<Integer> callbackInfoReturnable) {
    final World world = thisAsWorld;
    try {
        int i = world.getLightFromNeighborsFor(EnumSkyBlock.SKY, pos);
        int j = world.getLightFromNeighborsFor(EnumSkyBlock.BLOCK, pos);
        AxisAlignedBB lightBB = new AxisAlignedBB(pos.getX() - 2, pos.getY() - 2, pos.getZ() - 2, pos.getX() + 2, pos.getY() + 2, pos.getZ() + 2);
        final List<PhysicsObject> physicsObjectList = ValkyrienUtils.getPhysObjWorld(world).getAllLoadedThreadSafe().stream().filter((physicsObject -> lightBB.intersects(physicsObject.getShipBB()))).collect(Collectors.toList());
        final BlockPos.MutableBlockPos mutableBlockPos = new BlockPos.MutableBlockPos();
        for (final PhysicsObject physicsObject : physicsObjectList) {
            final Vector3dc posInLocal = physicsObject.getShipTransformationManager().getRenderTransform().transformPositionNew(JOML.convertTo3d(pos).add(.5, .5, .5), TransformType.GLOBAL_TO_SUBSPACE);
            final int minX = (int) Math.floor(posInLocal.x());
            final int minY = (int) Math.floor(posInLocal.y());
            final int minZ = (int) Math.floor(posInLocal.z());
            int shipSkyLight = 0;
            for (int x = minX; x <= minX + 1; x++) {
                for (int y = minY; y <= minY + 1; y++) {
                    for (int z = minZ; z <= minZ + 1; z++) {
                        mutableBlockPos.setPos(x, y, z);
                        final IBlockState blockState = world.getBlockState(mutableBlockPos);
                        // Ignore the light of full blocks
                        if (blockState.isFullBlock()) {
                            continue;
                        }
                        final int localBlockLight = world.getLightFromNeighborsFor(EnumSkyBlock.BLOCK, mutableBlockPos);
                        final int localSkyLight = world.getLightFromNeighborsFor(EnumSkyBlock.SKY, mutableBlockPos);
                        j = Math.max(j, localBlockLight);
                        shipSkyLight = Math.max(shipSkyLight, localSkyLight);
                    }
                }
            }
            if (i > shipSkyLight) {
                i = shipSkyLight;
            }
        }
        if (j < lightValue) {
            j = lightValue;
        }
        callbackInfoReturnable.setReturnValue(i << 20 | j << 4);
    } catch (Exception e) {
        System.err.println("Something just went wrong here, getting default light value instead!");
        e.printStackTrace();
    }
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) Vector3dc(org.joml.Vector3dc) IBlockState(net.minecraft.block.state.IBlockState) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) PhysicsObject(org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject) Inject(org.spongepowered.asm.mixin.injection.Inject) SideOnly(net.minecraftforge.fml.relauncher.SideOnly)

Example 18 with Vector3dc

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

the class EntityCollisionInjector method alterEntityMovement.

// Returns false if game should use default collision
@Nullable
public static IntermediateMovementVariableStorage alterEntityMovement(Entity entity, MoverType type, double dx, double dy, double dz) {
    final double origDx = dx;
    final double origDy = dy;
    final double origDz = dz;
    final double origPosX = entity.posX;
    final double origPosY = entity.posY;
    final double origPosZ = entity.posZ;
    boolean isLiving = entity instanceof EntityLivingBase;
    Vec3d velocity = new Vec3d(dx, dy, dz);
    Polygon playerBeforeMove = new Polygon(entity.getEntityBoundingBox());
    List<Polygon> colPolys = getCollidingPolygonsAndDoBlockCols(entity, velocity);
    PhysicsObject worldBelow = null;
    IDraggable draggable = EntityDraggable.getDraggableFromEntity(entity);
    final EntityShipMovementData lastTickEntityShipMovementData = draggable.getEntityShipMovementData();
    Vector3d total = new Vector3d();
    // Used to reset the player position after collision processing, effectively
    // using the player to integrate their velocity
    double posOffestX = 0;
    double posOffestY = 0;
    double posOffestZ = 0;
    // True IFF the player is on a ladder
    boolean isPlayerOnLadder = false;
    // region Ladder movement
    if (entity instanceof EntityLivingBase) {
        final EntityLivingBase base = (EntityLivingBase) entity;
        final List<PhysicsObject> collidingShips = ((IHasShipManager) entity.getEntityWorld()).getManager().getPhysObjectsInAABB(base.getEntityBoundingBox());
        final Iterable<Triple<PhysicsObject, BlockPos, IBlockState>> ladderCollisions = getLadderCollisions(base, collidingShips);
        // For now, just ignore the y component. I may or may not use it later.
        final float forward = ((EntityLivingBase) entity).moveForward;
        final float strafe = ((EntityLivingBase) entity).moveStrafing;
        final double f1 = Math.sin(Math.toRadians(entity.rotationYaw));
        final double f2 = Math.cos(Math.toRadians(entity.rotationYaw));
        final double intendedXVel = strafe * f2 - forward * f1;
        final double intendedYVel = 0;
        final double intendedZVel = forward * f2 + strafe * f1;
        final Vector3dc originalVelocityDirection = new Vector3d(intendedXVel, intendedYVel, intendedZVel).normalize();
        final World world = entity.world;
        final Polygon playerPolygon = new Polygon(base.getEntityBoundingBox());
        for (final Triple<PhysicsObject, BlockPos, IBlockState> ladderCollision : ladderCollisions) {
            final IBlockState ladderState = ladderCollision.getRight();
            EnumFacing ladderFacing = null;
            // For now, we only support a few blocks
            if (ladderState.getPropertyKeys().contains(BlockHorizontal.FACING)) {
                ladderFacing = ladderState.getValue(BlockHorizontal.FACING);
            }
            // We need the EnumFacing of the ladder for the code to work. If we couldn't find it then just give up :/
            if (ladderFacing != null) {
                final Vector3d ladderNormal = JOML.convertDouble(ladderFacing.getDirectionVec());
                final ShipTransform shipTransform = ladderCollision.getLeft().getShipTransform();
                // Grow the ladder BB by a small margin (makes the ladder experience better imo)
                final AxisAlignedBB ladderBB = ladderCollision.getRight().getBoundingBox(world, ladderCollision.getMiddle()).offset(ladderCollision.getMiddle()).grow(.4);
                final Polygon ladderPoly = new Polygon(ladderBB, shipTransform.getSubspaceToGlobal());
                // Determine if the player is actually colliding with the ladder
                final PhysPolygonCollider collider = new PhysPolygonCollider(playerPolygon, ladderPoly, ladderCollision.getLeft().getShipTransformationManager().normals);
                collider.processData();
                shipTransform.transformDirection(ladderNormal, TransformType.SUBSPACE_TO_GLOBAL);
                // Don't use "floor ladders"
                final boolean isLadderFacingDown = ladderNormal.y > .8;
                if (isLadderFacingDown) {
                    continue;
                }
                // If the ladder is facing up, then let the player use them like monkey bars
                final boolean isLadderFacingUp = ladderNormal.y < -.8;
                // Whether or not the player is actually colliding with a ladder, since it is close to one we give the player ladder movement.
                dx = MathHelper.clamp(dx, -.15, .15);
                dz = MathHelper.clamp(dz, -.15, .15);
                base.fallDistance = 0;
                if (!isLadderFacingUp) {
                    // Use ladders like normal
                    if (dy < -.15) {
                        dy = -.15;
                    }
                    final boolean isPlayerGoingTowardsLadder = originalVelocityDirection.dot(ladderNormal) < -.1;
                    final boolean isPlayerSneakingOnLadder = base.isSneaking() && base instanceof EntityPlayer;
                    if (isPlayerSneakingOnLadder && dy < 0) {
                        dy = 0;
                    }
                    if (!collider.seperated && isPlayerGoingTowardsLadder) {
                        dy = .2;
                    }
                } else {
                    // Use ladders like monkey bars
                    dy = .2;
                }
                worldBelow = ladderCollision.getLeft();
                isPlayerOnLadder = true;
                break;
            }
        }
    }
    // endregion
    final Vector3dc velVec = new Vector3d(dx, dy, dz);
    for (Polygon poly : colPolys) {
        if (poly instanceof ShipPolygon) {
            ShipPolygon shipPoly = (ShipPolygon) poly;
            try {
                EntityPolygonCollider fast = new EntityPolygonCollider(playerBeforeMove, shipPoly, shipPoly.normals, velVec.add(total, new Vector3d()));
                if (!fast.arePolygonsSeparated()) {
                    // fastCollisions.add(fast);
                    worldBelow = shipPoly.shipFrom;
                    Vector3d response = fast.getCollisions()[fast.getMinDistanceIndex()].getResponse();
                    // TODO: Add more potential yResponses
                    double stepSquared = entity.stepHeight * entity.stepHeight;
                    // Do not do stair stepping if the player is on a ladder.
                    boolean isStep = isLiving && entity.onGround && !isPlayerOnLadder;
                    if (response.y >= 0 && VSMath.canStandOnNormal(fast.getCollisionAxes()[fast.getMinDistanceIndex()])) {
                        Vector3d slowButStopped = new Vector3d(0, -fast.getCollisions()[fast.getMinDistanceIndex()].getCollisionPenetrationDistance() / fast.getCollisionAxes()[fast.getMinDistanceIndex()].y(), 0);
                        response = slowButStopped;
                    }
                    if (isStep) {
                        EntityLivingBase living = (EntityLivingBase) entity;
                        if (Math.abs(living.moveForward) > .01 || Math.abs(living.moveStrafing) > .01) {
                            for (int i = 3; i < 6; i++) {
                                Vector3d tempResponse = fast.getCollisions()[i].getResponse();
                                if (tempResponse.y > 0 && VSMath.canStandOnNormal(fast.getCollisions()[i].getCollisionNormal()) && tempResponse.lengthSquared() < stepSquared) {
                                    if (tempResponse.lengthSquared() < .1) {
                                        // Too small to be a real step, let it through
                                        response = tempResponse;
                                    } else {
                                        // System.out.println("Try Stepping!");
                                        AxisAlignedBB axisalignedbb = entity.getEntityBoundingBox().offset(tempResponse.x, tempResponse.y, tempResponse.z);
                                        // Don't allow the player to step if the step will put them in another polygon.
                                        boolean collidesWithAnything = false;
                                        {
                                            final AxisAlignedBB newEntityBBShrunk = axisalignedbb.shrink(.15);
                                            final Polygon newEntityBBShrunkPolygon = new Polygon(newEntityBBShrunk);
                                            for (Polygon potentialStepCollision : colPolys) {
                                                if (potentialStepCollision == poly) {
                                                    // Don't run this on ourself
                                                    continue;
                                                }
                                                if (potentialStepCollision.getEnclosedAABB().intersects(newEntityBBShrunk)) {
                                                    // Finer check
                                                    ShipPolygon potentialStepCollisionShipPoly = (ShipPolygon) potentialStepCollision;
                                                    final EntityPolygonCollider checkIfStepCollidesWithBlock = new EntityPolygonCollider(newEntityBBShrunkPolygon, potentialStepCollisionShipPoly, potentialStepCollisionShipPoly.normals, new Vector3d());
                                                    checkIfStepCollidesWithBlock.processData();
                                                    if (!checkIfStepCollidesWithBlock.arePolygonsSeparated()) {
                                                        collidesWithAnything = true;
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                        if (!collidesWithAnything) {
                                            entity.setEntityBoundingBox(axisalignedbb);
                                            // I think this correct, but it may create more problems than it solves
                                            response.zero();
                                            entity.resetPositionToBB();
                                        }
                                    }
                                }
                            }
                        }
                    }
                    // total.add(response);
                    if (Math.abs(response.x) > .01D) {
                        total.x += response.x;
                    }
                    if (Math.abs(response.y) > .01D) {
                        total.y += response.y;
                    }
                    if (Math.abs(response.z) > .01D) {
                        total.z += response.z;
                    }
                    entity.posX += response.x;
                    entity.posY += response.y;
                    entity.posZ += response.z;
                    posOffestX += response.x;
                    posOffestY += response.y;
                    posOffestZ += response.z;
                    AxisAlignedBB axisalignedbb = entity.getEntityBoundingBox().offset(response.x, response.y, response.z);
                    entity.setEntityBoundingBox(axisalignedbb);
                    entity.resetPositionToBB();
                }
            } catch (Exception e) {
            // Do nothing
            }
        }
    }
    AxisAlignedBB axisalignedbb = entity.getEntityBoundingBox().offset(-posOffestX, -posOffestY, -posOffestZ);
    entity.setEntityBoundingBox(axisalignedbb);
    entity.resetPositionToBB();
    // We are on the ship that we are riding
    if (entity.ridingEntity instanceof EntityMountable) {
        final EntityMountable entityMountable = (EntityMountable) entity.ridingEntity;
        if (entityMountable.getReferencePosOptional().isPresent()) {
            final Optional<PhysicsObject> physicsObjectOptional = ValkyrienUtils.getPhysoManagingBlock(entity.world, entityMountable.getReferencePosOptional().get());
            if (physicsObjectOptional.isPresent()) {
                worldBelow = physicsObjectOptional.get();
            }
        }
    }
    if (worldBelow == null) {
        return null;
    }
    dx += total.x;
    dy += total.y;
    dz += total.z;
    boolean alreadyOnGround = entity.onGround && (dy == origDy) && origDy < 0;
    Vector3d original = new Vector3d(origDx, origDy, origDz);
    Vector3d newMov = new Vector3d(dx - origDx, dy - origDy, dz - origDz);
    entity.collidedHorizontally = original.dot(newMov) < 0;
    entity.collidedVertically = isDifSignificant(dy, origDy);
    entity.onGround = entity.collidedVertically && origDy < 0 || alreadyOnGround;
    entity.collided = entity.collidedHorizontally || entity.collidedVertically;
    // entity.resetPositionToBB();
    double motionYBefore = entity.motionY;
    float oldFallDistance = entity.fallDistance;
    Vector3d dxyz = new Vector3d(dx, dy, dz);
    ;
    Vector3d origDxyz = new Vector3d(origDx, origDy, origDz);
    Vector3d origPosXyz = new Vector3d(origPosX, origPosY, origPosZ);
    return new IntermediateMovementVariableStorage(dxyz, origDxyz, origPosXyz, alreadyOnGround, motionYBefore, oldFallDistance, worldBelow.getShipData());
}
Also used : EntityShipMovementData(org.valkyrienskies.mod.common.entity.EntityShipMovementData) AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) EnumFacing(net.minecraft.util.EnumFacing) EntityMountable(org.valkyrienskies.mod.common.entity.EntityMountable) World(net.minecraft.world.World) BlockPos(net.minecraft.util.math.BlockPos) Polygon(org.valkyrienskies.mod.common.collision.Polygon) ShipPolygon(org.valkyrienskies.mod.common.collision.ShipPolygon) IBlockState(net.minecraft.block.state.IBlockState) ShipPolygon(org.valkyrienskies.mod.common.collision.ShipPolygon) PhysicsObject(org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject) EntityPolygonCollider(org.valkyrienskies.mod.common.collision.EntityPolygonCollider) Vec3d(net.minecraft.util.math.Vec3d) Triple(org.apache.commons.lang3.tuple.Triple) Vector3dc(org.joml.Vector3dc) Vector3d(org.joml.Vector3d) ShipTransform(org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform) EntityLivingBase(net.minecraft.entity.EntityLivingBase) EntityPlayer(net.minecraft.entity.player.EntityPlayer) PhysPolygonCollider(org.valkyrienskies.mod.common.collision.PhysPolygonCollider) Nullable(javax.annotation.Nullable)

Example 19 with Vector3dc

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

the class ShipTransformationManager method createCollisionNormals.

private static Vector3dc[] createCollisionNormals(ShipTransform transform) {
    // We edit a local array instead of normals to avoid data races.
    final Vector3dc[] newNormals = new Vector3dc[15];
    // Used to generate Normals for the Axis Aligned World
    final Vector3dc[] alignedNorms = Polygon.generateAxisAlignedNorms();
    final Vector3dc[] rotatedNorms = generateRotationNormals(transform);
    for (int i = 0; i < 6; i++) {
        Vector3dc currentNorm;
        if (i < 3) {
            currentNorm = alignedNorms[i];
        } else {
            currentNorm = rotatedNorms[i - 3];
        }
        newNormals[i] = currentNorm;
    }
    int cont = 6;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            Vector3d norm = newNormals[i].cross(newNormals[j + 3], new Vector3d());
            norm.normalize();
            newNormals[cont] = norm;
            cont++;
        }
    }
    for (int i = 0; i < newNormals.length; i++) {
        if (newNormals[i].lengthSquared() < .01) {
            newNormals[i] = new Vector3d(0.0D, 1.0D, 0.0D);
        }
    }
    newNormals[0] = new Vector3d(1, 0, 0);
    newNormals[1] = new Vector3d(0, 1, 0);
    newNormals[2] = new Vector3d(0, 0, 1);
    return newNormals;
}
Also used : Vector3dc(org.joml.Vector3dc) Vector3d(org.joml.Vector3d)

Example 20 with Vector3dc

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

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