PDA

View Full Version : Mouse control on a plane



SionFly
10-14-2010, 06:33 AM
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 control. Below is some part of my coding, i do not know how to get the explosion when i click on the plane. Hope can get some ideas or opinions from you guys. Thanks.



#define PLANESIZE 150
float Plane[2][PLANESIZE][PLANESIZE];


void OnKeyPress(unsigned char key,int,int) {
switch (key)
{
case 27:
exit (0);
break;

case 'w':
//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();
}

SionFly
11-02-2010, 11:49 AM
Can anyone help me? If i do like this, sometimes it will break my program and that is not the position i want when i click.. Can anyone let me know the reason? Thanks..



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

if (x>100 &amp;&amp; x<300 &amp;&amp; y>110 &amp;&amp; y<600)
{
Plane[1][x][y] = -10;
}
}
}

blink
11-02-2010, 01:06 PM
Not sure if it is the problem but your plane is the size of [2][150][150] but your code ("x>100 &amp;&amp; x<300 &amp;&amp; y>110 &amp;&amp; y<600") is able to address far more than that range so it does and breaks it.
It breaks here at 110<y<600 and 100<x<300, you should change it to for example: 0<x<(PLANESIZE-1) and 0<y<(PLANESIZE-1)

blink
11-02-2010, 01:18 PM
Uh sorry i meant "0 <= x < PLANESIZE" and "0 <= y < PLANESIZE".

SionFly
11-02-2010, 02:11 PM
I tried it before but still have the same problem, my program still will break. And that is not the position i want. Anyway, thanks for blink.
Anyone have idea or suggestion?