8
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – MichaelHouse Feb 26 '13 at 22:06
  • \$\begingroup\$ 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 :] \$\endgroup\$ – Matthew Morgan Feb 26 '13 at 22:16
  • \$\begingroup\$ Good catch Sean, I thought there was something but when I looked before I answering, I couldn't find one that was just right. \$\endgroup\$ – MichaelHouse Feb 26 '13 at 22:27
  • \$\begingroup\$ 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; \$\endgroup\$ – RoughPlace Mar 1 '13 at 11:07
12
\$\begingroup\$

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;
\$\endgroup\$
  • \$\begingroup\$ Ahh good, that works now. You're a gent. \$\endgroup\$ – Matthew Morgan Feb 26 '13 at 22:23
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – MichaelHouse Feb 26 '13 at 22:35
  • \$\begingroup\$ 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 :] \$\endgroup\$ – Matthew Morgan Feb 26 '13 at 22:37
3
\$\begingroup\$

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 .

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$

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