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

The following are Jave code examples for showing how to use sub() of the com.badlogic.gdx.math.Vector2 class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: cocos2d-java   File: Segment.java   Source Code and License Vote up 5 votes
public boolean testSegment(RaycastResult out, Segment segment, float maxLambda) {
		Vector2 s = segment.p1;
		Vector2 r = tlR.get().set(segment.p2);
//		r.subLocal(s);
		r.sub(s);
		Vector2 d = tlD.get().set(p2);
//		d.subLocal(p1);
		d.sub(p1);
		Vector2 n = tlN.get();
		V2.crossToOut(d, 1.0f, n);
		Vector2 b = tlB.get();

		float k_slop = 100.0f * Settings.EPSILON;
		float denom = -V2.dot(r, n);

		// Cull back facing collision and ignore parallel segments.
		if (denom > k_slop){
			
			// Does the segment intersect the infinite line associated with this segment?
			b.set(s);
			b.sub(p1);
			float a = V2.dot(b, n);

			if (0.0f <= a && a <= maxLambda * denom){
				float mu2 = -r.x * b.y + r.y * b.x;

				// Does the segment intersect this segment?
				if (-k_slop * denom <= mu2 && mu2 <= denom * (1.0f + k_slop)){
					a /= denom;
//					n.normalize();
					n.nor();
					out.lambda = a;
					out.normal.set(n);
					return true;
				}
			}
		}
		return false;
	}
 
Example 2
Project: feup-lpoo-armadillo   File: GameModel.java   Source Code and License Vote up 5 votes
/**
 * Checks whether the ball's model reached the level's end position.
 *
 * @return True if ball reached end position, false otherwise.
 */
private boolean ballReachedEnd() {
    Vector2 diff = new Vector2(endPos);
    diff.sub(ballModel.getX(), ballModel.getY());

    return diff.len() < BallModel.RADIUS;
}
 
Example 3
Project: GDX-Engine   File: VRope.java   Source Code and License Vote up 5 votes
public void resetWithPoints(Vector2 pointA, Vector2 pointB) {
	float distance = pointA.dst(pointB);
	Vector2 diffVector = pointA.sub(pointB);
	float multiplier = distance / (numPoints - 1);
	for(int i=0;i<numPoints;i++) {
		Vector2 tmpVector = pointA.add(diffVector.nor().mul(multiplier*i*(1-antiSagHack)));
		VPoint tmpPoint = vPoints.get(i);
		tmpPoint.setPos(tmpVector.x, tmpVector.y);
		
	}
}
 
Example 4
Project: SpaceChaos   File: FastMath.java   Source Code and License Vote up 5 votes
public static boolean checkIfLinesAreEquals(Line a, Line b) {
    // check if lines are parallel, else they cannot be equals
    if (!checkVectorsParallel(a.getDirection(), b.getDirection())) {
        return false;
    }

    Vector2 d = Vector2Pool.create();
    d.set(a.getBase().x, a.getBase().y);

    d.sub(b.getBase());

    Vector2Pool.free(d);

    return checkVectorsParallel(d, a.getDirection());
}
 
Example 5
Project: feup-lpoo-armadillo   File: PolygonIntersector.java   Source Code and License Vote up 4 votes
/**
 * Computes the Polygon Properties of a given Polygon.
 *
 * @param polygon The polygon to be analyzed.
 * @return The Polygon Properties computed.
 */
public static PolygonProperties computePolygonProperties(Vector2[] polygon) {
    PolygonProperties polygonProperties = null;

    int count = polygon.length;

    if (count >= 3) {
        Vector2 centroid = new Vector2(0, 0);
        float area = 0;

        Vector2 refPoint = new Vector2(0, 0);
        float threeInverse = 1 / 3f;

        for (int i = 0; i < count; i++) {
            /*
 * Create a new vector to represent the reference point for
 * forming triangles. Then use refPoint, polygonVertex and
 * thirdTriangleVertex as vertices of a triangle.
 */
            refPoint.set(0, 0);
            Vector2 polygonVertex = polygon[i];
            Vector2 thirdTriangleVertex = i + 1 < count ? polygon[i + 1]
                    : polygon[0];

            Vector2 firstDirectionVector = polygonVertex.sub(refPoint);
            Vector2 secondDirectionVector = thirdTriangleVertex
                    .sub(refPoint);

            float triangleArea = firstDirectionVector
                    .crs(secondDirectionVector) / 2;
            area += triangleArea;

/* Area weighted centroid */
            centroid.add(refPoint.add(polygonVertex)
                    .add(thirdTriangleVertex)
                    .scl(triangleArea * threeInverse));
        }

        if (area > EPSILON) {
            centroid.scl(1 / area);
        } else {
            area = 0;
        }

        polygonProperties = new PolygonProperties(centroid, area);
    }

    return polygonProperties;
}
 
