Object rotation at camera look at direction

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
simdev
Posts: 8
Joined: Tue Mar 11, 2008 3:18 pm
Location: Bursa,Turkey

Object rotation at camera look at direction

Post by simdev »

Hi all,
I want to rotate my object node at camera look at direction, any body knows how to do it ?
In the former mails there is object rotation but not in the camera axis direction. Only you can rotate the node in its native coordinate space.
I investigated maya camera but could not resolve for this situation

Thanks
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Not really sure what you want to do... You want the object to look in the same direction as the camera... or the point the camera is looking at?
Image Image Image
Xplod
Posts: 32
Joined: Sat Mar 29, 2008 5:48 pm
Contact:

Post by Xplod »

node->setRotation(camera->getRotation());

I suppose
I'm Ouverta on the chat |
Project leader/Lead Developer of Ouverta project.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

camera->getRotation is likely to not return the camera's rotation as the camera doesn't set its own rotation so unless you've set the camera's rotation yourself (which won't affect where it's looking) it will just return 0,0,0 i guess.

this is because the direction the camera is looking is specified by a target position in 3D space at which it should look, rather than a rotation of the node representing the camera.
Image Image Image
simdev
Posts: 8
Joined: Tue Mar 11, 2008 3:18 pm
Location: Bursa,Turkey

Post by simdev »

To clarify, I am giving it as an example
we have a fixed camera looking at target direction (0,0,1) and positioned at (0,0,0)
now we have an object(node) positioned at (0,0,1) in front of the camera rotation (0,90,0)
To rotate object we can use the function

//--- rotate node relative to its current rotation -used in turn,pitch,roll ---
void rotate(irr::scene::ISceneNode *node, irr::core::vector3df rot)
{
irr::core::matrix4 m;
m.setRotationDegrees(node->getRotation());
irr::core::matrix4 n;
n.setRotationDegrees(rot);
m *= n;
node->setRotation( m.getRotationDegrees() );
node->updateAbsolutePosition();
}

But this rotation is done in the local coordinate of the node not at camera lookat direction.
We can also get the camera look at vector by using "camera ray =GetRayFromScreenCoordinates ".
Now the problem is how to use the camera ray to modify the rot variable stated in the rotation function above.
Last edited by simdev on Thu Jul 03, 2008 1:27 pm, edited 1 time in total.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Just out of interest why do you want to do this?

And i'm not sure i really understand how the camera could really have any affect on how you'd want the node to rotate... Maybe if you explain the reason behind it....
Image Image Image
simdev
Posts: 8
Joined: Tue Mar 11, 2008 3:18 pm
Location: Bursa,Turkey

Post by simdev »

I have an object at my view and I want to see different sides of the object by mouse controlled rotations. (I am not using maya or fps camera) Of course another option is to position and rotate the camera but I dont want to move the camera just rotate the object. CAD/CAM programs can do it.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

OK, is your core requirement that the object rotates in a way that's visually consistent with the mouse movement?

So, when you move the mouse to the right, you want the object to appear to rotate towards the right of the screen using whatever axis rotations are necessary to achieve that?

Code: Select all

Camera view   Camera up    Required rotation
===========   =========    =================
0,0,1         0,1,0        clockwise around Y
0,0,-1        0,1,0        clockwise around Y
0,0,1         0,-1,0       anticlockwise around Y
0,-1,0        0,0,1        clockwise around X
0,-1,0        0,0,-1       anticlockwise around X
If that's the case, then the required object rotation would seem to be dependent on the camera's up vector, not its view vector. I'll wait for you to confirm or clarify the problem before pushing on any further towards an up-vector based solution.

I fear that it may be an exploding head problem, since Irrlicht uses Euler rotations, and it may not be trivial to calculate them. Maths was never my strong suit though. :)
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
simdev
Posts: 8
Joined: Tue Mar 11, 2008 3:18 pm
Location: Bursa,Turkey

Post by simdev »

You should be right its related to up vector to my calculations but also there is the up/down issue


Camera view Camera up Required rotation
=========== ========= =================
1,0,0 --- 0,1,0 clockwise? around Y (with mouse right/left) and around Z (with mouse up/down)
0,0,1 --- 0,1,0 clockwise? around Y (with mouse right/left) and around X (with mouse up/down)
0,-1,0 --- 0,0,1 clockwise? around Z (with mouse right/left) and around X (with mouse up/down)
0,-1,0 --- 1,0,0 clockwise? around X (with mouse right/left) and around Z (with mouse up/down)



