PDA

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.

speed = 5;
xcenter = 275;
zcenter = 100;
angle = 0;
fl = 100;
}
onClipEvent (enterFrame) {
scale = fl/(fl+z);
_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.

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.

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) {
scale = f/d;

// Tangent of the angle displaced left or right

// 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