Java Code Examples for com.badlogic.gdx.math.Vector2#dst2()

The following examples show how to use com.badlogic.gdx.math.Vector2#dst2() . 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: VanishEffectFactory.java    From Klooni1010 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setInfo(Cell deadCell, Vector2 culprit) {
    cell = deadCell;

    vanishSize = cell.size;
    vanishColor = cell.getColorCopy();
    vanishLifetime = 1f;

    // The vanish distance is this measure (distanceĀ² + sizeĀ³ * 20% size)
    // because it seems good enough. The more the distance, the more the
    // delay, but we decrease the delay depending on the cell size too or
    // it would be way too high
    Vector2 center = new Vector2(cell.pos.x + cell.size * 0.5f, cell.pos.y + 0.5f);
    float vanishDist = Vector2.dst2(
            culprit.x, culprit.y, center.x, center.y) / ((float) Math.pow(cell.size, 4.0f) * 0.2f);

    // Negative time = delay, + 0.4*lifetime because elastic interpolation has that delay
    vanishElapsed = vanishLifetime * 0.4f - vanishDist;
}
 
Example 2
Source File: CreatureAction.java    From dice-heroes with GNU General Public License v3.0 6 votes vote down vote up
protected final Array<Grid2D.Coordinate> coordinates(Creature creature, float radius, ICondition<Grid2D.Coordinate> condition) {

        int checkRadius = MathUtils.ceil(radius);
        float radius2 = radius * radius;

        Vector2 position = tmpVector.set(creature.getX(), creature.getY());
        Array<Grid2D.Coordinate> result = new Array<Grid2D.Coordinate>();

        for (int i = creature.getX() - checkRadius; i <= creature.getX() + checkRadius; i++) {
            for (int j = creature.getY() - checkRadius; j <= creature.getY() + checkRadius; j++) {

                if (position.dst2(i, j) > radius2)
                    continue;
                Grid2D.Coordinate coordinate = Grid2D.obtain(i, j);
                if (condition.isSatisfied(coordinate)) {
                    result.add(coordinate);
                } else {
                    Grid2D.free(coordinate);
                }
            }
        }
        return result;
    }
 
Example 3
Source File: WorldBodyUtils.java    From killingspree with MIT License 6 votes vote down vote up
public ArrayList<Vector2> getPlayers(Vector2 point, float distance) {
    ArrayList<Vector2> playersPosition = new ArrayList<Vector2>();
    distance *= distance;
    for (ServerPlayer player: worldManager.playerList.values()) {
        Vector2 position = player.body.getPosition();
        if (point.dst2(position.x, position.y) < distance) {
            playersPosition.add(tempPlayerPosition.set(position.x, position.y));
        }
        else if (point.dst2(position.x + WorldRenderer.VIEWPORT_WIDTH, position.y) < distance) {
            playersPosition.add(tempPlayerPosition.set(position.x + WorldRenderer.VIEWPORT_WIDTH, position.y));
        }
        else if (point.dst2(position.x - WorldRenderer.VIEWPORT_WIDTH, position.y) < distance) {
            playersPosition.add(tempPlayerPosition.set(position.x - WorldRenderer.VIEWPORT_WIDTH, position.y));
            
        }
        else if (point.dst2(position.x, position.y + WorldRenderer.VIEWPORT_HEIGHT) < distance) {
            playersPosition.add(tempPlayerPosition.set(position.x, position.y + WorldRenderer.VIEWPORT_HEIGHT));
        }
        else if (point.dst2(position.x, position.y - WorldRenderer.VIEWPORT_HEIGHT) < distance) {
            playersPosition.add(tempPlayerPosition.set(position.x, position.y - WorldRenderer.VIEWPORT_HEIGHT));
        }
    }
    return playersPosition;
}
 
Example 4
Source File: VMath.java    From uracer-kotd with Apache License 2.0 5 votes vote down vote up
public static boolean isBetween (Vector2 a, Vector2 b, Vector2 c) {
	float dotproduct = (c.x - a.x) * (b.x - a.x) + (c.y - a.y) * (b.y - a.y);
	if (dotproduct < 0) {
		return false;
	}

	float squaredlengthba = a.dst2(b);
	if (dotproduct > squaredlengthba) {
		return false;
	}

	return true;
}
 
Example 5
Source File: Field.java    From homescreenarcade with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Checks whether the ball appears to be stuck, and nudges it if so.
 */
void checkForStuckBall(long nanos) {
    // Only do this for single balls. This means it's theoretically possible for multiple
    // balls to be simultaneously stuck during multiball; that would be impressive.
    if (this.getBalls().size() != 1) {
        nanosSinceBallMoved = -1;
        return;
    }
    Ball ball = this.getBalls().get(0);
    Vector2 pos = ball.getPosition();
    if (nanosSinceBallMoved < 0) {
        // New ball.
        lastBallPositionX = pos.x;
        lastBallPositionY = pos.y;
        nanosSinceBallMoved = 0;
        return;
    }
    if (ball.getLinearVelocity().len2() > 0.01f ||
            pos.dst2(lastBallPositionX, lastBallPositionY) > 0.01f) {
        // Ball has moved since last time; reset counter.
        lastBallPositionX = pos.x;
        lastBallPositionY = pos.y;
        nanosSinceBallMoved = 0;
        return;
    }
    // Don't add time if any flipper is activated (the flipper could be trapping the ball).
    List<FlipperElement> flippers = this.getFlipperElements();
    for (int i=0; i<flippers.size(); i++) {
        if (flippers.get(i).isFlipperEngaged()) return;
    }
    // Increment time counter and bump if the ball hasn't moved in a while.
    nanosSinceBallMoved += nanos;
    if (nanosSinceBallMoved > STUCK_BALL_NANOS) {
        showGameMessage("Bump!", 1000);
        // Could make the bump impulse table-specific if needed.
        Vector2 impulse = new Vector2(RAND.nextBoolean() ? 1f : -1f, 1.5f);
        ball.applyLinearImpulse(impulse);
        nanosSinceBallMoved = 0;
    }
}
 
Example 6
Source File: PolygonUtils.java    From bladecoder-adventure-engine with Apache License 2.0 4 votes vote down vote up
public static boolean isPointInside(Polygon polygon, float x, float y,
		boolean toleranceOnOutside) {
	float verts[] = polygon.getTransformedVertices();

	boolean inside = false;

	float oldX = verts[verts.length - 2];
	float oldY = verts[verts.length - 1];

	float oldSqDist = Vector2.dst2(oldX, oldY, x, y);

	for (int i = 0; i < verts.length; i += 2) {
		float newX = verts[i];
		float newY = verts[i + 1];
		float newSqDist = Vector2.dst2(newX, newY, x, y);

		if (oldSqDist + newSqDist + 2.0f * Math.sqrt(oldSqDist * newSqDist)
				- Vector2.dst2(newX, newY, oldX, oldY) < TOLERANCE_IS_POINT_INSIDE)
			return toleranceOnOutside;

		float leftX = newX;
		float leftY = newY;
		float rightX = oldX;
		float rightY = oldY;

		if (newX > oldX) {
			leftX = oldX;
			leftY = oldY;
			rightX = newX;
			rightY = newY;
		}

		if (leftX < x
				&& x <= rightX
				&& (y - leftY) * (rightX - leftX) < (rightY - leftY)
						* (x - leftX))
			inside = !inside;

		oldX = newX;
		oldY = newY;
		oldSqDist = newSqDist;
	}

	return inside;
}