/*
* 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.physics;
import aspect.entity.Entity;
import aspect.physics.force.Force;
import aspect.physics.force.IntensityModel;
import aspect.util.Matrix4x4;
import aspect.util.Trig;
import aspect.util.Vector3;
/**
*
* @author MillerV
*/
public class Cohesion implements Force {
public Entity ent;
public float strength;
public IntensityModel model;
public Cohesion(Entity ent, float strength, IntensityModel model) {
this.ent = ent;
this.strength = strength;
this.model = model;
}
@Override
public Vector3 getForce(Entity ent2) {
if (ent2 == ent) {
return Vector3.zero();
}
Vector3 cur = ent2.rigidBody().vel.normalize();
Vector3 dest = ent.pos.minus(ent2.pos).normalize();
Vector3 cross = Vector3.cross(dest, cur);
Vector3 direction = Matrix4x4.identity().rotate(cross, 90).apply(cur);
float magnitude = model.multiplier(Vector3.distance(ent.pos, ent2.pos)) * Trig.sin(Vector3.angleBetween(cur, dest)) * strength;
return direction.times(magnitude);
}
}