Java Code Examples for toxi.geom.Vec3D#magSquared()

The following examples show how to use toxi.geom.Vec3D#magSquared() . 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: FlockingBoidsShiffmanToxi.java    From haxademic with MIT License 6 votes vote down vote up
Vec3D align (ArrayList boids) {
	Vec3D steer = new Vec3D();
	int count = 0;
	for (int i = boids.size()-1 ; i >= 0 ; i--) {
		Boid other = (Boid) boids.get(i);
		if (this != other) {
			if (loc.distanceToSquared(other.loc) < neighborDist) {
				steer.addSelf(other.vel);
				count++;
			}
		}
	}
	if (count > 0) {
		steer.scaleSelf(1.0f/count);
	}

	// As long as the vector is greater than 0
	if (steer.magSquared() > 0) {
		// Implement Reynolds: Steering = Desired - Velocity
		steer.normalizeTo(maxspeed);
		steer.subSelf(vel);
		steer.limit(maxforce);
	}
	return steer;
}
 
Example 2
Source File: AttractionBehavior3D.java    From toxiclibs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void apply(VerletParticle3D p) {
    Vec3D delta = attractor.sub(p);
    float dist = delta.magSquared();
    if (dist < radiusSquared) {
        Vec3D f = delta.normalizeTo((1.0f - dist / radiusSquared))
                .jitter(jitter).scaleSelf(attrStrength);
        p.addForce(f);
    }
}
 
Example 3
Source File: FlockingBoidsShiffmanToxi.java    From haxademic with MIT License 5 votes vote down vote up
Vec3D separate (ArrayList boids) {
	Vec3D steer = new Vec3D();
	int count = 0;
	// For every boid in the system, check if it's too close
	for (int i = boids.size()-1 ; i >= 0 ; i--) {
		Boid other = (Boid) boids.get(i);
		if (this != other) {
			float d = loc.distanceTo(other.loc);
			// If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
			if (d < desiredSeparation) {
				// Calculate vector pointing away from neighbor
				Vec3D diff = loc.sub(other.loc);
				diff.normalizeTo(1.0f/d);
				steer.addSelf(diff);
				count++;
			}
		}
	}
	// Average -- divide by how many
	if (count > 0) {
		steer.scaleSelf(1.0f/count);
	}

	// As long as the vector is greater than 0
	if (steer.magSquared() > 0) {
		// Implement Reynolds: Steering = Desired - Velocity
		steer.normalizeTo(maxspeed);
		steer.subSelf(vel);
		steer.limit(maxforce);
	}
	return steer;
}