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

The following examples show how to use com.badlogic.gdx.math.Vector2#set() . 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: Scene2dUtils.java    From gdx-vfx with Apache License 2.0 6 votes vote down vote up
public static Vector2 setPositionRelative(Actor srcActor, int srcAlign, Actor dstActor, int dstAlign, float dstX, float dstY, boolean round) {
    Vector2 pos = tmpVec2.set(srcActor.getX(srcAlign), srcActor.getY(srcAlign));

    if ((dstAlign & right) != 0)
        pos.x -= dstActor.getWidth();
    else if ((dstAlign & left) == 0)
        pos.x -= dstActor.getWidth() / 2;

    if ((dstAlign & top) != 0)
        pos.y -= dstActor.getHeight();
    else if ((dstAlign & bottom) == 0)
        pos.y -= dstActor.getHeight() / 2;

    pos.add(dstX, dstY);

    if (round) {
        pos.set(pos.x, pos.y);
    }
    dstActor.setPosition(pos.x, pos.y);
    return pos;
}
 
Example 2
Source File: SinglePlayer.java    From uracer-kotd with Apache License 2.0 6 votes vote down vote up
@Override
public void updateCameraPosition (Vector2 positionPx) {
	if (hasPlayer()) {
		// update player's headlights and move the world camera to follows it, if there is a player
		if (gameWorld.isNightMode()) {
			gameWorldRenderer.updatePlayerHeadlights(playerCar);
		}
		positionPx.set(playerCar.state().position);
		if (!paused) {
			positionPx.add(camShaker.compute(getCollisionFactor()));
			positionPx.add(camShaker.compute(getOutOfTrackFactor()));
		}
	} else if (isGhostActive(0)) {
		// FIXME use available/choosen replay
		positionPx.set(getGhost(0).state().position);
	} else {
		// no ghost, no player, WTF?
		positionPx.set(Convert.mt2px(gameWorld.playerStart.position));
	}
}
 
Example 3
Source File: RaycastCollisionDetector.java    From riiablo with Apache License 2.0 6 votes vote down vote up
public boolean findCollision(Ray<Vector2> ray, int flags, int size, Collision<Vector2> collision) {
  Vector2 start = ray.start;
  Vector2 end = ray.end;

  Vector2 last = collision.point.setZero();
  Vector2 normal = collision.normal.setZero(); // TODO: calculate normal

  sample.set(start);
  delta.set(end).sub(start).setLength(DELTA);
  float add = delta.len();
  for (float curDist = 0, maxDist = start.dst(end); curDist < maxDist; curDist += add, last.set(sample), sample.add(delta)) {
    if (map.flags(sample) != 0 || graph.getOrCreate(sample).clearance < size) {
      return true;
    }
  }

  return false;
}
 
Example 4
Source File: GameStage.java    From GdxDemo3D with Apache License 2.0 5 votes vote down vote up
@Override
public Vector2 screenToStageCoordinates(Vector2 screenCoords) {
	tmp.set(screenCoords.x, screenCoords.y, 1);
	cameraUI.unproject(tmp, viewport.getScreenX(), viewport.getScreenY(),
			viewport.getScreenWidth(), viewport.getScreenHeight());
	screenCoords.set(tmp.x, tmp.y);
	return screenCoords;
}
 
