Java Code Examples for codechicken.lib.math.MathHelper#between()

The following examples show how to use codechicken.lib.math.MathHelper#between() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Vector3.java    From CodeChickenLib with GNU Lesser General Public License v2.1 6 votes vote down vote up
public Vector3 XYintercept(Vector3 end, double pz) {
    double dx = end.x - x;
    double dy = end.y - y;
    double dz = end.z - z;

    if (dz == 0)
        return null;

    double d = (pz - z) / dz;
    if (MathHelper.between(-1E-5, d, 1E-5))
        return this;

    if (!MathHelper.between(0, d, 1))
        return null;

    x += d * dx;
    y += d * dy;
    z = pz;
    return this;
}
 
Example 2
Source File: Vector3.java    From CodeChickenLib with GNU Lesser General Public License v2.1 6 votes vote down vote up
public Vector3 XZintercept(Vector3 end, double py) {
    double dx = end.x - x;
    double dy = end.y - y;
    double dz = end.z - z;

    if (dy == 0)
        return null;

    double d = (py - y) / dy;
    if (MathHelper.between(-1E-5, d, 1E-5))
        return this;

    if (!MathHelper.between(0, d, 1))
        return null;

    x += d * dx;
    y = py;
    z += d * dz;
    return this;
}
 
Example 3
Source File: Vector3.java    From CodeChickenLib with GNU Lesser General Public License v2.1 6 votes vote down vote up
public Vector3 YZintercept(Vector3 end, double px) {
    double dx = end.x - x;
    double dy = end.y - y;
    double dz = end.z - z;

    if (dx == 0)
        return null;

    double d = (px - x) / dx;
    if (MathHelper.between(-1E-5, d, 1E-5))
        return this;

    if (!MathHelper.between(0, d, 1))
        return null;

    x = px;
    y += d * dy;
    z += d * dz;
    return this;
}
 
Example 4
Source File: BlockTranslocator.java    From Translocators with MIT License 6 votes vote down vote up
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) {
    if (world.isRemote)
        return true;

    MovingObjectPosition hit = RayTracer.retraceBlock(world, player, x, y, z);
    TileTranslocator ttrans = (TileTranslocator) world.getTileEntity(x, y, z);

    if (hit != null) {
        if (hit.subHit < 6) {
            Vector3 vhit = new Vector3(hit.hitVec);
            vhit.add(-x - 0.5, -y - 0.5, -z - 0.5);
            vhit.apply(sideRotations[hit.subHit % 6].inverse());
            if (MathHelper.between(-2 / 16D, vhit.x, 2 / 16D) && MathHelper.between(-2 / 16D, vhit.z, 2 / 16D))
                hit.subHit += 6;
        }

        return ttrans.attachments[hit.subHit % 6].activate(player, hit.subHit / 6);
    }

    return false;
}
 
Example 5
Source File: Vector3.java    From CodeChickenLib with GNU Lesser General Public License v2.1 6 votes vote down vote up
public Vector3 YZintercept(Vector3 end, double px) {
    double dx = end.x - x;
    double dy = end.y - y;
    double dz = end.z - z;

    if (dx == 0) {
        return null;
    }

    double d = (px - x) / dx;
    if (MathHelper.between(-1E-5, d, 1E-5)) {
        return this;
    }

    if (!MathHelper.between(0, d, 1)) {
        return null;
    }

    x = px;
    y += d * dy;
    z += d * dz;
    return this;
}
 
Example 6
Source File: Vector3.java    From CodeChickenLib with GNU Lesser General Public License v2.1 6 votes vote down vote up
public Vector3 XZintercept(Vector3 end, double py) {
    double dx = end.x - x;
    double dy = end.y - y;
    double dz = end.z - z;

    if (dy == 0) {
        return null;
    }

    double d = (py - y) / dy;
    if (MathHelper.between(-1E-5, d, 1E-5)) {
        return this;
    }

    if (!MathHelper.between(0, d, 1)) {
        return null;
    }

    x += d * dx;
    y = py;
    z += d * dz;
    return this;
}
 
Example 7
Source File: Vector3.java    From CodeChickenLib with GNU Lesser General Public License v2.1 6 votes vote down vote up
public Vector3 XYintercept(Vector3 end, double pz) {
    double dx = end.x - x;
    double dy = end.y - y;
    double dz = end.z - z;

    if (dz == 0) {
        return null;
    }

    double d = (pz - z) / dz;
    if (MathHelper.between(-1E-5, d, 1E-5)) {
        return this;
    }

    if (!MathHelper.between(0, d, 1)) {
        return null;
    }

    x += d * dx;
    y += d * dy;
    z = pz;
    return this;
}
 
