List of usage examples for com.badlogic.gdx.math Vector2 len
@Override public float len()
From source file:com.tnf.ptm.handler.input.Mover.java
License:Apache License
public void update(PtmGame game, PtmShip ship, Vector2 dest, Planet np, float maxIdleDist, boolean hasEngine, boolean avoidBigObjs, float desiredSpdLen, boolean stopNearDest, Vector2 destSpd) { myUp = false;/*from ww w .jav a 2 s . co m*/ myLeft = false; myRight = false; if (!hasEngine || dest == null) { return; } Vector2 shipPos = ship.getPosition(); float toDestLen = shipPos.dst(dest); if (toDestLen < maxIdleDist) { if (!stopNearDest) { return; } myDesiredSpd.set(destSpd); } else { updateDesiredSpd(game, ship, dest, toDestLen, stopNearDest, np, avoidBigObjs, desiredSpdLen, destSpd); } Vector2 shipSpd = ship.getSpd(); float spdDeviation = shipSpd.dst(myDesiredSpd); if (spdDeviation < MAX_ABS_SPD_DEV || spdDeviation < MAX_REL_SPD_DEV * shipSpd.len()) { return; } float shipAngle = ship.getAngle(); float rotSpd = ship.getRotSpd(); float rotAcc = ship.getRotAcc(); float desiredAngle = PtmMath.angle(shipSpd, myDesiredSpd); float angleDiff = PtmMath.angleDiff(desiredAngle, shipAngle); myUp = angleDiff < MIN_ANGLE_TO_ACC; Boolean ntt = needsToTurn(shipAngle, desiredAngle, rotSpd, rotAcc, MIN_MOVE_AAD); if (ntt != null) { if (ntt) { myRight = true; } else { myLeft = true; } } }
From source file:com.tnf.ptm.handler.input.Shooter.java
License:Apache License
public static float calcShootAngle(Vector2 gunPos, Vector2 gunSpd, Vector2 ePos, Vector2 eSpd, float projSpd, boolean sharp) { Vector2 eSpdShortened = PtmMath.getVec(eSpd); if (!sharp) { eSpdShortened.scl(E_SPD_PERC);/* www. j av a2 s. c o m*/ } Vector2 relESpd = PtmMath.distVec(gunSpd, eSpdShortened); PtmMath.free(eSpdShortened); float rotAngle = PtmMath.angle(relESpd); float v = relESpd.len(); float v2 = projSpd; PtmMath.free(relESpd); Vector2 toE = PtmMath.distVec(gunPos, ePos); PtmMath.rotate(toE, -rotAngle); float x = toE.x; float y = toE.y; float a = v * v - v2 * v2; float b = 2 * x * v; float c = x * x + y * y; float t = PtmMath.genQuad(a, b, c); float res; if (t != t) { res = Float.NaN; } else { toE.x += t * v; res = PtmMath.angle(toE) + rotAngle; } PtmMath.free(toE); return res; }
From source file:com.tnf.ptm.screens.game.CollisionWarnDrawer.java
License:Apache License
public boolean shouldWarn(PtmGame game) { hero = game.getHero();/*from w ww . j a va 2 s. co m*/ if (hero == null) { return false; } Vector2 pos = hero.getPosition(); Vector2 spd = hero.getSpd(); float acc = hero.getAcc(); float spdLen = spd.len(); float spdAngle = PtmMath.angle(spd); if (acc <= 0 || spdLen < 2 * acc) { return false; } // t = v/a; // s = att/2 = vv/a/2; float breakWay = spdLen * spdLen / acc / 2; breakWay += 2 * spdLen; Vector2 finalPos = PtmMath.getVec(0, 0); PtmMath.fromAl(finalPos, spdAngle, breakWay); finalPos.add(pos); warnCallback.show = false; game.getObjMan().getWorld().rayCast(warnCallback, pos, finalPos); PtmMath.free(finalPos); return warnCallback.show; }
From source file:de.bioviz.ui.DrawableLine.java
License:Open Source License
/** * Draws the line on the biochip./*from w ww . jav a2 s . c o m*/ */ public void draw() { Color col = this.getColor(); col.a = 1f; Vector2 toTarget = from.cpy().sub(to); final float len = toTarget.len(); final DrawableAssay assay = viz.currentAssay; setX(assay.xCoordOnScreen((to.x + from.x) / 2f)); setY(assay.yCoordOnScreen((to.y + from.y) / 2f)); setScaleX(assay.getSmoothScale() * len); setScaleY(3f); float rotation = (float) (Math.atan2(toTarget.y, toTarget.x) * (180f / Math.PI)); setRotation(rotation); setColorImmediately(col); if (showHead) { arrowHead.setColorImmediately(col); arrowHead.setX(assay.xCoordOnScreen(to.x)); arrowHead.setY(assay.yCoordOnScreen(to.y)); // the file has a 2 to 1 ratio arrowHead.setScaleX(assay.getSmoothScale() * 0.6f); arrowHead.setScaleY(assay.getSmoothScale() * 0.3f); // the image points into the opposite direction // therefore we rotate it by 180 degrees arrowHead.setRotation(rotation + 180f); arrowHead.draw(); } super.draw(); }
From source file:de.cubicvoxel.openspacebox.input.AbstractInputHandler.java
License:Open Source License
protected void postMoveInputEventWithClampedInput(float x, float y, float deltaTime) { Vector2 input = new Vector2(x, y); float clampedLength = MathUtils.clamp(input.len(), -1.0f, 1.0f); input.setLength(clampedLength);//from w w w.j a v a 2 s .co m inputEventBus.post(new MoveInputEvent(deltaTime, input)); }
From source file:es.eucm.ead.engine.collision.AreaWrapper.java
License:Open Source License
/** * Calculates distance from {@code this area} to {@code end}, stores the * resulting vector in {@code intersection}, and returns the float value of * the distance calculated./*w ww. j a va2 s . c om*/ * * @param end * Area to calculate distance to * @param intersection * Vector to store distance. If distance cannot be calculated, * this vector is not updated. Resulting vector points from this * area to {@code end}. * @param fromBorder * True if distance is to be calculated between areas' borders, * false if distance must be calculated between centers. If * {@code fromBorder} is true, and one of the areas contains the * other, this method may return {@code -1} if each area contains * the other's center and {@code intersection} is not updated. If * {@code fromBorder} is true but each area does not contain the * other's center (but both areas overlap), distance is * calculated although it is not representative, and * {@code intersection} points the other way (from end to start). * @return The float value of the distance calculated, or -1 (see comment * above). */ public float getDistance(AreaWrapper end, Vector2 intersection, boolean fromBorder) { Vector2 c1 = Pools.obtain(Vector2.class); Vector2 c2 = Pools.obtain(Vector2.class); end.getCenter(c1); getCenter(c2); if (fromBorder) { Vector2 i1 = Pools.obtain(Vector2.class); Vector2 i2 = Pools.obtain(Vector2.class); boolean intersected = end.intersectToSegment(c1, c2, i1) && intersectToSegment(c1, c2, i2); if (!intersected) { return -1; } i1.sub(i2); intersection.set(i1); Pools.free(i1); Pools.free(i2); } else { intersection.set(c1.sub(c2)); } Pools.free(c1); Pools.free(c2); return intersection.len(); }
From source file:es.eucm.ead.engine.collision.CircleWrapper.java
License:Open Source License
@Override protected boolean intersectToSegment(Vector2 start, Vector2 end, Vector2 intersection) { Vector2 v = Pools.obtain(Vector2.class); if (circle.contains(start) && !circle.contains(end)) { v.set(end);/*from ww w . ja va2 s . c o m*/ v.sub(start); } else if (!circle.contains(start) && circle.contains(end)) { v.set(start); v.sub(end); } else { return false; } v.scl(circle.radius / v.len()); getCenter(intersection); intersection.add(v); Pools.free(v); return true; }
From source file:es.eucm.ead.engine.systems.positiontracking.ChaseEntitySystem.java
License:Open Source License
private void move(EngineEntity chasing, EngineEntity target, float speedX, float speedY, boolean fromBorder) { /*/*from w w w . ja v a2 s .c o m*/ * Calculate distance vector. It is a vector that has the direction of * distance vector between target and chasing entities, and module * equals to the speed calculated */ Vector2 distanceVector = Pools.obtain(Vector2.class); BoundingAreaComponent targetBoundingArea = getBoundingArea(target); BoundingAreaComponent chasingBoundingArea = getBoundingArea(chasing); chasingBoundingArea.distanceTo(targetBoundingArea, distanceVector, fromBorder); float l = distanceVector.len(); distanceVector.scl(speedX / l, speedY / l); // Move chasing.getGroup().moveBy(distanceVector.x, distanceVector.y); Pools.free(distanceVector); }
From source file:es.eucm.ead.engine.systems.positiontracking.MoveByEntitySystem.java
License:Open Source License
@Override public void processEntity(Entity entity, float deltaTime) { MoveByEntityComponent moveByEntityComponent = entity.getComponent(MoveByEntityComponent.class); updateTarget(moveByEntityComponent); if (moveByEntityComponent.getTrackedEntity() == null) { Gdx.app.debug("MoveByEntitySystem", "Invalid entity target defined for MoveByEntity component. Entity will not be processed."); return;//from w w w.ja va 2 s. c om } // Check target entity actually moved Vector2 targetVelocity = Pools.obtain(Vector2.class); getSpeedOfTrackedEntity(moveByEntityComponent, targetVelocity); if (!MathUtils.isEqual(targetVelocity.len(), 0, 0.1F)) { // Calculate how much this entity's position must be adjusted float speedX = targetVelocity.x * moveByEntityComponent.getSpeedX(); float speedY = targetVelocity.y * moveByEntityComponent.getSpeedY(); ((EngineEntity) entity).getGroup().moveBy(speedX, speedY); } // Update remembered position moveByEntityComponent.rememberPosition(); }
From source file:headmade.arttag.Guard.java
License:Apache License
public void update(ArtTagScreen artTag, float delta) { if (body == null) { return;// ww w . j a v a 2 s. co m } isRunning = isAlert || isCautious; moveSpeed = isRunning ? maxMoveSpeed * runFactor : isSuspicious ? maxMoveSpeed * 0.75f : isHeardPlayer ? 0.5f : maxMoveSpeed; final Vector2 oldMoveVec = targetMoveVec.cpy(); Vector2 targetPoint = null; Float distanceToPlayer = null; if (isLightOn) { for (final Fixture fixture : playerInView) { targetPoint = fixture.getBody().getWorldCenter().cpy(); final Vector2 diffVec = body.getWorldCenter().cpy().sub(targetPoint); boolean seesPlayer = false; distanceToPlayer = diffVec.len(); if (distanceToPlayer < 1f) { seesPlayer = true; } else { final Vector2 outOfBodyVec = diffVec.scl(fixture.getShape().getRadius() * 1.01f); targetPoint.add(outOfBodyVec); seesPlayer = light.contains(targetPoint.x, targetPoint.y); } // Gdx.app.log(TAG, light.contains(targetPoint.x, targetPoint.y) + " diffVec.length " + diffVec.len()); if (seesPlayer) { // Gdx.app.log(TAG, "Guard sees player"); playerLastSeenVec = fixture.getBody().getWorldCenter().cpy(); if (!isAlert) { Assets.instance.playSound(AssetSounds.whosThere); } isAlert = true; Player.instance.isSpotted = true; reactionTime = 0f; } else { if (isAlert) { Assets.instance.playSound(AssetSounds.huh); reactionTime = MAX_REACTION_TIME; isCautious = true; } isAlert = false; } } } // handle hearing if (Player.instance.isRunning && (Player.instance.isMoveDown || Player.instance.isMoveUp || Player.instance.isMoveLeft || Player.instance.isMoveRight) && null != Player.instance.body) { if (distanceToPlayer == null) { distanceToPlayer = body.getWorldCenter().cpy().sub(Player.instance.body.getWorldCenter()).len(); } if (distanceToPlayer < MAX_LIGHT_CONE_LENGTH * 0.9f) { sumDeltaSinceHeardPlayer = 0; final RayCastCallback callback = new RayCastCallback() { @Override public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) { // Gdx.app.log(TAG, "reportRayFixture " + point + " normal " + normal); if (fixture.isSensor()) { // Gdx.app.log(TAG, "Raycast ignoring sensor"); return 1; } if (body.equals(fixture.getBody())) { // this is the guards body so there is nothing inbetween them // Gdx.app.log(TAG, "Guard CAN see the point where the noise happened" + Player.instance.body.getWorldCenter()); // playerLastHeardVisibleVec = Player.instance.body.getWorldCenter().cpy(); return 1; } // Gdx.app.log(TAG, "Fall through"); isHearingObstructed = true; return 0; } }; try { isSuspicious = true; playerLastHeardVec = Player.instance.body.getWorldCenter().cpy(); isHearingObstructed = false; // Gdx.app.log(TAG, "###################################"); // Gdx.app.log(TAG, "Guard " + body.getWorldCenter()); // Gdx.app.log(TAG, "Player " + playerLastHeardVec); artTag.world.rayCast(callback, playerLastHeardVec, body.getWorldCenter()); if (!isHearingObstructed) { playerLastHeardVisibleVec = Player.instance.body.getWorldCenter().cpy(); } } catch (final Exception e) { Gdx.app.error(TAG, "Error Raycasting :(", e); } } else { sumDeltaSinceHeardPlayer += delta; } } else { sumDeltaSinceHeardPlayer += delta; } isHeardPlayer = playerLastHeardVisibleVec != null || sumDeltaSinceHeardPlayer < 3; if (isTouchingPlayerLightCone && Player.instance.isLightOn) { // isAlert = true; } // handle backToPath if (isAlert || isCautious || (isSuspicious && playerLastHeardVisibleVec != null)) { if (lastVisibleVecBackToPath == null) { lastVisibleVecBackToPath = body.getWorldCenter(); } Vector2 checkPoint = path.get(currentPathIndex); if (backToPath.size > 0) { checkPoint = backToPath.get(backToPath.size - 1); } if (BODY_RADIUS < checkPoint.dst(body.getWorldCenter())) { // not touching checkpoint // Gdx.app.log(TAG, "Goard not touching checkpoint"); final RayCastCallback callback = new RayCastCallback() { @Override public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) { // Gdx.app.log(TAG, "reportRayFixture adds new backToPathPoint" + lastVisibleVecBackToPath); backToPath.add(lastVisibleVecBackToPath.cpy()); return 0; } }; try { artTag.world.rayCast(callback, body.getWorldCenter(), checkPoint); } catch (final Exception e) { Gdx.app.error(TAG, "Error Raycasting :(", e); } } lastVisibleVecBackToPath = body.getWorldCenter(); } // determine targetPoint if (isAlert && playerInView.size > 0 && Player.instance.body != null) { targetPoint = Player.instance.body.getWorldCenter(); } else if (isCautious && playerLastSeenVec != null) { targetPoint = playerLastSeenVec; if (BODY_RADIUS / 10 > targetPoint.dst2(body.getPosition())) { // Lost player Assets.instance.playSound(AssetSounds.hm); Gdx.app.log(TAG, "Guard no longer cautious"); isCautious = false; isSuspicious = true; } } else if (isSuspicious && playerLastHeardVisibleVec != null) { targetPoint = playerLastHeardVisibleVec; // Gdx.app.log(TAG, "Guard going to playerLastHeardVisibleVec"); if (BODY_RADIUS / 10 > targetPoint.dst2(body.getPosition())) { // Lost player Assets.instance.playSound(AssetSounds.hm); Gdx.app.log(TAG, "Guard no longer suspicious"); isSuspicious = false; playerLastHeardVisibleVec = null; } } else { lastVisibleVecBackToPath = null; if (backToPath.size > 0) { // following Path back to path targetPoint = backToPath.get(backToPath.size - 1); if (BODY_RADIUS / 10 > targetPoint.dst(body.getPosition())) { // Gdx.app.log(TAG, "Guard reached target back to path point " + targetPoint); backToPath.pop(); } } else { // following path isSuspicious = false; targetPoint = path.get(currentPathIndex); if (BODY_RADIUS > targetPoint.dst(body.getPosition())) { // Gdx.app.log(TAG, "Guard reached target point " + targetPoint); currentPathIndex++; if (currentPathIndex >= path.size) { currentPathIndex = 0; } targetPoint = path.get(currentPathIndex); // Gdx.app.log(TAG, "New target point " + targetPoint); } } } targetMoveVec = targetPoint.cpy().sub(body.getPosition()); targetMoveVec.nor().scl(moveSpeed); if (MathUtils.isEqual(0f, oldMoveVec.angle(targetMoveVec))) { sumDeltaSinceMoveChange += delta; } else { // movment direction changed sumDeltaSinceMoveChange = 0f; } final float alpha = reactionTime > 0 ? MathUtils.clamp(sumDeltaSinceMoveChange / reactionTime, 0.1f, 1f) : 1f; body.setLinearVelocity(targetMoveVec); final Vector2 bodyRotVec = new Vector2(1f, 0f); bodyRotVec.setAngleRad(body.getAngle()); float angleDiff; if (!isAlert && !isSuspicious && isHeardPlayer && null == playerLastHeardVisibleVec && null != playerLastHeardVec) { // look at last heard angleDiff = bodyRotVec.angleRad(playerLastHeardVec.cpy().sub(body.getWorldCenter()).rotate90(-1)); } else { angleDiff = bodyRotVec.angleRad(targetMoveVec.cpy().rotate90(-1)); } final float rotByRad = MathUtils.clamp(angleDiff, -(MAX_ROTATION_SPEED * delta) / reactionTime, MAX_ROTATION_SPEED * delta / reactionTime); // Gdx.app.log(TAG, "angleDiff: " + angleDiff + " rotByRad: " + rotByRad + " bodyRotVec: " + bodyRotVec + " - targetMoveVec: // " // + targetMoveVec); // is moving? if (!MathUtils.isEqual(targetMoveVec.len2(), 0f)) { if (Player.instance.body != null) { final float dist = body.getPosition().dst(Player.instance.body.getPosition()); float volume = isRunning ? STEP_VOLUME * 2 : STEP_VOLUME; if (dist > 1) { volume = volume / dist; } sound.setVolume(stepSoundId, volume); sound.setPitch(stepSoundId, isRunning ? runFactor : 1f); body.setTransform(body.getPosition(), body.getAngle() + rotByRad); } } else { sound.setVolume(stepSoundId, 0f); sound.setPitch(stepSoundId, 1f); } light.setActive(isLightOn); }