Example 5
Source File: VisTextField.java    From vis-ui with Apache License 2.0 5 votes vote down vote up
private VisTextField findNextTextField (Array<Actor> actors, VisTextField best, Vector2 bestCoords, Vector2 currentCoords, boolean up) {
	Window modalWindow = findModalWindow(this);

	for (int i = 0, n = actors.size; i < n; i++) {
		Actor actor = actors.get(i);
		if (actor == this) continue;
		if (actor instanceof VisTextField) {
			VisTextField textField = (VisTextField) actor;

			if (modalWindow != null) {
				Window nextFieldModalWindow = findModalWindow(textField);
				if (nextFieldModalWindow != modalWindow) continue;
			}

			if (textField.isDisabled() || !textField.focusTraversal || isActorVisibleInStage(textField) == false)
				continue;

			Vector2 actorCoords = actor.getParent().localToStageCoordinates(tmp3.set(actor.getX(), actor.getY()));
			if ((actorCoords.y < currentCoords.y || (actorCoords.y == currentCoords.y && actorCoords.x > currentCoords.x)) ^ up) {
				if (best == null
						|| (actorCoords.y > bestCoords.y || (actorCoords.y == bestCoords.y && actorCoords.x < bestCoords.x)) ^ up) {
					best = (VisTextField) actor;
					bestCoords.set(actorCoords);
				}
			}
		} else if (actor instanceof Group)
			best = findNextTextField(((Group) actor).getChildren(), best, bestCoords, currentCoords, up);
	}
	return best;
}
 
Example 6
Source File: ViewController.java    From dice-heroes with GNU General Public License v3.0 5 votes vote down vote up
public Vector2 stageToWorldCoordinates(Vector2 input) {
    root.stageToLocalCoordinates(input);
    input.scl(1 / ViewController.CELL_SIZE);
    int x = MathUtils.floor(input.x);
    int y = MathUtils.floor(input.y);
    if (input.x < 0) {
        x = -Math.abs(x);
    }
    if (input.y < 0) {
        y = -Math.abs(y);
    }
    input.set(x, y);
    return input;
}
 
Example 7
Source File: ServerPlayer.java    From killingspree with MIT License 5 votes vote down vote up
@Override
public boolean kill() {
    if (spawnTime < 0) {
        world.audio.hurt();
        Vector2 position = body.getPosition();
        position.set(startX, startY);
        body.setTransform(position, 0);
        spawnTime = 0.1f;
        totalBombs = 3;
        return true;
    }
    return false;
}
 
Example 8
Source File: ScnWidget.java    From bladecoder-adventure-engine with Apache License 2.0 5 votes vote down vote up
public void screenToWorldCoords(Vector2 coords) {
	tmpV2.set(0, 0);
	localToStageCoordinates(tmpV2);
	// getStage().stageToScreenCoordinates(tmpV2);
	tmpV3.set(coords.x, coords.y, 0);
	camera.unproject(tmpV3, tmpV2.x, tmpV2.y, getWidth(), getHeight());
	coords.set(tmpV3.x, tmpV3.y);
}
 
Example 9
Source File: Box2DUtils.java    From Entitas-Java with MIT License 4 votes vote down vote up
/**
 * @see #position(Fixture)
 */
public static Vector2 position(Fixture fixture, Vector2 output) {
    return output.set(position(fixture.getShape(), fixture.getBody()));
}
 
Example 10
Source File: OrthographicCamera.java    From riiablo with Apache License 2.0 4 votes vote down vote up
public Vector2 unproject(float x, float y, Vector2 dst) {
  tmpVec3.set(x, y, 0);
  unproject(tmpVec3);
  return dst.set(tmpVec3.x, tmpVec3.y);
}
 
Example 11
Source File: Box2DUtils.java    From Entitas-Java with MIT License 4 votes vote down vote up
/**
 * @see #position(Fixture)
 */
public static Vector2 position(Fixture fixture, Vector2 output) {
    return output.set(position(fixture.getShape(), fixture.getBody()));
}
 
Example 12
Source File: IsometricCamera.java    From riiablo with Apache License 2.0 4 votes vote down vote up
public Vector2 screenToWorld(float x, float y, Vector2 dst) {
  dst.set(x, y);
  unproject(dst);
  return toWorld(dst);
}
 
Example 13
Source File: Pointer.java    From bladecoder-adventure-engine with Apache License 2.0 4 votes vote down vote up
private void getInputUnproject(Viewport v, Vector2 out) {
	out.set(Gdx.input.getX(), Gdx.input.getY());

	v.unproject(out);
}
 
