Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Game Timer problems

Started by precious roy Mar 13, 2016 at 7:37 PM 3 replies 1.9k views
Original Post
precious roy
precious roy

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)
	{
	case AD_Up:
		Acceleration.Y -= speed; // speed = 0.05
		break;
	case AD_Down:
		Acceleration.Y += speed;
		break;
	case AD_Left:
		Acceleration.X -= speed;
		break;
	case AD_Right:
		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);
	m_Frequency = double(Frequency.QuadPart) / 1000.0;
}

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_CurrentTick = double(CurrentTick.QuadPart) / m_Frequency;
	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.

Khatharr
Khatharr

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.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
nfries88
nfries88

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.

precious roy
precious roy

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

nfries88
nfries88

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

I was referring to timer value truncation (when dividing by frequency), I wasn't recommending any change aside for the timer.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.