Mouse Click - Unhandle Exception Problem

Started by
6 comments, last by FXACE 13 years, 5 months ago
Currently I am creating an explosion on a plane and that is explode when the mouse click on the plane. But i fail to get the mouse click event. It will have unhandle exception break when i click on the plane. Below is some part of my coding. Hope can get some ideas or opinions from you guys. Thanks in advanced.

**break: Unhandled exception at 0x00ffd7dc in project2.exe: 0xC0000005: Access violation writing location 0x010516c0.

#define PLANESIZE 150float Plane[2][PLANESIZE][PLANESIZE];void OnKeyPress(unsigned char key,int,int) {	switch (key)	{	case 27:		exit (0);		break;	case 'a':		        //explosion happen at the middle of the plane if divide by 2		Plane[1][PLANESIZE/2][PLANESIZE/2] = 100;		break;	}	// ask glut to redraw the screen for us... 	glutPostRedisplay();}void OnMouse(int button, int state, int x, int y){	if ( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN ) {		if (x>100 && x<300 && y>110 && y<600)		{		Plane[1][x][y] = -10;		}	}}
Advertisement
It's not an OpenGL bug.
It fails because the 'x' and 'y' are out of range of 'Plane' array.
Make sure that 'x' < 'PLANESIZE' and 'y' < 'PLANESIZE' in "void OnMouse(int button, int state, int x, int y)". Or just set 'PLANESIZE' to your screen dimensions.
i set it to the code below, still have the same problem. May i know what is the problem? And how to solve it?

Between, if i set it to a point like Plane[1][PLANESIZE/2][PLANESIZE/2], the explosion happen in the middle of plane, it will not have any problem in the program, just the problem is Plane[1][x][y] = -10. How to set the explosion when i click in certain point?


void OnMouse(int button, int state, int x, int y)
{
if ( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN ) {

if (0<=x<(PLANESIZE-1) && 0<=y<(PLANESIZE-1))
{
Plane[1][x][y] = -10;
}
}
}
This code doesn't do what you think it does.
0 <= x < (PLANESIZE-1)

It compares two values, and then uses the result of that comparison (which is 1 or 0) against the third value.

The correct way to do this is:
(x >= 0) && (x < PLANESIZE)
Thanks for the opinion from you guys.. rip-off, I try your method, but it do not explore on the point I want. Anyway, thanks for your idea and suggestion. Thanks a lot.
If you are using 2D mode you can just set the projection like this:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,WindowWidth,WindowHeight,0,-1,1);
Also I recommend to use ^ this when you resing your window...

I don't know what and how you doing next... but...
To test mouse focus you can use this:
glPointSize(5.0f);//for example
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();
And make sure that your model view matrix is identity...
FXACE, I don't understand what u means. Sorry. Can you explain clearly? If using GL_TRIANGLES, not using GL_POINTS also can? And how to implement into my code?
SionFly, there is no difference between:

glPointSize(5.0f);
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();

and:

glBegin(GL_QUADS);
glVertex2f(x+5.0f,y+5.0f);
glVertex2f(x-5.0f,y+5.0f);
glVertex2f(x-5.0f,y-5.0f);
glVertex2f(x+5.0f,y-5.0f);
glEnd();

for projection:
glOrtho(0,WindowWidth,WindowHeight,0,-1,1);

And how to do with GL_TRIANGLES you must know that quadratic consists of 2 trangles. If you doesn't know how to work with primitives you can read OpenGL Red Book to learn it out...

Sorry, but it's hard to help you without knowing of what method u are using to creating explosions. I wrote a simple code that small quadratic (or point with dimensions that looks like small quadratic) how it follows your cursor. Maybe when you check it out you can understand how to solve your problem yourself....

This topic is closed to new replies.

Advertisement