Example 14
Source File: PolygonUtils.java    From bladecoder-adventure-engine with Apache License 2.0 4 votes vote down vote up
/**
 * Clamp the point to the nearest polygon segment
 * 
 * @param poly
 *            The polygon
 * @param x
 *            The original point X
 * @param y
 *            The original point Y
 * @param dest
 *            The clamped point
 * @return The segment where the clamped point belongs
 */
public static int getClampedPoint(Polygon poly, float x, float y,
		Vector2 dest) {
	float verts[] = poly.getTransformedVertices();
	float dTmp;

	Intersector.nearestSegmentPoint(verts[0], verts[1], verts[2], verts[3],
			x, y, dest);

	int nearest = 0;
	float d = Vector2.dst(x, y, dest.x, dest.y);

	for (int i = 2; i < verts.length; i += 2) {
		Intersector.nearestSegmentPoint(verts[i], verts[i + 1],
				verts[(i + 2) % verts.length],
				verts[(i + 3) % verts.length], x, y, tmp);
		dTmp = Vector2.dst(x, y, tmp.x, tmp.y);

		if (dTmp < d) {
			d = dTmp;
			nearest = i;
			dest.set(tmp);
		}
	}
	
	// ERROR CONTROL:
	// If the clamped point is not in the walkzone 
	// we search for the nearest walkzone vertex
	if (!PolygonUtils.isPointInside(poly, dest.x, dest.y, true)) {
		EngineLogger.debug("> PolygonalPathFinder: CLAMPED FAILED!!");
		
		tmp.set(verts[0], verts[1]);
		d = Vector2.dst(x, y, tmp.x, tmp.y);
		nearest = 0;
		dest.set(tmp);
		
		for (int i = 2; i < verts.length; i += 2) {
			tmp.set(verts[i], verts[i + 1]);
			dTmp = Vector2.dst(x, y, tmp.x, tmp.y);

			if (dTmp < d) {
				d = dTmp;
				nearest = i;
				dest.set(tmp);
			}
		}
	}

	return nearest;
}
 
Example 15
Source File: IsometricCamera.java    From riiablo with Apache License 2.0 4 votes vote down vote up
public Vector2 getTileOffset(Vector2 dst) {
  return dst.set(tileOffset);
}
 
Example 16
Source File: GameWorld.java    From uracer-kotd with Apache License 2.0 4 votes vote down vote up
private List<OrthographicAlignedStillModel> createWalls () {
	List<OrthographicAlignedStillModel> models = null;

	if (mapUtils.hasObjectGroup(ObjectGroup.Walls)) {
		Vector2 fromMt = new Vector2();
		Vector2 toMt = new Vector2();
		Vector2 offsetMt = new Vector2();

		// create material
		TextureAttribute ta = new TextureAttribute(Art.meshTrackWall, 0, "u_texture");
		ta.uWrap = TextureWrap.Repeat.getGLEnum();
		ta.vWrap = TextureWrap.Repeat.getGLEnum();
		Material mat = new Material("trackWall", ta);

		MapLayer group = mapUtils.getObjectGroup(ObjectGroup.Walls);
		if (group.getObjects().getCount() > 0) {
			models = new ArrayList<OrthographicAlignedStillModel>(group.getObjects().getCount());

			for (int i = 0; i < group.getObjects().getCount(); i++) {
				PolylineMapObject o = (PolylineMapObject)group.getObjects().get(i);

				//@off
				List<Vector2> points = MapUtils.extractPolyData(
					o.getPolyline().getVertices());
				//@on
				if (points.size() >= 2) {
					float wallTicknessMt = 0.75f;
					float[] mags = new float[points.size() - 1];

					offsetMt.set(o.getPolyline().getX(), o.getPolyline().getY());
					offsetMt.set(Convert.px2mt(offsetMt));

					fromMt.set(Convert.px2mt(points.get(0))).add(offsetMt);
					fromMt.y = worldSizeMt.y - fromMt.y;

					for (int j = 1; j <= points.size() - 1; j++) {
						toMt.set(Convert.px2mt(points.get(j))).add(offsetMt);
						toMt.y = worldSizeMt.y - toMt.y;

						// create box2d wall
						Box2DFactory.createWall(box2dWorld, fromMt, toMt, wallTicknessMt, 0f);

						// compute magnitude
						mags[j - 1] = (float)Math.sqrt((toMt.x - fromMt.x) * (toMt.x - fromMt.x) + (toMt.y - fromMt.y)
							* (toMt.y - fromMt.y));

						fromMt.set(toMt);
					}

					Mesh mesh = buildWallMesh(points, mags);

					StillSubMesh[] subMeshes = new StillSubMesh[1];
					subMeshes[0] = new StillSubMesh("wall", mesh, GL20.GL_TRIANGLES);

					OrthographicAlignedStillModel model = new OrthographicAlignedStillModel(new StillModel(subMeshes), mat);

					model.setPosition(o.getPolyline().getX(), worldSizePx.y - o.getPolyline().getY());
					model.setScale(1);

					models.add(model);
				}
			}
		}
	}

	return models;
}
 
