com.badlogic.gdx.math.collision.BoundingBox Java Examples

The following examples show how to use com.badlogic.gdx.math.collision.BoundingBox. 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: CameraController.java    From GdxDemo3D with Apache License 2.0 6 votes vote down vote up
public void setWorldBoundingBox(BoundingBox worldBoundingBox) {
	this.worldBoundingBox = new BoundingBox(worldBoundingBox);
	Vector3 min = new Vector3();
	Vector3 max = new Vector3();
	// Set height of bounding box to zero (y dimension)
	this.worldBoundingBox.getMax(max).y = 0;
	this.worldBoundingBox.getMin(min).y = 0;
	this.worldBoundingBox.set(min, max);

	ray.set(camera.targetPosition, camera.targetDirection);
	if (!Intersector.intersectRayBounds(ray, this.worldBoundingBox, worldGroundTarget)) {
		// TODO: What happens if the center of camera is not aimed at bounding box?
		// Probably move the camera until it is...
	}

}
 
Example #2
Source File: StillModel.java    From uracer-kotd with Apache License 2.0 5 votes vote down vote up
@Override
public void getBoundingBox (BoundingBox bbox) {
	bbox.inf();
	for (int i = 0; i < subMeshes.length; i++) {
		subMeshes[i].mesh.calculateBoundingBox(tmpBox);
		bbox.ext(tmpBox);
	}
}
 
Example #3
Source File: HeadlessModel.java    From gdx-proto with Apache License 2.0 5 votes vote down vote up
/** Extends the bounding box with the bounds of this model instance. This is a potential slow operation, it is advised to cache
 * the result.
 * @param out the {@link com.badlogic.gdx.math.collision.BoundingBox} that will be extended with the bounds.
 * @return the out parameter for chaining */
public BoundingBox extendBoundingBox (final BoundingBox out) {
	final int n = nodes.size;
	for (int i = 0; i < n; i++)
		nodes.get(i).extendBoundingBox(out);
	return out;
}
 
Example #4
Source File: LivingEntity.java    From Radix with MIT License 5 votes vote down vote up
public int getBlockInFeet(IWorld world) {
    int x = MathUtils.floor(getPosition().getX());
    int y = MathUtils.floor(getPosition().getY());
    int z = MathUtils.floor(getPosition().getZ());
    IChunk chunk = world.getChunk(x, z);
    if (chunk != null && y < world.getHeight()) {
        int cx = x & (world.getChunkSize() - 1);
        int cz = z & (world.getChunkSize() - 1);
        try {
            int id = chunk.getBlockId(cx, y, cz);
            Block block = RadixAPI.instance.getBlock(id);
            if (block == null) return 0;
            BoundingBox blockBox = block.calculateBoundingBox(chunk, cx, y, cz);
            float halfWidth = width / 2f;
            Vector3 bottomBackLeft = getPosition().cpy().add(-halfWidth, 0, -halfWidth);
            Vector3 bottomBackRight = bottomBackLeft.cpy().add(width, 0, 0);
            Vector3 bottomFrontRight = bottomBackRight.cpy().add(0, 0, width);
            Vector3 bottomFrontLeft = bottomBackLeft.cpy().add(width, 0, 0);

            boolean inFeet = blockBox.contains(bottomBackLeft) || blockBox.contains(bottomBackRight) || blockBox.contains(bottomFrontLeft) || blockBox.contains(bottomFrontRight);

            return inFeet ? id : 0;
        } catch (BlockStorage.CoordinatesOutOfBoundsException ex) {
            ex.printStackTrace();
            return 0;
        }
    } else {
        return 0;
    }
}
 
Example #5
Source File: MovementHandler.java    From Radix with MIT License 5 votes vote down vote up
public boolean checkDeltaCollision(LivingEntity e, float deltaX, float deltaY, float deltaZ) {
    BoundingBox curBB = e.calculateBoundingBox();
    BoundingBox newBB = new BoundingBox(curBB.min.cpy().add(deltaX, deltaY, deltaZ), curBB.max.cpy().add(deltaX, deltaY, deltaZ));

    boolean collideSuccess = false;

    int x = MathUtils.floor(e.getPosition().x);
    int y = MathUtils.floor(e.getPosition().y);
    int z = MathUtils.floor(e.getPosition().z);

    IChunk chunk = game.getWorld().getChunk(x, z);
    if (chunk == null)
        return true;

    int cx = x & (game.getWorld().getChunkSize() - 1);
    int cz = z & (game.getWorld().getChunkSize() - 1);
    try {
        Block block = chunk.getBlock(cx, y, cz);

        for (Vector3 corner : getCorners(newBB)) {
            collideSuccess = collideSuccess || checkCollision(corner);
        }

        return collideSuccess ||
                (block != null && block.isSolid()
                        && block.calculateBoundingBox(chunk, cx, y, cz).intersects(newBB));
    } catch(CoordinatesOutOfBoundsException ex) {
        ex.printStackTrace();
        return true;
    }
}
 
