This site requires JavaScript, please enable it in your browser!
Greenfoot back
Abwatts
Abwatts wrote ...

2019/3/2

Checking to see if circles are overlapping

Abwatts Abwatts

2019/3/2

#
Hey guys! I'm trying to create an Agar.io looking game for my computer science course. I come up with this code to see if my new created circle object will overlap with any other object, however, I still get many objects overlapping with one another. Since the getObjectsAt method can only get the object based on its X and Y location, this does not help me much. I thought about perhaps making a method in my Actor class (All this work is done in the child class of World), but I'm not exactly sure how I can go about doing this. This is my code:
private boolean placeRandomBall()
    {
        //Get a random location and size for the ball to be placed
        int randX = Greenfoot.getRandomNumber(getWidth()); 
        int randY = Greenfoot.getRandomNumber(getHeight()); 
        int randSize = Greenfoot.getRandomNumber(MAXSIZE);

        //Test to see if this location is already in use
        if (getObjectsAt(randX, randY, Ball.class).isEmpty()) {
            //Add a ball to the world
            Ball tempBall = new Ball(randSize);
            super.addObject (tempBall,randX,randY); //if size is too great
            tempBall.updateImage();
            //Successfully complete task so return true
            return true;
        }

        //Something went wrong, so return a false value
        return false;
    }
This what actually happens when I execute the program: Any help will be greatly appreciated!
danpost danpost

2019/3/2

#
Use the addedToWorld Actor class method to re-position the circles until none are in radial range.
Abwatts Abwatts

2019/3/3

#
Got it, thanks!
You need to login to post a reply.