Bullets Not Working

Started by
4 comments, last by Darobat 19 years, 4 months ago
Greetings, In the game I am working on, a top-down shooter, the bullets are currently not working. When you fire, they don't start in the right spot, nor do they travel at the propper angle. ?
void CBullet::Draw()
{
	glPushMatrix();
		DisableTextures();
		glColor3ub(255, 255 ,255);
		glTranslatef(x, y, 0.0f);
		glRotatef(angle, 0.0f, 0.0f, 1.0f);
		glBegin(GL_LINES);
			glVertex2i(x, y);
			glVertex2i(x + 3, y);
		glEnd();
		EnableTextures();
	glPopMatrix();
}
//...  some stuff that isn't pertanant.
void CEntity::Fire()
{
	CBullet *index = NULL;
	for(int i = 0; i < MAX_BULLETS; i++)
	{
		if(!bullets.inUse)
		{
			index = &bullets;
			break;
		}
	}
	// If all the bullets are in use, kill all of them.
	if(index == NULL)
	{
		for(i = 0; i < MAX_BULLETS; i++)
			bullets.KillBullet();
		index = &bullets[0];
	}

	index->angle = rot;
	index->inUse = true;
	index->x = x + 25;
	index->y = y + 13;
}
Any Ideas? I add 25 and 13 because x and y are the positions of the center of the player. Adding that moves it to the position of the gun. Any ideas why this doesn't work? Thanks.
--------------------C++ Home - Check it out!Lol... - Amazing video
Advertisement
The only thing I can think of off the top of my head is that you might be using radians instead of degrees for the glRotatef call. Apart from that nothing obvious jumps out.

More information might be handy. Do the bullets always start in the same spot, or just the same spot relative to the player? Likewise with the rotation.

[edit: typos]
The bullets are relative to the players position
	index->angle = rot;	index->inUse = true;	index->x = x + 25;	index->y = y + 13;

The function is a member of CEntity, so rot is the rotation of the player. The rotation for the player works fine.

If I do a messagebox that has index->angle,x,and y I get

Angle: 0 (correct)
X: 1079410688 (wrong)
Y: 0 (wrong)

Heres the code I use to update the bullets position
// Update the bullets positions	for(int i = 0; i < MAX_BULLETS; i++)	{		if(player.bullets.inUse)		{			player.bullets.y -= sin(DEG_TO_RAD(player.bullets.angle + 90)) * 25;			player.bullets.x -= cos(DEG_TO_RAD(player.bullets.angle + 90)) * 25;			if(player.bullets.x > 800 || player.bullets.x < 0 ||				player.bullets.y > 600 || player.bullets.y < 0)				player.bullets.KillBullet();		}	}


I get the same results if I use x, y and rot from the actual player, yet it draws correctly... :S
--------------------C++ Home - Check it out!Lol... - Amazing video
In your Draw() function, you are both translating by the bullet's position, and drawing at the bullet's position. I don't know what else is going on in your render function so I can't be sure about this, but you might try replacing

glVertex2i(x, y);
glVertex2i(x + 3, y);

with

glVertex2i(0, 0);
glVertex2i(3, 0);
In what order are you drawing everthing? Or more specifically, are there any glTranslate calls before the CBullet::Draw function is called? Because if so then the bullet will be drawn relative to the current glTranslate position rather than relative to the origin, and the same will be true for rotations. You can solve this by loading the identity matrix onto the stack after you call glPushMatrix.
lmao, thats the second time I've done that. It works now. Thanks. :D:D:D
--------------------C++ Home - Check it out!Lol... - Amazing video

This topic is closed to new replies.

Advertisement