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

2019/11/4

Contact with transparent images

PowerDj PowerDj

2019/11/4

#
I'm making a game which currently relies heavily on the isTouching() method to see if two actors are making contact. For the most part, it works, but I've run into a very frustrating problem. The images, which I made myself, have transparent sections, since images need to be saved as rectangles. Unfortunately, the transparent parts of the image still trigger the isTouching() method, meaning images can make contact even though no visible part of them is touching. Is there any way to avoid this, like making an image with no transparent parts or ignoring those parts of an image for contact?
danpost danpost

2019/11/4

#
There are ways around this issue; but, beyond resizing to removing totally transparent rows and columns of pixels from the image, none are simple. Option include, but not limited to: * using a combination of range, neighbor and touching methods (usually not sufficient; but sometimes); * applying direct pixel-by-pixel checking (CPU-extensive and may cause lagging); * using hit-boxes to better fit the image;
PowerDj PowerDj

2019/11/4

#
Thanks for the reply! I've been looking around for solutions and it seems that checking the color at specific locations could be a good option. However, every example of the getColorAt method that I've seen uses a specific object as a reference for checking color. Is there some way to get the world itself to check colors, so any object's color can be checked? This is the code I tried to use; it didn't work:
1
2
3
World world = getWorld();
 
Color colour = world.getColorAt(getX(), getY());
If I can get the method to work, I'll change the getX and getY, it's just that I want to know how I could do this before implementing it fully.
danpost danpost

2019/11/4

#
PowerDj wrote...
Is there some way to get the world itself to check colors, so any object's color can be checked? This is the code I tried to use; it didn't work: << Code Omitted >> If I can get the method to work, I'll change the getX and getY, it's just that I want to know how I could do this before implementing it fully.
There is no getColorAt method for a World instance. HINT: but, there is one for a GreenfootImage instance.
PowerDj PowerDj

2019/11/4

#
danpost wrote...
PowerDj wrote...
Is there some way to get the world itself to check colors, so any object's color can be checked? This is the code I tried to use; it didn't work: << Code Omitted >> If I can get the method to work, I'll change the getX and getY, it's just that I want to know how I could do this before implementing it fully.
There is no getColorAt method for a World instance. HINT: but, there is one for a GreenfootImage instance.
Been thinking about this for a while and I hate to say it, but I'm stumped! Are you saying there's a way to turn the world into a GreenfootImage, or am I missing the point?
danpost danpost

2019/11/4

#
PowerDj wrote...
<< Quote Omitted >> Been thinking about this for a while and I hate to say it, but I'm stumped! Are you saying there's a way to turn the world into a GreenfootImage, or am I missing the point?
HINT 2: what is the only method in the greenfoot.World class that returns a GreenfootImage object?
PowerDj PowerDj

2019/11/5

#
danpost wrote...
HINT 2: what is the only method in the greenfoot.World class that returns a GreenfootImage object?
Ah, I see what you mean here. I think it would be better to describe my problem with an image: Basically, the red block has already been placed while the blue block is being controlled. I want to use color methods to check when the red block is underneath the blue block, which will prevent the blue block from being placed (obviously I could just use isTouching(), but I'm using the squares to get the hang of the color detection for other images). Getting colors from the background of the image only returns grey, so I need something that checks the color not just of the world, but of anything inside it.
danpost danpost

2019/11/5

#
PowerDj wrote...
Ah, I see what you mean here. I think it would be better to describe my problem with an image: << Image Link Omitted >> Basically, the red block has already been placed while the blue block is being controlled. I want to use color methods to check when the red block is underneath the blue block, which will prevent the blue block from being placed (obviously I could just use isTouching(), but I'm using the squares to get the hang of the color detection for other images). Getting colors from the background of the image only returns grey, so I need something that checks the color not just of the world, but of anything inside it.
My bad. I should have realized you were doing checks between two actors, not one against the world background, when you referred to the isTouching method. The checking, then, would be very complicated if either one, or both, of the actors rotate. Without any rotations, it is just a matter of pairing the points that overlap and checking that neither of each pair is transparent. The sizes of the images of the actors along with their locations will be used to pair up the points.
You need to login to post a reply.