Package boids

Source Code of boids.Boid

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package boids;

import aspect.entity.Entity;
import static aspect.resources.Resources.*;
import aspect.util.Color;
import static aspect.core.AspectRenderer.GeometryType.*;
import static aspect.core.AspectRenderer.*;
import aspect.entity.behavior.Behavior;
import aspect.physics.RigidBody;
import aspect.physics.force.Force;
import aspect.physics.force.IntensityModel;
import aspect.util.Vector2;
import aspect.util.Vector3;
import boids.physics.Alignment;
import boids.physics.Cohesion;

/**
*
* @author MillerV
*/
public class Boid extends Entity {
    public Force cohesion;
    public Force alignment;
    public Force separation;
   
    public Boid() {
        Color color = Color.random(false);
       
        float[] vertices = {
            0, 0, 50, 0
        };
       
        //addComponent(new VBOModel2D(LINES, vertices, colors(color, 2)));
        addBehavior(ellipse(color, 16, 16, 10));
        addBehavior(new RigidBody());
       
        cohesion = new Cohesion(this, Boids.BOID_COHESION, IntensityModel.INVERSE);
        alignment = new Alignment(this, Boids.BOID_ALIGNMENT, IntensityModel.INVERSE);
        separation = new Cohesion(this, -Boids.BOID_SEPARATION, IntensityModel.INVERSE_SQUARE);
       
        pos.x = getCanvasWidth() * (float)Math.random();
        pos.y = getCanvasHeight() * (float)Math.random();
       
        RigidBody rb = rigidBody();
       
        rb.vel = Vector2.fromAngle((float)Math.random() * 360, 200).asXY();
       
       
        addBehavior(new Behavior() {
            @Override
            public void update() {
                RigidBody rb = ent.rigidBody();
                if (ent.pos.x > getCanvasWidth() || ent.pos.x < 0) {
                    rb.vel.x *= -1;
                }
               
                if (ent.pos.y > getCanvasHeight() || ent.pos.y < 0) {
                    rb.vel.y *= -1;
                }
               
                if (rb.vel.mag() > 201f) {
                    //rb.vel = rb.vel.normalize().times(200);
                }
            }
           
            @Override
            public void render() {
                loadIdentity();
                begin(LINES);
                Vector3 force = ent.rigidBody().netForce();
                Vector3 vel = ent.rigidBody().vel.normalize().times(50);
                /*
                color(Color.RED);
                vertex(ent.pos.xy());
                vertex(ent.pos.plus(force).xy());*/
                color(Color.BLUE);
                vertex(ent.pos.xy());
                vertex(ent.pos.plus(vel).xy());
                end();
            }
        });
               
    }
}
TOP

Related Classes of boids.Boid

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.