Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

This question already has an answer here:

So here's what I thought might work but half the time I press the 'Right' key, it results in a crash and the rest of the time seems to produce no acceleration at all.

if (KeyboardState.IsKeyDown(Keys.Right))

            while (motion.X < 1)
                motion.X += 0.001f + motion.X;

I want to know why exactly it won't work, and any possible alternate algorithms.

share|improve this question
1  
I think you need to practice following code flow. If you were to analyze your code above, you'd realize that what you have is no different than setting the motion.X value to something greater than 1. Then it doesn't modify it anymore. There's no reason for the while loop. Take a step back and look into some basic tutorials for programming. You should know how to use the basic loops and other control flow statements before you go much further. –  Byte56 Feb 26 '13 at 22:06
    
Oh, of course, I feel kinda silly for not seeing that now. I'm fairly new to this but I know how to use loops and know the basics pretty well. Excuse me this one mistake :] –  Matthew Morgan Feb 26 '13 at 22:16
    
Good catch Sean, I thought there was something but when I looked before I answering, I couldn't find one that was just write. –  Byte56 Feb 26 '13 at 22:27
    
also do you really want to add motion.X onto itself twice i imagine what you are really after is motion.X = motion.X < 1 ?motion.X + 0.001f: 1; –  Yakyb Mar 1 '13 at 11:07
add comment

marked as duplicate by Byte56, Sean Middleditch, Laurent Couvidou, bummzack, Josh Petrie Mar 1 '13 at 15:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers

up vote 4 down vote accepted

To implement basic acceleration you need to know a little physics. The basics you need to know about are the relationships between:

  • Position - Where your object is
  • Velocity - The direction and rate your object is changing its position
  • Acceleration - The direction and rate your object is changing its velocity

So, to add acceleration, you need to have Position and Velocity already implemented. I'll assume you're doing that already, since you're asking about Acceleration.

Since you have velocity already implemented, I'll assume you have something like:

player.x += player.velocity.x * dt;
player.y += player.velocity.y * dt;

Where dt is the time since the last update. Now if we want to change the velocity with respect to time, we just need to add a line like this:

player.velocity.x += player.acceleration.x * dt;
player.velocity.y += player.acceleration.y * dt;

Now, if we want to modify that acceleration with the keyboard, we can do something simple like:

if (KeyboardState.IsKeyDown(Keys.Right))
    player.acceleration.x += .01f;
share|improve this answer
    
Ahh good, that works now. You're a gent. –  Matthew Morgan Feb 26 '13 at 22:23
1  
No problem Matthew. Sean has found a duplicate of this question, so this one will likely be closed, but that's no big deal. Just make sure to do a search in the future for similar questions and you'll hopefully find what you need. –  Byte56 Feb 26 '13 at 22:35
    
I found that other question before posting but I wasn't sure if it was exactly what I was looking for. But I see how it could have solved the problem now :] –  Matthew Morgan Feb 26 '13 at 22:37
add comment

You should get rid of the while loop. Your game is stalling in that loop since it can't escape the loop until motion.X is >= 1. You should just use the the statement within the loop and experiment with larger float values such as .01 or .1 .

share|improve this answer
add comment

you need to make the acceleration happens over time (per game tick) gradually.

you need to time the acceleration in such that every N seconds, it make your character moves faster until a limit is reached.

your while statement make it happen in a single tick, it make no difference than assigning X = 1.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.