How would you go about simulating accleration on an object....

This is a discussion on How would you go about simulating accleration on an object.... within the Game Programming forums, part of the General Programming Boards category; I need to simulate accelration on an object when the the left or right key is held down. At this ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    131

    How would you go about simulating accleration on an object....

    I need to simulate accelration on an object when the the left or right key is held down. At this point when hold down the left/right key I have the object moving 2 pxiles at an interval of 1 millisecond. I am guessing there will have to be some equation that works this over time. None the less I am confused and any insight would be great....

    Thanks

    CHad

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    I guess it would be worth mentioning that there is no gravity issues invloved. This is simply the longer you hold the right key the faster the object will move to the right.

  3. #3
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Code:
    if(KeyIsDown)
    {
    
    //We Want to Accelerate (Set an Acceleration Value)
    
    }
    else
    {
    
    //We Don't Want to Accelerate (Set an Acceleration Value)
    
    }
    
    //Update Object's Velocity Based on Acceleration
    
    //Update Object's Position Based on Velocity
    There's a Pseudo Start
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    The key components to acceleration are velocity and time. Therefor you will need to handle each.

    For example, working in purely one dimension (the x-plane):
    Code:
    float x,xv;
    You have an x position and an x velocity variable. To create acceleration/deceleration, you add and subtract to the velocity (usually multiplying the amount you add/subtract by the time that has passed). You then add the velocity to the position and yet again multiply by the time that has passed.

    The reasoning behind this is as such:

    Velocity is in m/s
    Acceleration is in m/s/s or v/s

    Therefor we take this scenario:

    A car is accelerating at 10 m/s/s from rest (initial velocity=0m/s). How fast is the car going after one half second?

    You figure this out by taking v=vi+a*t (v=velocity, vi=initial velocity, a=acceleration, t=time). Giving us: v=0m/s + 10m/s/s * 1/2s

    Canceling out the units we get: 5m/s. This is how fast the object is going after one half second.

    Now, to get back to the code, the reasoning why I showed you the above example is because it works the same way code-wise. Here is an example program:

    Code:
    float x,xv;
    x=0;   // Initial position is 0
    xv=0; // Initial velocity is 0 m/s
    
    xv+=10*0.5f; // 10 is our acceleration (in m/s/s) and 0.5 is the time elapsed in seconds
    
    x+=xv*0.5f; // Now we add the velocity to the position and this will give us our new position.
    If you run the above code's last two lines repeatedly, you will have an object accelerating faster and faster. Replace the 10 with how much acceleration you wish, and the 0.5f with the actual amount of time elapsed since the last calculation and you're set!

  5. #5
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    http://nehe.gamedev.net deals with alot of thatt just ignore the opengl and look how it increses texture mapping tutorials has this i think
    New Function!!!!

    glAddIdol(C+noob);

    The feeling of rusty spoons against my salad fingers is almost ORGASMIC

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    I just want to thank everyone for the help....

    One problem is that as far as I can tell I do not have control over my time variable(at least as far as I can tell). I am coding in manage c++ and this is what I have in my Keydown event handler......
    Code:
    	private: System::Void keyDown_press(System::Object *  sender, System::Windows::Forms::KeyEventArgs *  e)
    		{
    			switch(e->KeyCode)
    			{
    				case Keys::Right:
    					dx=2;
    					break;
    				case Keys::Left:
    					dx=-2;
    					break; 	
    				case Keys::Space:
    					fire();
    					break;
    			}
    		}
    The variable dx represent the accleration of 2pixels per 1 millisecond (The 1 millisecond is handle in another function). Is there way to do this with out the variable time?

    I was messing around with things like dx +=2 but that made the object acclerate way to fast.

    EDIT: Beign that I am newer to programming and espically mananged C++ I don't even konw if I am correct in my assumptions. Now that I am thinking about it the 1 millisecond might not be the "time" I was thinking about. I think it is how oftern my the event handler is triggered when I hold a Keydown. I have to do some more thinking ...
    Last edited by chadsxe; 08-16-2005 at 08:41 AM.

  7. #7
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Basing updates on time isn't generally a good idea (in my opinion). I could lead to problems such as, "What if the user only accelerates for half a second?", etc.

    In many games, you generally want to update as fast as possible (i.e. every frame). Most operate on a level of looping through:

    CheckWhichKeysArePressed();
    UpdateAllPositionsAndVariables();
    RedrawTheFrame();

    Of course, other functions can be added for specific needs.

    By doing this, you can avoid the problem above. If we throw in a "CheckTime()" function which returns the current time, before UpdateAllPositionsAndVariables(), then we can speed up the object, regardless of how much time has passed...I'll try to explain it well...

    Code:
    long TimeNow, TimeLast, TimeDifference;
    
    TimeLast = CheckTime();
    
    while(OurGameIsRunning == true)
    {
    	CheckWhichKeysArePressed();
    
    	TimeNow = CheckTime();
    
    	TimeDifference = TimeNow - TimeLast;
    
    	UpdateAllPositionsAndVariables(TimeDifference);
    	RedrawTheFrame();
    
    	TimeLast = TimeNow;
    }
    Then, in the UpdateAllPositionsAndVariables() function, (I.e. jverkoey's example), you can manipulate each variable (In his example, TimeDifference = 0.5 seconds), and you're not relying on a necessary 1 millisecond to pass.

    Note: CheckTime() isn't a real function. Other functions such as GetTickCount() (To name one) are however.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 07:49 AM
  2. synchronization object choosing
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 04:33 AM
  3. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21