Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6021631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:44:29+00:00 2026-05-23T03:44:29+00:00

(iPhone) I’m trying to make it so that the user can rotate an object

  • 0

(iPhone) I’m trying to make it so that the user can rotate an object by dragging his finger on the screen. Dragging left or right should rotate around the Y-axis, and dragging up or down should rotate around the X-axis. Here’s my original code:

glRotatef( yAmt, 0, 1, 0 );
glRotatef( xAmt, 1, 0, 0 );

The rotation is all crazy and seems to go in random directions. I’m pretty sure it has to do with the second statement being dependent on the first statement, but I’ve tried to negate that with some fancy math and still couldn’t get it right.

Well I partially figured it out, but now I have a new problem (how to sum two rotations?).

MY SOLUTION:

Let’s say you wanted to rotate the cube left 90 then down 90. You try this:

glRotatef( 90, 0, 1, 0 );
glRotatef( 90, 1, 0, 0 );

It doesn’t work. The second function behaves strangely because the first function has swapped the X and Z axes. So you try this:

glRotatef( 90, 0, 1, 0 );
glRotatef( 90, 0, 0, 1 );

Now you have the desired result, but if you change the amount of Y rotation in the first function to 180 or 45 or whatever, it fails again. The desired rotation vector is dependent of the amount of Y rotation from the first function. Desired result is achieved with this:

glRotatef( yAmt, 0, 1, 0 );
glRotatef( xAmnt, cos(yAmt), 0, sin(yAmt) );

Now what if you want to rotate up and down first and then left to right? This last solution fails because, first of all, the functions are in the wrong order, and second of all, the Y axis is also dependent on the X rotation. You have to account for this, AND you have to combine the two functions, resulting in:

glRotatef( totalAmt, xFactor*cos(yAmt), yFactor*cos(xAmt), xFactor*sin(yAmt) + yFactor*sin(xAmt) );

Where xFactor and yFactor add up to desired rotation vector (1,0 for up and down, 0,1 for left and right, sqrt(.5), sqrt(.5) for diagonal rotation).

MY PROBLEM:

The problem comes in when the user finishes one rotation and starts a new one. You have to remember and “reinact” the previous rotation, otherwise you get an undesired “jump” back to 0 rotation. One new rotation is fine, I can implement touchesEnded to remember the parameters of glRotatef and then plug them in to a new rotation like 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 the second new rotation, I now have two rotations to “reinact”. I can’t figure out what values to assign to amt_old, x_old, y_old, and z_old. What it boils down to is…how do you sum two glRotatef rotations?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:44:30+00:00Added an answer on May 23, 2026 at 3:44 am

    Got the answer from a user on another forum:

    // init the matrix

    … view init method

    theCurrentMatrix = CATransform3DIdentity;
    

    …

    // computing rotation when user touch screen :

    - (void) rotateX:(float)inX Y:(float)inY
    {
         GLfloat lAngle=sqrt(inX*inX+inY*inY);
    
     if (lAngle != 0.0f)
     {
          static const float _degToRad = M_PI / 180.0;
                // we compute a rotation matrix (in radians)
          CATransform3D lRotation = CATransform3DMakeRotation(lAngle * _degToRad, inX, inY, 0.0f);
    
                // apply the rotation to the current matrix (cumulative effect)
                // order is important
          theCurrentMatrix = CATransform3DConcat(theCurrentMatrix, lRotation);
     }
    

    }

    - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
         // Get touch
         UITouch *touch = [[touches allObjects] objectAtIndex:0];
    
         if ([touches count] == 1)
         {
              UITouch *touch = [[touches allObjects] objectAtIndex:0];
              CGPoint location = [touch locationInView:touch.view];
          CGPoint previousLocation = [touch previousLocationInView:touch.view];
    
          CGFloat lDeltaX = (location.x - previousLocation.x)*1.0;
          CGFloat lDeltaY = (location.y - previousLocation.y)*1.0;
    
          // rotate the current matrix
          [self rotateX:lDeltaYY:lDeltaX];
     }
    

    }

    // Applying the rotation(s)
    …

    glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();
    
    
     glTranslatef(0.0f,0.0f,-100.0f);   // move a little to see the object
    
    
     glMultMatrixf(theCurrenMatrixPtr]);
    

    … draw your objects

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing an iPhone app that uses the built-in SQLite database. I'm trying to
Does iPhone support XML-RPC, Is their any open source framework which I can use?
Can iPhone applications compiled against 2.1 be successfully installed via iTunes on a 2.0
When I load my iPhone app it always loads a black screen first then
I have an iPhone app that hides the status bar. However, my main view
I have a simple iphone app that's based on the CrashLanding sample app. So
iPhone Apps built for the simulator are stored here: /Users/<username>/Library/Application Support/iPhone Simulator/User/Applications Is it
iPhone resources by default show up in a Resources group that's visible in the
iPhone application, which has two buttons -right side- of Navigation Bar title. I want
Some iPhone applications, such as Pandora seem to directly manipulate the hardware volume and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.