A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [RESOLVED] AS3 centering 3D objects on stage

  1. #1
    dmacker = new Person()
    Join Date
    Jan 2005
    Location
    Woburn, MA
    Posts
    143

    resolved [RESOLVED] AS3 centering 3D objects on stage

    I am new to working with 3D in Flash CS4. My project has a center-registered movie clip with a z-axis setting of 850 that I need to keep centered on the stage at all times, including when the stage is re-sized.

    Code like this for centering 2D objects doesn't seem to work:

    Code:
    mc.x = stage.stageWidth/2;
    mc.y = stage.stageHeight/2;
    My guess is that I need to convert the movie clip back into 2D space somehow, center it using the code above and then change the z-axis back to 850. Does this sound about right, or is there another way to do this? A simplified code example would be much appreciated.

    Thanks.
    I am an instance of the Person class.

  2. #2
    Flactionscrish Baby Minion's Avatar
    Join Date
    Nov 2005
    Location
    Planet Earth
    Posts
    312
    I'm hoping that you have set the stage.scaleMode = "noScale" and the stage.align = "tl"

    If that is the case, then I think you might have to adjust the vanishing point of the application whenever the stage resizes. Try looking up info about that.

    For my testing I put this code in the IDE and tested with this:


    PHP Code:
    stage.scaleMode "noScale";
    stage.align "tl";

    // draw square sprite and place it at z=850
    var s:Sprite = new Sprite ();
    s.graphics.beginFill(0);
    s.graphics.drawRect(00200200);
    s.graphics.endFill();
    s.graphics.lineStyle(20xFF0000);
    s.graphics.moveTo(1000);
    s.graphics.lineTo(100200);
    s.graphics.moveTo(0100);
    s.graphics.lineTo(200100);
    s.850;
    addChild(s);

    // create holder sprite for stage center
    var st:Sprite = new Sprite();
    addChild(st);

    // when you click on the stage, call the center function
    stage.addEventListener (MouseEvent.CLICKcenter);


    function 
    center (e:MouseEvent null):void {
        
    // reset the holder sprite to show cross on stage
        
    st.st.0;
        
    st.graphics.clear();
        
    st.graphics.lineStyle(20xFF0000);
        
    st.graphics.moveTo(stage.stageWidth/20);
        
    st.graphics.lineTo(stage.stageWidth/2stage.stageHeight);
        
    st.graphics.moveTo(0stage.stageHeight/2);
        
    st.graphics.lineTo(stage.stageWidthstage.stageHeight/2);

        
    // try to center the square using the pixel bounds
        
    s.= (stage.stageWidth 2) - (s.transform.pixelBounds.width 2);
        
    s.= (stage.stageHeight 2) - (s.transform.pixelBounds.height 2);

    ktu[k-two]
    he who hesitates is lost; so i guess i'll wander intently

    Are you sure this is real?
    Life is Love, Love is Blind, Blind we go through Life.
    Life isn't hard, dealing with your self is.

    The concept of life in a human brain is weakening day after day. Live every day like its your last. Take the chances, and opportunities, and never let authority push you around for fun.


  3. #3
    Flactionscrish Baby Minion's Avatar
    Join Date
    Nov 2005
    Location
    Planet Earth
    Posts
    312
    I did some more digging cause this interested me. I'm very interesting in alignment of DisplayObject.

    Here's what I found:

    The PerspectiveProjection was key. changing the perspectiveProjectionCenter of the display object was what made it work, along with using the pixelBounds of the display object rather than its width and height properties.

    view my results here. (keep resizing the stage and double clicking.)
    download the fla here (makes more sense when you see the code)

    *Note: I'm not sure if this is the best way or only way to do it, but none the less it seems like it could be a solution. I also didn't fully test everything and I don't fully understand how the perspective projection is really working in this case so keep playing around with it.
    ktu[k-two]
    he who hesitates is lost; so i guess i'll wander intently

    Are you sure this is real?
    Life is Love, Love is Blind, Blind we go through Life.
    Life isn't hard, dealing with your self is.

    The concept of life in a human brain is weakening day after day. Live every day like its your last. Take the chances, and opportunities, and never let authority push you around for fun.


  4. #4
    dmacker = new Person()
    Join Date
    Jan 2005
    Location
    Woburn, MA
    Posts
    143

    Smile Thank you!

    Thank you Baby Minion for putting together the code to get this all working; your solution was EXACTLY what I needed!

    Once you added the perspective projection code, I threw the whole thing into an Event.RESIZE handler and got the results I was looking for.

    Cheers!
    I am an instance of the Person class.

  5. #5
    Flactionscrish Baby Minion's Avatar
    Join Date
    Nov 2005
    Location
    Planet Earth
    Posts
    312
    NP

    For future reference, this is how I did it:

    PHP Code:
    // put the center of the projection at the center of the stage
    var pp:PerspectiveProjection=new PerspectiveProjection();
    pp.projectionCenter = new Point(stage.stageWidth/2stage.stageHeight/2);
    square.transform.perspectiveProjection pp;
    // center the object using the pixelBounds
    square.= (stage.stageWidth 2) - (square.transform.pixelBounds.width 2);
    square.= (stage.stageHeight 2) - (square.transform.pixelBounds.height 2); 

    One interesting note as I was checking things.

    The width and height of a DisplayObject when the z is modified change to match what you see on screen, but the pixelBounds returns the original dimensions of the DisplayObject. Using getBounds(stage) returns the proper values for the object's dimensions...

    PHP Code:
    square.width square.height 200;
    square.850;
    trace(square.transform.pixelBounds.width " : " square.width);  // 200 : 70
    trace(square.transform.pixelBounds.height " : " square.height);// 200 : 70
    trace(square.getBounds(stage)) // (x=154, y=180, w=70, h=70) 
    dmacker, please set this thread to resolved
    ktu[k-two]
    he who hesitates is lost; so i guess i'll wander intently

    Are you sure this is real?
    Life is Love, Love is Blind, Blind we go through Life.
    Life isn't hard, dealing with your self is.

    The concept of life in a human brain is weakening day after day. Live every day like its your last. Take the chances, and opportunities, and never let authority push you around for fun.


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