Click to See Complete Forum and Search --> : Basic Trigonometry: flipping an image around a Y-axis. ??

cliffdangler

01-17-2003, 06:05 AM

Hi,

I have this code that rotates an object (a square) around a y axis.

onClipEvent (load) {

radius = 100;

speed = 5;

xcenter = 275;

zcenter = 100;

angle = 0;

fl = 100;

}

onClipEvent (enterFrame) {

z = Math.sin(angle*Math.PI/180)*radius+zcenter;

scale = fl/(fl+z);

_x = Math.cos(angle*Math.PI/180)*radius+xcenter;

_xscale = _yscale = scale*100;

angle += speed;

}

The problem is that I want the object to flip around as it goes around the radius (so that the user will see the reverse side of the square). I've tried using -Math.sin() on the _xscale but it's not perfect.

Please help a Math dropout. Thanks for your help.

jonmack

01-17-2003, 10:50 PM

_yscale = 100*scale;

_xscale = 100*scale*Math.sin(angle*Math.PI/180);

this will scale it so it flips around, so that when it is "furthest" away, you see the reverse of the mc. But i looks a bit "off" because it remains a rectangle always, but it would have some perspective really. See what i mean? At least it's a start though.

No idea how to do the perspective, other than what ericlin said about masks on the other recent thread (http://www.flashkit.com/board/showthread.php?threadid=411106) on this board.

Oh, and that code could be optimised a bit. But i guess the standard practice is too concentrate on that when it's all working properly!

mkantor

01-18-2003, 11:16 PM

This is my second attempt to respond....

There are two effects. Your code accounts for the scaling down due to the object being farther away. The other effect, which works only on the x axis, is due to viewing the object (which I'm assuming is 2-dimensional) at an angle. You need to multiply _xscale ALSO by cos(angle).

There are other interesting issues here. You seem to have the viewer positioned pretty close to the path of the object. The code you've got (with the correction I've suggested) is really for a viewer very far away. You nearby observer will, for instance see the object move across the field of view faster from L to R than vice versa. And the angle between the object and the line of sight is not really "angle".

Try it anyway, you'll probably be happy with it. If you want to account for some subtleties, try something like this: (my f is your fl)

onClipEvent (enterFrame) {

d = Math.sqrt(f*f + radius*radius + 2*f*radius*Math.sin(angle*Math.PI/180));

scale = f/d;

// Tangent of the angle displaced left or right

tanalpha = (Math.cos(angle*Math.PI/180)*radius / d);

// Angle at which object is viewed is angle + alpha. We compute its cosine.

foreshortening = (Math.cos(angle*Math.PI/180) - tanalpha * Math.sin(angle*Math.PI/180)) /Math.sqrt(1 + tanalpha*tanalpha);

// A constant related to the distance from eye to screen. See what looks good.

screenfactor = 2.3

_x = screenfactor* f * tanalpha + xcenter;

_yscale = scale*100;

_xscale = -_yscale * foreshortening;

angle += speed;

}

cliffdangler

01-27-2003, 01:49 AM

Thanks for all your help guys.

mkantor, thanks for trying to help - although your explaination is great - it's a little over my head. I've pretty much got the globe to rotate along the y-axis.

Cheers