XNA Creators Club Online
Page 1 of 1 (10 items)
Sort Posts: Previous Next

Help with the gamestate sample

Last post 04-15-2008 9:07 PM by CSM. 9 replies.
  • 04-11-2008 2:59 PM

    Help with the gamestate sample

    I seem to be unable to get my screen dimension variables to be a 0-1 value. so far i have a width and height int value that are equal to ScreenManager.Game.GraphicsDevice.Viewport.Width/Height;. The main reason i want the 0-1 value is because i want to say no matter what size the screen is my guy that i have walking around in my level cannot go outside the screen. If there is an easier way you could suggest that also thx in advance.
    Contrary to popular belief chuck norris is what was dropped on Hiroshima and Nagasake.
  • 04-11-2008 3:27 PM In reply to

    Re: Help with the gamestate sample

    So you have screen width and screen height as integers, correct? And you want this to be converted to between 0 and 1?

    Why not just do something like this:

    When you have a screen coordinate, you just need to divide the value by the total width and height to get a percentage (a number between 0 and 1). So if you are at 400, 300 position on 800x600 screen, the value will be 0.5, 0.5.

    To go from 0.5 back to real screen coordinates, just multiply instead of dividing.

    The below code is untested and just written in the forum without any IDE or testing/debugging, so YMMV.

    int screenWidth; //init this somewhere
    int screenHeight; //init this somewhere

    public float GetLocalWidthFromScreenX(int screenX)
    {
    float myWidth = screenX / screenWidth;
    return myWidth;
    }

    public float GetLocalHeightFromScreenY(int screenY)
    {
    float myHeight = screenY / screenHeight;
    return myHeight ;
    }

    public Vector2 GetLocalCoordinateFromScreenCoordinates(Point screenXY)
    {
    return new Vector2( GetLocalWidthFromScreenX(screenXY.X), GetLocalHeightFromScreenY(screenXY.Y) );
    }

    -----------------------------------------------
    Jim Welch
  • 04-11-2008 4:04 PM In reply to

    Re: Help with the gamestate sample

    How does having the width and height as values between 0 and 1 help with this? Why not just set width and height properties of the gameplayscreen object to the viewport's width and height and check the character's position against them? A comparison is a comparison whether it's pixels against pixels or a value between 0 and 1 against another 0-1 value.
    Jim Perry

    Here's what I'm up to.

  • 04-14-2008 12:55 PM In reply to

    Re: Help with the gamestate sample

    sorry i hadn't replied to this since thursday, but i had a big test to study for (still am actually), but anyways i have a small question: for screedWidth and the screenHeight this would be my ScreenManager.Game.GraphicsDevice.Viewport.Width/Height right? , and then if that is the case what are my screenX and screenY (where do they come from)? are they the float numbers that i want it at like .5f, .5f? i believe that is the case and if that is the case thanks for helping out I really appreciate the help and these forums are perfect for this yeah i am happy now.

    Contrary to popular belief chuck norris is what was dropped on Hiroshima and Nagasake.
  • 04-14-2008 2:45 PM In reply to

    Re: Help with the gamestate sample

    CSM:
    for screedWidth and the screenHeight this would be my ScreenManager.Game.GraphicsDevice.Viewport.Width/Height right?(where do they come from)


    yes

    CSM:
    , and then if that is the case what are my screenX and screenY are they the float numbers that i want it at like .5f, .5f?


    those are screen coordinates from your mouse or other screen objects

    Storing things in your own coordinate system (like between 0 and 1) is something that you'll need to reprogram everything to do, since nothing does that. The above code just does simple math to covert the real coordinates to percentages (between 0 and 1).

    If I were you, I'd look into a different method for doing this, since I don't see using this technique as a good idea. You'll also end up with wierd coordinates, because screens are not at a 1:1 ratio (like your code will produce, since your width and heights are both between 0 and 1). Monitors and TVs are usually wider than longer so your horizontal and vertical coordinate system will be all messed up if you change everything to between 0 and 1.



    -----------------------------------------------
    Jim Welch
  • 04-15-2008 1:38 PM In reply to

    Re: Help with the gamestate sample

    From above:

    If I were you, I'd look into a different method for doing this, since I don't see using this technique as a good idea. You'll also end up with wierd coordinates, because screens are not at a 1:1 ratio (like your code will produce, since your width and heights are both between 0 and 1). Monitors and TVs are usually wider than longer so your horizontal and vertical coordinate system will be all messed up if you change everything to between 0 and 1.

     

    What would you suggest I do, because i know that the gameplay screen isn't a square anyways.

    Contrary to popular belief chuck norris is what was dropped on Hiroshima and Nagasake.
  • 04-15-2008 2:13 PM In reply to

    Re: Help with the gamestate sample

    CSM:
     

    What would you suggest I do, because i know that the gameplay screen isn't a square anyways.



    What exactly are you trying to do? Just move a sprite on the screen? Then just check the position versus the screen dimensions. There's samples and tutorials on this site and other XNA sites with example code on this. I think the GamePlay screen in the GameStateManagement example even does this type of checking.
    -----------------------------------------------
    Jim Welch
  • 04-15-2008 2:29 PM In reply to

    Re: Help with the gamestate sample

    right now i have a sprite that moves with the arrow keys, but he can move off the screen. so what i want are borders for the screen. I used to have something that let me do this easily, but i didn't create the format for that and i didn't understand how it worked anyways, but i was used to it. Essentially that is all i want. the whole 0-1 thing was just what i was used to but that doesn't have to be what i am looking for, but what i want are screen borders.

     

     

    Contrary to popular belief chuck norris is what was dropped on Hiroshima and Nagasake.
  • 04-15-2008 3:18 PM In reply to

    Re: Help with the gamestate sample

    Here's how to modify the enemyPosition drawing code on the GSM example


    if(enemyPosition.X > this.ScreenManager.GraphicsDevice.Viewport.Width || 
    enemyPosition.X < 0 ||
    enemyPosition.Y > this.ScreenManager.GraphicsDevice.Viewport.Height ||
    enemyPosition.Y < 0 )
    {
    // then it's not allowed
    }


    -----------------------------------------------
    Jim Welch
  • 04-15-2008 9:07 PM In reply to

    Re: Help with the gamestate sample

    i just tried
                    if (movement.X > width)
    movement.X = width;
    if (movement.Y > height)
    movement.Y = height;
    if (movement.X < 0)
    movement.X = 0;
    if (movement.Y < 0)
    movement.Y = 0;
    //width, height = this.ScreenManager.GraphicsDevice.Viewport.Width/Height;
    but it has a problem when i say this.ScreenManager.GraphicsDevice.Viewport.Width/Height.
    Contrary to popular belief chuck norris is what was dropped on Hiroshima and Nagasake.
Page 1 of 1 (10 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG