iphone - 2-dimensional glRotatef() -
(iphone) i'm trying make user can rotate object dragging finger on screen. dragging left or right should rotate around y-axis, , dragging or down should rotate around x-axis. here's original code:
glrotatef( yamt, 0, 1, 0 ); glrotatef( xamt, 1, 0, 0 ); the rotation crazy , seems go in random directions. i'm pretty sure has second statement being dependent on first statement, i've tried negate fancy math , still couldn't right.
well partially figured out, have new problem (how sum 2 rotations?).
my solution:
let's wanted rotate cube left 90 down 90. try this:
glrotatef( 90, 0, 1, 0 ); glrotatef( 90, 1, 0, 0 ); it doesn't work. second function behaves strangely because first function has swapped x , z axes. try this:
glrotatef( 90, 0, 1, 0 ); glrotatef( 90, 0, 0, 1 ); now have desired result, if change amount of y rotation in first function 180 or 45 or whatever, fails again. desired rotation vector dependent of amount of y rotation first function. desired result achieved this:
glrotatef( yamt, 0, 1, 0 ); glrotatef( xamnt, cos(yamt), 0, sin(yamt) ); now if want rotate , down first , left right? last solution fails because, first of all, functions in wrong order, , second of all, y axis dependent on x rotation. have account this, , have combine 2 functions, resulting in:
glrotatef( totalamt, xfactor*cos(yamt), yfactor*cos(xamt), xfactor*sin(yamt) + yfactor*sin(xamt) ); where xfactor , yfactor add desired rotation vector (1,0 , down, 0,1 left , right, sqrt(.5), sqrt(.5) diagonal rotation).
my problem:
the problem comes in when user finishes 1 rotation , starts new one. have remember , "reinact" previous rotation, otherwise undesired "jump" 0 rotation. 1 new rotation fine, can implement touchesended remember parameters of glrotatef , plug them in new rotation this:
glrotatef( amt_old, x_old, y_old, z_old ); glrotatef( totalamt, xfactor*cos(yamt), yfactor*cos(xamt), xfactor*sin(yamt) + yfactor*sin(xamt) ); but on second new rotation, have 2 rotations "reinact". can't figure out values assign amt_old, x_old, y_old, , z_old. boils down is...how sum 2 glrotatef rotations?
are xamt , yamt in pixels? input glrotatef in degrees (edit: meant radians, of course)? if so, multiply small number; try (float)(m_pi/180) one-pixel-per-degree.
the 2 rotations not commutative. if you're sufficiently bothered, can try calculate combined axis of rotation (normalize vector, pick perpendicular one, , rotate length of original vector), negligible sufficiently small rotations.
edit: perhaps want glgetmatrix() , glloadmatrix().
Comments
Post a Comment