rotate the scene

52 views
Skip to first unread message

Yanev

unread,
Jul 20, 2011, 8:15:51 AM7/20/11
to PhiloGL
Hi,
I m a student and I have some knowledge of OpenGL from school.

I've been trying for a while now, to make my scene rotate around the
coordinate origin (0,0,0) as a whole, when i press the left/right
arrows on the keyboard.
The problem is that they do rotate but not as a whole, all of them
have fixed X-axis positions and stay on those positions while rotating
around Y, and what i get is a mess!

What i do in the code is change the positions: ....
moon.position.set(1,1.7,z); ........... to put my object where i want
it to be on the scene, but when i rotate the scene they stay on that
position instead of getting new coordinates.
I think that this can be solved with translating the objects matrices
instead of giving each object a different fixed position? In openGL
it's pretty simple, u have Push/PopMatrix() and inside that u can do
whatever u want with the object(translation, rotation, scale...).


Some help with this?

Nicolas Garcia Belmonte

unread,
Jul 21, 2011, 11:14:10 AM7/21/11
to phi...@googlegroups.com
Hi,

Yeah... I need to add some simple scene graph capability to the framework. I've been working on many things in the framework, (but not sharing anything yet), probably will work on this in the next few weeks.

I'm not sure on what axis you're trying to rotate the objects, but lets say you have an object 'obj', on (1, 0, 1) and your center of rotation 'c' is (0.5, 0.5, 0.5), and you would like to make a rotation around the Y axis of angle 'alpha', then the rotation could be calculated by:

//take the distance of the object to the center of rotation and its current angle
var  pos  = obj.position,
       dist = Math.sqrt((pos.x - c.x) * (pos.x - c.x) + (pos.y - c.y) * (pos.y - c.y) + (pos.z - c.z) * (pos.z - c.z)),
       currentAlpha = Math.atan2(pos.z, pos.x),
       //get the final angle the object should be at by adding its current angle and the displacement
       endAlpha = alpha + currentAlpha;

//update the object position with this new angle:
pos.x = Math.cos(endAlpha) * dist;
pos.z = Math.cos(endAlpha) * dist;

//update the rotation of the object
obj.rotate.set(0, alpha, 0);

//update coordinates and matrix
obj.update();

I'm not sure the math is fine, but I guess this should be a good start. I'm thinking about adding a Node class and have a tree structure in which multiple objects can be manipulated at the same time, this is something I'll definitely work on.

Let me know if you have any further questions.
--
Nicolas Garcia Belmonte - http://philogb.github.com/

Antonio González Peña

unread,
Sep 8, 2011, 6:58:13 PM9/8/11
to PhiloGL
Hi Nicolas,

Just to check if there is any updates on this topic or if there is an
easier way to rotate 2 elements in the same scene (rotate the scene).

Thanks

Nicolas Garcia Belmonte

unread,
Sep 9, 2011, 1:29:29 PM9/9/11
to phi...@googlegroups.com
A scene rotation is a camera movement... I should implement probably a
scene.rotate, scene.translate, scene.scale that would apply those
transformations to the camera under the hood.

Unfortunately I haven't made any progress on this.

2011/9/8 Antonio González Peña <antg...@gmail.com>:

--

Reply all
Reply to author
Forward
0 new messages