Example 8
Source File: TileTranslocatorRenderer.java    From Translocators with MIT License 6 votes vote down vote up
private void renderLink(int src, int dst, double time, int x, int y, int z)
{
    double d = ((time+src+dst*2)%10)/6;
    //0 is head
    for(int n = 0; n < 20; n++)
    {
        double dn = d-n*0.1;
        int spriteX = (int) (7-n*1.5-d*2);
        if(!MathHelper.between(0, dn, 1) || spriteX < 0)
            continue;
        
        Vector3 pos = getPath(src, dst, dn).add(x, y, z);
        double b = 1;//d*0.6+0.4;
        double s = 1;//d*0.6+0.4;
        
        double u1 = spriteX/8D;
        double u2 = u1+1/8D;
        double v1 = 0;
        double v2 = 1;
        
        RenderParticle.render(pos.x, pos.y, pos.z, gradient.getColour((dn-0.5)*1.2+0.5).multiplyC(b), s*0.12, u1, v1, u2, v2);
    }
}
 
Example 9
Source File: IconVertexRangeUVTransform.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Retrieves a TextureAtlasSprite from the internal map based on a vertex index.
 *
 * @param index The vertex index.
 * @return Returns the Icon, Null if no transform for the vertex index.
 */
public TextureAtlasSprite getSpriteForVertexIndex(int index) {
    for (Triple<Integer, Integer, TextureAtlasSprite> entry : transformMap) {
        if (MathHelper.between(entry.getLeft(), index, entry.getMiddle())) {
            return entry.getRight();
        }
    }
    return null;
}
 
Example 10
Source File: PacketCustom.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
public PacketCustom(ResourceLocation channel, int type) {
    super(Unpooled.buffer());
    if (!MathHelper.between(0, type, 255)) {
        throw new RuntimeException("Invalid packet type, Must be between 0 and 255. Got: " + type);
    }
    this.channel = channel;
    this.type = type;
    writeByte(type);
}
 
Example 11
Source File: IconVertexRangeUVTransform.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Reserves a range for a specific transform.
 *
 * @param start  The start index to reserve.
 * @param end    The end vertex to reserve.
 * @param sprite The sprite to transform to in the range.
 * @return The same instance of the builder.
 * @throws IllegalArgumentException Thrown if the specified vertex range is already reserved.
 */
public Builder addTransform(int start, int end, TextureAtlasSprite sprite) {
    for (Triple<Integer, Integer, TextureAtlasSprite> entry : transformMap) {
        if (MathHelper.between(entry.getLeft(), start, entry.getMiddle()) || MathHelper.between(entry.getLeft(), end, entry.getMiddle())) {
            throw new IllegalArgumentException("Unable to have overlapping sprite transforms!");
        }
    }
    transformMap.add(Triple.of(start, end, sprite));
    return this;
}
 
Example 12
Source File: IndexedCuboid6.java    From CodeChickenLib with GNU Lesser General Public License v2.1 4 votes vote down vote up
public CuboidRayTraceResult calculateIntercept(Vector3 start, Vector3 end) {
    Vector3 hit = null;
    Direction sideHit = null;
    double dist = Double.MAX_VALUE;

    for (Direction face : Direction.BY_INDEX) {
        Vector3 suspectHit = null;
        switch (face) {
            case DOWN:
                suspectHit = start.copy().XZintercept(end, min.y);
                break;
            case UP:
                suspectHit = start.copy().XZintercept(end, max.y);
                break;
            case NORTH:
                suspectHit = start.copy().XYintercept(end, min.z);
                break;
            case SOUTH:
                suspectHit = start.copy().XYintercept(end, max.z);
                break;
            case WEST:
                suspectHit = start.copy().YZintercept(end, min.x);
                break;
            case EAST:
                suspectHit = start.copy().YZintercept(end, max.x);
                break;
        }

        if (suspectHit == null) {
            continue;
        }

        switch (face) {

            case DOWN:
            case UP:
                if (!MathHelper.between(min.x, suspectHit.x, max.x) || !MathHelper.between(min.z, suspectHit.z, max.z)) {
                    continue;
                }
                break;
            case NORTH:
            case SOUTH:
                if (!MathHelper.between(min.x, suspectHit.x, max.x) || !MathHelper.between(min.y, suspectHit.y, max.y)) {
                    continue;
                }
                break;
            case WEST:
            case EAST:
                if (!MathHelper.between(min.y, suspectHit.y, max.y) || !MathHelper.between(min.z, suspectHit.z, max.z)) {
                    continue;
                }
                break;
        }
        double suspectDist = suspectHit.copy().subtract(start).magSquared();
        if (suspectDist < dist) {
            sideHit = face;
            dist = suspectDist;
            hit = suspectHit;
        }
    }

    if (sideHit != null && hit != null) {
        return new CuboidRayTraceResult(hit, sideHit, false, this, dist);
    }
    return null;
}
 
