Community Forum

How to rotate a 3D vector

Forum Overview >> Scripting

CategoryScripting
Created30.06.2010 13:49


Christer F (Unknown) 30.06.2010 14:03
In the load-phase of my mod (Vehicle) I need to do some work with vectors. Thus I don't have access to getWorld/setWorld functions (Transformation/Rotation).
I have a transformation group setup from the giants editor. As child nodes to this I create some transformation groups via Lua code. They are offset from the parent node and can have rotation. Under these child I create another level of child nodes, which are also offset from their parents.

Now comes the part I can't do. I link the childs to their "grandparent" but I want them to stay in the same position (seen from the grandparent's perspective). It all works well the middle-parent hasn't been rotated, then it is just to sum the two 3D vectors. But if the middle-parent has rotation (i.e. rotation is not set to 0,0,0) the vector pointing out the grandchild must be rotated so it will have the vector that would have pointed to the same point in space if the middle-parent didn't have rotation. After that they can be summed together.

As said, if one would do this when the game has "started", in-world, it would be easy as I could have asked for the world transformation, re-link grandchild's to their grandparents and set the world transformation I saved.

I searched some on the internet about this and from what I understand it is not the easiest 3D vector operation, often they described it as using vector * matrix multiplication but what to put into that transformation matrix I didn't understand and is hoping someone here knows.

thanks in advance!

Stefan Geiger - GIANTS Software 30.06.2010 14:42
You can use the function localToWorld, wordToLocal, localDirectionToWorld and worldDirectionToLocal.

With this, you can first link the object to the parent with which it has the correct localtion. Then calculate the position and orientation relative to the real parent.
Whereas the orientation is the z and the y axis, which you can transform using the worldDirectionToLocal and localDirectionToWorld.

So for the position this would be
local x,y,z = worldToLocal(newParent, getWorldTranslation(child))

Similarly this works for the orientation:
local dx,dy,dz = worldDirectionToLocal(newParent, localDirectionToWorld(child, 0,0,1))
local upx, upy, upz = worldDirectionToLocal(newParent, localDirectionToWorld(child, 0,1,0))

Finally you can relink and set the new values:
link(newParent, child)
setTranslation(child, x,y,z)
setDirection(child, dx,dy,dz, upx,upy,upz)

Christer F (Unknown) 30.06.2010 16:59
Ok I haven't tested the above code but as I remembered when I tried before with the getWorldTranslation function in the load (XMLfile) function it said it was a nil value. Have I mixed it up with some other error I got? I thought it seemed reasonable because in that stage the vehicle hasn't been placed in the map (I guess).
I'm only guessing but isn't that true? And all the functions involving the _world_ is unavailable.

Stefan Geiger - GIANTS Software 02.07.2010 10:00
In this stage the vehicle is only placed at the 0/0/0 position, however calling getWorldTranslation should work.
So the above code should work, since the position where the vehicle is does not matter, as you transform the position pack to local coordinates.

Christer F (Unknown) 13.07.2010 12:25
Thanks Stefan - the code worked perfectly!


Note: Log in to post. Create a new account here.