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

Comments for Pixel-perfect collision detection

Return to Pixel-perfect collision detection

KartoffelbrotKartoffelbrot

2013/11/9

Ok, da war meins viel schlimmer: public final CActor getOneIntersectingObject(Class clss){ List<CActor>cactor = super.getIntersectingObjects(clss); for(CActor c : cactor){ for(int y = getY()-getHeight()/2; y < getY()+getHeight()/2; y++){ for(int x = getX()-getWidth()/2; x < getX()+getWidth()/2; x++){ for(int cy = c.getY()-c.getHeight()/2; cy < c.getY()+c.getHeight()/2; cy++){ for(int cx = c.getX()-c.getWidth()/2; cx < c.getX()+c.getWidth()/2; cx++){ if(x==cx && y==cy){ GreenfootImage a = new GreenfootImage(getImage()), b = new GreenfootImage(c.getImage()); a.rotate(getRotation()); b.rotate(c.getRotation()); int ix = x-(getX()-getWidth()/2), iy = y-(getY()-getHeight()/2), icx = cx-(c.getX()-c.getWidth()/2), icy = cy-(c.getY()-c.getHeight()/2); if(a.getColorAt(ix,iy).getAlpha()>50 && b.getColorAt(icx, icy).getAlpha()>50) return c; } } } } } } return null; } Dann werd ich wohl lieber deins verwenden. Danke :)
KartoffelbrotKartoffelbrot

2013/11/9

Ich hab jede vom Bild abgedeckte Position des ersten Actors mit jeder vom Bild abgedeckten Position des zweiten Actors vergleichen.
A new version of this scenario was uploaded on Sat Nov 30 18:43:39 UTC 2013 two more methods added
JetLennitJetLennit

2013/12/3

Very cool!
bournebourne

2013/12/11

FYI, it seems someone uploaded a copy of your scenario here: http://www.greenfoot.org/scenarios/10060
Busch2207Busch2207

2013/12/11

thank you bourne, but I don't care, when he uploads one of my demo-scenarios without the source code. :) I already got an email from the greenfoot-team. :)
jojothejmanjojothejman

2015/5/4

I've been trying to use the touch method, but it will not allow me and says "method touch in class Block cannot be applied to given types; required: java.lang.class found: greefoot.Actor" All I did was copy this code: public boolean touch(Class clss) { List<Actor> list = getWorld().getObjects(clss), list2 = new ArrayList(); for(Actor A : list) if(intersects(A)&&touch(A)) return true; return false; } And I got this error that doesn't seem to be a problem for your program. Do you know why this could be happening?
hoyaduyinhoyaduyin

2015/11/24

Can somebody explain how the touch method works?
danpostdanpost

2015/11/24

@hoyaduyin, there are two 'touch' methods in the project. The one listed above iterates through the actors of the given class. If any of these actors are found to intersect this object, the other 'touch' method is called to check for non-transparent collision between the two objects.
hoyaduyinhoyaduyin

2015/11/24

Thanks for the fast answer! Would it be possible that you explain me the second touch method a bit more exact? Thanks
danpostdanpost

2015/11/24

@hoyaduyin, I cannot be sure what the first part (deciding which image should be 'a_big' or 'a_small') is for or even if it matters. However, the rest is pretty straight-forward. Copy the images only a image base large enough to rotate them without losing any of the image, rotating them to the rotation of the actor the image belongs to and comparing the pixels that overlay each other to see if any set compared are both non-transparent.
hoyaduyinhoyaduyin

2015/11/25

I am still not sure about the part with the image base?
danpostdanpost

2015/11/25

@hoyaduyin, it is a bit hard to explain. Basically, the image itself needs to be rotated to the rotation of the actor to determine where each pixel of the image resides in the world. Only those parts of the image that are inside the largest circle you can draw within the frame of the image are guaranteed to remain within the frame of the image when rotating it. Therefore, the image needs to be drawn onto a larger frame before rotating it to avoid loss of image. That larger frame is what I called the 'image base'.
16cruzs16cruzs

2015/12/12

Hey Dan, so im a highschool student in pennsylvania and im taking this intro to programming class. The teacher teaches us the basics but other than that, we are on our own to problem solve and what not. I go on this website all the time for answers, and all the time i see your answers to peoples questions! and you didnt even go to college for this, im so impressed haha if it means anything. i made this account just to ask you for help personally. if you could email me at cruzsam00000@gmail.com id appreciate it. or if theres a private message thing on here, i dont know
danpostdanpost

2015/12/12

Any issues you have with programming can be discussed here in this forum. Just create a new thread for whatever issues you have.
danpostdanpost

2015/12/12

^^ My apologies, this is not the forum ^^
Nosson1459Nosson1459

2017/3/2

I don't really understand the touch method but I like it anyway (I knew that I liked this scenario already but for some reason I hadn't clicked "Do I like this"). (I didn't comment until now is because the last comment on this scenario is about 15 months ago so I supposed that it was "abandoned" but now I see that Busch2207 is a little bit back so I can comment on his scenarios and say that it seems that his specialty is dealing with colors and pixels.)
Busch2207Busch2207

2017/3/4

Well, in the end it is not so complicated. First I check, if the rectangles of the actors collide at all. Then I create an image, wich I rotate like the two original images are rotated. Then I check the overlapping parts of the images for pixels that are colliding.
Nosson1459Nosson1459

2017/3/5

Right, but the way you did that isn't that simple.
Nosson1459Nosson1459

2017/3/17

I changed the three getWorld().getObjects(... in your methods to getIntersectingObjects(... since that was the only way I can use your methods for collision checking between two actors of the same class. It works just fine, since there is no reason why it shouldn't but I'm just wondering why you did what you did? Maybe there is a reason that I should use getObjects as opposed to getIntersectingObjects.
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). (ツ)