Search in sources :

Example 11 with Polygon

use of org.valkyrienskies.mod.common.collision.Polygon in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class PhysicsObject method getPhysicsTransformAABB.

public AxisAlignedBB getPhysicsTransformAABB() {
    AxisAlignedBB subspaceBB = getBlockPositions().makeAABB();
    if (subspaceBB == null) {
        // The aabbMaker didn't know what the aabb was, just don't update the aabb for now.
        return null;
    }
    // Expand subspaceBB by 1 to fit the block grid.
    subspaceBB = subspaceBB.expand(1, 1, 1);
    // Now transform the subspaceBB to world coordinates
    Polygon largerPoly = new Polygon(subspaceBB, getShipTransformationManager().getCurrentPhysicsTransform(), TransformType.SUBSPACE_TO_GLOBAL);
    // Set the ship AABB to that of the polygon.
    AxisAlignedBB worldBB = largerPoly.getEnclosedAABB();
    return worldBB;
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) Polygon(org.valkyrienskies.mod.common.collision.Polygon)

Example 12 with Polygon

use of org.valkyrienskies.mod.common.collision.Polygon in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class ValkyrienUtils method transformEntity.

/**
 * Applies the given transform matrix to the position/velocity/look of the given entity.
 * @param transform The transform matrix to be applied.
 * @param entity The entity that will be transformed.
 */
public void transformEntity(final Matrix4dc transform, final Entity entity, final boolean transformEntityBoundingBox) {
    Vec3d entityLookMc = entity.getLook(1.0F);
    Vector3d entityPos = new Vector3d(entity.posX, entity.posY, entity.posZ);
    Vector3d entityLook = new Vector3d(entityLookMc.x, entityLookMc.y, entityLookMc.z);
    Vector3d entityMotion = new Vector3d(entity.motionX, entity.motionY, entity.motionZ);
    if (entity instanceof EntityFireball) {
        EntityFireball ball = (EntityFireball) entity;
        entityMotion.x = ball.accelerationX;
        entityMotion.y = ball.accelerationY;
        entityMotion.z = ball.accelerationZ;
    }
    transform.transformPosition(entityPos);
    transform.transformDirection(entityLook);
    transform.transformDirection(entityMotion);
    entityLook.normalize();
    // This is correct, works properly when tested with cows
    if (entity instanceof EntityLiving) {
        EntityLiving living = (EntityLiving) entity;
        living.rotationYawHead = entity.rotationYaw;
        living.prevRotationYawHead = entity.rotationYaw;
    }
    // Get the player pitch/yaw from the look vector
    final Tuple<Double, Double> pitchYawTuple = VSMath.getPitchYawFromVector(new Vector3d(entityLook.x, entityLook.y, entityLook.z));
    entity.rotationPitch = pitchYawTuple.getFirst().floatValue();
    entity.rotationYaw = pitchYawTuple.getSecond().floatValue();
    if (entity instanceof EntityFireball) {
        EntityFireball ball = (EntityFireball) entity;
        ball.accelerationX = entityMotion.x;
        ball.accelerationY = entityMotion.y;
        ball.accelerationZ = entityMotion.z;
    }
    entity.motionX = entityMotion.x;
    entity.motionY = entityMotion.y;
    entity.motionZ = entityMotion.z;
    if (transformEntityBoundingBox) {
        // Transform the bounding box too
        final AxisAlignedBB oldBB = entity.getEntityBoundingBox();
        final Polygon newBBPoly = new Polygon(oldBB, transform);
        final AxisAlignedBB newBB = newBBPoly.getEnclosedAABB();
        final double oldBBSize = (oldBB.maxX - oldBB.minX) * (oldBB.maxY - oldBB.minY) * (oldBB.maxZ - oldBB.minZ);
        final double newBBSize = (newBB.maxX - newBB.minX) * (newBB.maxY - newBB.minY) * (newBB.maxZ - newBB.minZ);
        final double scaleFactor = Math.pow(oldBBSize / newBBSize, 1.0 / 3.0);
        // Scale the bounding box such that the new bounding box is the same size as the old bounding box.
        final AxisAlignedBB newBBScaled = newBB.grow((scaleFactor - 1) * (newBB.maxX - newBB.minX) / 2.0, (scaleFactor - 1) * (newBB.maxY - newBB.minY) / 2.0, (scaleFactor - 1) * (newBB.maxZ - newBB.minZ) / 2.0);
        entity.setPosition(entityPos.x, entityPos.y, entityPos.z);
        entity.setEntityBoundingBox(newBBScaled);
    } else {
        // Just use the regular bounding box
        entity.setPosition(entityPos.x, entityPos.y, entityPos.z);
    }
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) EntityLiving(net.minecraft.entity.EntityLiving) Vector3d(org.joml.Vector3d) Polygon(org.valkyrienskies.mod.common.collision.Polygon) Vec3d(net.minecraft.util.math.Vec3d) EntityFireball(net.minecraft.entity.projectile.EntityFireball)

Aggregations

Polygon (org.valkyrienskies.mod.common.collision.Polygon)12 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)7 ShipPolygon (org.valkyrienskies.mod.common.collision.ShipPolygon)7 PhysicsObject (org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject)7 EntityPlayer (net.minecraft.entity.player.EntityPlayer)4 World (net.minecraft.world.World)4 Vector3d (org.joml.Vector3d)4 ArrayList (java.util.ArrayList)3 IBlockState (net.minecraft.block.state.IBlockState)2 BlockPos (net.minecraft.util.math.BlockPos)2 Vec3d (net.minecraft.util.math.Vec3d)2 Triple (org.apache.commons.lang3.tuple.Triple)2 Inject (org.spongepowered.asm.mixin.injection.Inject)2 EntityPolygonCollider (org.valkyrienskies.mod.common.collision.EntityPolygonCollider)2 ShipTransform (org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform)2 IPhysObjectWorld (org.valkyrienskies.mod.common.ships.ship_world.IPhysObjectWorld)2 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 Entity (net.minecraft.entity.Entity)1 EntityLiving (net.minecraft.entity.EntityLiving)1