Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8651435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:04:16+00:00 2026-06-12T14:04:16+00:00

I am new to open GL, I just want to move a circle randomly

  • 0

I am new to open GL, I just want to move a circle randomly in the screen , when the user touches the circle , I need to know the hit and miss.
this is what I’m able to achieve through the online classes.

render class:


    public class HelloOpenGLES10Renderer implements Renderer {
    private int points = 250;
    private float vertices[] = { 0.0f, 0.0f, 0.0f };
    private FloatBuffer vertBuff;
    public float x = 0.0f, y = 0.0f;
    public boolean color = false;
    boolean first = true;

    @Override
    public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glTranslatef(x, y, 0);
        gl.glColor4f(1.0f, 1.0f, 1.0f, 0f);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuff);
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, points);

    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        Log.i("TAG", "change" + width + ":" + height);
        gl.glViewport(0, 0, width, height);
        float ratio = (float) width / height;
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glFrustumf(-ratio, ratio, -1, 1, 3, 7);
        GLU.gluLookAt(gl, 0f, 0f, -5f, 0.0f, 0.0f, 0f, 0.0f, 1.0f, 0.0f);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {

        Log.i("TAG", "CREATE");
        gl.glClearColor(0.1f, 0.1f, 0.0f, 1.0f);
        initShapes();

    }

    private void initShapes() {

        vertices = new float[(points + 1) * 3];
        for (int i = 3; i < (points + 1) * 3; i += 3) {
            double rad = (i * 360 / points * 3) * (3.14 / 180);
            vertices[i] = (float) Math.cos(rad) * 0.10f;
            vertices[i + 1] = (float) Math.sin(rad) * 0.10f;
            vertices[i + 2] = 0;
        }
        ByteBuffer bBuff = ByteBuffer.allocateDirect(vertices.length * 4);
        bBuff.order(ByteOrder.nativeOrder());
        vertBuff = bBuff.asFloatBuffer();
        vertBuff.put(vertices);
        vertBuff.position(0);

    }

}

Activity:
    package com.example.opengltrail;

    import java.util.Random;

    import android.app.Activity;
   import android.opengl.GLSurfaceView;
    import android.os.Bundle;
    import android.util.FloatMath;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;

    public class MainActivity extends Activity implements OnTouchListener {

    private GLSurfaceView mGLView;
    float oldx = 0, oldy = 0;
    private HelloOpenGLES10Renderer m;
    ProgressDialogThread p;
    ProgressDialogThread123 t;
    boolean stop = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mGLView = new GLSurfaceView(this);

        m = new HelloOpenGLES10Renderer();

        mGLView.setRenderer(m);
        mGLView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
        setContentView(mGLView);

        mGLView.setOnTouchListener(this);
        p = new ProgressDialogThread();

    }

    @Override
    protected void onPause() {
        super.onPause();
        mGLView.onPause();
        p.stop();
        stop = false;

    }

    @Override
    protected void onResume() {
        super.onResume();
        mGLView.onResume();
        t = new ProgressDialogThread123();

        t.start();
        p.start();

    }

    /**
     * gets u the random number btw 0.0 to 1.0
     */
    public float getmearandom() {
        Random rng = new Random();
        float next = rng.nextFloat();
        Integer i = rng.nextInt();
        if (i % 2 == 0)
            next = next * (-1);
        return next;
    }

    class ProgressDialogThread123 extends Thread {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            super.run();

            for (; stop;) {
                p.run();
            }

        }
    }

    class ProgressDialogThread extends Thread {
        @Override
        public void run() {
            super.run();
            if (stop) {

                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                m.x = getmearandom();
                m.y = getmearandom();
                Log.i("m.x=" + m.x + ":", "m.y=" + m.y);
                mGLView.requestRender();
            }

        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        float x = event.getX();
        float y = event.getY();
        float x1 = ((x * 2) / 480);
        float y1 = ((y * 2) / 724);
        x1 = -1 + x1;
        y1 = -1 + y1;
        if (y < 362) {
            if (y1 > 0)
                y1 = -y1;
        } else if (y > 362) {
            if (y1 < 0)
                y1 = -y1;
        } else {
            y1 = 0;
        }

        if (x > 240) {
            if (x1 < 0)
                x1 = -x1;

        } else if (x < 240) {
            if (x1 > 0)
                x1 = -x1;
        } else {
            x1 = 0;
        }

        Log.i("x1=" + x1, "y1=" + y1);
        float d = (((m.x - x1) * (m.x - x1)) + ((m.y - y1) * (m.y - y1)));

        float dd = FloatMath.sqrt(d);
        if (dd <= 0.10f) {
            m.color = true;
            // mGLView.requestRender();
            Log.i("Tag", "Circle");
        }
        // m.x += 0.10f;

        return true;
    }
}

Please anyone help me !! thanks in advance
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:04:17+00:00Added an answer on June 12, 2026 at 2:04 pm

    If you are only drawing in 2d I suggest you loose “frustum” and “lookAt” and replace them with “ortho” with coordinates: ortho(viewOrigin.x, viewOrigin.x + viewSize.width, viewOrigin.y + viewSize.height, viewOrigin.y, -1.0f, 1.0f). This will make your GL coordinate system same as your view coordinates. As for circle vertices rather create them with radius of 1.0f. Now to draw the circle, before the draw call you just have to “push” matrix, translate to screen coordinates (X, Y, .0f), scale to radius R in pixels (R, R, 1.0f) and “pop” the matrix after the call.. Now to check on touch if the circle was hit:

    `bool didHit = ((touch.x-cicrcleCenter.x)*(touch.x-cicrcleCenter.x) +   
                   (touch.y-cicrcleCenter.y)*(touch.y-cicrcleCenter.y))  
                   <  R*R`;
    

    Note that “viewOrigin” in “ortho” depends on where you are catching touches: If you are catching them in the same view it will probably be at (0,0) no matter where the view is. Those coordinates should correspond to coordinates you receive in your touch event when pressing at upper left and bottom right corners of your view.

    If you really need to draw in 3d and you will have to use frustum, you will need to get inverse of your projection matrix to create 3d rays from your touches and then check for hits the same way you would for bullets if you like.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Rookie question, I think. I'm just trying to replicate this: http://rpubs.com/gallery/googleVis Open a new
I just want to ask what is the best way to open a new
I just want to be completely clear. I am still new to Open GL
How can I easily open a new browser with just some help text in
I tried this: QPushButton* openHostsPushButton = new QPushButton(Open Hosts); Button_SetElevationRequiredState(openHostsPushButton->winId(), true); openHostsPushButton->setMaximumSize(aPushButtonMaxSize); connect(openHostsPushButton, SIGNAL(clicked()),
I just want to create a dropdown list whenever I choose a new value
I think I just open a new question and also could help who are
Just want to make sure this is the best way to call a connection,
I do not know what to do... I just want to close all opened
Hello am new with mongoengine. I just want to retrieving an image from mongoengine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.