Recording the game, drops the collision detection algorithm

Started by
3 comments, last by Aardvajk 9 years, 2 months ago

Hi All,

I'm doing space invadres clone game, and when I tried to record the game using camcoder, the collision detection between bullets and the invaders are droped and it's not behaving correctly i.e some bullets can hit invaders that shouldn't be hit, because there are in-front other invaders.

The collision detection is here:


void CheckBulletsInvadersCollision(Ship *ship)
{
	for (vector<Invader*>::iterator invIter = g_Invaders.invaders->begin(); invIter != g_Invaders.invaders->end();)
	{
		if ((*invIter)->Alive)
		{
			bool isBulletsInvaderCollided = false;

			for (list<Bullet*>::iterator it = ship->Bullets->begin(); it != ship->Bullets->end();)
			{

				float left = (*it)->Position.x - 5;
				float top = (*it)->Position.y - 13;

				if (CheckCollision(left, top, (*invIter)->AlienSprite->m_X, (*invIter)->AlienSprite->GetImage()->GetWidth() / 4
					, (*invIter)->AlienSprite->m_Y, (*invIter)->AlienSprite->GetImage()->GetHeight() / 4))
				{
				 	isBulletsInvaderCollided = true;
  					Bullet *bullet = *it;
					it = ship->Bullets->erase(it);
					delete bullet;

					ship->Canfire = true;
				}
				else
				{
					it++;
				}

			}
			if (isBulletsInvaderCollided)
			{
				Invader *inv = *invIter;
				g_Invaders.invaders->erase(invIter);
				delete inv;
				g_Invaders.killed++;
				g_Invaders.alive = false;
			}
			else
			{
				invIter++;
			}
		}
	}
}

Advertisement

Is CheckCollision a sweeping algorithm, or a static intersection test?

Do you have a fixed time step?

It is generally best to have a fixed time step to avoid bugs related to faster computers, slower computers, and bogged down computers. That may be your problem. It is also generally a good idea to use a swept volume collision test to scan for collisions over the entire time between frames. Otherwise it is possible for fast moving objects to hop over each other, for players to hide inside walls, and other trickery.

The collision is a simple intersection test.

That is my game loop looks like:


uint32 timer = (uint32)s3eTimerGetMs();
 	// Loop forever, until the user or the OS performs some action to quit the app
	while (!s3eDeviceCheckQuitRequest())
	{
		// Calculate the amount of time that's passed since last frame
		int delta = uint32(s3eTimerGetMs()) - timer;
		timer += delta;

		g_Input.Update();

		Iw2DSurfaceClear(0x00000000);

		Move_Invaders(delta);

		ship->Update(delta);
		ship->Render();

		if (ship->NumFire == 9)
		{
			ship->NumFire = 0;
			saucer.mAlive = true;
		}
		saucer.Update(delta);
		saucer.Render();

		Render_Barriers(barriers);

		CheckBulletsSaucerCollision(&saucer, ship);
		CheckBulletsInvadersCollision(ship);
		CheckBulletsBarriersCollision(ship, barriers, gameResources);
		 
		//Iw2DDrawString("Score = ", CIwFVec2(10, 10), CIwFVec2(50, 50), IW_2D_FONT_ALIGN_CENTRE, IW_2D_FONT_ALIGN_CENTRE);
        //Draws Surface to screen
        Iw2DSurfaceShow();

        // Sleep for 0ms to allow the OS to process events etc.
        s3eDeviceYield(0);
    }

Using a (Marmalade??) milliseconds timer may well be the problem. I don't know how s3eTimerGetMs works under the hood (Windows timer?), but it may have a resolution, at best, of several milliseconds, possibly as much as 10-15 milliseconds. As frob mentions, you should fix your timestep, use a sweep algorithm for collision, and probably should be using a high-resolution timer if you're working in Windows.

By fixing your time-step, you'll obviate problems caused by delays in updates resulting from things like frame-grabbing, etc.

In any case, you should check how long it takes to do a frame-grab, using a high-resolution timer, to confirm/refute the possibility that timing is the problem.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Just in case it isn't clear, recording is slowing things down so much that your delta is so large that the objects are literally jumping over each other. This is known as "tunnelling" in collision detection circles.

Solutions as above, just wanted to explain the problem in simpler terms.

This topic is closed to new replies.

Advertisement