View Full Version : Convert mouse point to box2d point in opengl

09-16-2009, 04:23 AM
I want to convert mouse point on window to a point in box2D engine and I don't have those magic numbers to convert x and y ;) regardless of screen resolution.
I'm using windows calls to get mouse input and opengl to draw stuff.

So box2d has this function ConvertScreenToWorld but i dont want to use GLUT to get those magic numbers...

For me the closest aproximation looks like this

b2Vec2 ConvertScreenToWorld(int32 x, int32 y)
float32 u = -x / 10.0;
float32 v = y / 10.0;
u+= GLwidth /20;
v-= GLheight/20;
u *= cameradistance/72.5;//for default opengl frustrum
v *= cameradistance/72.5;
b2Vec2 p(u,v);
return p;

Its for default settings of openGL and 800x600 res.

Does OpenGL have something to help me with this or do i need to guess numebers for every resolution to transform point from screen?
Can glFrustum or projection matrix be helpfull?

plz don't send to box2d forum ;)

09-16-2009, 12:25 PM
I think gluUnProject will do what you need.