Example 17
Source File: Box2DUtils.java    From Entitas-Java with MIT License 4 votes vote down vote up
/**
 * @see #position(Shape, Body)
 */
public static Vector2 position(Shape shape, Body body, Vector2 output) {
    return output.set(body.getPosition().add(positionRelative(shape, body.getTransform().getRotation(), output)));
}
 
Example 18
Source File: GameWorld.java    From uracer-kotd with Apache License 2.0 4 votes vote down vote up
private List<Vector2> createRoute () {
	List<Vector2> r = null;

	if (mapUtils.hasObjectGroup(ObjectGroup.Route)) {
		Vector2 fromMt = new Vector2();
		Vector2 toMt = new Vector2();
		Vector2 offsetMt = new Vector2();

		MapLayer group = mapUtils.getObjectGroup(ObjectGroup.Route);
		if (group.getObjects().getCount() == 1) {
			PolylineMapObject o = (PolylineMapObject)group.getObjects().get(0);

			//@off
			List<Vector2> points = MapUtils.extractPolyData(
				o.getPolyline().getVertices());
			//@on

			r = new ArrayList<Vector2>(points.size());

			offsetMt.set(o.getPolyline().getX(), o.getPolyline().getY());
			offsetMt.set(Convert.px2mt(offsetMt));

			fromMt.set(Convert.px2mt(points.get(0))).add(offsetMt);
			fromMt.y = worldSizeMt.y - fromMt.y;

			r.add(new Vector2(fromMt));

			for (int j = 1; j <= points.size() - 1; j++) {
				toMt.set(Convert.px2mt(points.get(j))).add(offsetMt);
				toMt.y = worldSizeMt.y - toMt.y;
				r.add(new Vector2(toMt));
			}
		} else {
			if (group.getObjects().getCount() > 1) {
				throw new GdxRuntimeException("Too many routes");
			} else if (group.getObjects().getCount() == 0) {
				throw new GdxRuntimeException("No route defined for this track");
			}
		}

	}

	return r;
}
 
Example 19
Source File: IsometricCamera.java    From riiablo with Apache License 2.0 4 votes vote down vote up
public Vector2 getPixOffset(Vector2 dst) {
  return dst.set(pixOffset);
}
 
Example 20
Source File: Box2DUtils.java    From Entitas-Java with MIT License 4 votes vote down vote up
/**
 * @see #position(Shape, Body)
 */
public static Vector2 position(Shape shape, Body body, Vector2 output) {
    return output.set(body.getPosition().add(positionRelative(shape, body.getTransform().getRotation(), output)));
}