Skip to main content
GameDev.net gamedev.net
🔒 Locked

Mouse Click Behavior Changes Based On Computer

Started by voxelgenesis Mar 27, 2009 at 10:59 PM 3 replies 1.4k views
Original Post
voxelgenesis
voxelgenesis
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;
	}

}

sephirath
sephirath
1) this happens because of the diffrent rendering speed.
Make movement be affected by time, by change of time between the previous rendering.

to test if that the problem, try with:

a pc with 10fps,
a pc with 1000fps,

do you notice any diffrence ?

2) you are adding rand is an number between 0 and 1.
a) is there a rounding error that makes it zero ?
b) i think adding 0 - 1 is so small that is negligible so it doesnt affect your position.








voxelgenesis
voxelgenesis
Quote:
Original post by sephirath
1) this happens because of the diffrent rendering speed.
Make movement be affected by time, by change of time between the previous rendering.

to test if that the problem, try with:

a pc with 10fps,
a pc with 1000fps,

do you notice any diffrence ?

2) you are adding rand is an number between 0 and 1.
a) is there a rounding error that makes it zero ?
b) i think adding 0 - 1 is so small that is negligible so it doesnt affect your position.


Thank you for the response.

Currently, the movement is affected by time. The distance the projectile moves is based on the elapsed time from the last time it was rendered. I was thinking, because of the difference in processing power, if my laptop rendered at say X speed but my laptop could render at 10x speed, would that affect it since the time elapsed between the two would differ? But this is the first time I've noticed any difference between laptop and desktop when it comes to OpenGL.

The rand is working well in terms of no rounding error. I am actually adding between 1-10 (float) actually.

However, I have noticed something. Below are vectors for two shotgun blasts. The first one turned out clumped and the second was scattered. I have just noticed a huge disparity in the z-value (somehow I must have missed it earlier) and a smaller unnatural one in the x-value (the y-value seems fine).

3.15272 - 0.9 = -43.50173.55272 - 0.8 = -43.10173.05272 - 0 = -43.10173.25272 - 0.3 = -43.90173.05272 - 0.3 = -43.10172.65272 - 0.3 = -43.40172.65272 - 0.4 = -43.50172.75272 - 0.4 = -44.00173.05272 - 0.2 = -43.50172.75272 - 0.1 = -43.80173.05272 - 0.6 = -44.00172.95272 - 0.3 = -43.70172.65272 - 0.7 = -44.00172.85272 - 0.9 = -43.40173.15272 - 0.3 = -43.90173.45272 - 0.7 = -43.70173.15272 - 0 = -43.10173.25272 - 0.9 = -43.30173.15272 - 0.7 = -43.70172.75272 - 0.7 = -43.3017projectile added 00.289843 - 0.6 = -3.0490.489843 - 0.7 = -3.0490.289843 - 0.5 = -3.0490.889843 - 0.5 = -3.0490.689843 - 0.1 = -2.2490.689843 - 0.8 = -2.4490.589843 - 0 = -2.8490.389843 - 0.3 = -3.1490.289843 - 0.9 = -2.4490.489843 - 0.7 = -3.0490.889843 - 0.7 = -2.3490.589843 - 0.9 = -2.5490.789843 - 0.3 = -2.4491.08984 - 0.6 = -3.0490.589843 - 0.7 = -2.7491.08984 - 0.3 = -3.0490.489843 - 0.3 = -2.7490.289843 - 0.9 = -2.9490.989843 - 0.2 = -2.3490.789843 - 0.2 = -2.349projectile added 0


The code is exactly the same on my laptop and it does not produce these strange differences between shots.

Also, even if the rand stuff was not working properly, that would not explain why my slice slot also looked like a clumped ball on the first on my desktop PC. Here is that function:

void Scene::addSliceShot(float x, float y, float z){	float hunterPosition[3], tempDiff[3];	float projectileDestination[] = {x, y, z};	float temp[3] = {projectileDestination[0] - 0.2, projectileDestination[1] - 0.1, projectileDestination[2]};	camera.getPosition(hunterPosition);	for (int i = 0; i < 10; i++)	{		temp[0] = temp[0] + 0.02;		temp[1] = temp[1] + 0.01;		temp[2] = temp[2];				differenceVector(temp, hunterPosition, tempDiff);			Projectile projectile;		projectile.initialize();		projectile.setPosition(hunterPosition);		projectile.setVelocity(tempDiff);		projectile.setSpeed(9.0);		projectiles.push_back(projectile);	}}


These values may seem small, but they are showing up differently on different computers.

I am beginning to think the problem may be in how my getSceneCoords function works. Perhaps it has to do with different monitor sizes. I will try to hook my laptop up to my desktop monitor and see what happens. Still, I can understand why they would all be clumped, but not why one shot would be and the next would not. I am going to check into this more.
voxelgenesis
voxelgenesis
Ah hah! I have at least figured out why shots would differ from one shot to the next.

When nothing has been fired, it is picking up the location of whatever object is in the distance (a zombie, fence post, ground, sky, whatever). However, after I fire the first shotgun blast, those projectiles are out there. So, when I click on it again, it is finding the location of those projectiles.

However, I am not sure why the picking is varying between computers :( It may have something to do with the way the mouse touch pad views/translates its coordinates. In fact, I am almost entirely certain it is, because the laptop vectors are always between -1 and 1.

I really have no idea how to rectify this.

[Edited by - voxelgenesis on March 28, 2009 2:24:13 AM]
Cakey
Cakey
I think it's because of unproject cuz OpenGL is rendered differently based on different hard-ware. I think what you gotta do is do something with the Math's instead. However, I'm a nublet, and I'm just guessing.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.