tags:

views:

369

answers:

2

I am using a 3D engine called Electro which is programmed using Lua. It's not a very good 3D engine, but I don't have any choice in the matter.

Anyway, I'm trying to take a flat quadrilateral and transform it to be in a specific location and orientation. I know exactly where it is supposed to go (i.e. I know the exact vertices where the corners should end up), but I'm hitting a snag in getting it rotated to the right place.

Electro does not allow you to apply transformation matrices. Instead, you must transform models by using built-in scale, position (that is, translate), and rotation functions. The rotation function takes an object and 3 angles (in degrees):

E.set_entity_rotation(entity, xangle, yangle, zangle)

The documentation does not speficy this, but after looking through Electro's source, I'm reasonably certain that the rotation is applied in order of X rotation -> Y rotation -> Z rotation.

My question is this: If my starting object is a flat quadrilateral lying on the X-Z plane centered at the origin, and the destination position is in a different location and orientation where the destination vertices are known, how could I use Electro's rotation function to rotate it into the correct orientation before I move it to the correct place?

I've been racking my brain for two days trying to figure this out, looking at math that I don't understand dealing with Euler angles and such, but I'm still lost. Can anyone help me out?

A: 

First do the translation to bring one corner of the quadrilateral to the point you'd like it to be, then apply the three rotational transformations in succession: euler angles

If you know where the quad is, and you know exactly where it needs to go, and you're certain that there are no distortions of the quad to fit it into the place where it needs to go, then you should be able to figure out the angles using the vector scalar product.

If you have two vectors, the angle between them can be calculated by taking the dot product.

duffymo
Unfortunately, this doesn't really help. What I need are specific angles. How do I find the X, Y, and Z angles by which I would rotate the model if I rotated it first by the X angle, then Y angle, then Z angle?
Ben Torell
+1  A: 

Can you tell us more about the problem? It sounds odd phrased in this way. What else do you know about the final orientation you have to hit? Is it completely arbitrary or user-specified or can you use more knowledge to help solve the problem? Is there any other Electro API you could use to help?

If you really must solve this general problem, then too bad, it's hard, and underspecified. Here's some guy's code that may work, from euclideanspace.com.

Andrew Butts
Yes, the final orientation is arbitrary. There's not really much else to know other than that. I've also looked pretty thoroughly through the Electro PI to find anything that could help. The set_entity_position() function is an absolute rotation. However, I just remembered that there is a relative rotation function turn_entity. This could help, since it would allow me to apply rotations in any order I want, though the rotations will be along the reference frame's axes, not the object's local axes. Hmmm...
Ben Torell
Cool. Maybe this'll work: Find rotation angles about the X and Y axes that would make the source normal coincide with the destination normal. Given that partially aligned source quad, find the rotation angle that would twist the quad so it completely aligns with the destination quad. Then apply the angles in Z, X, Y order.
Andrew Butts
... Could you add a set_entity_basis or something?
Andrew Butts
Unfortunately, you can't set the basis of an entity. I'm going to try your idea in the above comment, though -- it seems promising, and it's the simplest solution I've seen so far. If you're interested, you can find the Electro API here: http://www.evl.uic.edu/rlk/electro/electro.html
Ben Torell
Ah what I meant was, since Electro is open-source, are you able to use a custom build of it, which you have modified to include a set_entity_basis function?
Andrew Butts
I see. Unfortunately, the program I'm writing needs to run on an environment using a default build of Electro, and I don't have access to modify that build. Also, that sounds like it's a bit beyond my expertise, and not worth the trouble. Thanks for the idea, though. I'll let you know what happens with your original idea. It's working well so far.
Ben Torell
It worked! Thank you SO much!
Ben Torell