Android Open Source - Verlet Angle Constraint






From Project

Back to project page Verlet.

License

The source code is released under:

Copyright 2013 Devicement http://devicement.com/ Copyright 2013 Sub Protocol and other contributors http://subprotocol.com/ Permission is hereby granted, free of charge, to any person obtaining a co...

If you think the Android project Verlet listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.verletandroid.VerletCore.Contraints;
/* w  w  w.  ja  v a2  s  . c om*/
import android.graphics.Color;
import android.graphics.Paint;
import org.verletandroid.Graphics.IGraphics;
import org.verletandroid.VerletCore.Particle;
import org.verletandroid.VerletCore.Vec2;

/**
 * Created with IntelliJ IDEA.
 * User: George
 * Date: 03.05.13
 * Time: 11:31
 * To change this template use File | Settings | File Templates.
 */
public class AngleConstraint implements IConstraint {

    private final Particle a;
    private final Particle b;
    private final Particle c;
    private final float angle;
    private final float stiffness;
    private int color = Color.argb((int) (0.2f*255), 255, 255, 0);

    Vec2[] drawPath = {  new Vec2(),
            new Vec2(),
            new Vec2()};

    public AngleConstraint(Particle a, Particle b, Particle c, float stiffness) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.angle = this.b.pos.angle2(this.a.pos, this.c.pos);
        this.stiffness = stiffness;
    }

    @Override
    public void relax(float stepCoef) {
        float angle = this.b.pos.angle2(this.a.pos, this.c.pos);
        float diff = angle - this.angle;

        if (diff <= -Math.PI)
            diff += 2*Math.PI;
        else if (diff >= Math.PI)
            diff -= 2*Math.PI;

        diff *= stepCoef*this.stiffness;

        this.a.pos = this.a.pos.rotate(this.b.pos, diff);
        this.c.pos = this.c.pos.rotate(this.b.pos, -diff);
        this.b.pos = this.b.pos.rotate(this.a.pos, diff);
        this.b.pos = this.b.pos.rotate(this.c.pos, -diff);
    }

    @Override
    public void draw(IGraphics graphics) {
        graphics.setColorPen(color);
        graphics.setStrokeWidth(5);
        graphics.setPenStyle(Paint.Style.STROKE);

        drawPath[0].mutableSet(this.a.pos.x, this.a.pos.y);
        drawPath[1].mutableSet(this.b.pos.x, this.b.pos.y);
        drawPath[2].mutableSet(this.b.pos.x, this.b.pos.y);

        graphics.drawPath(drawPath);
    }

    @Override
    public Vec2 getPos() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
}




Java Source Code List

com.northerneyes.VerletObjects.SpiderWeb.java
com.northerneyes.VerletObjects.Spider.java
com.northerneyes.VerletObjects.Tree.java
com.northerneyes.activities.MainActivity.java
com.northerneyes.activities.SampleList.java
org.verletandroid.Graphics.Graphics.java
org.verletandroid.Graphics.IGraphics.java
org.verletandroid.Graphics.IPixmap.java
org.verletandroid.Graphics.PixmapFormat.java
org.verletandroid.Graphics.Pixmap.java
org.verletandroid.Handlers.AccelerometerVerletHandler.java
org.verletandroid.Handlers.InputVerletHandler.java
org.verletandroid.Handlers.MultyTouchVerletHandler.java
org.verletandroid.VerletCore.Composite.java
org.verletandroid.VerletCore.IEntity.java
org.verletandroid.VerletCore.Particle.java
org.verletandroid.VerletCore.Utils.java
org.verletandroid.VerletCore.Vec2.java
org.verletandroid.VerletCore.Verlet.java
org.verletandroid.VerletCore.Contraints.AngleConstraint.java
org.verletandroid.VerletCore.Contraints.DistanceConstraint.java
org.verletandroid.VerletCore.Contraints.IConstraint.java
org.verletandroid.VerletCore.Contraints.PinConstraint.java
org.verletandroid.VerletCore.Objects.Cloth.java
org.verletandroid.VerletCore.Objects.LineSegments.java
org.verletandroid.VerletCore.Objects.Tire.java
org.verletandroid.componets.IUpdatable.java
org.verletandroid.componets.RenderView.java