Original Post
Hello, I am making a simple FPS game for one of my classes. In this game, I am using the left mouse button to fire bullets. This involves using the mouse click coordinates and translating to world coordinates, then finding the difference vector and sending a projectile along this vector at a velocity. I have three modes. One is a single bullet, one is a shotgun blast, and the other a slice shot (10 bullets fanned out in a row). I do a simple if/else chain to determine what function to call depending on which mode is selected (which is selected by pressing 1, 2, or 3). On my laptop, this works great. When I hit '2' for shotgun blast, it delivers a nice wide spray of bullets exactly as expected. However, on my desktop, with the code exactly the same, it clumps the bullets so tightly together that they can barely be differentiated. Even when I add a greater range of randomness to the vector differences, they still stay very clumped (while on my laptop they become super spread out). Oh, forgot to mention what's really weird. On my desktop, when I click once, I get the clumped behavior. But if I double click, I first get a clumped shot, then the expected spread shot. I do not understand why it would do that. Does anyone know why this might be? I have included what I think are the relevant functions.
void mouseButton(int btn, int state, int x, int y)
{
float scenePosition[3];
switch (btn)
{
case (0):
if (active == false)
{
scene1.getSceneCoords(winSizeX/2.0, winSizeY/2.0, scenePosition);
if (scene1.getGunMode() == 1)
scene1.addProjectile(scenePosition[0], scenePosition[1], scenePosition[2]);
if (scene1.getGunMode() == 2)
scene1.addShotgunBlast(scenePosition[0], scenePosition[1], scenePosition[2]);
else if (scene1.getGunMode() == 3)
scene1.addSliceShot(scenePosition[0], scenePosition[1], scenePosition[2]);
cout << "added projectile" << endl;
active = true;
}
break;
case (1):
break;
case (2):
break;
default:
break;
}
cout << " at (" << x << ", " << y << ")" << endl;
} // end mouseButton
void Scene::getSceneCoords(int x, int y, float *coords)
{
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX, winY, winZ;
GLdouble posX, posY, posZ;
glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );
winX = (float)x;
winY = (float)viewport[3] - (float)y;
glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
coords[0] = posX;
coords[1] = posY;
coords[2] = posZ;
}
void Scene::addShotgunBlast(float x, float y, float z)
{
float hunterPosition[3], temp[3], tempDiff[3];
float projectileDestination[] = {x, y, z};
camera.getPosition(hunterPosition);
for (int i = 0; i < 20; i++)
{
temp[0] = projectileDestination[0] + (rand() % 10)/10.0;
temp[1] = projectileDestination[1] + (rand() % 10)/10.0;
temp[2] = projectileDestination[2] + (rand() % 10)/10.0;
differenceVector(temp, hunterPosition, tempDiff);
Projectile projectile;
projectile.initialize();
projectile.setPosition(hunterPosition);
projectile.setVelocity(tempDiff);
projectile.setSpeed(9.0);
projectiles.push_back(projectile);
cout << tempDiff[0] << " - " << tempDiff[1] << " = " << tempDiff[2] << endl;
}
}