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 9164889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:47:22+00:00 2026-06-17T14:47:22+00:00

I m woring on an android opengl 1.1 2d game with a top view

  • 0

I m woring on an android opengl 1.1 2d game with a top view on a vehicule and a camera zoom relative to the vehicule speed. When the speed increases the camera zoom out to offer the player a best road visibility.

I have litte trouble finding the exact way to detect if a sprite is visible or not regarding his position and the current camera zoom.

Important precision, all of my game’s objects are on the same z coord. I use 3d just for camera effect. (that’s why I do not need frustrum complicated calculations)

here is a sample of my GLSurfaceView.Renderer class

public static float fov_degrees =  45f;
public static float fov_radians =  fov_degrees / 180 * (float) Math.PI;
public static float aspect; //1.15572 on my device
public static float camZ;  //927 on my device



@Override
public void onSurfaceChanged(GL10 gl, int x, int y) {

    aspect =  (float) x / (float) y;
    camZ = y / 2 / (float) Math.tan(fov_radians / 2);

    Camera.MINIDECAL = y / 4;   // minimum cam zoom out (192 on my device)

    if (x == 0) { // Prevent A Divide By Zero By
        x = 1; // Making Height Equal One
    }
    gl.glViewport(0, 0, x, y); // Reset The Current Viewport
    gl.glMatrixMode(GL10.GL_PROJECTION); // Select The Projection Matrix
    gl.glLoadIdentity(); // Reset The Projection Matrix

    // Calculate The Aspect Ratio Of The Window
    GLU.gluPerspective(gl, fov_degrees, aspect , camZ / 10, camZ * 10);

    GLU.gluLookAt(gl, 0, 0, camZ, 0, 0, 0, 0, 1, 0); // move camera back


    gl.glMatrixMode(GL10.GL_MODELVIEW); // Select The Modelview Matrix

    gl.glLoadIdentity(); // Reset The Modelview Matrix

when I draw any camera relative object I use this translation method :

gl.glTranslatef(position.x - camera.centerPosition.x , position.y -camera.centerPosition.y , - camera.zDecal);

Eveything is displayed fine, the problem comes from my physic thread when he checks if an object is visible or not:

public static boolean isElementVisible(Element element) {

    xDelta =  (float) ((camera.zDecal + GameRenderer.camZ) * GameRenderer.aspect *  Math.atan(GameRenderer.fov_radians));
    yDelta = (float) ((camera.zDecal + GameRenderer.camZ)*   Math.atan(GameRenderer.fov_radians));

    //(xDelta and yDelta are in reallity updated only ones a frame or when camera zoom change)

    Camera camera = ObjectRegistry.INSTANCE.camera;

    float xMin = camera.centerPosition.x - xDelta/2;
    float xMax = camera.centerPosition.x + xDelta/2;

    float yMin = camera.centerPosition.y - yDelta/2;
    float yMax = camera.centerPosition.y + yDelta/2;

    //xMin and yMin are supposed to be the lower bounds x and y of the visible plan
    // same for yMax and xMax

    // then i just check that my sprite is visible on this rectangle.


    Vector2 phD = element.getDimToTestIfVisibleOnScreen();
    int sizeXd2 = (int) phD.x / 2;
    int sizeYd2 = (int) phD.y / 2;


    return (element.position.x + sizeXd2 > xMin)
            && (element.position.x - sizeXd2 < xMax)
            && (element.position.y - sizeYd2 < yMax)
            && (element.position.y + sizeYd2 > yMin);


}

Unfortunately the object were disapearing too soon and appearing to late so i manuelly added some zoom out on the camera for test purpose.

I did some manual test and found that by adding approx 260 to the camera z index while calculating xDelta and yDelta it, was good.
So the line is now :

xDelta =  (float) ((camera.zDecal + GameRenderer.camZ + 260) * GameRenderer.aspect *  Math.atan(GameRenderer.fov_radians));
yDelta = (float) ((camera.zDecal + GameRenderer.camZ + 260)*   Math.atan(GameRenderer.fov_radians));

Because it’s a hack and the magic number may not work on every device I would like to understand what i missed. I guess there is something in that “260” magic number that comes from the fov or ration width/height and that could be set as a formula parameter for pixel perfect detection.

Any guess ?

  • 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-17T14:47:24+00:00Added an answer on June 17, 2026 at 2:47 pm

    My guess is that you should be using Math.tan(GameRenderer.fov_radians) instead of Math.atan(GameRenderer.fov_radians).

    Reasoning:

    If you used a camera with 90 degree fov, then xDelta and yDelta should be infinitely large, right? Since the camera would have to view the entire infinite plane.

    tan(pi/2) is infinite (and negative infinity). atan(pi/2) is merely 1.00388…

    tan(pi/4) is 1, compared to atan(pi/4) of 0.66577…

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

Sidebar

Related Questions

I'm working on an android game with OpenGL, and I have a game update
Ahoy, I'm working on an OpenGL ES based game for Android using the NDK.
I'm currently developing a game for Android using OpenGL ES 2.0. I have almost
I am working on a game in OpenGL using libGDX on Android. I currently
I'm working on an opengl game for Android. When user looses the game should
I'm working on a 2D game for android using OpenGL ES 1.1 and I
I'm writing completely native OpenGL ES 2.0 game engine for Android. Previously I developed
I am working on a board game for android using opengl es 1.0. The
I've been coding a simple android game with openGL ES 1.0 and require a
I am working in android. I designed a video player in my application. Most

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.