martes, 9 de febrero de 2010

PAPER - EXACT TOTATION OF A SPHERE IN ANY AXIS

Universidad La Salle Noroeste
Computer Animation IV
Mtro. Enrique Rosales
Pescador Rosas Carlos Mauricio
09-February-2010

EXACT ROTATION OF A SPHERE IN ANY AXIS

INTRODUCTION

This paper solves the strange motion made when you rotate a sphere in 2 axis at the same time, using quaternions in 3d Studio Max, but also giving a fair example of what not to do, in case you want to rotate in 2 axis at the same time that is.

DEVELOPMENT

1. What not to do?

This explanation below will help us understand the solution however it will only serve as a reference because it only works with an axis at a time.

Well first of all you must create a sphere (which must be named), with the ratio you want (does not matter because you can make a wire parameter so it never passes the ground)


Img. 1 Creation of a sphere on position (0,0,0)

and a plane the size you want and move them to the position (0,0,0). This is nothing more than a rule of animation, which greatly facilitates workflow. Make the plane dad of the sphere with the chain and link tool.

Img.2 Chain and Link tool (Remember, is son to parent)

Apply to the sphere a checkers texture with good tiling so we can see when it's moving.

Img 3. Apply the checkers texture to the sphere.

Now, if you move the sphere in X (perspective), we see no change in the texture. This is obvious because there is no rotation (in addition to that the motion will be on the plane). So what you have to do is an expression that helps the sphere rotate as it moves in X. We check in the local view in which axis the sphere rotates. This step is very important.

Open the curve editor, and because we wish it to rotate when it moves on X, we select the axis on where we want the rotation and right click, float expression.

Img. 4: Select float expression on the correct axis.

Since it works with the position in X, we create a variable posX and attach it to the controller .. which controls the position of the sphere. However, as the son of the plane, is the position X in the plane we need. We also need to create a variable for the ratio of the sphere.

Img. 5: Assign the controllers to the variables

We write the following formula (which will be explained in one second) :

degToRad ((posX) / (2 * pi * radius)) * 360

degToRad () = is the function in 3ds Max that converts degrees to radians, which is necessary because the result of the division will be given in degrees.

(posX / 2 * pi * radius) * 360 = equation that comes from a rule of 3, says that for every unit that moves in position X is divided by the perimeter and multiplied by 360 degrees.

Now that we have this, move the plane in the X, and it should appear as the sphere is rolling. Do the same with Y position of the plane and X rotation on the sphere, and you should be able to move the sphere in both directions, however, when you want to rotate simultaneousl in both axis we'll see a problem with the animation. This problem is called Gimbal Lock. This is the situation in which one of the axis (of rotation)'s control is loose because it's overlapped by previous rotations.

The problem is solved with the following:

2. What to do?

A rotation script in which quaternions (4d complex numbers using 3ds max to make an object rotate, it basically creates an imaginary vector which the scale and rotation values use). To use them, there must be at least movement.

Img. 6: Rotation scr

The script for making a sphere rotate in every axis you want is as follows (thanks to Cuneyt Ozdas for this)

obj = $ Sphere01
timer = 1f
fn getrot t =
(

if t <= 0f then return quat 0 0 0 1 t0 = t-timeres
t1 = t
rot0 = getrot(t0)
p0 = at time t0 obj.position
p1 = at time t1 obj.position
if(p0==p1) then return rot0
dif = p1-p0
len = Length(dif)
vec = dif / len

r0 = at time t0 obj.radius
r1 = at time t1 obj.radius

rotax = cross vec [0, 0, 1]
angle = 360*len/((r0+r1)*pi)
rotdif = quat angle rotax
rot1 = rot0 + rotdif
)

getrot(currentTime)

Now what this code does? It's not as difficult as it seems

[Reference 1]

1) Find the movement's direction and distance

As previously stated we need mandatory movement at least in 2 different frames because you need different times. To know the motion's direction subtract the position of the old frame to the new. That is if we have that X = 0 in frame 0, and X = 10 in frame 2, subtract:
10 to 0 = 10 (solution is positive, turn right)

We can also find the unit vector, divided by the length of vector (this length is the distance the ball crossed)

p0 = at time t0 obj.position

p1 = at time t1 obj.position

dif = p1-p0

len = Length (diff)

vec = diff / len

2) Find the rotation's axis

With both the distance and motion's direction we can get the rotation axis. Assuming the floor is oriented horizontally, we can say that the force applied to the floor area is in the Z axis (gravity). The axis of rotation must be perpendicular to this vector as well, then it can already find the vector of the rotation's axis using a cross product between vectors, 3ds Max already has a predefined function called cross.

rotax cross vec = [0,0,1]


3) Quantity of rotation.

If the sphere gives a complete turn, would have to travel a distance equal toits circumference. So if we divide the distance found before between its circumference, you will find how many rotations will have to give to travel that distance. The circumference of a circle is 2 * pi * r. We have to think changes in the radio then we use the average of the radtio at r0 and r1. Of course that's the rotation angle (in degrees) needed to travel a len distance:

angle = 360 * len / ((r0 + r1) * pi)

4) Building the quaternion

Now with both the axis of rotation and knowing how many times it rotated we can build the quaternion. 3ds Max now comes with a function that makes use of these values to make a quaternion:

rotax rotdif = quat angle

Rot1 = rot0 + rotdif

his quaternion defines the rotation from previous frame to current frame. But what scripted rotation controller wants from us to return the total rotation (that is including the all past rotations).

5) Completed


CONCLUSIONS

To make a rotation work in two axis at the same time we must work with quaternions, this is because without a vector to make an average from the distances and 2-axis rotations, it confuses the program and superimposes a rotation to another. Quaternions work with time (3ds max) and is very important to consider this.

I hope you have enjoyed the work.

REFERENCES

[Reference 1]: http://www.cuneytozdas.com/tutorials/maxscript/

It would be a robbery to say that the code was written by me. Both the code and its explanation was written by TOZDAS Cuney. Thank you very much for your simple explanation of quaternions!

No hay comentarios:

Publicar un comentario