Face next Position, relative to current Rotation [SOLVED]

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
paddy
Posts: 25
Joined: Tue Oct 23, 2007 12:30 pm

Face next Position, relative to current Rotation [SOLVED]

Post by paddy »

Hi there,

I've got a little problem with relative rotations. I spend the last hour searching for other related threads and test some found code but it does not work the way it should.

the scenario :

There is an array of positions for a SceneNode. At step 0 the user can rotate the mesh to look torward step one. This is the start direction of the mesh.

Now i need a method, which rotates the node, relative
to its "initial rotation" (set by the user), to face the next step position.

I saw other code snippets, using a direction vector for the mesh, but I want to rotate the node without using such one. The function should only rely on the current rotation and the position of the next step.

In 2D its not that hard, but I got no idea how to do that in 3 dimensional space.

hope someone can help me out :)

greets
paddy
Last edited by paddy on Sun Mar 16, 2008 2:45 pm, edited 1 time in total.
igorfk
Posts: 15
Joined: Sat Mar 08, 2008 10:39 pm

Post by igorfk »

You should already think in that, but, what about store the last position in three temporary variables, x, y and z, for example?
paddy
Posts: 25
Joined: Tue Oct 23, 2007 12:30 pm

Post by paddy »

I know, but I doesn't know the formula which calculates the two needed angles in relation to the current rotation.

what i have so far is a code snippet (dont beat me, i think it was from rogerbug ?)

Code: Select all

		vector3df toTarget = sim->get_Pos(ind, cur_step) - elements[ind]->getAbsolutePosition();

		vector3df rotation((-atan2(vector2df(toTarget.Z, toTarget.X).getLength(), toTarget.Y) * RADTODEG) - 90.f,
							(atan2(-toTarget.Z, toTarget.X) * RADTODEG) + 90.f,
							 0.f);

		elements[ind]->setRotation(rotation); 
a short explanation:

sim->get_Pos() gets the Position of the Node with number <ind> for the current step (its step+1, so the next position). Now we subtract the current Position.

the rotation formular isnt that clear to me, but it needs some kind of extension so it considers the current rotation of the Node to calculate the new rotation vector for the next step.

I've made a little pic :

Image

the car mesh is loaded at runtime (but could be anything, not only a car... so thats the problem, my app never knows whats front on the mesh).

At step 0 the user has 3 sliders to adjust the "initial rotation" after loading the mesh, to make it face step 1.
For all other steps, the rotation of the last step should be used to calculate the new angles for the following step.

The image is just the XZ - plane, the y coordinate dont have to be zero at all.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Is the mesh that's loaded completely arbitrary and you have no control over it? If you have control over the mesh then just rotate it to face along a certain axis in a modeller and then load it into irrlicht and all your models will face the same direction for you. If it's not in your control (i.e. the user can load their own mesh) then it's a bit harder i guess...

You could look at my irrAI source (link in my signature) as i've got some stuff for rotating to face a target in there which was taken from code on this forum.
Image Image Image
paddy
Posts: 25
Joined: Tue Oct 23, 2007 12:30 pm

Post by paddy »

In my case its arbitrary. I've made a simulator which plays different agent-object relationship scenarios (read from xml). There are different objects and their positions for each step in these simulations.

I've integrated the feature to dynamically load a mesh and apply it to the selected entity. Theres the hook (?), so nobody
really knows where's front of the model.

So I have implemented the feature to rotate it, to make it
face the next step, so that the app can rely on this initial rotation and the next step position.

I already thought about showing a half transparent bounding box cube, and select by mouse cursor the face of the cube which represents the front of the mesh. But thats lot of work isnt it ?! I dont have that much time left to complete my project :(

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

Post by JP »

I would say that your current solution is fine, probably better and definetly easier to implement than clicking on a face of the bounding box, it also allows for more freedome than clicking on the face of a box which only gives you 4 options for which way the model is facing, wheras your current setup allows for 360 (?) different options for which was is forward. Some models may not be aligned to face down an axis so it's a good generic solution.

You still have the problem of not being able to get it to rotate properly? See if my code in irrAI would work... it's in NPC.cpp and is a function called rotateToFace (i think). I think it may preserve the initial rotation you give to a model... or maybe it won't... but shouldn't be too hard to change it to do so i guess. All you'd have to do is keep a reference of what the user has set the rotation to be to get the model facing forwards and then each time you rotate it then add on that initial rotation again and i guess that would work...
Image Image Image
paddy
Posts: 25
Joined: Tue Oct 23, 2007 12:30 pm

Post by paddy »

I'll check your source now :) and you are right, the idea with the bounding box face select isnt that proper at all :)

I come back later to tell if it worked :)

