Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Rotating Cube Problem

This thread is locked; no one can reply to it. rss feed Print
Rotating Cube Problem
Scooter
Member #16,799
January 2018

Hi all:

Please find above files I have been working on. The file cube_test2 shows

a 3D cube rotating right of center. Here is my problem: I want the cube to

rotate as shown but also to rotate around the center of the screen. The file

cube_test1 shows one rotating but that is all I could do with it. If anyone

could take a look at my files and show me what to do I would appreciate it.

3D rotation is a bear and I could use some help, Thanks and have a great day!

This was done using Linux.

DanielH
Member #934
January 2001
avatar

float dist = 14.0;  //distance from origin
...
glTranslatef(dist, 0, -45);

It's to the right because you translated it to the right. Set it to 0 if you want in the center. Or maybe I misunderstood the problem.

Rotation basics:
1. determine a point of center of rotation (x, y, z)
2. Translate all vertices of cube so that point (From step 1) is now at 0,0,0 (vec[i].x -= x, vec[i].y -= y, vec[i].z -= z)
3. Rotate all vertices
4. Inverse translate all vertices (vec[i].x += x, vec[i].y += y, vec[i].z += z)

If your cube is already at center then no need for translation.

Dizzy Egg
Member #10,824
March 2009
avatar

We’ve been here before and I provided full code…

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

Scooter
Member #16,799
January 2018

Dizzy:

I have absolutely no idea what you are talking about. If you are insinuating

this is a duplicate post you are dead wrong. I have NEVER posted this problem

before. Please provide proof. In the future please get your facts straight

before replying to my post. Thank you kindly for my consideration!

Dizzy Egg
Member #10,824
March 2009
avatar

Rubik Cube

You already had it rotating in the centre.

And you're still using 7 bitmaps instead of 6.

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

Scooter
Member #16,799
January 2018

Hey Dizzy:

I see you will never forget I made a mistake! Big deal! Actually you are wrong

again! It was 5 and should have been 6! Can't believe you forgot that! Especially

since you have never made a mistake! Please read my post carefully! I told you this

was different and it is!

The cube is NOT supposed to be in the center! Oh my! Could that be the reason

it is plotted 14 units right of center? Could very well be that is where I intend

it to be! So at the center of the screen if you would draw a circle 28 units in

diameter you would find that the circle would be drawn thru the center of the

rotating 3D cube. That circle is the path that I want the ROTATING cube to follow.

That's all it is to it! Being you think I am so STUPID, I could NOT find a way to

solve the problem. I hate to admit to you that I need help with it! I waited days

before I posted this because when I do, I always regret it! Sometimes it is

necessary! I am very sorry about that! Have a great day!!!!

DanielH
Member #934
January 2001
avatar

Ok, take a breath. Count to 10.

Scooter, if what I said didn't work, draw a picture so I have a better understanding of what the problem is.

Scooter said:

I want the cube to rotate as shown

Nothing was shown. I don't really understand the problem.

Like I said before: If the origin is not the center of rotation then find the point at the center of rotation (x, y, z). Everything that you want to be rotated will need to be translated (-= x, -= y, -=z). Now that center of rotation is at the origin. Do the rotation. Translate everything back the inverse amount (+= x, += y, += z).

Scooter
Member #16,799
January 2018

Hi DanielH:

Thanks for the reply and an offer to help me. If you ran cube_test2 file

you should see a 3D cube rotating on the left side of the screen. This file

was part of a zip file. Hope you are with me so far. This rotating 3D cube is

located 14 units to the direct left of screen center (0, 0, 0). This is what

you should be looking at right now, nothing else. Now go back to (0, 0, 0) at

screen center. At this point draw, in your mind, a circle 28 units in diameter.

This circle will pass directly thru the center of the 3D rotating cube. This

is the path I want the 3D rotating cube to follow around the center of the

screen. The 3D cube is ALWAYS rotating. Hope this helps, but if something is

NOT clear, please let me know and I will be happy to fix an image for you.

Thanks for your time. Have a great day!

Indeterminatus
Member #737
November 2000
avatar

So if I understand you correctly, you want the cube to rotate around itself, and you also want the cube to move along a circular path?

