Jump to content
  • Advertisement
Sign in to follow this  
precious roy

Game Timer problems

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

If you intended to correct an error in the post then please contact us.

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)
	{
	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.

 

Share this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
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

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!