Example #6
Source File: Liquid.java    From Radix with MIT License 5 votes vote down vote up
@Override
public BoundingBox calculateBoundingBox(IChunk c, int x, int y, int z) {
    short metadata = 0;
    try {
        metadata = c.getMeta(x, y, z);
    } catch (CoordinatesOutOfBoundsException e) {
        e.printStackTrace();
    }

    return new BoundingBox(new Vector3(c.getStartPosition().x+x, y, c.getStartPosition().z+z),
            new Vector3(c.getStartPosition().x+x+1, y+getHeight(metadata), c.getStartPosition().z+z+1));
}
 
Example #7
Source File: Snow.java    From Radix with MIT License 5 votes vote down vote up
@Override
public BoundingBox calculateBoundingBox(IChunk c, int x, int y, int z) {
    short metadata = 0;
    try {
        metadata = c.getMeta(x, y, z);
    } catch (CoordinatesOutOfBoundsException e) {
        e.printStackTrace();
    }

    return new BoundingBox(new Vector3(c.getStartPosition().x+x, y, c.getStartPosition().z+z),
            new Vector3(c.getStartPosition().x+x+1, y+getHeight(metadata), c.getStartPosition().z+z+1));
}
 
Example #8
Source File: Fence.java    From Radix with MIT License 4 votes vote down vote up
@Override
public BoundingBox calculateBoundingBox(IChunk c, int x, int y, int z) {
    float x1 = c.getStartPosition().x+x+0.5f-R;
    float z1 = c.getStartPosition().z+z+0.5f-R;
    float x2 = x1+WIDTH;
    float z2 = z1+WIDTH;
    for(Side side : Side.values()) {
        if(side == Side.TOP || side == Side.BOTTOM)
            continue;
        int sx = x;
        int sz = z;
        IChunk sChunk = c;
        switch(side) {
            case EAST:
                sx += 1;
                break;
            case WEST:
                sx -= 1;
                break;
            case NORTH:
                sz += 1;
                break;
            case SOUTH:
                sz -= 1;
                break;
        }
        if (sz < 0) {
            sChunk = c.getWorld().getChunk(c.getStartPosition().x+sx, c.getStartPosition().z+sz);
            sz += c.getWorld().getChunkSize();
        } else if (sz > c.getWorld().getChunkSize() - 1) {
            sChunk = c.getWorld().getChunk(c.getStartPosition().x+sx, c.getStartPosition().z+sz);
            sz -= c.getWorld().getChunkSize();
        }
        if (sx < 0) {
            sChunk = c.getWorld().getChunk(c.getStartPosition().x+sx, c.getStartPosition().z+sz);
            sx += c.getWorld().getChunkSize();
        } else if (sx > c.getWorld().getChunkSize() - 1) {
            sChunk = c.getWorld().getChunk(c.getStartPosition().x+sx, c.getStartPosition().z+sz);
            sx -= c.getWorld().getChunkSize();
        }

        if (sChunk == null)
            continue;

        try {
            Block sBlock = sChunk.getBlock(sx, y, sz);
            if (sBlock != null) {
                if (sBlock.isSolid()) {
                    switch (side) {
                        case EAST:
                            x2 = c.getStartPosition().x + x + 1;
                            break;
                        case WEST:
                            x1 = c.getStartPosition().x + x;
                            break;
                        case NORTH:
                            z2 = c.getStartPosition().z + z + 1;
                            break;
                        case SOUTH:
                            z1 = c.getStartPosition().z + z;
                            break;
                    }
                }
            }
        } catch (CoordinatesOutOfBoundsException ex) {
            ex.printStackTrace();
        }
    }
    Vector3 corner1 = new Vector3(x1, c.getStartPosition().y+y, z1);
    Vector3 corner2 = new Vector3(x2, c.getStartPosition().y+y+1.5f, z2);
    return new BoundingBox(corner1, corner2);
}
 
