/*
* Copyright (C) 2014 MillerV
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package aspect.physics;
import aspect.util.Vector3;
import aspect.entity.behavior.Behavior;
import aspect.util.Matrix4x4;
import java.util.LinkedList;
/**
*
* @author MillerV
*/
public class Motion extends Behavior {
public Vector3 velocity; // meters / second
public Vector3 acceleration; // meters / second ^ 2
public Vector3 angularVelocity;
public Vector3 angularAcceleration;
public Motion() {
velocity = Vector3.zero();
acceleration = Vector3.zero();
angularVelocity = Vector3.zero();
angularAcceleration = Vector3.zero();
}
public Motion(Vector3 vel, Vector3 acc) {
this.velocity = vel;
this.acceleration = acc;
this.angularVelocity = Vector3.zero();
this.angularAcceleration = Vector3.zero();
}
public Motion(Vector3 vel, Vector3 acc, Vector3 rvel, Vector3 racc) {
this.velocity = vel;
this.acceleration = acc;
this.angularVelocity = rvel;
this.angularAcceleration = racc;
}
@Override
public void update() {
Vector3 velOld = velocity;
velocity = Vector3.add(velocity, acceleration.times(Time.deltaTime()));
Vector3 avgVel = Vector3.divide(velOld.plus(velocity), 2.0f);
ent.transform.position = ent.transform.position.plus(avgVel.times(Time.deltaTime()));
Vector3 rVelOld = angularVelocity;
angularVelocity = angularVelocity.plus(angularAcceleration.times(Time.deltaTime()));
Vector3 avgRVel = Vector3.divide(rVelOld.plus(angularVelocity), 2.0f);
Matrix4x4 m = Matrix4x4.identity().rotate(avgRVel, Time.deltaTime());
ent.transform.up = m.transformVector(ent.transform.up);
ent.transform.forward = m.transformVector(ent.transform.forward);
}
}