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

The following examples show how to use com.badlogic.gdx.math.Vector2#isZero() . 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: VelocityModeChanger.java    From riiablo with Apache License 2.0 5 votes vote down vote up
@Override
protected void begin() {
  Vector2 velocity = mVelocity.get(Riiablo.game.player).velocity;
  if (velocity.isZero()) return;
  if (Gdx.input.isKeyPressed(Input.Keys.SHIFT_LEFT)) {
    mRunning.remove(Riiablo.game.player);
    velocity.setLength(6);
  } else {
    mRunning.create(Riiablo.game.player);
    velocity.setLength(9);
  }
}
 
Example 2
Source File: VelocityModeChanger.java    From riiablo with Apache License 2.0 5 votes vote down vote up
/**
 * TODO: it would appear after testing that monsters may require a separate system to override
 *       their movement speed correctly from players. Need to investigate more when I can create
 *       and environment where I can adjust speeds to try and compare and see if I can refine
 *       my algorithm. Below method looks sufficient for now.
 */
@Override
protected void process(int entityId) {
  Velocity velocity = mVelocity.get(entityId);
  Vector2 currentVelocity = velocity.velocity;
  if (currentVelocity.isZero()) {
    cofs.setMode(entityId, mMovementModes.get(entityId).NU);
    mAnimData.get(entityId).override = -1;
  } else if (mMonster.has(entityId)) {
    AnimData animData = mAnimData.get(entityId);
    if (mRunning.has(entityId)) {
      cofs.setMode(entityId, mMovementModes.get(entityId).RN);
      animData.override = MathUtils.roundPositive(animData.speed * currentVelocity.len() / velocity.runSpeed);
    } else {
      cofs.setMode(entityId, mMovementModes.get(entityId).WL);
      animData.override = MathUtils.roundPositive(animData.speed * currentVelocity.len() / velocity.walkSpeed);
    }
  } else {
    if (mRunning.has(entityId)) {
      cofs.setMode(entityId, mMovementModes.get(entityId).RN);
      mAnimData.get(entityId).override = MathUtils.roundPositive(16 * currentVelocity.len());
    } else {
      cofs.setMode(entityId, mMovementModes.get(entityId).WL);
      mAnimData.get(entityId).override = MathUtils.roundPositive(32 * currentVelocity.len());
    }
  }
}
 
Example 3
Source File: Box2DSynchronizerPre.java    From riiablo with Apache License 2.0 5 votes vote down vote up
@Override
protected void process(int entityId) {
  Body body = mBox2DBody.get(entityId).body;
  Vector2 velocity = mVelocity.get(entityId).velocity;
  body.setLinearVelocity(velocity);

  // FIXME: This is a temp fix to prevent shoving NPCs -- need to explore Contact Filters
  if (velocity.isZero()) {
    body.setType(BodyDef.BodyType.StaticBody);
  } else {
    body.setType(BodyDef.BodyType.DynamicBody);
  }
}
 
Example 4
Source File: Ray.java    From killingspree with MIT License 5 votes vote down vote up
public static Body findBody(World world, Body srcBody,
        Vector2 step, float length, boolean staticOnly) {
    Vector2 start = srcBody.getPosition();
    Body body = null;
    if (step.isZero()) {
        return null;
    }
    
    step.scl(2);
    
    float bestLength = 100000;
    length *= length;
    for (int i = 0; i < world.bodies.size(); i++) {
        if (staticOnly &&
                world.bodies.get(i).bodyType != BodyType.StaticBody) {
            continue;
        }
        if (world.bodies.get(i) == srcBody) {
            continue;
        }
        temp .set(start);
        float currentLength = temp.dst2(start);
        while (currentLength < length && currentLength < bestLength) {
            if(world.bodies.get(i).rectangle.contains((temp.x + 
                    WorldRenderer.VIEWPORT_WIDTH) % 
                    WorldRenderer.VIEWPORT_WIDTH,
                    (temp.y + WorldRenderer.VIEWPORT_HEIGHT) %
                    WorldRenderer.VIEWPORT_HEIGHT)) {
                body = world.bodies.get(i);
                bestLength = currentLength;
                continue;
            }
            temp.add(step);
            currentLength = temp.dst2(start);
        }
    }
    return body;
}
 
