Object rotation at camera look at direction
Object rotation at camera look at direction
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
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
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.
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.
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.
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.
-
- Admin
- Posts: 3590
- Joined: Mon Oct 09, 2006 9:36 am
- Location: Scotland - gonnae no slag aff mah Engleesh
- Contact:
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?
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.
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
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
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
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.
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.
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...
i'm still figuring the values as to where they should map into.
let me know if this is in line with your interest.
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'
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.
you also try this code, no quats in there.
see this thread if you want to see a working version...
http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=29084
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);
}
http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=29084
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)
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!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)
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!