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

  • Home
  • SEARCH
  • 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 160245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:00:02+00:00 2026-05-11T11:00:02+00:00

I’m working on a 2D game where I’m trying to accelerate an object to

  • 0

I’m working on a 2D game where I’m trying to accelerate an object to a top speed using some basic physics code.

Here’s the pseudocode for it:

 const float acceleration = 0.02f; const float friction     = 0.8f;  // value is always 0.0..1.0       float velocity     = 0;       float position     = 0;  move() {    velocity += acceleration;    velocity *= friction;    position += velocity; } 

This is a very simplified approach that doesn’t rely on mass or actual friction (the in-code friction is just a generic force acting against movement). It works well as the ‘velocity *= friction;’ part keeps the velocity from going past a certain point. However, it’s this top speed and its relationship to the acceleration and friction where I’m a bit lost.

What I’d like to do is set a top speed, and the amount of time it takes to reach it, then use them to derive the acceleration and friction values.

i.e.,

 const float max_velocity = 2.0;  const int   ticks;       = 120; // If my game runs at 60 FPS, I'd like a                                  // moving object to reach max_velocity in                                  // exactly 2 seconds. const float acceleration = ? const float friction     = ? 
  • 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. 2026-05-11T11:00:03+00:00Added an answer on May 11, 2026 at 11:00 am

    I found this question very interesting since I had recently done some work on modeling projectile motion with drag.

    Point 1: You are essentially updating the position and velocity using an explicit/forward Euler iteration where each new value for the states should be a function of the old values. In such a case, you should be updating the position first, then updating the velocity.

    Point 2: There are more realistic physics models for the effect of drag friction. One model (suggested by Adam Liss) involves a drag force that is proportional to the velocity (known as Stokes’ drag, which generally applies to low velocity situations). The one I previously suggested involves a drag force that is proportional to the square of the velocity (known as quadratic drag, which generally applies to high velocity situations). I’ll address each one with regard to how you would deduce formulas for the maximum velocity and the time required to effectively reach the maximum velocity. I’ll forego the complete derivations since they are rather involved.


    Stokes’ drag:

    The equation for updating the velocity would be:

    velocity += acceleration - friction*velocity 

    which represents the following differential equation:

    dv/dt = a - f*v 

    Using the first entry in this integral table, we can find the solution (assuming v = 0 at t = 0):

    v = (a/f) - (a/f)*exp(-f*t) 

    The maximum (i.e. terminal) velocity occurs when t >> 0, so that the second term in the equation is very close to zero and:

    v_max = a/f 

    Regarding the time needed to reach the maximum velocity, note that the equation never truly reaches it, but instead asymptotes towards it. However, when the argument of the exponential equals -5, the velocity is around 98% of the maximum velocity, probably close enough to consider it equal. You can then approximate the time to maximum velocity as:

    t_max = 5/f 

    You can then use these two equations to solve for f and a given a desired vmax and tmax.


    Quadratic drag:

    The equation for updating the velocity would be:

    velocity += acceleration - friction*velocity*velocity 

    which represents the following differential equation:

    dv/dt = a - f*v^2 

    Using the first entry in this integral table, we can find the solution (assuming v = 0 at t = 0):

    v = sqrt(a/f)*(exp(2*sqrt(a*f)*t) - 1)/(exp(2*sqrt(a*f)*t) + 1) 

    The maximum (i.e. terminal) velocity occurs when t >> 0, so that the exponential terms are much greater than 1 and the equation approaches:

    v_max = sqrt(a/f) 

    Regarding the time needed to reach the maximum velocity, note that the equation never truly reaches it, but instead asymptotes towards it. However, when the argument of the exponential equals 5, the velocity is around 99% of the maximum velocity, probably close enough to consider it equal. You can then approximate the time to maximum velocity as:

    t_max = 2.5/sqrt(a*f) 

    which is also equivalent to:

    t_max = 2.5/(f*v_max) 

    For a desired vmax and tmax, the second equation for tmax will tell you what f should be, and then you can plug that in to the equation for vmax to get the value for a.


    This seems like a bit of overkill, but these are actually some of the simplest ways to model drag! Anyone who really wants to see the integration steps can shoot me an email and I’ll send them to you. They are a bit too involved to type here.

    Another Point: I didn’t immediately realize this, but the updating of the velocity is not necessary anymore if you instead use the formulas I derived for v(t). If you are simply modeling acceleration from rest, and you are keeping track of the time since the acceleration began, the code would look something like:

    position += velocity_function(timeSinceStart) 

    where ‘velocity_function’ is one of the two formulas for v(t) and you would no longer need a velocity variable. In general, there is a trade-off here: calculating v(t) may be more computationally expensive than simply updating velocity with an iterative scheme (due to the exponential terms), but it is guaranteed to remain stable and bounded. Under certain conditions (like trying to get a very short tmax), the iteration can become unstable and blow-up, a common problem with the forward Euler method. However, maintaining limits on the variables (like 0 < f < 1), should prevent these instabilities.

    In addition, if you’re feeling somewhat masochistic, you may be able to integrate the formula for v(t) to get a closed form solution for p(t), thus foregoing the need for a Newton iteration altogether. I’ll leave this for others to attempt. =)

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

Sidebar

Ask A Question

Stats

  • Questions 107k
  • Answers 107k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Server Side Files : No, unless you serve them via… May 11, 2026 at 9:03 pm
  • Editorial Team
    Editorial Team added an answer If it's a one-off operation you don't need to set… May 11, 2026 at 9:03 pm
  • Editorial Team
    Editorial Team added an answer Generally, the best practice is to keep indexes and constraints… May 11, 2026 at 9:03 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.