• 10
• 10
• 12
• 12
• 14

# Game Timer problems

This topic is 766 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello all,

I have been working on a small 2d rpg game, i am at the point where i need to use my game timer but it seems there is a problem with it and i can not figure out what it is. i have a fire ball that gets spawned and moves in a certain direction and continues untill outside of screen.

void FireBall::FireBall()
{
//set movement
switch (Direction)
{
Acceleration.Y -= speed; // speed = 0.05
break;
Acceleration.Y += speed;
break;
Acceleration.X -= speed;
break;
Acceleration.X += speed;
break;
}
}

void FireBall::Update()
{
double dt = ThePrecisionDeltaTime::Instance().GetDt();
Velocity += Acceleration * dt;
Acceleration = 0;
Location += Velocity * dt;
}

void FireBall::Draw(Camera* camera)
{
Vector2F camtrans = Location - Vector2F(camera->Screen.Location());

mFireBallSheet->Draw(animation.GetAnimationFrame(), camtrans);
}



and my timer:

Windows_PrecisionDeltaTime::Windows_PrecisionDeltaTime()
{
LARGE_INTEGER Frequency;
QueryPerformanceFrequency(&Frequency);
}

double Windows_PrecisionDeltaTime::GetTicks() const
{
return m_CurrentTick;
}

double Windows_PrecisionDeltaTime::GetDt() const
{
return m_DeltaTime;
}

void Windows_PrecisionDeltaTime::Update()
{
LARGE_INTEGER CurrentTick;
QueryPerformanceCounter(&CurrentTick);

m_DeltaTime = m_CurrentTick - m_LastTick;
m_LastTick = m_CurrentTick;
}



The problem is when i play the game in release or debug the fireball will fly at different speeds, also in release if i shoot them in close proximity some fireballs go faster than other fireballs when the speed should be a constant.

do any of you know what i am doing wrong here. i can record a vid if that will help illustratie the problem more.

##### Share on other sites

1)  Think about what's happening here and why it would mean different speeds for different fireballs.

Velocity += Acceleration * dt;
Acceleration = 0;


It looks like you may be cargo-culting acceleration. If you want to set a velocity then just set a velocity.

2) In your FireBall constructor you're using n += x and n -= x when you really mean n = x and n = -x. This is unsafe and I'm surprised you're not getting extremely wild variance from it (unless you're setting them to zero in the declaration). POD members do not have implicit initial values unless they're static.

3) In the timer I think you'll have better luck if you apply precision to deltas instead of ticks. Ticks are not really meaningful except relative to one another. Exporting tick values seems a little suspect as well. I'd instinctively return a custom TimeStamp type (probably just a LONGLONG) in that case instead of just doubles. This is a minor concern though.

##### Share on other sites

agreed with above on all counts, especially #3 - integer comparisons are cheaper than floating-point comparisons and the only time you have a practical reason to be concerned with the truncation might on very slow (tiny fraction of a pixel per second) moving objects, at which point floating-point accuracy error accumulation will probably trump out the difference from truncation; and when timing operations for performance reasons.

##### Share on other sites

Tanks for the help. (Nfries88) I am indeed making a very small game 192x192 pixels and i do mind the truncation.  the timer was indeed working but i was having a truncation error. where moving right = to 2 pixels moved and right 1 pixel. when in fact i was moving 1.5 in ether direction. all fixed now. thanks for your help