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

  • SEARCH
  • Home
  • 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 7997969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:06:14+00:00 2026-06-04T15:06:14+00:00

I have created a cube using OpenGL, but at the moment it has free

  • 0

I have created a cube using OpenGL, but at the moment it has free rotation so it rotates in any direction.

How can I code it so it only rotates up, down, left and right?

Here’s my my code for rotations:

    + (void)applyRotation:(GLfloat *)m x:(GLfloat)x y:(GLfloat)y z:(GLfloat)z {
    GLfloat tempMatrix[16];

    if(x != 0) {
    GLfloat c = cosf(x);
    GLfloat s = sinf(x);

    [self applyIdentity:tempMatrix];

    tempMatrix[5] = c;
    tempMatrix[6] = -s;
    tempMatrix[9] = s;
    tempMatrix[10] = c;

    [self multiplyMatrix:tempMatrix by:m giving:m];
    }

    if(y != 0) {
    GLfloat c = cosf(y);
    GLfloat s = sinf(y);

    [self applyIdentity:tempMatrix];

    tempMatrix[0] = c;
    tempMatrix[2] = s;
    tempMatrix[8] = -s;
    tempMatrix[10] = c;

    [self multiplyMatrix:tempMatrix by:m giving:m];
    }

    if(z != 0) {
    GLfloat c = cosf(z);
    GLfloat s = sinf(z);

    [self applyIdentity:tempMatrix];

    tempMatrix[0] = c;
    tempMatrix[1] = -s;
    tempMatrix[4] = s;
    tempMatrix[5] = c;

    [self multiplyMatrix:tempMatrix by:m giving:m];
    }
    }
  • 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-06-04T15:06:17+00:00Added an answer on June 4, 2026 at 3:06 pm

    “each rotation should be only 90*n degrees around one of the axes”

    The rotation matrices in this case are almost trivial and consist only of zeros, ones and minus ones.

    sin(90 * n) = 0, 1, 0, -1, 0, 1, 0, -1, ...
    cos(90 * n) = 1, 0, -1, 0, 1, 0, -1, 0, ...
    

    Rotation around Oz (we assume a = 90 * n):

    | cos(a)  -sin(a)   0 |
    | sin(a)   cos(a)   0 | 
    |  0        0       1 |
    

    Rotation around Ox:

    |   1      0      0    |
    |   0    cos(a) sin(a)  |
    |   0   -sin(a) cos(a)  |
    

    Rotation around Oy:

    |  cos(a)   0   sin(a)  |
    |   0       1     0    |
    |  -sin(a)  0   cos(a)  |
    

    Unroll the multiplication of vectors by these matrices and you will get the required rotations with minimal roundoff errors.

    If you want the absolute precision, store the original (x,y,z) vector and for each rotation triple (ax, ay, az) calculate the R matrix (of ones and zeros) or just remember that these matrices represent the even permutations of the basis vectors so you can rotate the (x,y,z) with swaps only.

    One more thing. If you’re thinking about smooth rotation from side to side of the cube, then you have to look for SLerp and interpolate the Src-to-Dest rotation using some animation timer.

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

Sidebar

Related Questions

I have created a 3d cube in JOGL using the following code: /** *
I have a 3D cube created using GL_TRIANGLE_STRIP.Is it possible to draw points(using GL_POINTS)
I have created a rotating CUBE with the help of OPENGL in Android now
I have the following scenario. A cube created in SSAS 2008. I can connected
I have created a basic site using ASP.NET routing according to Mike Ormond's example
I have created a file with XmlDocument xmldoc = new XmlDocument(); Can I make
I have created one table using the below command: create table Table1( Id int
I have created an ActiveX dll using VB6 and packaged it using the Package
Currently I have a 3D Cube that I drew by writing coordinates, that can
I need to draw a gridded image using opengl. I've read that images created

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.