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.
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.
We’ve been here before and I provided full code…
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!
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!!!!
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.
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).
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!
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.
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.
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:
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):
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.
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 :
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!
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!
You’re welcome, and no need for forgiveness, it was me that was in the wrong!