-
Time Dependent Movement?
Hi I got my little paddle object and player working together to move around, and it does what I want it to except it does it at whatever frame rate the window is going at, (alot I assume). So when I tell my paddle to move up it flies off the screen, how do I make the movement updates time dependent?
My project is based off of Nehe's Lesson 1
Player.h
Code:
#include "vector2.h"
#include "Paddle.h"
class Player
{
public:
vector2 location;
Paddle paddle;
const vector2 & get_position() const { return location; }
Player()
{
location.x = 0;
location.y = 0;
}
void draw_paddle()
{
paddle.draw(location);
}
void move_paddle(vector2 direction)
{
location += direction;
}
};
Player player;
Paddle.h
Code:
#include "vector2.h"
class Paddle
{
public:
void draw(vector2 vec)
{
glTranslatef(vec.x,vec.y,-100.0f);
glBegin(GL_QUADS); // Start Drawing Quads
glVertex3f(-1.0f, -5.0f, 1.0f); // Bottom Left Of The Texture and Quad
glVertex3f( 1.0f, -5.0f, 1.0f); // Bottom Right Of The Texture and Quad
glVertex3f( 1.0f, 5.0f, 1.0f); // Top Right Of The Texture and Quad
glVertex3f(-1.0f, 5.0f, 1.0f); // Top Left Of The Texture and Quad
glEnd();
}
};
-
So don't make direction so big, or multiply it by the time-step involved.
-
You will need a time delta:
Code:
LARGE_INTEGER freq;
LARGE_INTEGER lastTime;
LARGE_INTEGER nowTime;
QueryPerformanceFrequency(&freq);
...
...
QueryPerformanceCounter(&lastTime);
while (inGame)
{
QueryPerformanceCounter(&nowTime);
float timeDelta = (nowTime.QuadPart - lastTime.QuadPart) * (1.0f / freq.QuadPart);
Render();
Update(timeDelta);
lastTime = nowTime;
}
Now multiply all movements by the timeDelta passed to Update(). I would imagine something like this:
Code:
GameApp::Update(float timeDelta)
{
m_pScene->Update(timeDelta);
...
}
-
Thanks bubba I'll use the information you gave me in conjunction with this tidbit I found on google: Precise (and simple!) timer using time.h - C++ - Source Code | DreamInCode.net
-
First question, is this what you meant? Second off am I doing this right? I'm getting an error
binary '*' : 'vector2' does not define this operator or a conversion to a type acceptable to the predefined operator
Code:
#include "vector2.h"
#include "Paddle.h"
#include "time.h"
time_t timer;
class Player
{
public:
vector2 location;
Paddle paddle;
const vector2 & get_position() const { return location; }
Player()
{
location.x = 0;
location.y = 0;
}
void draw_paddle()
{
paddle.draw(location);
}
void move_paddle(vector2 direction)
{
location += direction * time(&timer) * 1;
}
};
Player player;
-
Around this time I will also note a bit of an oddity.
Only when I move in the right direction it flies off screen now, it appears as up down and left are working.
That is when I remove the extra silliness from the move function i just added.
-
Code:
void move_paddle(vector2 direction)
{
location += direction * time(&timer) * 1;
}
What is this? Are you going to recompute your delta for every object? If you do your system will fail 100% because each delta will be slightly different. Compute your delta once before calling update and then use that delta for ALL objects.
It's as simple as this:
Code:
...
m_vecPosition += m_vecVelocity * timeDelta;
Or:
Code:
...
ComputeForces();
RigidBody.Accel = RigidBody.Force / RigidBody.Mass;
RigidBody.Velocity += RigidBody.Accel * timeDelta;
RigidBody.Position += RigidBody.Velocity * timeDelta;
...
This does not take into account friction or drag which 'normally' act in the exact opposite direction of velocity.
But regardless if you are simulating physics or just moving things across the screen arbitrarily you will always multiply by the timeDelta which is expressed in seconds.
-
Bubba thank you yet again for pointing out the obvious (in all seriousness) :D