thanks for your help!
paddy
Posts: 25
Joined: Tue Oct 23, 2007 12:30 pm

Post by paddy »

hmm no success at all, I'm just too dumb i think :(

your Code from IrrAI:

Code: Select all

	  vector3df r = sim->get_Pos(ind, cur_step) - elements[ind]->getPosition();
	  vector3df angle;
		   
	  angle.Y = atan2 (r.X, r.Z) * RADTODEG;
	  elements[ind]->setRotation(angle);
sim->get_Pos() returns the next Position for step+1. elements[ind]->getPos() is the current position of the object.

the problem is, its just turning around XZ and it
does not rely on an initial rotation. I tried a lot
till now, but nothing worked fine. I'm getting mad :(

also tried with Matrix funcs like :

buildCameraLookAtMatrixLH(current_step_pos, next_step_pos, up);

but it also ignores the startrotation. I've lack of vector mathematics O_O
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Ahh yes, sorry, my code does just rotate in the XZ plane as that's all i require.

If you use that camera lookat matrix then do what i suggested before and just add on the user defined start rotation to that matrix and it should work shouldn't it?
Image Image Image
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

paddy wrote:what i have so far is a code snippet (dont beat me, i think it was from rogerbug ?)
Uh huh. I was going to contribute to this thread, but now I'm not so sure. :P
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
paddy
Posts: 25
Joined: Tue Oct 23, 2007 12:30 pm

Post by paddy »

uuuuups :D many sorry :) but i remembered you as correct author ;)

ok rogerborg, I know you've got much math skills as I read many posts searching for my problem. You were always hanging around in that interesting rotation-problem-threads so do you eventually know more than we ? I'm totally stuck at the moment and for now i'm browsing my math formula book in the hope of getting it work at some time :)
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Urgh, I'll try. However, it's been a long week, so I'll need a beer and a blowj- I mean, a sensual massage first. Is anyone offering, or will I have to buy my wife some more new shoes again?
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
paddy
Posts: 25
Joined: Tue Oct 23, 2007 12:30 pm

Post by paddy »

don't stress yourself with my crazy problems ;)
get your massage, relax and if you feel bored come back :)

I'm still working on that, maybe I'm near but I show you :

at first two pics, the first is the simulator i'm working on, so just for an overview of my prob for the others

Image

so you see, object 1 is selected, and the start rotation is set. all objects in _this_ simulation moves around this circular orbit. The circle has as many edges as the simulation has steps.

The app knows only the start rotation of the loaded mesh and all step positions.

now my new thoughts about that ;)

Image


ok I have b1 and b2. The angle between both is defined by :

angle = atan (b1.dotProduct(b2) / (b1.getLength()*b2.getLength())

the wanted rotation axis is defined by :

rotation axis = b1.crossProduct(b2);

now, I have the angle and the rotation axis... but how to apply this one to the node ? Ive searched but found no method to rotate a node around a user specific axis.

if I would have such a function i could just call rotate(axis, angle, initial_rotatation_vec) ?

I guess I'm near it, can anyone offer the next step
to the solution ?

greets :D
paddy
Posts: 25
Joined: Tue Oct 23, 2007 12:30 pm

Post by paddy »

I solved it, with only one little bug remaining, at first the working code... omg it was simpler than I thought :/

I already was juggling around with quaternions & matrices but all I needed was :

Code: Select all


vector3df n = sim->get_Pos(ind, cur_step) - sim->get_Pos(ind, cur_step-1);	

vector3df phi = n.getHorizontalAngle() + initial_rotations[ind];

elements[ind]->setRotation(phi);
now all objects facing the next position, based on their initla rotation. Hail to getHorizontalAngle() ;-)

but another problem occured :

as you saw on the simulator pic, the cars just turning around Y... the start rotation calculated by the above formula is ~351° and decreases with every step about
0.2° ...

at step 272 strange things happen. The X Value of phi is suddenly 270 and Y is 0. One step earlier the vector looks like ~ (0, 296.74, 0), one step later it is (0, 296.44, 0).

Wheres the problem there does anybody know ?!

[edit]
now..... /me knows ;)

another dumb problem ! There were no position changes
in the mentioned steps, so the formula n = Pos(n) - Pos(n-1) returned the vector (0,0,0) ...

and getHorizontalAngle() returns X = 270°, Y=Z=0° if the vector if totally zero...

omg but finally solved by myself :) I'm a little bit proud, but anyway a great thanks to JP for his initial help :)

greetings
paddy
link3rn3l
Posts: 81
Joined: Wed Nov 15, 2006 5:51 pm

Post by link3rn3l »

hello pady, you can share the source code,
only main rotation code solved

thanks is for my IA prj
Post Reply