small drawing problem

Started by
4 comments, last by Thaumaturge 16 years, 10 months ago
Hi new opengl coder looking for some help. Basically ive drawn a rotatable box (gonna be a tank some day :p) and a circle around it to show its sort of collision detection. When I move it from the centre of the screen the circle becomes offset from the box as in the following screenies: piccy1 piccy2 piccy3 code:

                Gl.glPushMatrix();

                Gl.glTranslatef(x, y, -1.01f);
                Gl.glScalef(0.02f, 0.02f, 0.02f);
                Gl.glRotatef(facing, 0, 0, 1);

                Gl.glBegin(Gl.GL_QUADS);
                Gl.glColor3f(1, 1, 1);
                Gl.glVertex3f(-1.0f, 1.0f, 0.0f);
                Gl.glVertex3f(1.0f, 1.0f, 0.0f);
                Gl.glVertex3f(1.0f, -1.0f, 0.0f);
                Gl.glVertex3f(-1.0f, -1.0f, 0.0f);
                Gl.glEnd();

                Gl.glPopMatrix();
                
                Gl.glPushMatrix();
                
                Gl.glTranslatef(x, y, -1.0f);
                Gl.glScalef(0.02f, 0.02f, 0.02f);
               
                float vectorX; float vectorY;
                
                Gl.glBegin(Gl.GL_LINES);
                Gl.glColor3f(1, 1, 1);
                for (float angle = 0.0f; angle <= (2.0f * Math.PI); angle += 0.01f)
                {
                    vectorX = (radius * (float)Math.Sin((double)angle));
                    vectorY = (radius * (float)Math.Cos((double)angle));
                    Gl.glVertex3f(vectorX, vectorY, 0.0f);
                    
                }
                Gl.glEnd();

               
                Gl.glPopMatrix();



I really cant see the cause. Seems like one object is behind the other? Any help is most welcome. :) [Edited by - tim-the-inventor on June 8, 2007 4:34:53 PM]
Advertisement
I think that the problem may be that you seem to be generating your circle vertices around an offset origin when you have already had OpenGL offset the geometry. Perhaps try removing the "origin(X/Y) + " from your vectorX and -Y calculations.

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

Thanks for quick reply, ive just realised how stupid that code was :p. Its updated and the offset is alot less but its still there.
My pleasure - I'm glad that I helped. ^_^

As to your remaining offset, it occurs to me to ask: are you using a perspective or an orthographic projection? If the former, it could simply be that perspective projection is causing an apparent offset as a result of one element being placed slightly closer to the camera than the other (Gl.glTranslatef(x, y, -1.01f) as opposed to Gl.glTranslatef(x, y, -1.0f) (emphasis mine)).

If you are using a perspective projection, perhaps try placing both at the same distance from the camera.

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

Thanks its fixed now. I made some small change to test and forgot to revert :]
My pleasure - I'm glad that you got it working. ^_^

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

This topic is closed to new replies.

Advertisement