Example 6
Project: SpaceChaos   File: ColliderUtils.java   Source Code and License Vote up 4 votes
public static boolean testSegmentCollision(Segment a, Segment b) {
    Vector2 d = Vector2Pool.create(a.getPoint2()).sub(a.getPoint1());

    Line axisA = LinePool.create();
    axisA.setBase(a.getPoint1());
    axisA.setDirection(d);

    Line axisB = LinePool.create();

    if (onOneSide(axisA, b)) {
        // recycle objects
        LinePool.free(axisA, axisB);
        Vector2Pool.free(d);

        return false;
    }

    axisB.setBase(b.getPoint1());
    d.set(b.getPoint2());
    d = d.sub(b.getPoint1());
    axisB.setDirection(d);

    if (onOneSide(axisB, a)) {
        // recycle objects
        LinePool.free(axisA, axisB);
        Vector2Pool.free(d);

        return false;
    }

    if (FastMath.checkVectorsParallel(axisA.getDirection(), axisB.getDirection())) {
        Range rangeA = a.projectSegment(axisA.getDirection());
        Range rangeB = b.projectSegment(axisB.getDirection());

        // recycle objects
        LinePool.free(axisA, axisB);
        Vector2Pool.free(d);

        return rangeA.overlaps(rangeB);
    } else {
        // recycle objects
        LinePool.free(axisA, axisB);
        Vector2Pool.free(d);

        return true;
    }
}
 
Example 7
Project: miniventure   File: Level.java   Source Code and License Vote up 4 votes
public void dropItem(@NotNull Item item, Vector2 dropPos, @Nullable Vector2 targetPos) {
	
	/* this drops the itemEntity at the given coordinates, with the given direction (random if null).
	 	However, if the given coordinates reside within a solid tile, the adjacent tiles are checked.
	 		If all surrounding tiles are solid, then it just uses the given coordinates.
	 		But if it finds a non-solid tile, it drops it towards the non-solid tile.
	  */
	
	Rectangle itemBounds = new Rectangle(dropPos.x, dropPos.y, item.getItemData().getTexture().getRegionWidth(), item.getItemData().getTexture().getRegionHeight());
	Tile closest = getClosestTile(itemBounds);
	
	if(closest == null) {
		System.err.println("ERROR dropping item, closest tile is null");
		return;
	}
	
	ItemEntity ie = new ItemEntity(item, Vector2.Zero.cpy()); // this is a dummy variable.
	
	if(!closest.isPermeableBy(ie)) {
		// we need to look around for a tile that the item *can* be placed on.
		Array<Tile> adjacent = closest.getAdjacentTiles(true);
		Tile.sortByDistance(adjacent, targetPos);
		for(Tile adj: adjacent) {
			if(adj.isPermeableBy(ie)) {
				closest = adj;
				break;
			}
		}
	}
	
	// make sure the item will be fully inside the "closest" tile when dropped.
	MyUtils.moveRectInside(itemBounds, closest.getBounds(), 1);
	
	dropPos.x = itemBounds.x;
	dropPos.y = itemBounds.y;
	
	if(targetPos == null)
		targetPos = dropPos.cpy().add(new Vector2(MathUtils.random(Tile.SIZE/2), MathUtils.random(Tile.SIZE/2)));
	
	ie = new ItemEntity(item, targetPos.sub(dropPos));
	
	ie.moveTo(this, dropPos);
	addEntity(ie);
}
 
Example 8
Project: Klooni1010   File: PieceHolder.java   Source Code and License Vote up 4 votes
public void update() {
    Piece piece;
    if (heldPiece > -1) {
        piece = pieces[heldPiece];

        Vector2 mouse = new Vector2(
                Gdx.input.getX(),
                Gdx.graphics.getHeight() - Gdx.input.getY()); // Y axis is inverted

        if (Klooni.onDesktop) { //FIXME(oliver): This is a bad assumption to make. There are desktops with touch input and non-desktops with mouse input.
            // Center the piece to the mouse
            mouse.sub(piece.getRectangle().width * 0.5f, piece.getRectangle().height * 0.5f);
        } else {
            // Center the new piece position horizontally
            // and push it up by it's a cell (arbitrary) vertically, thus
            // avoiding to cover it with the finger (issue on Android devices)
            mouse.sub(piece.getRectangle().width * 0.5f, -pickedCellSize);
        }
        if (Klooni.shouldSnapToGrid())
            mouse.set(board.snapToGrid(piece, mouse));

        piece.pos.lerp(mouse, DRAG_SPEED);
        piece.cellSize = Interpolation.linear.apply(piece.cellSize, pickedCellSize, DRAG_SPEED);
    }

    // Return the pieces to their original position
    // TODO This seems somewhat expensive, can't it be done any better?
    Rectangle original;
    for (int i = 0; i < count; ++i) {
        if (i == heldPiece)
            continue;

        piece = pieces[i];
        if (piece == null)
            continue;

        original = originalPositions[i];
        piece.pos.lerp(new Vector2(original.x, original.y), 0.3f);
        piece.cellSize = Interpolation.linear.apply(piece.cellSize, original.width, 0.3f);
    }
}