rotation - 3D projection to screen PHP -
i trying 3d object position on screen.
i have studied wiki atricle https://en.wikipedia.org/wiki/3d_projection (and many others) seems getting incorrect answers.
$center =array(0,0,0); $point = array(0, 30, 30); $rot = array(90,0,0); $a=array( array( 1, 0, 0), array( 0, cos($rot[0]), sin($rot[0])), array( 0, -sin($rot[0]), cos($rot[0])) ); $b=array( array( cos($rot[1]), 0, -sin($rot[1])), array( 0, 1, 0), array( sin($rot[1]), 0, cos($rot[1])) ); $c=array( array( cos($rot[2]), sin($rot[2]), 0), array( -sin($rot[2]), cos($rot[2]), 0), array( 0, 0, 1) ); $a=array( array($point[0]), array($point[1]), array($point[2]) ); $help = matrixmult(matrixmult($a,$b),$c); $c = matrixmult($help, $a); var_dump($c); function matrixmult($m1,$m2){ $r=count($m1); $c=count($m2[0]); $p=count($m2); if(count($m1[0])!=$p){throw new exception('incompatible matrixes');} $m3=array(); ($i=0;$i< $r;$i++){ for($j=0;$j<$c;$j++){ $m3[$i][$j]=0; for($k=0;$k<$p;$k++){ $m3[$i][$j]+=$m1[$i][$k]*$m2[$k][$j]; } } } return($m3); }
matrixmulti function got online , tested correct answer when multipling matrices (answers correct) , getting :
array(3) { [0]=> array(1) { [0]=> float(0) } [1]=> array(1) { [0]=> float(13.377691424142) } [2]=> array(1) { [0]=> float(-40.262108391892) } }
but seems incorrect answer, since point on y,z plane , rotating x 90 degrees should give me answer (0,30,-30) or (0,-30,30) depending on sign. wrong somewhere or missing?
php trigonometric functions take angles in radians not degrees, case in majority of languages.
$rot = array(90,0,0);
should be:
$rot = array(m_pi/2,0,0);
Comments
Post a Comment