🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

using a vectror to shoot a bullet

Started by
1 comment, last by pbivens67 4 years, 2 months ago

I am able to draw a bullet at the front of the cannon of my tank sprite. I want the bullet to shoot at the enemy tank in a parabolic arc. I have drawn two tanks that move horizontally along a grassy ground sprite. the tanks are also sprites.

int i = 0;

int animate;

void vec_shoot()
{
	vector<float> move_sprite;

	for (float x = -14.0f; x <= 14.0f; x += 2.0f)
	{
		y = (-0.1f*(x*x)) + 20.0f;
		move_sprite.push_back(y);
	}

	y = move_sprite[i];
	x += 2.0f;
	if (x >= 30.0f)
	{
		x = 0.0f;
	}
	i++;
	if (i >= 15)
	{
		i = 0;
		animate = 0;
		glutIdleFunc(NULL);
	}
	glutPostRedisplay();
}
void handleKeypress(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 27:
		exit(0);
		break;
	case 32:
		drawFireSprite();
		drawBulletSprite();
		animate = !animate;
		if (animate)
		{
			glutIdleFunc(vec_shoot);
		}
		else
		{
			glutIdleFunc(NULL);
		}
		glutSwapBuffers();
		break;
Advertisement

I am still working on my tank shooting bullets. I am able to get the bullets to shoot multiple times using a timing loop. I want it to shoot only once when I hit the space bar.

void renderScene(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glPushMatrix();
	drawSprite();
	drawEnemySprite();
	drawBkgnd();
	drawBkgndone();
	drawBkgndtwo();
	drawBulletSprite();
	glPopMatrix();
	glutSwapBuffers();
}

int i = 0;

void vec_shoot()
{
	vector<float> move_sprite;

	for (float x = -14.0f; x <= 14.0f; x += 2.0f)
	{
		y = (-0.1f*(x*x)) + 20.0f;
		move_sprite.push_back(y);
	}

	y = move_sprite[i];
	x += 2.0f;
	if (x >= 30.0f)
	{
		x = 0.0f;
	}
	i++;
	if (i >= 15)
	{
		i = 0;
	}
	glutPostRedisplay();
}

void shoot(int val)
{
	vec_shoot();
	glutPostRedisplay();
	glutTimerFunc(50, shoot, 0);
}

void handleKeypress(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 27:
		exit(0);
		break;
	case 32:
		drawFireSprite();
		drawBulletSprite();
		shoot(0);
		glutSwapBuffers();
		break;

This topic is closed to new replies.

Advertisement