Newer
Older
CarrotServer / src / game / Entity.java
t-nakanishi on 18 Jul 2017 1 KB [add] project
package game;

import framework.model3D.BaseObject3D;
import framework.model3D.GeometryUtility;
import framework.model3D.Object3D;
import framework.model3D.Placeable;
import framework.model3D.Position3D;
import framework.model3D.Quaternion3D;
import framework.physics.Solid3D;
import java3d.AxisAngle4d;
import java3d.Transform3D;
import java3d.TransformGroup;
import java3d.Vector3d;

public class Entity implements Placeable {
protected Object3D body;
protected Vector3d direction = new Vector3d(1.0, 0.0, 0.0);

	@Override
	public TransformGroup getTransformGroupToPlace() {
		// TODO Auto-generated method stub
		return body.getTransformGroupToPlace();
	}

	@Override
	public BaseObject3D getBody() {
		// TODO Auto-generated method stub
		return body;
	}
	
	public Position3D getPosition() {
		return body.getPosition3D();
	}
	
	public void setPosition(Position3D p) {
		body.apply(p, false);
	}

	public Vector3d getDirection() {
		Vector3d dir = new Vector3d(direction);
		Transform3D trans = new Transform3D();
		trans.set(((Solid3D)body).getQuaternion().getAxisAngle());
		trans.transform(dir);
		return dir;
	}	
	
	public void setDirection(Vector3d vec) {
		Vector3d v1 = new Vector3d();
		Vector3d v2 = new Vector3d();
		v1.cross(direction, GeometryUtility.Y_AXIS);
		v2.cross(vec, GeometryUtility.Y_AXIS);
		if (v2.length() < GeometryUtility.TOLERANCE) return;
		v1.normalize();
		v2.normalize();
		double cos = v1.dot(v2);
		v1.cross(v1, v2);
		double sin = v1.dot(GeometryUtility.Y_AXIS);
		double angle = Math.atan2(sin, cos);
		AxisAngle4d axisAngle = new AxisAngle4d(GeometryUtility.Y_AXIS, angle); 
		Quaternion3D quat = new Quaternion3D(axisAngle);
		((Solid3D)body).apply(quat, false);
	}
	
	public void setInitialDirection(Vector3d dir) {
		direction = dir;
	}
	
}