Advertisement

Spaceolopy

Started by July 25, 2022 01:09 AM
86 comments, last by pbivens67 1 year, 11 months ago

Well, I had to adjust the TransitionLimit but it works thanks!!! I have one more question. I have got the ship to shoot a bullet at a bug and draw a collision sprite when the bullet hits the bug and the collision sprite is erased. Then I shoot a bullet at an erased bug sprite, and it draws a collision sprite again which is what I don't what I want it to do.

here is my code

void renderScene()
{
	srand(time(NULL));
	glClear(GL_COLOR_BUFFER_BIT);
	drawBullet();
	coll_ship_one();
	go_space();
	if (coll == -1)
	{
		timer(0);
	}
	if (coll == 1 && coll_count == 1)
	{
		drawCollision(-10.0f, 90.0f, 9, 0);
		coll = 0;
	}
	if (coll == 0 || coll_count >= 2)
	{
		// no bug
	}
	ship();
	drawDice();
	glFinish();
}
Advertisement

here is more code sorry

void coll_ship_one()
{
	//draw bullet
	float x = -10.0f + move_x;
	float y = -90.0f + bullet;
	float oWidth = 5.0f;
	float oHeight = 5.0f;
	//draw bug
	float xTwo = -10.0f;
	float yTwo = 90.0f;
	float oTwoWidth = 10.0f;
	float oTwoHeight = 10.0f;

	if (checkCollide(x, y, oWidth, oHeight, xTwo, yTwo, oTwoWidth, oTwoHeight) == 1)
	{
		coll = 1;
		coll_count++;
//	cout << coll << " " << coll_count << endl;
		if (coll_count >= 2)
		{
			coll_count = 1;
		}
		glutPostRedisplay();
	}
}

pbivens67 said:

here is more code sorry

void coll_ship_one()
{
	//draw bullet
	float x = -10.0f + move_x;
	float y = -90.0f + bullet;
	float oWidth = 5.0f;
	float oHeight = 5.0f;
	//draw bug
	float xTwo = -10.0f;
	float yTwo = 90.0f;
	float oTwoWidth = 10.0f;
	float oTwoHeight = 10.0f;

	if (checkCollide(x, y, oWidth, oHeight, xTwo, yTwo, oTwoWidth, oTwoHeight) == 1)
	{
		coll = 1;
		coll_count++;
//	cout << coll << " " << coll_count << endl;
		if (coll_count >= 2)
		{
			coll_count = 1;
		}
		glutPostRedisplay();
	}
}

I would suggest putting the collusion code in separately from the renderScene code. I would be updating the scene with each tick of the timer. Updating the scene means updating the bugs locations, the player's locations, the bullet(s) locations, and information about current collusions. The renderScene() function should just be displaying that stuff.

I'd also keep information for each bug relating to its current position, and whether it is still alive. I would probably go with a list of bugs, and then just knock them off the list as they die, but if you are more comfortable with arrays/vectors those can work too. In any case, you shouldn't be testing for collusions with dead bugs. And if you weren't testing for collusions with dead bugs, that would help keep you from drawing collusion sprites from hitting those dead (erased) bugs.

This is more like how I'd set things up. . .

void renderScene()
{
	glClear(GL_COLOR_BUFFER_BIT);

	drawBullets(); // draw any bullets which are currently flying though the air
	drawBugs(); // draw all the bugs that are currenly alive
	drawCollusions(); // draw all the collusions that are currently happening
	drawPlayer();
	drawDice();
	
	glFinish();
}

And this is how I'd update the scene. . . I'd probably call this function (below) inside the timer function. . .

void updateScene()
{
	srand(time(NULL)); 
	
	moveBugs();
	movePlayer();
	moveBullets();
	
	testCollusions(); // here we test the various things which might collide in the game
}

How are things going with Spaceolopy?

Advertisement

well, I am still trying to get the collision sprite to only be drawn once when after the bullet is shot upwards it hits the bug and draws the collision sprite and then undraws the collision sprite then when it draws a second bullet it does not draw the collision sprite again.

void coll_ship_one()
{
	//draw bullet
	float x = -10.0f + move_x;
	float y = -90.0f + bullet;
	float oWidth = 5.0f;
	float oHeight = 5.0f;
	//draw bug
	float xTwo = -10.0f;
	float yTwo = 90.0f;
	float oTwoWidth = 10.0f;
	float oTwoHeight = 10.0f;

	if (checkCollide(x, y, oWidth, oHeight, xTwo, yTwo, oTwoWidth, oTwoHeight) == 1)
	{
		coll = 1;
		coll_count++;
//	cout << coll << " " << coll_count << endl;
		if (coll_count >= 1)
		{
			coll_count = 1;
		}
		glutPostRedisplay();
	}
}

void renderScene()
{
	srand(time(NULL));
	glClear(GL_COLOR_BUFFER_BIT);
	drawBullet();
	coll_ship_one();
	go_space();
	if (coll == -1)
	{
		timer(0);
	}
	if (coll == 1 && coll_count == 1)
	{
		drawCollision(-10.0f, 90.0f, 9, 0);
		coll = 0;
	}
	if (coll == 0 || coll_count >= 2)
	{
		// no bug
	}
	ship();
	drawDice();
	glFinish();
}
 

@pbivens67 : One of the things that is going on here is that inside of coll_ship_one() you are calling glutPostRedisplay() which triggers a scene render, however, inside of your RenderScene() function you call coll_ship_one(), so I'm not sure that is the best way to set up the code.

Anyway, to address your specific problem. . . which you stated previously as

Then I shoot a bullet at an erased bug sprite, and it draws a collision sprite again which is what I don't what I want it to do.

I'm guessing that your plan was to have the coll_count variable take care of that problem. So, after the first hit, collusion count gets bumped up above 1, and so you stop drawing the collision stuff, as seen in this code here :

	if (coll == 1 && coll_count == 1)
	{
		drawCollision(-10.0f, 90.0f, 9, 0);
		coll = 0;
	}

However, if that is your plan, then you shouldn't be resetting the coll_count back to 1, in your coll_ship_one() function, as seen here :

		if (coll_count >= 1)
		{
			coll_count = 1;
		}

Another way to handle the problem is to have the coll_ship_one() function not register a hit, if you're shooting at a dead bug. But I don't see how you take erased bugs into account in the code you're showing me. It seems like it is just hard coded to register collusions at -10.0, 90.0 (which is where I presume the target bug is supposed to be). So collusions always occur whether or not the bug has been erased.

If you were to track whether or not your target was still alive, and not test for collusions with a dead target, that should fix your problem.

Warp9 said:
If you were to track whether or not your target was still alive, and not test for collusions with a dead target, that should fix your problem.

can you elaborate on this?

@pbivens67 : yes, the simplest way would be to have a BugAlive variable. At the start, the bug will be alive, but once it gets hit it will not be alive. And then, if the bug is not alive, don't test for collusions, and don't draw collusions.

This topic is closed to new replies.

Advertisement