Sign in to follow this  
bako

OpenGL Some question: moving object, draw a ball, bouncing ball

Recommended Posts

Hi all. i've some trouble in opengl. 1) how i can automaticaly move a object? i.e. i would that my ball is moving into my window?. if i modify the value of the matrix using translatematix i didn't see anything because the display is done too fast. there is a method to move the object? 2) how i can draw a ball? i've seen the gluSphere, there's some problem using this function? i try this code, but dosen't work.
    private void drawBall(GL gl) {
        ArrayList etas = new ArrayList();
        ArrayList fis = new ArrayList();
        int mslice = 10;
        int nslice = 50;
        double eta;
        double fi;
        ArrayList<Vertex3d> vertex = new ArrayList<Vertex3d>();
        for (int i = 0; i < nslice - 1; i++) {
            eta = ((2 * Math.PI) / nslice) * i;
            etas.add(eta);
        }
        for (int j = 0; j < mslice; j++) {
            fi = -1 * (Math.PI / 2) + (Math.PI / mslice) * j;
            fis.add(fi);
        }
        for (int j = 0; j < fis.size(); j++) {
            for (int i = 0; i < etas.size(); i++) {
                eta = Double.valueOf("" + etas.get(i)).doubleValue();
                fi = Double.valueOf("" + fis.get(j)).doubleValue();
                Vertex3d v = new Vertex3d(Math.cos(eta) * Math.sin(fi), Math.sin(eta) * Math.sin(fi), Math.cos(fi));
                vertex.add(v);
            }
        }
        gl.glColor3f(1.0f, 0.0f, 0.0f);
        gl.glBegin(GL.GL_TRIANGLES);
        for (int i = 0; i < nslice; i++) {
            int j = 0;
            gl.glVertex3d(vertex.get(j).x, vertex.get(j).y, vertex.get(j).z);
            gl.glVertex3d(vertex.get(j + i + 1).x, vertex.get(j + i + 1).y, vertex.get(j + i + 1).z);
            gl.glVertex3d(vertex.get(j + i + 1 + 1).x, vertex.get(j + i + 1 + 1).y, vertex.get(j + i + 1 + 1).z);
            gl.glVertex3d(vertex.get(j).x, vertex.get(j).y, vertex.get(j).z);
            gl.glVertex3d(vertex.get(j + 1 + (nslice - 1)).x, vertex.get(j + 1 + (nslice - 1)).y, vertex.get(j + 1 + (nslice - 1)).z);
            gl.glVertex3d(vertex.get(j + 1).x, vertex.get(j + 1).y, vertex.get(j + 1).z);

        }
        gl.glEnd();
        gl.glColor3f(0.0f, 1.0f, 0.0f);
        gl.glBegin(GL.GL_QUADS);
        for (int i = 0; i < nslice - 1; i++) {
            for (int j = 1; j < mslice; j++) {
                gl.glVertex3d(vertex.get(j + i).x, vertex.get(j + i).y, vertex.get(j + i).z);
                gl.glVertex3d(vertex.get(j + i + 1).x, vertex.get(j + i + 1).y, vertex.get(j + i + 1).z);
                gl.glVertex3d(vertex.get(j + nslice + i + 1).x, vertex.get(j + nslice + i + 1).y, vertex.get(j + nslice + i + 1).z);
                gl.glVertex3d(vertex.get(j + nslice + i).x, vertex.get(j + nslice + i).y, vertex.get(j + nslice + i).z);
                gl.glVertex3d(vertex.get(j + (nslice - 1)).x, vertex.get(j + (nslice - 1)).y, vertex.get(j + (nslice - 1)).z);
                gl.glVertex3d(vertex.get(j + 0).x, vertex.get(j + 0).y, vertex.get(j + 0).z);
                gl.glVertex3d(vertex.get(j + nslice).x, vertex.get(j + nslice).y, vertex.get(j + nslice).z);
                gl.glVertex3d(vertex.get(j + nslice + (nslice - 1)).x, vertex.get(j + nslice + (nslice - 1)).y, vertex.get(j + nslice + (nslice - 1)).z);

            }
        }
        gl.glEnd();
    }
3) someone of you have code about bouncing ball (java preferd). thanks.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this