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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:13:18+00:00 2026-06-12T00:13:18+00:00

I’ve just started with Android programming using eclipse and recently came across this problem.

  • 0

I’ve just started with Android programming using eclipse and recently came across this problem. I have a bunch of sprites assigned to an arraylist. Now, I want to make it so that collision is detected automatically between sprites but the template I’m currently using can only detect collision between the surface’s borders and the moving sprites. Each sprite’s position and speed is generated randomly.
How can I change the update() function in my Sprite() class to detect collision between the moving sprites themselves and at the same changing/bouncing to the opposite direction?
Here’s my Sprite class template:

package com.gameproject.cai_test;

import java.util.Random;


   public Sprite(GameView gameView, Bitmap bmp) {
         this.width = bmp.getWidth() / BMP_COLUMNS;
         this.height = bmp.getHeight() / BMP_ROWS;
         this.gameView = gameView;
         this.bmp = bmp;


         Random rnd = new Random();
         x = rnd.nextInt(gameView.getWidth() - width);
         y = rnd.nextInt(gameView.getHeight() - height);
         xSpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED;
         ySpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED;
   }

private void update() {
         if (x >= gameView.getWidth() - width - xSpeed || x + xSpeed <= 0) {
                xSpeed = -xSpeed;
         }
         x = x + xSpeed;
         if (y >= gameView.getHeight() - height - ySpeed || y + ySpeed <= 0) {
                ySpeed = -ySpeed;
         }
         y = y + ySpeed;

         currentFrame = ++currentFrame % BMP_COLUMNS;
   }

   public void onDraw(Canvas canvas) {
         update();
         int srcX = currentFrame * width;
         int srcY = getAnimationRow() * height;
         Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
         Rect dst = new Rect(x, y, x + width, y + height);

         canvas.drawBitmap(bmp, src, dst, null);
   }

   private int getAnimationRow() {
         double dirDouble = (Math.atan2(xSpeed, ySpeed) / (Math.PI / 2) + 2);
         int direction = (int) Math.round(dirDouble) % BMP_ROWS;
         return DIRECTION_TO_ANIMATION_MAP[direction];
   }


   //gameplay operations

   //only values from 0 to 9 will be picked; each assigned its own sprite in the list
   public int randomValue(){
       Random rnd = new Random();
       RandomValue = rnd.nextInt(10);
       return RandomValue; 
   }

   //sequence operation from addition, subtraction, multiplication, and division
   public int produceSum(){
       int addOne = 0;
       int addTwo = 0;
       Sum = addOne + addTwo;
       return Sum;
   }

   public int produceDiff(){
       int deductOne = 0;
       int deductTwo = 0;
       Difference = deductOne - deductTwo;
       return Difference;
   }

   public int produceProduct(){
       int multiOne = 0;
       int multiTwo = 0;
       Product = multiOne * multiTwo;
       return Product;
   }

   public int produceQuotient(){
       int divideOne = 0;
       int divideTwo = 0;
       Quotient = divideOne / divideTwo;
       return Quotient;
   }

   //each time this returns true, the game is reset with new operation
   //compares the value of the bubble picked to the random number being compared through operations
   public boolean compareBubbleValue(int randomBubble, int bubbleValue){

       if (randomBubble == bubbleValue){
           return true;
       }
       return false;
   }

}

As you can see, the update() method only checks the collision between the moving sprites and the borders.

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

    Okey, so lets name your array of sprites spriteArray and loop through it twice

     public Rect getBounds(){ //put this in your sprite and enemy-class.
       return new Rect(x, y, x+width, y+height);
     }
    
     Public void checkCollision(){
        for (int i = 0; i<spriteArray.size(); i++){
               Rect mySprite = spriteArray.get(i).getBounds(); //create rect for every sprite in array
                 for (int j = 0; j<spriteArray.size(); i++){
                    Rect myOtherSprite = spriteArray.get(i).getBounds();
                      if(mySprite.intersect(myOtherSprite)){ //check if they touch
                       //Todo code here
                 }
              }
          }
      }
    

    then you just put this method in your update-method.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.