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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:31:15+00:00 2026-05-29T22:31:15+00:00

Working on game. I have a movable basket and a – for now –

  • 0

Working on game. I have a movable basket and a – for now – stationairy apple. In the Update step of the game, I check for collisions between the two. In the future, I want to add multiple apples, so I’m looking for the best way to detect collision between the basket and apple x.

Got code:

 public void update() {
 int basketcenterX = basket.getX() + (basket.getWidth() / 2);
 int basketcenterY = basket.getY() + (basket.getHeight() / 2);

 if (basketcenterX < apple.getX() + apple.getWidth() && basketcenterX > apple.getX())
     if ( basketcenterY < apple.getY() + apple.getHeight() && basketcenterY > apple.getY())

 {

     Log.d(TAG, "Collision detected!!");

 }

 Log.d(TAG, "Apple Safe!");}

It seems pretty laggy in the emulator, I’m worried that it will be too slow when I add more apple-instances. Any suggestions on how to improve this and / or how to revamp this to check multiple apples?

  • 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-05-29T22:31:18+00:00Added an answer on May 29, 2026 at 10:31 pm

    There are 2 main improvements for your code.

    Access methods

    Don’t use getters and setters toO excessively. Those cannot be optimized in android in contrast to normal java applications. Just make public properties like

    public float positionX;
    

    and access them directly.

    Precalculate

    You don’t want to move and have the bitmaps left top. You want to have its position and its position is its center. So wrap your bitmaps into a custom class GameObject or similar and precalculate the center. You just modify the center.

    gameObjectPosition = basket.getX() + (basket.getWidth() / 2)
    

    Store width and height of your bitmap in public fields of your GameObject class and access them directly.

    /**
     A possible GameObject class.
    
    **/
    public class GameObject{
        private Bitmap bitmap;
    
        public float width, height;
        public float positionX;
        public float positionY;
    
        public GameObject(Bitmap bitmap){
                this.bitmap = bitmap;
                
                width = bitmap.getWidth(); 
                height = btimap.getHeight(); 
        }
    
        public void draw(...){
                bitmap.draw(positionX-width/2, positionY-height/2);
        }
    

    }

    If you are still concerned about performance, you also may precalculate the width/2 and height/2.

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

Sidebar

Related Questions

Hi! I have a working prototype of a game engine and right now I'm
I am working on game engine prototype and have the following question: Right now
I am working on a game that will have a two dimensional board of
I'm working With Game Center right now and I have a issue with GC.
I've been working with some game engines lately. They all have an Update() function
I'm working on a game (using Ruby) and planning to have it available in
I'm working on a game engine in C++ and I have methods like setColour
I'm working with xna in C# and in my game I will have a
I am working on Conway's Game of Life currently and have gotten stuck. My
I am working on a 2D scrolling game for iPhone. I have a large

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.