A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Basic Trigonometry: flipping an image around a Y-axis. ??

  1. #1
    Senior Member
    Join Date
    Jun 2001
    Location
    South Africa
    Posts
    316

    Basic Trigonometry: flipping an image around a Y-axis. ??

    Hi,

    I have this code that rotates an object (a square) around a y axis.
    Code:
    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.
    someday i'll be a kick-ass flash developer - you'll see.

  2. #2
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835
    Code:
    _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 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!
    jonmack
    flash racer blog - advanced arcade racer development blog

  3. #3
    ActionScript Insomniac
    Join Date
    Jan 2003
    Location
    43d03.21'N, 89d23.65'W
    Posts
    1,173

    Rotating around y axis

    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;
    }

  4. #4
    Senior Member
    Join Date
    Jun 2001
    Location
    South Africa
    Posts
    316
    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
    someday i'll be a kick-ass flash developer - you'll see.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center