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.
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.
#include "CGame.h"
#include "CPlayer.h"
CPlayer Player;
CGame Game;
constint FPS = 30;
constint 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?