martes, 9 de febrero de 2010

Correct rotation of a sphere when is moved in x and y axis to be animated.

Universidad La Salle Noroeste

Computer animation IV

Profesor: Enrique Rosales

Jesús Alejandro Chávez García

Correct rotation of a sphere when is moved in x and y axis to be animated.

Problem: when creating a sphere for animating in order to roll over a plane or a surface, when moves over x and y axis at the same time, the rotation doesn’t seem natural, looks like the sphere was rotating in a different direction than it goes forward.

Try #1.- before going into details, we must create a sphere named sphere01, the first attempt to give it movement that looks right rotating as it moves was doing a plane that worked like a reference and function as father to the sphere in order to hold it over the plane and move it along with the plane.

We assign a float expression controller

Just go to graph editor>curve editor and select the Y rotation of the sphere, you can do it by right clicking the Y rotation under the sphere object menu and click on float expression.

Once created the controller you must erase all the expression by default and create the variable called posX (representing the x position of the plane) then enter the next formula.

Where degToRad is the function to convert degrees into radianes ( which are the ones 3ds max works with)

Apply de previous steps but now for the Y position of the plane and the X rotation of the sphere.

Error Try #1: as we can see the error in this solution is that when we move forward In x and then in y axes, vice versa, or both at the same time, the sphere rotates on an unnatural way, the axis turns off and spins like it was going in other direction.

This obbeys the reason that max needs angles for spinning and also has to have information about the rotation axis where all spinning angles are going to be spined, when this rotation axis’s missing 3ds max just uses the default and stay that way.

Try #2: on try #2 just créate the sphere named it sphere01, again selecting the sphere enter curve editor>sphere>rotation then assign rotation script controller

inside rotation script enter the following code:

--sphere rotation code

obj = $Sphere01            -- change this
timeres = 1f               -- time resolution
-----------------------------------------------------
fn getrot t =
(
    if t<=0f then return quat 0 0 0 1 -- t=0 => no rotation
    t0 = t-timeres         -- previous frame time 
    t1 = t                 -- current time 
 
    rot0 = getrot(t0)           -- previous rotation:
 
    p0 = at time t0 obj.position-- previous position
    p1 = at time t1 obj.position-- current position
    if(p0==p1) then return rot0 -- no distance is traveled
 
    dif = p1-p0                 -- difference in positions
    len = Length(dif)           -- distance that's traveled  
    vec = dif / len             -- normalized movement vector.
 
    r0 = at time t0 obj.radius  -- previous radius
    r1 = at time t1 obj.radius  -- current radius
 
    rotax = cross vec [0, 0, 1] -- rotation axis
    angle = 360*len/((r0+r1)*pi)-- rotation amount (in degs) 
    rotdif = quat angle rotax   -- rotation from t0 to t1
    rot1 = rot0 + rotdif        -- total rotation
)
 
getrot(currentTime) 

important to know that the function used is recursive, it means it is called continuously as the time passes(currentTime). That’s why the function keeps executing along the animation.

Now evaluate the code you entered and as result we finally got the sphere spinning as the rotation axis angles and makes sense with the direction it is moving forward.

Why did it worked right?

The reason that explains the code worked out in perfection according to our purposes is that the sphere besides the rotation angles it needs the angles of the rotation axe as they change as it moves along the scene in diferrent axis, the rotation plane changes and so its axes.

You may handle this with a tool named quaternion, a quaternion are complex numbers in 4d 3ds max uses to rotate, they are made by creating an imaginary vector 3d in which values of scale and rotation saves through sin and cos functions and calculates to find out the right rotation.

Quat q = {x,y,z,w} = {vx*sin(T/2), vy*sin(T/2), vz*sin(T/2), cos(T/2)}

That’s why the rotation script is where we must implement our solution of quaternions.

http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/geometric/axisAngle/axisAngle1.png

Conclusions.

When you need rotation and displacement at the same time in several axes, you will find out that the answer is on quaternions that allow us to define new angles of rotation as it moves.

version in english

versión en español.

reference go to

No hay comentarios:

Publicar un comentario