Example 5
Source File: Pathfinder.java    From riiablo with Apache License 2.0 4 votes vote down vote up
@Override
protected void process(int entityId) {
  Vector2 position0 = mPosition.get(entityId).position;
  tmpVec2.set(position0);
  Pathfind pathfind = mPathfind.get(entityId);
  Vector2 target = pathfind.target;
  Iterator<Vector2> targets = pathfind.targets;
  if (target.isZero()) return;
  if (tmpVec2.epsilonEquals(target, 0.1f)) { // TODO: tune this appropriately
    if (!targets.hasNext()) {
      findPath(entityId, null);
      return;
    }
  }

  Velocity velocity = mVelocity.get(entityId);
  float speed    = (mRunning.has(entityId) ? velocity.runSpeed : velocity.walkSpeed);
  float distance = speed * world.delta;
  float traveled = 0;
  while (traveled < distance) {
    float targetLen = tmpVec2.dst(target);
    float part = Math.min(distance - traveled, targetLen);
    if (part == 0) break;
    tmpVec2.lerp(target, part / targetLen);
    traveled += part;
    if (MathUtils.isEqual(part, targetLen, 0.1f)) {
      if (targets.hasNext()) {
        target.set(targets.next());
      } else {
        break;
      }
    }
  }

  /**
   * FIXME: there is a lot of jitter here in the direction for shorter movements because of
   *        repathing every frame-- need to create some kind of target component which is a target
   *        entity or target point and if it's down to the last remaining waypoint, set angle to
   *        the actual point or entity.
   */
  tmpVec2.sub(position0);
  mAngle.get(entityId).target.set(tmpVec2).nor();

  velocity.velocity.set(tmpVec2).setLength(speed);
}
 
Example 6
Source File: Box2dSteeringEntity.java    From gdx-ai with Apache License 2.0 4 votes vote down vote up
protected void applySteering (SteeringAcceleration<Vector2> steering, float deltaTime) {
	boolean anyAccelerations = false;

	// Update position and linear velocity.
	if (!steeringOutput.linear.isZero()) {
		// this method internally scales the force by deltaTime
		body.applyForceToCenter(steeringOutput.linear, true);
		anyAccelerations = true;
	}

	// Update orientation and angular velocity
	if (isIndependentFacing()) {
		if (steeringOutput.angular != 0) {
			// this method internally scales the torque by deltaTime
			body.applyTorque(steeringOutput.angular, true);
			anyAccelerations = true;
		}
	} else {
		// If we haven't got any velocity, then we can do nothing.
		Vector2 linVel = getLinearVelocity();
		if (!linVel.isZero(getZeroLinearSpeedThreshold())) {
			float newOrientation = vectorToAngle(linVel);
			body.setAngularVelocity((newOrientation - getAngularVelocity()) * deltaTime); // this is superfluous if independentFacing is always true
			body.setTransform(body.getPosition(), newOrientation);
		}
	}

	if (anyAccelerations) {
		// body.activate();

		// TODO:
		// Looks like truncating speeds here after applying forces doesn't work as expected.
		// We should likely cap speeds form inside an InternalTickCallback, see
		// http://www.bulletphysics.org/mediawiki-1.5.8/index.php/Simulation_Tick_Callbacks

		// Cap the linear speed
		Vector2 velocity = body.getLinearVelocity();
		float currentSpeedSquare = velocity.len2();
		float maxLinearSpeed = getMaxLinearSpeed();
		if (currentSpeedSquare > maxLinearSpeed * maxLinearSpeed) {
			body.setLinearVelocity(velocity.scl(maxLinearSpeed / (float)Math.sqrt(currentSpeedSquare)));
		}

		// Cap the angular speed
		float maxAngVelocity = getMaxAngularSpeed();
		if (body.getAngularVelocity() > maxAngVelocity) {
			body.setAngularVelocity(maxAngVelocity);
		}
	}
}