# Bullets Not Working

## Recommended Posts

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[i].inUse)
{
index = &bullets[i];
break;
}
}
// If all the bullets are in use, kill all of them.
if(index == NULL)
{
for(i = 0; i < MAX_BULLETS; i++)
bullets[i].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.

##### Share on other sites
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]

##### Share on other sites
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[i].inUse)		{			player.bullets[i].y -= sin(DEG_TO_RAD(player.bullets[i].angle + 90)) * 25;			player.bullets[i].x -= cos(DEG_TO_RAD(player.bullets[i].angle + 90)) * 25;			if(player.bullets[i].x > 800 || player.bullets[i].x < 0 ||				player.bullets[i].y > 600 || player.bullets[i].y < 0)				player.bullets[i].KillBullet();		}	}

I get the same results if I use x, y and rot from the actual player, yet it draws correctly... :S

##### Share on other sites
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);

##### Share on other sites
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.

##### Share on other sites
lmao, thats the second time I've done that. It works now. Thanks. :D:D:D

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628334
• Total Posts
2982146

• 9
• 24
• 9
• 9
• 13