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

The following examples show how to use com.badlogic.gdx.math.Vector2#sub() . 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: AnimatePositionSystem.java    From Entitas-Java with MIT License 6 votes vote down vote up
public void moveFocused(Vector2 center, Body body) {
    MoveData moveData = new MoveData();
    body.setLinearVelocity(new Vector2());
    moveData.start = body.getWorldCenter().cpy();
    moveData.direction = new Vector2(center.x - moveData.start.x, center.y - moveData.start.y);
    moveData.current = 1;
    moveData.total = 10;

    if (moveData.current == moveData.total + 1) {
        body.setLinearVelocity(new Vector2());
        return;
    }

    int t = easeInOut(moveData.current, 0, 1, moveData.total);

    Vector2 sEnd = moveData.direction.cpy();
    sEnd.scl(t);
    sEnd.add(moveData.start.cpy());

    Vector2 sStart = body.getWorldCenter();

    sEnd.sub(sStart);
    sEnd.scl(60);
    moveData.current++;
    body.setLinearVelocity(sEnd);
}
 
Example 2
Source File: ViewScroller.java    From dice-heroes with GNU General Public License v3.0 5 votes vote down vote up
public void centerOn(float x, float y) {
    Vector2 viewCoordinate = tmp1.set(
        x * ViewController.CELL_SIZE,
        y * ViewController.CELL_SIZE
    );
    Vector2 currentCenter = tmp2.set(stage.getWidth(), stage.getHeight()).scl(0.5f);
    Vector2 targetPosition = currentCenter.sub(viewCoordinate);
    clamp(targetPosition);
    root.clearActions();
    root.addAction(Actions.moveTo(
        hScrollEnabled ? targetPosition.x : root.getX(),
        vScrollEnabled ? targetPosition.y : root.getY(),
        CENTER_ON_TIME
    ));
}
 
Example 3
Source File: ViewScroller.java    From dice-heroes with GNU General Public License v3.0 5 votes vote down vote up
public void centerOnImmediately(float x, float y) {
    Vector2 viewCoordinate = tmp1.set(
        x * ViewController.CELL_SIZE,
        y * ViewController.CELL_SIZE
    );
    Vector2 currentCenter = tmp2.set(stage.getWidth(), stage.getHeight()).scl(0.5f);
    Vector2 targetPosition = currentCenter.sub(viewCoordinate);
    clamp(targetPosition);
    root.setPosition(
        hScrollEnabled ? targetPosition.x : root.getX(),
        vScrollEnabled ? targetPosition.y : root.getY()
    );
}
 
Example 4
Source File: GameLevelController.java    From tilt-game-android with MIT License 5 votes vote down vote up
private void checkCollisionSound(Contact contact, Body bodyA, Body bodyB) {
    WorldManifold manifold = contact.getWorldManifold();
    Vector2 contactPoint = manifold.getPoints()[0];
    Vector2 vel1 = bodyA.getLinearVelocityFromWorldPoint(contactPoint);
    Vector2 vel2 = bodyB.getLinearVelocityFromWorldPoint(contactPoint);
    Vector2 impactVelocity = vel1.sub(vel2);
    float impactLen = impactVelocity.len();
    if (impactLen > MIN_BALL_SOUND_SPEED) {
        float dot = Math.abs(impactVelocity.dot(manifold.getNormal()));
        if (dot > MIN_IMPACT_SOUND_SPEED) {
            float volume = (float) Math.min((dot - MIN_IMPACT_SOUND_SPEED) / MAX_IMPACT_SOUND_SPEED, 1.0);
            SoundManager.getInstance().play(R.raw.bounce_1, volume);
        }
    }
}
 
Example 5
Source File: WorldBodyUtils.java    From killingspree with MIT License 5 votes vote down vote up
public void destroyEntities(ServerBomb bomb, float radius, Vector2 position) {
    Body body = bomb.body;
    circle.set(position, radius);
    for (ServerEntity entity: worldManager.entities) {
        if (entity.body == body || entity.body.toDestroy) {
            continue;
        }
        if (Intersector.overlaps(circle, entity.body.rectangle)) {
            Vector2 step = entity.body.getPosition();
            float length = position.dst(step);
            step.sub(position);
            float max = Math.max(step.x, step.y);
            step.scl(4 / max);
            Body otherBody = Ray.findBody(world,
                    body, step, length, true);
            if (otherBody == null) {
                if (entity instanceof LivingCategory) {
                    if (((LivingCategory)entity.body.getUserData()).kill()) {
                        if (bomb.bomber != entity.body.getUserData())
                            bomb.bomber.addKill();
                        else {
                            bomb.bomber.reduceKill();
                        }
                    }
                }
            }
        }
    }
}
 
