Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Fixing axis after llLookAt

Tranquillity Dexler
Registered User
Join date: 20 Feb 2009
Posts: 7
06-12-2009 08:06
Hello.

Currently I have an object that is built from multiple sculpted prims. When I use llLookAt to make the root object point at a target the object is sometimes rotated on it's LOCAL x axis so that it looks like it has done a partial barrel roll. I would like to correct this roll and any other rotations besides for the z rotation that the object needs to be pointing at the target which will be at the same altitude. In other words, I would like the object to "stand up straight" after the look at before I move it towards the target.

I have tried the following which almost works... I figured all I wanted to do was align the current up axis with the world axis. When I think about it that should solve my problem.

Code:

vector toUp = llRot2Up(llGetRot());
rotation diff = llRotBetween(toUp, <0.0, 0.0, 1.0>;);
llSetRot(llGetRot() * diff);

However, depending on the orientation of the subject to the target sometimes to correct the difference in the "up" axis, the object will be rotated on it's local y axis by almost 180 degrees, which does fix the up axis, but causes the object to face the wrong way... I would rather it fix it's roll by using the local x axis instead thus keeping the facing direction the same..

I hope someone can figure out what im talking about. Let me know if you need pictures or anything.

Thank you very much ahead of time.
Dora Gustafson
Registered User
Join date: 13 Mar 2007
Posts: 779
06-12-2009 10:45
Similar problems are treated numerous times in this forum:)
What about this thread: /54/c6/322162/1.html
_____________________
From Studio Dora
Tranquillity Dexler
Registered User
Join date: 20 Feb 2009
Posts: 7
06-12-2009 11:14
Thank you.

Can you explain how this would work for me? I am having trouble following the relation between the posters question and my own. It looks like they have a velocity and they want it converted to a rotation for an object to point in the direction of travel. I simply want to either have a better function to point me towards the target that wont contort my "level stance" or be able to correct the contortion brought on by llLookAt. What input is this function looking for in my situation?

Please note that I am still a beginner in the mathematics involved here and am in the process of reading a book to improve my understanding.


rotation VectorToRotation ( vector V )
{
V = llVecNorm( V );
vector UP = < 0.0, 0.0, 1.0 >;
vector LEFT = llVecNorm(UP%V);
// V = llVecNorm(LEFT%UP); // you want V confined to the horizontal plane
// UP = llVecNorm(V%LEFT); // you want to keep the direction of V
return llAxes2Rot(V, LEFT, UP);
}
Dora Gustafson
Registered User
Join date: 13 Mar 2007
Posts: 779
06-12-2009 12:47
From: Tranquillity Dexler

Please note that I am still a beginner in the mathematics involved here and am in the process of reading a book to improve my understanding.
Of course we must all learn:) Note that rotations are just about the hardest thing to understand in all of LSL, so don't despair.
I don't understand 'Quaternions' and I don't know anyone that do, I just try to fit them into what I know:)
I fixed the program so it will match your purpose:
CODE

vector whereToLook=<< the point you want to look at >>; // Do not use the llLookAt() function

rotation VectorToRotation( vector V )
{
V = llVecNorm( V );
vector UP = < 0.0, 0.0, 1.0 >;
vector LEFT = llVecNorm(UP%V);
V = llVecNorm(LEFT%UP); // confined to the horizontal plane
return llAxes2Rot(V, LEFT, UP);
}

default
{
state_entry()
{
llSetRot( VectorToRotation( whereToLook - llGetPos()));
}
}

Do not use the llLookAt() funktion! it is replaced by this.
_____________________
From Studio Dora
Tranquillity Dexler
Registered User
Join date: 20 Feb 2009
Posts: 7
06-12-2009 15:50
Thank you very much for your help. You have no idea how much I appreciate it.

With a few modifications this function worked for me. I am still trying to figure out what is happening line by line however.. I am sorry ahead of time for so many questions, but it seems easier to learn with people that know what they're doing.

//getting the distance between me and the object of interest
whereToLook - llGetPos()

//removes the magnitude from the difference and leaves
//a unit vector, but why? I guess this leaves only the direction?
//so is this now our goal for our forward facing direction?
V = llVecNorm(V)

//we are taking the cross product of the UP unit vector and our
//normalized difference. What exactly is this doing though? I know
//that this will return a vector perpendicular to both, but why is that
//what we wanted to find? If V is our goal for forward direction then
//this would make sense because the plane defined by up and forward
//would be left and right?
vector LEFT = llVecNorm(UP%V);

//this would give us a new forward direction.. but wouldn't it leave
//V unchanged since LEFT and up were based on V?
V = llVecNorm(LEFT%UP);

Again, thank you so much for all your help.
Dora Gustafson
Registered User
Join date: 13 Mar 2007
Posts: 779
06-12-2009 16:32
I will try and answer the best I can although I am not good at explaining, I would rather script:)
From: Tranquillity Dexler

//getting the distance between me and the object of interest
whereToLook - llGetPos()
A: This is the vector pointing from the object to the point to look at. It has the direction we want.
From: Tranquillity Dexler

//removes the magnitude from the difference and leaves
//a unit vector, but why? I guess this leaves only the direction?
//so is this now our goal for our forward facing direction?
V = llVecNorm(V)
A: You are absolutely right about that
From: Tranquillity Dexler

//we are taking the cross product of the UP unit vector and our
//normalized difference. What exactly is this doing though? I know
//that this will return a vector perpendicular to both, but why is that
//what we wanted to find? If V is our goal for forward direction then
//this would make sense because the plane defined by up and forward
//would be left and right?
vector LEFT = llVecNorm(UP%V);
Right again:) (thanks god you know the cross product)
From: Tranquillity Dexler

//this would give us a new forward direction.. but wouldn't it leave
//V unchanged since LEFT and up were based on V?
V = llVecNorm(LEFT%UP);
A: You would be right if V were in the horizontal plane, but can we rely on that? This new V is!

The reason we need all these cross products and normal vectors lies in the llAxes2Rot(V, LEFT, UP); function.
See: http://wiki.secondlife.com/wiki/LlAxes2Rot

WOW that is interesting, the Wiki is changed for that function!

It used to state: "All three vectors must be mutually orthogonal unit vectors".
I have no idea why it is changed but it still is true.
The function still works if two vectors are not orthogonal and if the vectors are not unit vectors, it just don't work correctly.
Take my word for it, I have experinced that.
Edit: It still states: "All three vectors must be mutually orthogonal unit vectors".
Sorry for my mistake, it is late at night here.
_____________________
From Studio Dora
Tranquillity Dexler
Registered User
Join date: 20 Feb 2009
Posts: 7
06-12-2009 17:08
I think I fully understand this function now.

Thank you for your help and good explanation! I hope one day to be able to return the favor.