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

Moving Objects Based on Time

Last post 8/5/2009 1:38 AM by BGold. 6 replies.
  • 8/3/2009 8:06 PM

    Moving Objects Based on Time

    I have a 2D game I`m working on that requires me to move sprite objects in a single direction (say right to left across a portion of the screen)

    I have no problem moving these sprites on a frame by frame basis (@ 60 FPS) in a linear fashion.

    However the game requires the ability of the sprite objects to `move faster` (not accelerate though) on higher difficulty levels -

    So my requirement is to be able to set my sprite speed based on time (eg. 50 pixels / second raising to say 100 pixels / second)

    As standard XNA sets the FPS (gametime) at 60 fps .I presume what I`m trying to do won`t effect the FPS but would be an independant timeing issue.

    Can anybody point me in the right direction ?

    Cheers

    GP
    I might finish something I start......one day !
  • 8/3/2009 8:31 PM In reply to

    Re: Moving Objects Based on Time

    If you're moving 60 pixels/second at 60 frames/second then that's the equivalent of 1 pixel per frame.  You calculate this by taking 60 pixels divided by 60 frames.  60/60 = 1 pixel per frame.  Not that we're using 60 frames because that's how many you're getting each second.  Technically with XNA you're getting 60 Update calls per second, but not necessarily 60 frames, so we're really dealing with updates here instead of frames.

    When moving at 50 pixels/second, you basically have to move those 50 pixels during the same 60 updates.  So you do the same division: 50 pixels divided by 60 frames = 0.8333 pixels per update.

    100 pixels/second works the same: 100/60 = 1.667 pixels per update.

    Store your sprite x and y position as floating point (using Vector2 is a great way to do this). 
  • 8/3/2009 8:41 PM In reply to

    Re: Moving Objects Based on Time

    Thanks Dave will do...Many thanks
    I might finish something I start......one day !
  • 8/4/2009 4:15 PM In reply to

    Re: Moving Objects Based on Time

    PixelsPerSecond * ElapsedSecond * Scale (1.0 would not change anything. 0.5 is half speed. 2.0 is double speed.)
  • 8/4/2009 4:40 PM In reply to

    Re: Moving Objects Based on Time

    It seems like the question has been thoroughly answered. I just wanted to throw in my two cents, however.

    Objects don't always have to move through every pixel. In fact, they can't, if you want them to go any faster than 1 px/frame (which you really do). It's okay for them to skip pixels, such as going 2, 3, or 4 pixels per frame.
  • 8/4/2009 6:49 PM In reply to

    Re: Moving Objects Based on Time

    Bgold I agree with what you say.

    However I wanted to be able to move an object at an almost infinite speed per update.

    Speaking of which. Will a Vector2 accept a floating point value or will it need to be cast to an integer ?

    Thanks
    I might finish something I start......one day !
  • 8/5/2009 1:38 AM In reply to

    Re: Moving Objects Based on Time

    Gingerprince:
    However I wanted to be able to move an object at an almost infinite speed per update.

    Almost infinite? As in, over 800 pixels per frame? If so, you won't see the sprite ever. However, as I said, you can go 2, 3,  4, 4.5, 81, 17, 12, however many pixels you want per frame. Just be aware that in an update loop of 60 frames per second, you'll be doing 60 of those updates in one second.


    Gingerprince:

    Speaking of which. Will a Vector2 accept a floating point value or will it need to be cast to an integer ?


    Vector2's use floats natively. You'd have to cast the other way around.
Page 1 of 1 (7 items) Previous Next