But this X, Y, Z rotations are in the world (or root whatever)
So for the rotated childs rotation can be on any axis X,Y,Z for left/right or up/down depending on the relative transformation of the child. My concern is the child rotation.
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

have you tried using quaternions, there's a code i posted somewhere, but it's buggy. actually, i stole it from arraz's code.

i'm willing to help you on this since i want to get a good quaternion code going for my test program.

have you checked the quaternion.h code?

if not, please go over that and see if u can recognize it and use it to rotate the object.

the formula i got while doing research on quats is...

Code: Select all

RotatedObject = RotateQuat * vectorQuat * RotateQuat'
i'm still figuring the values as to where they should map into.

let me know if this is in line with your interest.
Last edited by dlangdev on Thu Jul 03, 2008 6:25 pm, edited 3 times in total.
Image
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

you also try this code, no quats in there.

Code: Select all

      core::vector3df deltaR = core::vector3df(0,0.4,0); 
      core::vector3df r = sphere2->getRotation(); 
      //spin on its axis, no orbit. 
      if ( method == 1 ) { 
         core::matrix4 currentmat; 
         currentmat.setRotationDegrees(r); 
         core::matrix4 relmat; 
         relmat.setRotationDegrees(deltaR); 
         core::matrix4 resmat; 
         resmat=currentmat*relmat; 
         core::vector3df newrot=resmat.getRotationDegrees(); 
         sphere2->setRotation(newrot); 
      } 
see this thread if you want to see a working version...

http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=29084
Image
simdev
Posts: 8
Joined: Tue Mar 11, 2008 3:18 pm
Location: Bursa,Turkey

Post by simdev »

After 4 days of full throttle struggle, eventually (just before a heart attack)
I found the way the program likes. I dont mention about plenty of ways it does not like or crippled solutions.
I am programming .net cp and here is the solution and explanation

sn is an empty scene node just to perform rotation action
at every new mouse action I am attaching a non-ratated sn matrix to s1. Then rotating sn. Then eventually updating s1 parameters to fit global rotation (root scene node) and this is the key. Every time sn attaches s1 with a different transformation.


Point mousePositionAtClient = this.PointToClient(MousePosition);
s1RotationY = disp3DHalfWidth - mousePositionAtClient.X;
s1RotationX = disp3DHalfHeight - mousePositionAtClient.Y;
sn.Rotation = new Vector3D(0, 0, 0);
s1.Parent = sn;
sn.Rotation = new Vector3D(s1RotationX, s1RotationY, 0);

Matrix4 m1 = new Matrix4();
m1 = sn.RelativeTransformation * s1.RelativeTransformation;
s1.Rotation = m1.RotationDegrees;
s1.Parent = device.SceneManager.RootSceneNode;


Cursor.Position = this.PointToScreen(new Point(disp3DHalfWidth, disp3DHalfHeight));

Thanks all

(I always say; easy when you know how to do it)
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

awesome, dude.

ya finally made it.
Image
aren372
Posts: 4
Joined: Mon Feb 16, 2009 8:26 am

Post by aren372 »

simdev wrote:After 4 days of full throttle struggle, eventually (just before a heart attack)
I found the way the program likes. I dont mention about plenty of ways it does not like or crippled solutions.
I am programming .net cp and here is the solution and explanation

sn is an empty scene node just to perform rotation action
at every new mouse action I am attaching a non-ratated sn matrix to s1. Then rotating sn. Then eventually updating s1 parameters to fit global rotation (root scene node) and this is the key. Every time sn attaches s1 with a different transformation.


Point mousePositionAtClient = this.PointToClient(MousePosition);
s1RotationY = disp3DHalfWidth - mousePositionAtClient.X;
s1RotationX = disp3DHalfHeight - mousePositionAtClient.Y;
sn.Rotation = new Vector3D(0, 0, 0);
s1.Parent = sn;
sn.Rotation = new Vector3D(s1RotationX, s1RotationY, 0);

Matrix4 m1 = new Matrix4();
m1 = sn.RelativeTransformation * s1.RelativeTransformation;
s1.Rotation = m1.RotationDegrees;
s1.Parent = device.SceneManager.RootSceneNode;


Cursor.Position = this.PointToScreen(new Point(disp3DHalfWidth, disp3DHalfHeight));

Thanks all

(I always say; easy when you know how to do it)
nice!
I've searched in this forum all this week, and it is the only one works exactly as I want!
but can you explain why it works?
I can't quite understand it
thx, simdev!
Post Reply