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

2017/10/4

How to get intersecting objects to bounce off of each other?

falafel falafel

2017/10/4

#
I have many circles in my code that follow my mouse. Each circle is created when a mouse is clicked. The problem is that the circles keep ending up in the same place but I want them to bounce off of each other. At the moment, my code for that looks something like this:
public void bounce(){
        
        java.util.List<Circle> circleList = getWorld().getObjects(Circle.class);
        
        if (//something to do with isTouching?){
        }
    }
        }
Any advice on how to make this (even conceptually) work? Thanks!
falafel falafel

2017/10/4

#
Basically how do I say, "If two circles are intersecting"? :)
Super_Hippo Super_Hippo

2017/10/4

#
Two circles are touching if the distance between them is less than the sum of the radiuses of both circles. You can use isTouching instead of getting all circles in the world, because isTouching (using the rectangular image) will be true if the real circles are intersecting.
falafel falafel

2017/10/4

#
How do I use isTouching? I tried writing
        if (Circle.isTouching(Circle.class)){
but it keeps giving me the error message "non-static method isTouching(java.lang.Class<?>) cannot be referenced from a static context. The weird part is that I don't have any static methods so I don't know why it's showing me this...
Super_Hippo Super_Hippo

2017/10/4

#
You are trying to call it on the Circle class. You need to call it on a circle object because the isTouching method is not static. If the code is located in the Circle class, you can just remove the 'Circle.' at the beginning.
falafel falafel

2017/10/8

#
Thank you!
You need to login to post a reply.