Just implemented quaternions

K.I.L.E.R

Retarded moron
Veteran
Damn these junkies are smooth.

Although I find you have to multiply them by a value (preferbly time) in order to get faster rotations.

Could someone make sure my stuff is correct?

Code:
public Quaternion add(Quaternion p)
	{
		this.rotation += p.rotation;
		this.q.getDirection().x += p.q.getDirection().x;
		this.q.getDirection().y += p.q.getDirection().y;
		this.q.getDirection().z += p.q.getDirection().z;
		return this;
	}
	
	public Quaternion multiply(Quaternion p)
	{
		this.rotation = (this.rotation * p.rotation) - Operations.INSTANCE.getDotProduct(this.q, p.q);
		
		double x = p.q.getDirection().x * rotation;
		double y = p.q.getDirection().y * rotation;
		double z = p.q.getDirection().z * rotation;
		
		double x2 = q.getDirection().x *= p.rotation;
		double y2 = q.getDirection().y *= p.rotation;
		double z2 = q.getDirection().z *= p.rotation;
		
		Vector qXp = Operations.INSTANCE.getCrossProduct(q, p.q);
		
		this.q.getDirection().x = (float)(x + x2 + qXp.getDirection().x);
		this.q.getDirection().y = (float)(y + y2 + qXp.getDirection().y);
		this.q.getDirection().z = (float)(z + z2 + qXp.getDirection().z);
		
		return this;
	}
	
	public double getMagnitude()
	{
		return rotation * rotation + Operations.INSTANCE.getDotProduct(q, q);
	}
	
	public Quaternion calcInverse()
	{
		double invMag = 1 / this.getMagnitude();
		
		this.rotation *= invMag;
		this.q.getDirection().x = (float)(-this.q.getDirection().x * invMag);
		this.q.getDirection().y = (float)(-this.q.getDirection().y * invMag);
		this.q.getDirection().z = (float)(-this.q.getDirection().z * invMag);
		
		return this;
	}
 
Back
Top