0
\$\begingroup\$

I want to use delta time in my movement code for my player, and I'm not sure how to do it. At the moment, I have this:

else if (keyboardState.IsKeyDown(Keys.D))
        {
            double moveSpeed = 0.3 * delta;

            if (velocity < 0)
            {
                double turnSpeed = 0.5 * delta;
                velocity += turnSpeed;
            }
            else
            {
                if (velocity + moveSpeed > .1)
                {
                    velocity = .1;
                }
                else
                {
                    velocity += moveSpeed;
                }
            }
        }

        position.X += (float)velocity;

Is this correct? I'm trying to make my player's movement gradually speed up until it hits the maximum velocity.

position.X is the Vector2 X of the player. delta is set to gameTime.ElapsedGameTime.TotalSeconds.

\$\endgroup\$
  • 2
    \$\begingroup\$ is your code not working? what is the problem and what do you expect? \$\endgroup\$ – TomTsagk Jun 12 '18 at 12:18
  • \$\begingroup\$ @Logan, I suggest you register, this will allow you to edit your own question, accept one of the answers and comment here or on the answers. \$\endgroup\$ – Vaillancourt Jun 13 '18 at 0:44
2
\$\begingroup\$

The code you've got there is going to produce some really small numbers.

Let's say your game is running at 60 frames per second (that's pretty typical for MonoGame). The delta variable will be about 0.016666 seconds or 16 milliseconds.

So that means after the first calculation your moveSpeed is going to be equal to 0.0005. That's probably not what you want.

The first thing you need to do is decide what your numbers actually represent. I normally think about things in "pixel" units. So in my case moveSpeed = 300 would mean "move 300 pixels per second".

Once you've come to grips with that concept it's easier to reason about what's going on in your code. For example, you're trying to calculate how far your guy will move in "one frame" if he's moving at 300 pixels per second. So..

position.X += moveSpeed * delta;

will move the guy one frame. After 60 frames (1 second) he will have moved 300 pixels.

Honestly, I don't really understand what you're trying to do in the code you posted so it's quite difficult to tell you how to fix it. My guess is you've got a little confused along the way.

My suggestion, start over, start simple and work your way up to the more complex stuff.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ You're welcome. Click the tick to accept the answer. \$\endgroup\$ – craftworkgames Jun 15 '18 at 10:56
0
\$\begingroup\$

Generally the speed value is multiplied by Time.deltaTime to give a new velocity vector (Velocity is Direction + Speed) that is independent of how many frames a second any given client can handle.

This isn't always the case it depends what you're doing but in your case multiplying the velocity Vector by speed * delta below should work fine.

velocity += moveSpeed * Time.deltaTime;

Misread framework in question, How to work with delta time?

@craftworkgames Answers this question better than I.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ This is not a Unity question btw. \$\endgroup\$ – craftworkgames Jun 13 '18 at 0:19
  • \$\begingroup\$ I absolutely didn't notice that I just saw 'mono', the principle still applies but I'll remove the confusing parts, thanks! I also just found a question from December where you answer the same thing far better in the same framework! \$\endgroup\$ – Pheonix2105 Jun 13 '18 at 0:20
  • \$\begingroup\$ Hehe, no problem mate. I should probably put this stuff on a blog somewhere so I have it handy when people ask.. lol \$\endgroup\$ – craftworkgames Jun 13 '18 at 0:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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