Example 13
Source File: Rotation.java    From CodeChickenLib with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public boolean isRedundant() {
    return MathHelper.between(-1E-5, angle, 1E-5);
}
 
Example 14
Source File: RayTracer.java    From CodeChickenLib with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void traceSide(int side, Vector3 start, Vector3 end, Cuboid6 cuboid) {
    vec.set(start);
    Vector3 hit = null;
    switch (side) {
        case 0:
            hit = vec.XZintercept(end, cuboid.min.y);
            break;
        case 1:
            hit = vec.XZintercept(end, cuboid.max.y);
            break;
        case 2:
            hit = vec.XYintercept(end, cuboid.min.z);
            break;
        case 3:
            hit = vec.XYintercept(end, cuboid.max.z);
            break;
        case 4:
            hit = vec.YZintercept(end, cuboid.min.x);
            break;
        case 5:
            hit = vec.YZintercept(end, cuboid.max.x);
            break;
    }
    if (hit == null)
        return;

    switch (side) {
        case 0:
        case 1:
            if (!MathHelper.between(cuboid.min.x, hit.x, cuboid.max.x) || !MathHelper.between(cuboid.min.z, hit.z, cuboid.max.z))
                return;
            break;
        case 2:
        case 3:
            if (!MathHelper.between(cuboid.min.x, hit.x, cuboid.max.x) || !MathHelper.between(cuboid.min.y, hit.y, cuboid.max.y))
                return;
            break;
        case 4:
        case 5:
            if (!MathHelper.between(cuboid.min.y, hit.y, cuboid.max.y) || !MathHelper.between(cuboid.min.z, hit.z, cuboid.max.z))
                return;
            break;
    }

    double dist = vec2.set(hit).subtract(start).magSquared();
    if (dist < s_dist) {
        s_side = side;
        s_dist = dist;
        s_vec.set(vec);
    }
}
 
Example 15
Source File: UVTranslation.java    From CodeChickenLib with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public boolean isRedundant() {
    return MathHelper.between(-1E-5, du, 1E-5) && MathHelper.between(-1E-5, dv, 1E-5);
}
 
Example 16
Source File: Rotation.java    From CodeChickenLib with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public boolean isRedundant() {
    return MathHelper.between(-1E-5, angle, 1E-5);
}
 
Example 17
Source File: UVTranslation.java    From CodeChickenLib with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public boolean isRedundant() {
    return MathHelper.between(-1E-5, du, 1E-5) && MathHelper.between(-1E-5, dv, 1E-5);
}
 
Example 18
Source File: UVRotation.java    From CodeChickenLib with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public boolean isRedundant() {
    return MathHelper.between(-1E-5, angle, 1E-5);
}
 
Example 19
Source File: Vector3.java    From CodeChickenLib with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Equals method with tolerance
 *
 * @return true if this is equal to v within +-1E-5
 */
public boolean equalsT(Vector3 v) {
    return MathHelper.between(x - 1E-5, v.x, x + 1E-5) &&
            MathHelper.between(y - 1E-5, v.y, y + 1E-5) &&
            MathHelper.between(z - 1E-5, v.z, z + 1E-5);
}
 
Example 20
Source File: Vector3.java    From CodeChickenLib with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Equals method with tolerance
 *
 * @return true if this is equal to v within +-1E-5
 */
public boolean equalsT(Vector3 v) {
    return MathHelper.between(x - 1E-5, v.x, x + 1E-5) && MathHelper.between(y - 1E-5, v.y, y + 1E-5) && MathHelper.between(z - 1E-5, v.z, z + 1E-5);
}