I love Math

Yes I know I’m not writing as often as i should but today i wanted to share with you what crushed my brain for the past two hours
trying to understand quaternions.

function multiply($quad) {
$p = new Math_Quaternion();
$p->real = $this->real * $quad->real - $this->i *$quad->i - $this->j* $quad->j - $this->k * $quad->k;
$p->i = $this->real * $quad->i + $this->i * $quad->real+ $this->j * $quad->k - $this->k * $quad->j;
$p->j = $this->real * $quad->j - $this->i * $quad->k + $this->j * $quad->real + $this->k * $quad->i;
$p->k = $this->real * $quad->k + $this->i * $quad->j - $this->j * $quad->i + $this->k * $quad->real;

return $p;
}

static function fromRotationVector($x,$y,$z) {
$q = new self();
$x = deg2rad($x);
$y = deg2rad($y);
$z = deg2rad($z);
$q->real = cos($x/2)*cos($y/2)*cos($z/2)+sin($x/2)*sin($y/2)*sin($z/2);
$q->i = sin($x/2)*cos($y/2)*cos($z/2)-cos($x/2)*sin($y/2)*sin($z/2);
$q->j = cos($x/2)*sin($y/2)*cos($z/2)+sin($x/2)*cos($y/2)*sin($z/2);
$q->k = cos($x/2)*cos($y/2)*sin($z/2)-sin($x/2)*sin($y/2)*cos($z/2);
return $q;
}

function getRotationVector() {
$x = atan((2*($this->real*$this->i+$this->j*$this->k))/(1-2*($this->i*$this->i+$this->j*$this->j)));
$y = asin(2*($this->real*$this->j - $this->k*$this->i));
$z = atan((2*($this->real*$this->k+$this->i*$this->j))/(1-2*($this->j*$this->j+$this->k*$this->k)));
$x = rad2deg($x);
$y = rad2deg($y);
$z = rad2deg($z);
return array('x'=>$x,'y' => $y,'z'=>$z);
}

Just wanted to mention ;)
stay tuned
Thorian

Leave a Comment