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

2016/7/15

Cannot find a simple collision method.

elektrikpulse61 elektrikpulse61

2016/7/15

#
Hi fellow programmers! I have a seemingly simple question that I can't find an answer to. I am working on a simple game in Greenfoot, and I'm looking for a method that can detect when an Actor collides with any other Actor. From the little research I've done into this subject, it has occurred to me that all of the pre-provided methods in the Greenfoot package all require a specific class to be passed as an argument for the collision. However, for what I am doing, I need a general collision detector that will be able to know when a specific Actor collides with ANY other Actors in the World. I've done some basic searching through other forums, but to no avail. I can't seem to find a solution to this problem I have. If there is no Greenfoot-supplied method, would it instead be easier to just have a series of specific "Actor actor = getIntersectingObjects(whatever.class)" statements for each of the other Actors that I have created? Or would it be more efficient (though perhaps more difficult) to write my own collision detection method that will not require a specific class to be passed as an argument? Thanks for all of your help in advance.
danpost danpost

2016/7/15

#
I believe that in the Actor class API documentation, all collision type methods that return a list say the following about the Class parameter:
cls - Class of objects to look for (passing 'null' will find all objects).
The part I bolded should be of interest. BTW, you could also pass 'Actor.class' which would return the same thing.
elektrikpulse61 elektrikpulse61

2016/7/15

#
I see. It seems to me that not all of the collision detectors are created equal. Allow me to explain. Previously, I was using the getIntersectingObjects() method. Apparently, this method requires that you put a specific class (not Actor.class or null), or it'll throw an "incompatible types" error. However, using the getOneIntersectingObject() method, I was able to use your advice, danpost, with either null or Actor.class, and the code would compile without any errors. This is interesting. I mean, for what I'm doing, either method will work fine, but in the future, I may want to do more digging into the differences between, for example, getOneIntersectingObject() and getIntersectingObjects() (other than the fact that one returns an Actor and the other returns a list, respectively). Either way, thank you for your help, danpost!
davmac davmac

2016/7/15

#
Previously, I was using the getIntersectingObjects() method. Apparently, this method requires that you put a specific class (not Actor.class or null), or it'll throw an "incompatible types" error.
getIntersectingObjects() also accepts Actor.class or null as a parameter; on the other hand, it returns a List. I suspect the error was due to the way you were using the method - perhaps you were trying to assign the result to a variable of the wrong type, for example (including a List of the wrong type). The following works for me:
        List<Actor> l = getIntersectingObjects(Actor.class);
elektrikpulse61 elektrikpulse61

2016/7/16

#
Now that you've put it that way, your idea is much more logical. Of course when I try to assign a list-returning method to an Actor object, I'm going to get an incompatible types error! Thank you both davmac and danpost for your help on this. I'll be sure to remember your advice for next time!
You need to login to post a reply.