Creating acceleration

Hello once again ^^'

The next question I was asking myself is how to add acceleration to my Players movements and once again I've got no idea of how to do that Kind of Thing :/

I have seen a few similar Posts and questions, but I understood none of them.

I'm animating/moving the Player Frame dependant at the Moment. there is no such Thing as a Delta time or timestep. The Frame rate is capped in my Main.cpp-file.
Do I Need such a timestep or Frame independant movemet? And how is it related to the movement of my Player? And even more important, how do i create acceleration?

I guess I'll Need some floats for AccelX / AccelY, probably the SDL_GetTicks()-function for Frame independant movement and a Maximum Speed. Probably two booleans for the Animation. But how can all of These things be combined?
You should start by adding a concept of time. If your current implementation works on frame rate then does that mean that a user has a twice the frame rate of your system then the player moves twice as fast? That's not a good way to go about it.

You should understand the physics of acceleration before trying to code it:
http://en.wikipedia.org/wiki/Classical_mechanics
Alright :) I'll add a concept of time then. What would you suggest? Should I still cap the FPS in my Main.cpp?

If I have a Framerate of 35, how can I make my Player movement frame independant? Because my Playerfunctions are called in other functions (class is called CGame, the engine itself), which are called in main, where the Frame rate is regulated. So won't that mean I'll always run on 35 Frames (if my Computer is fast enough of course)?

Or should I update the Delta time in every Frame? Sorry if i'm kinda confusing ^^ So basically, where should I calculate my deltatime if i have set a Frame cap in my Main.cpp where everything else is called?

I've seen on the Internet something like:
PosX += VelX * deltatime;
When you're ready to draw each frame, figure out how much time has elapsed since the last frame. Between the current game time and the elapsed time from the previous frame, you should be able to figure out whatever you need.
Hmmm I've searched for a Frame independant movement tutorial and also found one, but it seems like I'm doing something wrong...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
void CPlayer::Update(CMap* Map)
{
    OldTime = CurrentTime; //Both are of type Uint32
    CurrentTime = SDL_GetTicks();
    dtTime = (CurrentTime - OldTime)/ 1000.0f; //dtTime is a float
    std::cout << dtTime;

    VelY = 1;

    SDL_PumpEvents();

    const Uint8* Keystates = SDL_GetKeyboardState(NULL);

    if (Keystates[SDL_SCANCODE_A])
    {
        m_bMovingLeft = true;
        VelX = -40;
        //Map->SetCameraPos(VelX,0);
        CurrentColumn = 1;
        CurrentFrame++;
    }
    else if (Keystates[SDL_SCANCODE_D])
    {
        m_bMovingRight = true;
        VelX = 40;
        //Map->SetCameraPos(VelX,0);
        CurrentColumn = 0;
        CurrentFrame++;

        if (VelX == 0)
        {
            CurrentFrame = 0;
        }
    }
    else if (Keystates[SDL_SCANCODE_SPACE])
    {
        if (m_bLockJump == false)
        {
            m_bisJumping = true;
            VelY = -50;
            CurrentFrame = 1;
            m_bLockJump = true;
            Mix_PlayChannel(-1,Jump,0);

        }
        else
        {
            m_bLockJump = false;
        }

    }
    else if (!Keystates[SDL_SCANCODE_D] || !Keystates[SDL_SCANCODE_A] && m_bisJumping == false)
    {
        VelX = 0;
        CurrentFrame = 4;
    }

    //Here Comes the collision
    int TilePos = 0;

	//x axis first (--)
	if(VelX > 0) //right
        {
		if(Collision_Ver(PosX + VelX + PLAYER_WIDTH - Map->StartX, PosY, TilePos, Map) == true)
            {
			PosX = (TilePos * Tile.GetSize()) - PLAYER_WIDTH - 1;
			Mix_PlayChannel(-1,Blockhit,0);
            }
		else
            {
               PosX += VelX * dtTime; //<-- moving veeeeery slowly
            }

    }
}


And this is where my Frame rate is capped:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "CGame.h"
#include "CPlayer.h"


CPlayer Player;
CGame Game;

const int FPS = 30;
const int DELAY_TIME = 1000.0f / FPS;



int main (int argc, char* argv [])
{
    Uint32 frameStart;
    Uint32 frameTime;

    if(Game.OnInit() == false)
    {
        return -1;
    }

    SDL_Event Event;

    while(Game.bRunning)
    {
        frameStart = SDL_GetTicks();

        while(SDL_PollEvent(&Event))
        {
            Game.OnEvent(&Event);
        }

        Game.OnUpdate(); //In here is the Player update function
        Game.OnRender();

        frameTime = SDL_GetTicks() - frameStart;

        if (frameTime < DELAY_TIME)
        {
            SDL_Delay((int) (DELAY_TIME - frameTime));
        }
    }

    Game.OnCleanup();


    return 0;
}


Should i get the Current Time at the end of the Update Function? Or even after I finished Rendering?

The dt is constantly between 0.0330 and0.0350... Only at the start its something like 1.180. Is it nomal? Do I only have to set the velocity a bit higher?
Topic archived. No new replies allowed.