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

The following examples show how to use com.badlogic.gdx.math.Vector2#len2() . 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: GhostAI.java    From Pacman_libGdx with MIT License 6 votes vote down vote up
public void applyingSteering(float deltaTime) {
    boolean anyAcceleration = false;

    if (!steeringOutput.linear.isZero()) {
        body.applyForceToCenter(steeringOutput.linear, true);
        anyAcceleration = true;
    }

    if (anyAcceleration) {

        // cap the linear speed
        Vector2 velocity = body.getLinearVelocity();
        float currentSpeedSquare = velocity.len2();
        if (currentSpeedSquare > maxLinearSpeed * maxLinearSpeed) {
            body.setLinearVelocity(velocity.scl(maxLinearSpeed / (float) Math.sqrt(currentSpeedSquare)));
        }
    }
}
 
Example 2
Source File: BayazitDecomposer.java    From RuinsOfRevenge with MIT License 6 votes vote down vote up
public static ArrayList<Vector2> reduceByDistance(ArrayList<Vector2> vertices, float distance) {
	// We can't simplify polygons under 3 vertices
	if (vertices.size() < 3) return vertices;
	ArrayList<Vector2> simplified = new ArrayList<Vector2>();
	for (int i = 0; i < vertices.size(); i++) {
		Vector2 current = vertices.get(i);
		int ii = i + 1;
		if (ii >= vertices.size()) ii = 0;
		Vector2 next = vertices.get(ii);
		Vector2 diff = new Vector2(next.x - current.x, next.y - current.y);
		// If they are closer than the distance, continue
		if (diff.len2() <= distance) continue;
		simplified.add(current);
	}
	return simplified;
}
 
Example 3
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);
		}
	}
}