A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: Help with 3D ball movement

  1. #1
    Senior Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    501

    Help with 3D ball movement

    I am creating a 3D penalty shootout game using Flash 8 and AS2 and am getting a bit muddled with how the z values and the origin should work to enable me to display the ball.



    Looking at the graphic, the horizon looks like it should have a Y value of about minus 100. Does that mean I should use a Y value for my origin of minus 100?

    My code as follows displays the ball at the origin.

    I was thinking that the ground level for ball should have a Y value of 0 hence I have set the 3D Y value to 0.

    What I confused about is how to get the ball to display on the black dot position with out making Y a value greater than zero.

    Using the value of 0 for X, Y and Z displays the ball on the horizon. Therefore to make the ball come closer I need to make Z go negative.

    I think I am a wee bit confused by this. Every time I do a game like this I end up fudging it but I would like to know how to sort this correctly.

    Any advice much appreciated.

    Thanks

    Paul

    Code:
    this.createEmptyMovieClip("theScene", 1);
    theScene._x = 340;
    theScene._y = -100;
    
    focalLength = 250;
    
    var pointX = 0;
    var pointY = 0;
    var pointZ = 0;
    
    make3DPoint = function(x,y,z){
    	var point = new Object();
    	point.x = x;
    	point.y = y;
    	point.z = z;
    	return point;
    };
    
    function convert3d(pointIn3D) {
    var pointIn2D = new Object();
    var scaleRatio = focalLength/(focalLength + pointIn3D.z);
    pointIn2D.x = pointIn3D.x * scaleRatio;
    pointIn2D.y = pointIn3D.y * scaleRatio;
    return pointIn2D;
    }
    
    
    var ballObject = theScene.attachMovie("ball","ball", 100);
    
    var point3D = make3DPoint(pointX, pointY, pointZ);
    var point2D = convert3d(point3D);
    
    var scaleRatio = focalLength/(focalLength + (pointZ));
    
    ballObject._xscale = ballObject._yscale = 100*scaleRatio;
    
    ballObject._x = point2D.x;
    ballObject._y = point2D.y;
    Paul Steven
    http://www.mediakitchen.co.uk

  2. #2
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    i think the best way to SEE the effects of changing a particular parameter in 3D calculations is to try rendering a grid.
    i have attached a .fla that will render (using the same 3D calculation that you do) a top down view of a grid using a camera, a view plane and a slope (to render surfaces at an angle).
    you can change various parameters as see shat happens to the render.


    one very important thing to remember is; rather than trying to move or rotate the camera (which you cannot). you most move ALL the objects in the scene in the opposite direction. ie. if you wanna move the camera forward then move everything else toward the camera.

    one other point; i have found that the focal length is the point at which objects pass behind the camera. so if your object is closer to the camera than the focal length then it will be flipped.

    let me know if you have any other questions
    Attached Files Attached Files
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

  3. #3
    Senior Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    501
    Thanks BlinkOK - that is really helpful. I have a few questions though.

    Am I right in assuming I will have no need for the slope element as I am modelling a football pitch which is flat? Therefore height will always be zero?

    Also I do not plan to move the camera in my game so can I therefore just have a fixed view? It will be the ball that does the movement withing a fixed view.

    What does the value for "camera" represent - the z displacement from what?

    Is vp the equivalent of the focal length?

    Can you explain how the scale property works?

    Is there any chance you can render a ball onto your flash movie just to give me a better idea of how this fits in.

    I have rendered what looks like my required view point below. Can you explain why it requires the properties it uses, especially the scale one?



    Many thanks

    Paul
    Last edited by chuckylefrek; 05-13-2009 at 02:59 AM.
    Paul Steven
    http://www.mediakitchen.co.uk

  4. #4
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    Am I right in assuming I will have no need for the slope element as I am modelling a football pitch which is flat?
    probably not
    Therefore height will always be zero?
    The height adjusts the position of the slope and therefore all other parameters. you would always need to adjust the height because the camera would be up above the ground. probably higher than the player so everything's y values would be positive or negative (can't remember off the top of my head).

    What does the value for "camera" represent - the z displacement from what?
    the camera value is the distance that the camera is from the center of the stage (the camera is the little green triangle)
    Is vp the equivalent of the focal length?
    yes it's the green line just above the camera.
    Can you explain how the scale property works?
    the scale value is multiplied by the x and y values. it just makes the rendered scene bigger
    Is there any chance you can render a ball onto your flash movie just to give me a better idea of how this fits in.
    i'm a little busy right now but should be ok in about 24 hours. i'll try to do it then
    I have rendered what looks like my required view point below. Can you explain why it requires the properties it uses, especially the scale one?
    what you are looking at is a plane sloping down towards you. see the black slope over to the right? that is the angle that the plane is sloping down.

    height: this is the height of all the points in the plane. if it wasn't sloping the rendered scene would appear to be a straight line along the horizon.

    camera: this is the distance from the center of the scene of the camera if you reduce the camera value it will be like moving the camera forwards

    vp: this is the view pane. it works a little like a lens you can get a real
    distorted view or something that looks like a box depending on it's value.

    scale: every point that is calculated is multiplied by 6 (so the render scene is scaled up 6 times)
    Last edited by BlinkOk; 05-13-2009 at 03:47 AM.
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

  5. #5
    Senior Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    501
    Thanks Blink for the swift reply. I think I am starting to understand this thanks to your help.

    I hadn't realised that what I was viewing was a sloped plane - I thought it was just a flat plane. I guess if I had been viewing a flat plane, I would see nothing as the camera is facing directly at its edge? So to display the plane as flat rather than sloping, would I just use a value of zero for all y co-ordinates? Is the camera Y positon therefore assumed to be zero?

    You said that "height: this is the height of all the points in the plane. if it wasn't sloping the rendered scene would appear to be a straight line along the horizon." I am unclear as to how I have a value of Zero for height in the render im my previous post but the slope still exists. I would have thought that each point on a sloped plane had a different height - I am assuming that height is the Y displacement.

    Rather than sloping the plane, would it not be better to keep the plane flat and have the camera at an angle as in the second image below? I am thinking this would effectively provide the same results visually. Is the camera value therefore the equivalent of the total of Z + the focal length (vp)? Therefore if the focal length (vp) is 50 and camera is 300 then Z must be 250?

    So is the following assumption correct? If I plotted a ball at the position 0,0,0 would it appear at the position I have shown in yellow in the grab below? And therefore if I increased the Z value of this ball it would simply stay in the same position and get smaller and smaller?

    Hence the vanishing point is the center of the world movie clip?

    With regards scale, am I right in assuming to get the same visual look I could just increase the size of the plane by 6 times and leave the scale value at 1?

    Sorry for all the questions - I am just trying to get this all clear in my head.

    Many thanks

    Paul



    Last edited by chuckylefrek; 05-13-2009 at 05:52 AM.
    Paul Steven
    http://www.mediakitchen.co.uk

  6. #6
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    I would see nothing as the camera is facing directly at its edge?
    correct. try this; go into the .fla and change the angle of the slope mc to zero

    Is the camera Y positon therefore assumed to be zero?
    yes, all other y co-ordinates need to be either greater than zero (to be above the camera) or less than zero (to be below the camera). i think that's right but it might be reversed > 0 = below, < 0 = above

    I am unclear as to how I have a value of Zero for height in the render im my previous post but the slope still exists.
    yes the points do endup with a different y value because they are translated using the slope which gives them their y value

    Rather than sloping the plane, would it not be better to keep the plane flat and have the camera at an angle as in the second image below?
    there is no way to change the angle of the camera. all you have for the camera are x,y,x co-ordinates. no angle. so you have to angle the plane to "simulate" an angled camera

    So is the following assumption correct? If I plotted a ball at the position 0,0,0 would it appear at the position I have shown in yellow in the grab below?
    yes

    And therefore if I increased the Z value of this ball it would simply stay in the same position and get smaller and smaller?
    no the ball would get smaller and travel upwards (because the plane is sloped). if it wasn't sloped then your statement would be true.

    Hence the vanishing point is the center of the world movie clip?
    again if it was a flat plane that would be true. with the slope the vanishing point is way up above.

    With regards scale, am I right in assuming to get the same visual look I could just increase the size of the plane by 6 times and leave the scale value at 1?
    yes you could. i however like to watch the action in the top-down mc (which you can't do if the plane is very large). it's very helpful in determining if you have a problem with your logic or rendering.

    the attached file shows a mapping scenario i used for a baseball game. it has a top-down and side view (which allows me to move an object in 3d space).

    ps: you can click and drag the ball in either top-down or side view and it will move the ball in 3D space
    Attached Files Attached Files
    Last edited by BlinkOk; 05-13-2009 at 06:40 AM.
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

  7. #7
    Senior Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    501
    Thanks BlinkOK - I have gone back to the drawing board and will see how my new found knowledge helps me try and crack this one.

    Many thanks for all your help on this.

    P.S I am curious as to why it is not possible to change the angle of a camera?
    Paul Steven
    http://www.mediakitchen.co.uk

  8. #8
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    there is no provision for changing the camera angle in the equation that maps the 3D (x,y,z) values to 2D. this is the equation;
    Code:
    function convert3d(pointIn3D) {
    var pointIn2D = new Object();
    var scaleRatio = focalLength/(focalLength + pointIn3D.z);
    pointIn2D.x = pointIn3D.x * scaleRatio;
    pointIn2D.y = pointIn3D.y * scaleRatio;
    return pointIn2D;
    }
    you could at this point rotate the point but your still just rotating the plane and not the camera.

    one thing i forgot to mention (and i think it is extremely important) is that;
    all values are relative to the camera. if you look at the code it will always use localtoglobal and globaltolocal to convert each point to the co-ordinate system of the camera mc.
    therefor the y value within the camera mc is the z value in 3D space and the x value in the camera is the x value in 3D space
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

  9. #9
    Senior Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    501
    Sorry I only just received the notification of the reply to this post. I understand now about the camera angle.

    The game is coming together quite well now thanks to your help. I can't put a link to it on here unfortunately.
    Paul Steven
    http://www.mediakitchen.co.uk

  10. #10
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    no worries chucky. as long as you're getting the job done. let me know when it's done and out there ok?
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

  11. #11
    Senior Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    501
    Sure will Blink! The game is due to go live on the site on 18th May so it will be out there very soon. The usual quick turnaround demanded by clients
    Paul Steven
    http://www.mediakitchen.co.uk

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