Without looking at your code, it appears that the rotating-around-itself-bit does already work the way you want it to.
You also seem to have managed translating the center of the cube while maintaining the rotating-around-itself-bit.

What's missing now is to move the center-translation around on a circular path. Correct?

For the future, the likelihood of quickly receiving helpful answers increases with how easy it is to grasp your problem. A picture/sketch of what you're trying to accomplish, and probably a screenshot of your actually running application (that outlines the difference).

That being said, everyone makes mistakes; there's no shame in admitting you're stuck and in asking for help. The community here usually goes overboard in providing aid.

_______________________________
Indeterminatus. [Atomic Butcher]
si tacuisses, philosophus mansisses

Scooter
Member #16,799
January 2018

Hi Indeterminatus:

You are correct. Please check out file cube_test2. This will show where I am

at this time. Translating the rotating cube around the circle path is what I am

having a problem with. I have tried everything I can think to do but no go. I

tried using two timers with two different angle variables but codeblocks would

would not compile it. I tried a separate loop for translating around the

circular path but again a no go. I think one of these two is probably the

answer.

I have been working on this for quite some time and finally decided I was not

going to figure this out myself. If you still need an image let me know.

Thanks for your time and have a great day.

Dizzy Egg
Member #10,824
March 2009
avatar

The attached file will get you started. It rotates the cube around itself, and also around a circle in the centre.

If you want to increase the size of the circle, you can increase the values:

    double x = 1.0 * cos(radius); //Increase 1.0 for bigger circle
    double y = 1.0 * cos(radius); //Increase 1.0 for bigger circle

on lines 312, 313.

To change the speed increase/decrease the line alpha += 0.05 on line 308.

The deltaX and deltaY positions (used in the translate) are calculated using the cosine function, which uses the radius and the alpha (0-360):

    double x = 1.0 * cos(radius);
    double y = 1.0 * cos(radius);
    double deltaX = x * cos(alpha) - x * sin(alpha);
    double deltaY = y * cos(alpha) + y * sin(alpha);

It works as expected, but you'd probably want to clean it up, create proper methods or timers, but should be enough to get you started.

I'm sure someone else can provide a more elegant solution.

[EDIT] actually no need for a timer, you can control the speed by increasing/decreasing the amount you add to alpha.

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

There are easier ways to do this. Ones that don't involve moving or rotating the cube at all. Do it with transformations.

First step to a 3D scene is the projection matrix and the camera matrix.

If you have both of those, then setup your view matrix and alter like so for the cube :

#SelectExpand
1ALLEGRO_TRANSFORM old = *al_get_current_transform(); 2 3ALLEGRO_TRANSFORM t; 4al_identity_transform(&t) 5/// We are at the camera. To move or rotate the world, we need to translate and then scale or rotate, and then translate back. 6 7al_translate_transform_3d(&t , -cubex , -cubey , -cubez);/// This centers the view on our cube 8al_rotate_transform_3d(&t , 1.0 , 0 , 0.0 , 360.0*M_PI/2.0*60.0);/// This will spin your cube around the x axis 9al_translate_transform_3d(&t , -14 , 0 , 0);/// This moves our cube 14 left 10al_rotate_transform_3d(&t , 1.0 , 0 , 0 , radians around circle);/// This will move our cube in a circle 11al_translate_transform_3d(&t , 14 , 0 , 0);/// This moves the camera back 12al_translate_transform_3d(&t , cubex , cubey , cubez);/// This moves the camera back to its original place 13al_use_transform(&t); 14/// Draw cube 15al_use_transform(&old);

Dizzy Egg
Member #10,824
March 2009
avatar

I'd be tempted to follow Edgars advice, as his knowledge of this stuff is greater than mine.

To give you options for playing around though, I've updated the attached version with a function you can call (just above draw_scene) that lets you rotate the cube by sending a circle size and speed.

Have fun!

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

Scooter
Member #16,799
January 2018

Hi Dizzy:

Thanks, absolutely beautiful! Works fine! I agree, Edgar has this figured

out completely! You are also good at this, much better than I am.

Thanks again and please forgive me for my actions!

Dizzy Egg
Member #10,824
March 2009
avatar

You’re welcome, and no need for forgiveness, it was me that was in the wrong!

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

Go to: