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

The following examples show how to use toxi.geom.Vec3D#magnitude() . 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: VerletConstrainedSpring3D.java    From toxiclibs with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected void update(boolean applyConstraints) {
    Vec3D delta = b.sub(a);
    // add minute offset to avoid div-by-zero errors
    float dist = delta.magnitude() + EPS;
    float normDistStrength = (dist - restLength)
            / (dist * (a.invWeight + b.invWeight)) * strength;
    if (!a.isLocked && !isALocked) {
        a.addSelf(delta.scale(normDistStrength * a.invWeight).limit(limit));
        if (applyConstraints) {
            a.applyConstraints();
        }
    }
    if (!b.isLocked && !isBLocked) {
        b.subSelf(delta.scale(normDistStrength * b.invWeight).limit(limit));
        if (applyConstraints) {
            b.applyConstraints();
        }
    }
}
 
Example 2
Source File: VerletSpring3D.java    From toxiclibs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Updates both particle positions (if not locked) based on their current
 * distance, weight and spring configuration *
 */
protected void update(boolean applyConstraints) {
    Vec3D delta = b.sub(a);
    // add minute offset to avoid div-by-zero errors
    float dist = delta.magnitude() + EPS;
    float normDistStrength = (dist - restLength)
            / (dist * (a.invWeight + b.invWeight)) * strength;
    if (!a.isLocked && !isALocked) {
        a.addSelf(delta.scale(normDistStrength * a.invWeight));
        if (applyConstraints) {
            a.applyConstraints();
        }
    }
    if (!b.isLocked && !isBLocked) {
        b.addSelf(delta.scale(-normDistStrength * b.invWeight));
        if (applyConstraints) {
            b.applyConstraints();
        }
    }
}
 
Example 3
Source File: ParticleString3D.java    From toxiclibs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Creates a number of particles along a line and connects them into a
 * string using springs.
 * 
 * @param physics
 *            physics engine
 * @param pos
 *            start position
 * @param step
 *            step direction & distance between successive particles
 * @param num
 *            number of particles
 * @param mass
 *            particle mass
 * @param strength
 *            spring strength
 */
public ParticleString3D(VerletPhysics3D physics, Vec3D pos, Vec3D step,
        int num, float mass, float strength) {
    this.physics = physics;
    particles = new ArrayList<VerletParticle3D>(num);
    links = new ArrayList<VerletSpring3D>(num - 1);
    float len = step.magnitude();
    VerletParticle3D prev = null;
    pos = pos.copy();
    for (int i = 0; i < num; i++) {
        VerletParticle3D p = new VerletParticle3D(pos.copy(), mass);
        particles.add(p);
        physics.particles.add(p);
        if (prev != null) {
            VerletSpring3D s = createSpring(prev, p, len, strength);
            links.add(s);
            physics.addSpring(s);
        }
        prev = p;
        pos.addSelf(step);
    }
}
 
Example 4
Source File: DLAParticle.java    From toxiclibs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void update(Vec3D target) {
    Vec3D d = target.sub(this);
    if (d.magnitude() > escapeRadius) {
        set(opos);
        reorientate();
        d = target.sub(this);
    }
    Vec3D ndir = d.getNormalizedTo(searchSpeed);
    dir.interpolateToSelf(ndir, particleSpeed);
    addSelf(dir);
}
 
Example 5
Source File: NurbsCreator.java    From toxiclibs with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static void pointToLine3D(ReadonlyVec3D p, ReadonlyVec3D t,
        Vec3D top, Vec3D out) {
    Vec3D dir = top.sub(p);
    float hyp = dir.magnitude();
    out.set(p.add(t.scale(t.dot(dir.normalize()) * hyp)));
}