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

Report as inappropriate.

Busch2207
Busch2207 presents ...

2013/11/9

Pixel-perfect collision detection

The methods for the collision detection are in the Collision-class.
If you want, you can use them in your own scenarios.

14802 views / 2457 in the last 7 days

27 votes | 3 in the last 7 days

Tags: simulation with-source

open in greenfoot
Your browser is ignoring the <APPLET> tag.
YehudaYehuda

2017/5/3

I forgot to mention (I posted the other comments) that I changed the touch(Class clss) method to just be getOneTouchedObject(clss) != null as all the code for it instead of having all the code there twice (not exactly the same code but does the same thing).
Busch2207Busch2207

2017/5/9

Well, I used 'getWorld().getObjects()', because getIntersectingObjects() also iterates over all Objects in the World-class, checks whether they intersect and then returns the list. So there's no affect to the running-time. In the 'getOneTouchedObject'-method it could also save some time, if one of the first objects in the list touches it... So the 'intersect'-method don't have to be called on all actors...
A new version of this scenario was uploaded on 2017-05-09 19:48:47 UTC Latest Greenfoot-Version...
YehudaYehuda

2017/5/10

But with using getWorld().getObjects(), I ran into problems when I tried to check if one instance of a class is touching a different instance of the same class, so I changed it (and if you leave it alone, other people can/will have the same problem). I got the idea of changing the touch method, from the greenfoot.Actor class which did that.
Busch2207Busch2207

2017/5/10

I think, this problem just occured, because in the list also was the object itself, which was not checked. I changed this in the code.
YehudaYehuda

2017/5/10

I know the reason for the problem, so I solved it on my downloaded version, now I told you about the problem.
davmacdavmac

2017/5/11

"Well, I used 'getWorld().getObjects()', because getIntersectingObjects() also iterates over all Objects in the World-class, checks whether they intersect and then returns the list. So there's no affect to the running-time" - this is not correct. The collision checking in Greenfoot is sophisticated and maintains a dynamic space partition tree to make intersection checks very fast.
mole2003mole2003

2017/7/29

how?
VenbhaVenbha

2017/8/4

I can make it simpler with less code.(in version 3.0.4). (ツ)
TakedaYeetTakedaYeet

2020/2/5

how do i see the code?

See all comments

Want to leave a comment? You must first log in.

Who likes this?

TheGrrMan Davidson AbdulrazaqAS Gabe1098 Zwemvis mole2003 BloodMate ET78759 Yehuda Wasupmacuz Nosson1459 Ajantabhattacharya darkmist255 lordhershey yedefei JetLennit Zamoht davemib123 erdelf SPower