martes, 9 de febrero de 2010

Rotation of a sphere


Sergio margarito cabrales moreno



Well 2 weeks ago on my animation class the teacher gave us a problem to solve, he wanted us to crate a sphere and that sphere should move in any direction, this doesn’t look really difficult, the thing is that if the user change the radius or the form of the sphere, the sphere should rotate in proportion of it shape, we were a little worried because this was going to be our grade of the partial. So the class resolved the problem or we thought we had the problem solve but then we realized that as soon you rotate the sphere on the X and Y axis at the same time the sphere rotation was turn to crap, so this was the real problem to be solve.



Development of the problem


My class and i saw this homework as a teamwork so we join into some teams each team tried to solve the problem, each team searched for information, until one of the teams found the correct info and this is the way we solved it:


we create a sphere the radius doesn’t matter, on the position (0,0,0), then you assign a checker texture to the sphere so you can see the movement with clarity, ok the next step it’s to create a plane on the same coordinates of the sphere, after this you link the sphere to the plane for some movements we will do later.


Now open the curve editor, select the axis that you want the sphere to rotate ( the view should be at local) select the X axis of rotation right click on it and select add component and the select float expression, create a new variable (any name) and to this variable assign the X position of the plane, this position will be control it by an assign controller then create an other variable to assign the radius, after this you need to write this expression:


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

what this expression means it’s that for each unit that the sphere moves in the Xposition it will be divided by its perimeter that will be multiplied by 360 and that result will be change into radians. with this you just need to do the same on the Y axis. But will happend what i said thte begining of the paper if you try to move the sphere in both axis at the same time the sphere will lose control.






This problel will be solve with quaternions, and a script for the sphere:


The script for the sphere its the next one:

obj = $Sphere01 -- change the name here

timeres = 1f --time which will make the difference (1frame)

fn getrot t = -- where the function is created to perform the rotation

(

if t<=0f then return quat 0 0 0 1 -- t=0 If no movement returns the quaternion basis (no rotation, because the value is 0) otherwise

t0 = t-timeres -- you subtract the current time at which requested the timer (first frame)

t1 = t -- the function is recorded


rot0 = getrot(t0) -- previous rotation


p0 = at time t0 obj.position—previous rotation

p1 = at time t1 obj.position—current rotation

if(p0==p1) then return rot0 – if they return the same value, then there’s no movement


dif = p1-p0 -- subtract positions

len = Length(dif) -- and the length function will give us the length of the previous subtraction difference

vec = dif / len -- is the normal vector with the difference between the length


r0 = at time t0 obj.radius -- previous ratdius

r1 = at time t1 obj.radius -- current radius


rotax = cross vec [0, 0, 1] – axis of rotation

angle = 360*len/((r0+r1)*pi) —amount of rotation in degrees

rotdif = quat angle rotax -- get the quaternion rotation in the difference between positions

rot1 = rot0 + rotdif -- total rotation

)


To make this work we need at least 2 different frames in motion since it requires two different times one for the position in X and the other for Y, We can also find the unit vector, dividing it by the length of the vector that is the length of 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

After that find the the rotation axis. with the distance and direction of motion we can get the rotation axis, the floor is oriented horizontally, so the force that it’s apply to the floor is in the z axis, The rotation axis has to be perpendicular to this vector as well, and then with that, we find the vector of the rotation axis using a cross product between vectors. 3ds Max has a function called cross. The function is rotax cross vec = [0,0,1].

So To get the quantity of rotation, if the sphere gives a complete turn, it would have to travel a distance equal to its circumference. So if we divide the distance we found prior to its circumference, well find how many rotations has to take to go that distance. The circumference of a circle is 2 * pi * r. We n to think about the radio changes then well use the average of the 2 radius at r0 and r1. So we can get the rotation angle (in degrees) needed to travel a the distance than is angle = 360 * len / ((r0 r1) * pi)


Already with the rotation axis and the times that is going to be rotated we build the QUATERNION. 3ds Max comes with a function that needs these values
rotax rotdif = quat angle
Rot1 = rot0 rotdif

and there you have it problem should be solve, this was a really complicated job to do it and to understand it, and our grades were in danger but i think we solved the problem


Sergio margarito cabrales moreno

059061

Http://www.cuneytozdas.com/tutorials/maxscript/
The code and the explanation was written by Tozdas Curney.

2)http://en.wikipedia.org/wiki/Quaternion

3)http://mathworld.wolfram.com/Quaternion.html














No hay comentarios:

Publicar un comentario