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

The following examples show how to use com.badlogic.gdx.math.Vector2#len() . 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: ViewWalking.java    From RuinsOfRevenge with MIT License 6 votes vote down vote up
@Override
public void draw(SpriteBatch batch, Entity e, float delta) {
	Vector2 linVel = e.getBody().getLinearVelocity();

	if (linVel.len() > 0.3f)
		direction = Dir.getDir(linVel.x, linVel.y);
	moving = linVel.len() > 0.1f;

	anims.setDirection(direction);
	anims.setMoving(moving);
	anims.setDeltaSpeed(delta);
	anims.apply(e);

	sprite.setRegion(anims.getKeyframe());
	draw(batch, e, sprite, width, xoffset, yoffset);
}
 
Example 2
Source File: ComponentNetwork.java    From RuinsOfRevenge with MIT License 6 votes vote down vote up
@Override
public void apply(Entity entity) {
	Vector2 entityPos = entity.getPos();
	Vector2 posDiff = posDiffPool.set(
			remoteState.posX - entityPos.x,
			remoteState.posY - entityPos.y);
	float distance = posDiff.len();

	if (distance > 0.5f) {
		entity.getBody().setTransform(remoteState.posX, remoteState.posY, remoteState.angle);
		System.out.println("Set position to: " + remoteState.posX + ", " + remoteState.posY);
	} else if (distance > 0.1f) {
		entity.getBody().setTransform(entityPos.add(posDiff.scl(0.1f)), remoteState.angle);
	}

	entity.getBody().setLinearVelocity(remoteState.velX, remoteState.velY);
}
 
Example 3
Source File: VMath.java    From uracer-kotd with Apache License 2.0 5 votes vote down vote up
public static Vector2 truncate (Vector2 v, float maxLength) {
	if (v.len() > maxLength) {
		v.nor().scl(maxLength);
	}

	return v;
}
 
Example 4
Source File: WorldRenderer.java    From SIFTrain with MIT License 5 votes vote down vote up
private void drawHoldBeam(Vector2 from, Vector2 to, float orgSize, float dstSize) {
    Vector2 delta = from.cpy().sub(to);

    float w = Math.max(orgSize, dstSize);
    float h = delta.len();

    float tw = holdBG.getRegionWidth();
    float th = holdBG.getRegionHeight();

    float factorScale = (tw / w) * 0.5f;
    float topFactor = Math.max(dstSize - orgSize, 0f) * factorScale;
    float botFactor = Math.max(orgSize - dstSize, 0f) * factorScale;

    float[] points = {
            topFactor,
            0f,

            botFactor,
            th,

            tw - botFactor,
            th,

            tw - topFactor,
            0f
    };

    PolygonRegion clamped = new PolygonRegion(holdBG, points, triangles);
    spriteBatch.draw(clamped, from.x - w * 0.5f, from.y, w * 0.5f, 0f, w, h, 1f, 1f, delta.angle() + 90);
}
 
Example 5
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 6
Source File: ComponentMovement.java    From RuinsOfRevenge with MIT License 5 votes vote down vote up
@Override
public void apply(Entity entity) {
	Body body = entity.getBody();
	moving = false;

	// If trying to move (pressing buttons on Keyboard, steering with Gamepad)
	if (xsteer != 0f || ysteer != 0f) {
		moving = true;

		if (Math.abs(xsteer) > Math.abs(ysteer)) {
			if (xsteer < 0) {
				direction = Dir.LEFT;
			} else {
				direction = Dir.RIGHT;
			}
		} else {
			if (ysteer < 0) {
				direction = Dir.DOWN;
			} else {
				direction = Dir.UP;
			}
		}
	}

	Vector2 linVel = body.getLinearVelocity();
	if (linVel.len() > maxspeed) {
		body.setLinearVelocity(linVel.cpy().nor().scl(maxspeed));
	}

	body.applyForceToCenter(strength * xsteer, strength * ysteer);

	if (friction > 1f && !moving) {
		body.setLinearVelocity(linVel.div(friction));
	}
}
 
Example 7
Source File: BuoyancyController.java    From Codelabs with MIT License 4 votes vote down vote up
private void applyForces(Fixture fixture, Vector2[] clippedPolygon) {
	PolygonProperties polygonProperties = PolygonIntersector
			.computePolygonProperties(clippedPolygon);

	/* Get fixtures bodies */
	Body fixtureBody = fixture.getBody();
	Body fluidBody = fluidSensor.getBody();

	/* Get fluid density */
	float density = fluidSensor.getDensity();

	/* Apply buoyancy force */
	float displacedMass = fluidSensor.getDensity()
			* polygonProperties.getArea();
	Vector2 gravity = world.getGravity();
	Vector2 buoyancyForce = new Vector2(-gravity.x * displacedMass,
			-gravity.y * displacedMass);
	fixtureBody.applyForce(buoyancyForce, polygonProperties.getCentroid(),
			true);

	/* Linear drag force */
	if (linearDrag != 0) {
		fixtureBody.applyForce(gravity.rotate90(1).nor().scl(linearDrag),
				polygonProperties.getCentroid(), true);
	}

	/* Apply drag and lift forces */
	int polygonVertices = clippedPolygon.length;
	for (int i = 0; i < polygonVertices; i++) {

		/* Apply drag force */

		/* End points and mid point of the edge */
		Vector2 firstPoint = clippedPolygon[i];
		Vector2 secondPoint = clippedPolygon[(i + 1) % polygonVertices];
		Vector2 midPoint = firstPoint.cpy().add(secondPoint).scl(0.5f);

		/*
		 * Find relative velocity between the object and the fluid at edge
		 * mid point.
		 */
		Vector2 velocityDirection = new Vector2(fixtureBody
				.getLinearVelocityFromWorldPoint(midPoint)
				.sub(fluidBody.getLinearVelocityFromWorldPoint(midPoint)));
		float velocity = velocityDirection.len();
		velocityDirection.nor();

		Vector2 edge = secondPoint.cpy().sub(firstPoint);
		float edgeLength = edge.len();
		edge.nor();

		Vector2 normal = new Vector2(edge.y, -edge.x);
		float dragDot = normal.dot(velocityDirection);

		if (dragDot >= 0) {

			/*
			 * Normal don't point backwards. This is a leading edge. Store
			 * the result of multiply edgeLength, density and velocity
			 * squared
			 */
			float tempProduct = edgeLength * density * velocity * velocity;

			float drag = dragDot * fluidDrag * tempProduct;
			drag = Math.min(drag, maxFluidDrag);
			Vector2 dragForce = velocityDirection.cpy().scl(-drag);
			fixtureBody.applyForce(dragForce, midPoint, true);

			/* Apply lift force */
			float liftDot = edge.dot(velocityDirection);
			float lift = dragDot * liftDot * fluidLift * tempProduct;
			lift = Math.min(lift, maxFluidLift);
			Vector2 liftDirection = new Vector2(-velocityDirection.y,
					velocityDirection.x);
			Vector2 liftForce = liftDirection.scl(lift);
			fixtureBody.applyForce(liftForce, midPoint, true);
		}
	}
}