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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:36:17+00:00 2026-06-14T00:36:17+00:00

To optimize my game I made it grid based. Objects lye in a grid

  • 0

To optimize my game I made it grid based. Objects lye in a grid square. Thus to do rendering I gather all objects in the regions visible to the camera and put them in a list by layer:

public ArrayList<Entity> queryRect(OBB2D rect)
{
    neighbourQueryObjs.clear();
    ArrayList<Region> reg = determineNeighbourRegions(rect);
    for(int i = 0; i < Entity.MAX_LAYERS; ++i)
    {
        for(Region r : reg)
        {
            for(Entity e : r.getStatic(i))
            {
                if(!neighbourQueryObjs.contains(e) && e.getRect().overlaps(rect))
                {
                    neighbourQueryObjs.add(e);
                }
            }

            for(Entity e : r.getDynamic(i))
            {
                if(!neighbourQueryObjs.contains(e) && e.getRect().overlaps(rect))
                {
                    neighbourQueryObjs.add(e);
                }
            }
        }
    }

    return neighbourQueryObjs;

}

This works great. The game is very fast. I call this evey frame
The problem is that every 1 or 2 seconds or so, the game just freezes for about 2-300 ms.

I suspect it is garbage collection. Could that be it?

Is there a way I could preallocate this stuff so it never has to use GC? I dont mind wasting memory, memory is no problem.

Here is my game loop, could that be it:

public void run() {
        Canvas canvas;

        long beginTime;     // the time when the cycle begun
        long timeDiff;      // the time it took for the cycle to execute
        int sleepTime;      // ms to sleep (<0 if we're behind)
        int framesSkipped;  // number of frames being skipped 

        sleepTime = 0;

        while (running) 
        {
            canvas = null;
            // try locking the canvas for exclusive pixel editing
            // in the surface
            try 
            {

                // we have to make sure that the surface has been created
                // if not we wait until it gets created
                if (!holder.getSurface ().isValid())
                    continue;
                canvas = this.holder.lockCanvas();
            synchronized (holder) 
            {
                    beginTime = System.currentTimeMillis();
                    framesSkipped = 0;  // resetting the frames skipped
                    // update game state
                    update();
                    // render state to the screen
                    // draws the canvas on the panel
                    display(canvas);
                    // calculate how long did the cycle take
                    timeDiff = System.currentTimeMillis() - beginTime;
                    // calculate sleep time
                    sleepTime = (int)(FRAME_PERIOD - timeDiff);

                    if (sleepTime > 0) 
                    {
                        // if sleepTime > 0 we're OK
                        try 
                        {
                            // send the thread to sleep for a short period
                            // very useful for battery saving
                            Thread.sleep(sleepTime);
                        } catch (InterruptedException e) {}
                    }

                    while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) 
                    {
                        // we need to catch up
                        // update without rendering
                        update();
                        // add frame period to check if in next frame
                        sleepTime += FRAME_PERIOD;
                        framesSkipped++;
                    }
                }
            } 
            finally 
            {
                // in case of an exception the surface is not left in
                // an inconsistent state
                if (canvas != null) 
                {
                    holder.unlockCanvasAndPost(canvas);
                }
            }   // end finally
        }
    }
    protected void onDrawTransformed(GraphicsContext g)
    {   
        OBB2D view = g.getCamera().getCamRect(getWidth(), getHeight());

        ArrayList<Entity> ents = world.queryRect(view);
        for(Entity e : ents)
        {
            e.draw(g);
        }
        //city.draw(g);
        //vehicle.draw(g);
    }

What could I do to stop the stutter?

Thanks

  • 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-14T00:36:18+00:00Added an answer on June 14, 2026 at 12:36 am

    in your public ArrayList<Entity> queryRect(OBB2D rect) you’re constantly calling neighbourQueryObjs.clear(); and neighbourQueryObjs.add(e);, which allocates and deallocates memory.

    to find out if this really is the problem, you may temporary comment out clear()/add() parts and return always the same (precomputed?) neighbourQueryObjs and see if the stutter is gone. something along the lines:

    if( neighbourQueryObjs.length() == 0 ) {
        // calculate the neighbourQueryObjs
    }
    return neighbourQueryObjs;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a game and I'm trying to optimize things. I made a
I'm programming a big game in Java and I'm trying to optimize the code
I am developing a web-based Pokemon Online game. Since it is online, I would
I am in a situation where I have game objects that have a virtual
I am programming an AI for a chess-like game, based on two types of
I am looking to create an android based game. I am planning for this
In coding a physics-based game, I have created everything from subclassed NSObjects. Force vectors
We have several game servers that produces events based on player actions. We want
Im almost finish with my game made with Adobe CS5.5 AIR 3.0 for iOS.
I'm trying to optimize my game using CCSpriteBatchNode to render multiple sprites at once.

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.