A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: 3D Math: Perspective and focal length

  1. #1
    Junior Member
    Join Date
    Sep 2007
    Posts
    15

    Smile 3D Math: Perspective and focal length

    I am trying to convert this example code to AS3

    Orginal source for code:

    http://www.kirupa.com/developer/mx/perspective.htm

    Code:
    onClipEvent (load)
    {
       z=0;
       zspeed=5;
       fl=300;
    }
    onClipEvent(enterFrame)
    {
       scale=fl/(fl+z);
       _xscale=_yscale=100*scale;
       z+=zspeed;
    }
    MY code:

    Code:
    import flash.events.Event
    import flash.display.Graphics
    
    var _z:Number = new Number() 
    var _zSpeed:Number = new Number()
    var _fl:Number = new Number()
    var _scale:Number = new Number()
    _z = 0
    _zSpeed = 5
    _fl = 300
    var _ball:MovieClip = new MovieClip()
    
    _ball.graphics.beginFill(0xff0000)
    _ball.graphics.drawCircle(0,0,200)
    _ball.graphics.endFill()
    _ball.x = _ball.y = 250
    
    addChild(_ball)
    
    _ball.addEventListener(Event.ENTER_FRAME, zoomOut)
    
    function zoomOut(evtObj:Event):void
    {
    	
    	_scale = _fl/(_fl + _zSpeed)
    	evtObj.target.scaleX = scaleY = 100*_scale
    	_z+=_zSpeed
    }
    Any ideas?

    Alan

  2. #2
    Member
    Join Date
    May 2007
    Posts
    35
    The line:

    _scale = _fl/(_fl + _zSpeed)

    should read:

    _scale = _fl/(_fl + _z)

  3. #3
    Junior Member
    Join Date
    Sep 2007
    Posts
    15
    You're right, I did have the variable wrong- but it still does the same thing

    The swf compiles, the object pops up, then flickers away.

    my revised code:

    Code:
    import flash.events.Event
    import flash.display.Graphics
    
    var z:Number = new Number() 
    var zspeed:Number = new Number()
    var fl:Number = new Number()
    var scale:Number = new Number()
    
    scale = 0
    z = 0
    zspeed = 5
    fl = 300
    
    
    var _ball:MovieClip = new MovieClip()
    
    _ball.graphics.beginFill(0xff0000)
    _ball.graphics.drawCircle(0,0,200)
    _ball.graphics.endFill()
    _ball.x = _ball.y = 250
    
    addChild(_ball)
    
    _ball.addEventListener(Event.ENTER_FRAME, zoomOut)
    
    function zoomOut(evtObj:Event):void
    {
    	
    	scale = fl/(fl+z)
    	evtObj.target.scaleX = scaleY = 100*scale
    	z+=zspeed
    }

  4. #4
    Junior Member
    Join Date
    Sep 2007
    Posts
    15

    Solved!

    I a retard:

    the line that is wrong is
    Code:
    evtObj.target.scaleX = scaleY = 100*scale
    DUH!!!!

    AS3 scales X & Y between 0 and 1
    Code:
    evtObj.target.scaleX = scaleY = 1*scale

  5. #5
    Member
    Join Date
    May 2007
    Posts
    35
    Good catch, I missed that. It's still a bit wrong though

    evtObj.target.scaleX = scaleY = 1*scale

    should read

    evtObj.target.scaleX = evtObj.target.scaleY = scale;

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