Example #9
Source File: BulletConstructor.java    From gdx-ai with Apache License 2.0 4 votes vote down vote up
/** Creates a btBoxShape with the same dimensions as the shape. */
public BulletConstructor (final Model model, final float mass) {
	final BoundingBox boundingBox = new BoundingBox();
	model.calculateBoundingBox(boundingBox);
	create(model, mass, boundingBox.getWidth(), boundingBox.getHeight(), boundingBox.getDepth());
}
 
Example #10
Source File: HeadlessModel.java    From gdx-proto with Apache License 2.0 4 votes vote down vote up
/** Calculate the bounding box of this model instance. This is a potential slow operation, it is advised to cache the result.
 * @param out the {@link com.badlogic.gdx.math.collision.BoundingBox} that will be set with the bounds.
 * @return the out parameter for chaining */
public BoundingBox calculateBoundingBox (final BoundingBox out) {
	out.inf();
	return extendBoundingBox(out);
}
 
Example #11
Source File: Actor3d.java    From Scene3d with Apache License 2.0 4 votes vote down vote up
public void setBoundingBox(BoundingBox box){
	boundBox = box;
}
 
Example #12
Source File: Actor3d.java    From Scene3d with Apache License 2.0 4 votes vote down vote up
public BoundingBox getBoundingBox(){
	return boundBox;
}
 
Example #13
Source File: MovementHandler.java    From Radix with MIT License 4 votes vote down vote up
private Vector3[] getCorners(BoundingBox bb) {
    return new Vector3[]{bb.getCorner000(new Vector3()), bb.getCorner001(new Vector3()), bb.getCorner010(new Vector3()), bb.getCorner011(new Vector3()), bb.getCorner100(new Vector3()), bb.getCorner101(new Vector3()), bb.getCorner110(new Vector3()), bb.getCorner111(new Vector3())};
}
 
Example #14
Source File: LivingEntity.java    From Radix with MIT License 4 votes vote down vote up
public BoundingBox calculateBoundingBox() {
    float halfWidth = width / 2f;
    return new BoundingBox(getPosition().cpy().add(-halfWidth, 0, -halfWidth), getPosition().cpy().add(halfWidth, height, halfWidth));
}
 
Example #15
Source File: StillSubMesh.java    From uracer-kotd with Apache License 2.0 4 votes vote down vote up
@Override
public void getBoundingBox (BoundingBox bbox) {
	mesh.calculateBoundingBox(bbox);
}
 
Example #16
Source File: DirectionalShadowLight.java    From gdx-gltf with Apache License 2.0 3 votes vote down vote up
public DirectionalShadowLight setBounds(BoundingBox box){
	float w = box.getWidth();
	float h = box.getHeight();
	float d = box.getDepth();
	
	float s = Math.max(Math.max(w, h), d);
	
	w = h = d = s * SQRT2;
	
	box.getCenter(center);
	
	return setViewport(w, h, 0, d);
}
 
Example #17
Source File: Block.java    From Radix with MIT License 2 votes vote down vote up
/**
 * Get the bounding box of an instance of the block at the specified coordinates
 *
 * @param c Chunk the block is in
 * @param x Chunk-relative x position of the block. 0->(chunkWidth-1) inclusive.
 * @param y Chunk-relative y position of the block. 0->(chunkHeight-1) inclusive.
 * @param z Chunk-relative z position of the block. 0->(chunkWidth-1) inclusive.
 */
public BoundingBox calculateBoundingBox(IChunk c, int x, int y, int z) {
    Vector3 corner1 = new Vector3(c.getStartPosition().x+x, c.getStartPosition().y+y, c.getStartPosition().z+z);
    Vector3 corner2 = corner1.cpy().add(1, 1, 1);
    return new BoundingBox(corner1, corner2);
}
 
Example #18
Source File: SubMesh.java    From uracer-kotd with Apache License 2.0 2 votes vote down vote up
/** Obtain the {@link BoundingBox} of this {@link SubMesh}.
 * 
 * @param bbox This {@link BoundingBox} will be modified so that its contain values that are the bounding box for this SubMesh. */
public abstract void getBoundingBox (BoundingBox bbox);
 
Example #19
Source File: Model.java    From uracer-kotd with Apache License 2.0 2 votes vote down vote up
/** Generates the bounding box for the Model.<br />
 * <br />
 * For every finite 3D object there exists a box that can enclose the object. This function sets the give {@link BoundingBox}
 * to be one such enclosing box.<br />
 * Bounding boxes are useful for very basic collision detection amongst other tasks.
 * @param bbox The provided {@link BoundingBox} will have its internal values correctly set. (To allow Java Object reuse) */
public void getBoundingBox (BoundingBox bbox);