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

  • Home
  • SEARCH
  • 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 7912901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:43:06+00:00 2026-06-03T13:43:06+00:00

I have a method that reflects balls against a wall, and also each other.

  • 0

I have a method that reflects balls against a wall, and also each other. The radius of each ball is 50.

public void reflectBalls(){
    for(int j = 0; j < 2; j++){
        if(ball[j].getY() <= NWall) ball[j].flipVertical();
        else if(ball[j].getY() >= (SWall - 50)) ball[j].flipVertical();
        else if(ball[j].getX() <= WWall) ball[j].flipHorizontal();
        else if(ball[j].getX() >= (EWall - 50)) ball[j].flipHorizontal();
    }
    for(int i = 0; i < 2; i++){
        for(int j = 0; j < 2; j++){
            if(i == j){
                continue;
            }else{
                double ax = (double)ball[i].getX();
                double ay = (double)ball[i].getY();
                double bx = (double)ball[j].getX();
                double by = (double)ball[j].getY();
                double distx = (ax-bx)*(ax-bx);
                double disty = (ay-by)*(ay-by);
                double distance = Math.sqrt(distx + disty);
                if(Math.floor(distance) <= 100){
                    ball[i].flipHorizontal();
                    ball[j].flipVertical();
                    ball[i].flipVertical();
                    ball[j].flipHorizontal();
                }
            }
        }
    }
}

The first part (reflection against walls) works perfectly fine. However, the balls do not collide; they just pass through each other. I’ve tried many different ways, but maybe there’s some sort of math I am missing. My algorithm works based on the Pythagorean theorem, where the difference between X coordinates and the difference between the Y coordinates are each squared, and their sum is the distance between the centers.

  • 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-03T13:43:07+00:00Added an answer on June 3, 2026 at 1:43 pm

    Here is your problem – you find that ball i collides with ball j and you flip it. Then you find that ball j collides with ball i and flip them again. As a result no collision happens. Only perform the collision check if j > i and all should be fine.

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

Sidebar

Related Questions

I have a method that prints private variable of the class. public class Test
I have method that returns Drawable , and if its Bitmap object is recycled
I have method that returned NSManagedObject and I don't know what kind of NSManagedObject
I have method that returns module path of given class name def findModulePath(path, className):
I have a method that uses ARC and takes an NSError pointer and I
I have a method that accepts a generic delegate as a parameter, and inserts
I have a method that calls another method (on ajax load, but that's irrelevant
I have a method that i have broken into smaller nested functions to break
I have a method that hash input string to generate MD5 pass from it
I have a method that I specify to be of a certain type. E.g.

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.