Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7630849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:12:16+00:00 2026-05-31T06:12:16+00:00

At the moment, I have a sprite that I have arbitrarily set to move

  • 0

At the moment, I have a sprite that I have arbitrarily set to move 1 pixel per second. The code is basically this (The code isn’t optimised at all, I could do it much nicer but it is the principle I am trying to solve first:):

private const long MOVEMENT_SPEED = 10000000; // Ticks in 1 second
private long movementTimeSpan = MOVEMENT_SPEED;


protected void PerformMovement(GameTime gameTime)
{
    movementTimeSpan -= gameTime.ElapsedGameTime.Ticks;

    if (movementTimeSpan <= 0)
    {
       // Do the movement of 1 pixel in here, and set movementTimeSpan back to MOVEMENT_SPEED
    }
}

Perform movement is called in a loop as you’d expect, and it equates to updating around 10 times per second. So if I lower the MOVEMENT_SPEED, my sprite speeds up, but it never gets any faster than 10 pixels per second. For projectiles and other stuff I obviously want it to update much faster than this.

If I alter the movement to 2 pixels or more, it creates issues with calculating collisions and suchlike, but these might be possible to overcome.

The other alternative is to store x and y as a float rather than an int, and increase the values as a fraction of the number of elapsed ticks. I am not sure if this will create smooth movement or not as there still has to be some rounding involved.

So my question is, does anyone know the standard way?

Should I increase the amount to more than 1 pixel and update my collision detection to be recursive, should I store X,Y as floats and move as a % of elapsed time, or is there a 3rd better way of doing it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:12:17+00:00Added an answer on May 31, 2026 at 6:12 am

    The standard way is to not count down a timer to move, but instead the opposite:

    private const float MOVEMENT_SPEED = 10.0f; //pixels per second
    private float time;
    
    protected void PerformMovement(GameTime gameTime)
    {
        time = (float)gameTime.ElapsedGameTime.TotalSeconds;
    
        character.X += MOVEMENT_SPEED * time;
    }
    

    Make the movement based on the time elapsed. The reason floats are commonly used is to get the fractional value of motion. Fixed-point is another common fractional representation but uses ints instead.

    As for collision, collision can be very tricky but in general you don’t absolutely need to do it once per pixel of motion (as you suggested with recursion); that’s overkill and will lead to terrible performance in no time. If you are currently having trouble with 2-pixel motion, I would reevaluate how you’re doing your collisions. In general, it becomes problematic when you’re moving very fast to the point of skipping over thin walls, or even passing over to the “wrong side” of a wall, depending on how your collision is set up. This is known as “tunnelling”. There are many ways of solving this. Look here and scroll down to “Preventing Tunnelling”. As the article states many people just cap their speed at a safe value. But another common method is to “step” through your algorithm in smaller time steps than is currently being passed in. For example, if the current elapsed time is 0.1, you could step by 0.01 within a loop and check each small step.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

At the moment I have some test code that starts something like this: CheckBoxPreference
At the moment I have this code (and I don't like it): private RenderedImage
At the moment I have this Code (Deepth = 2 Categories) private void Window_Loaded(object
At the moment I have code that is looking through a huge string. It
At the moment I have a DocumentViewer in a WPF window that displays an
At the moment I have a combobox that is populated from the name fields
At the moment I have the following code which works fine. label = new
at the moment i have this <div id=beau> <div> <input type=text id=lun name=lun size=24
At the moment I have just this route defined routes.MapRoute( Default, {controller}/{action}/{id}, new {
In a game that I am making whilst using Cocos2d, I have a sprite

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.