Example 6
Source File: ServerArrow.java    From killingspree with MIT License 5 votes vote down vote up
@Override
    public void update(float delta) {
        gravityTime -= delta;
        Vector2 velocityVector = body.getLinearVelocity();
        position.set(body.getPosition());
        Body targetBody = Ray.findBody(world.worldManager.getWorld(),
                body, tempVector.set(Math.signum(velocityVector.x), 0), 200f);
        position.set(body.getPosition());
//        Gdx.app.log(body.getPosition().toString(), target.toString());
        if (target != null) {
            Vector2 currentVelocity = body.getLinearVelocity();
            Vector2 targetVelocity = tempVector.set(target);
            targetVelocity.sub(position);
            targetVelocity.scl(1/targetVelocity.x);
            targetVelocity.scl(currentVelocity.x);
            if(Math.abs(currentVelocity.x) > 1) {
                if(Math.abs(position.x - target.x) < 200 &&
                        Math.abs(position.y - target.y) < 100) {
                    if (currentVelocity.y < targetVelocity.y) {
                        currentVelocity.y += 50f / Math.ceil(Math.abs(position.y - target.y));
                    } else {
                        currentVelocity.y -= 50f / Math.ceil(Math.abs(position.y - target.y));
                    }
                    body.setLinearVelocity(currentVelocity);
                }
            }
        }
        
        if (Utils.wrapBody(position)) {
            body.setTransform(position, 0);
        }
        
        if (gravityTime < 0) {
            body.setGravityScale(0.5f);
        }
    }
 
Example 7
Source File: Production.java    From libgdx-demo-pax-britannica with MIT License 5 votes vote down vote up
/**
	 * Span new Ship
	 * 
	 * 1 = Fighter
	 * 2 = Bomber
	 * 3 = Frigate
	 * 4 = Upgrade
	 * @param unitType
	 */
	public void spawn(int unitType) {
//		factory.ownShips = 0;
//		for (Ship fighter : GameInstance.getInstance().fighters) {
//			if(fighter.id == factory.id) factory.ownShips++;
//		}		
//		for (Ship bomber : GameInstance.getInstance().bombers) {
//			if(bomber.id != factory.id) factory.ownShips++;
//		}		
//		for (Ship frigate : GameInstance.getInstance().frigates) {
//			if(frigate.id != factory.id) factory.ownShips++;
//		}
//		if(factory.ownShips>MAXSHIPS) {
//			return;
//		}
		
		
		Vector2 spawn_pos = new Vector2(factory.collisionCenter.x + (SPAWN_OFFSET * factory.facing.x), 
				factory.collisionCenter.y + (SPAWN_OFFSET * factory.facing.y));

		if (unitType == 1) {
			factory.resourceAmount -= fighterCost;
			GameInstance.getInstance().fighters.add(new Fighter(factory.id, spawn_pos, new Vector2(factory.facing.x, factory.facing.y)));
		} else if (unitType == 2) {
			spawn_pos.sub(10, 10);
			factory.resourceAmount -= bomberCost;
			GameInstance.getInstance().bombers.add(new Bomber(factory.id, spawn_pos, new Vector2(factory.facing.x, factory.facing.y)));
		} else if (unitType == 3) {
			spawn_pos.sub(25, 25);
			factory.resourceAmount -= frigateCost;
			GameInstance.getInstance().frigates.add(new Frigate(factory.id, spawn_pos, new Vector2(factory.facing.x, factory.facing.y)));
		} else if (unitType == 4) {
			factory.resourceAmount -= upgradeCost;
			factory.upgradesUsed += 1;
			factory.harvestRate += (factory.harvestRateUpgrade/factory.upgradesUsed);
		}
	}
 
Example 8
Source File: PieceHolder.java    From Klooni1010 with GNU General Public License v3.0 4 votes vote down vote up
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);
    }
}
 
Example 9
Source File: PolygonIntersector.java    From Codelabs with MIT License 4 votes vote down vote up
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;
}