This site requires JavaScript, please enable it in your browser!
Greenfoot back
mich@elbecker.de
mich@elbecker.de wrote ...

2020/3/30

show bounding boxes and anchor points

mich@elbecker.de mich@elbecker.de

2020/3/30

#
Hi, 1. collision detection uses bounding boxes. Is it possible to show these bounding boxes around actors in the world? I mean something like this: https://static.packt-cdn.com/products/9781783980383/graphics/0383OS_01_13.jpg I think this would help to understand how collision detection works. 2. Every actor has an anchor point. (I don't know whether this is the correct term; I mean the point that I set with Actor.setLocation(int x, int y).). Where is this anchor point in the image of the actor? Is it allways the center of the image? 3. As far as I understand, getOneObjectAtOffset uses the anchor point (https://www.greenfoot.org/topics/4302). To understand this method, it would be nice if i could show the anchor points of the actors in the world (for example as red point in the image of the actor). Is this possible? Thanks Michael
danpost danpost

2020/3/30

#
mich@elbecker.de wrote...
1. collision detection uses bounding boxes. Is it possible to show these bounding boxes around actors in the world?
The bounds are determined by the dimensions of the images set to the actors -- meaning the bounded areas are always rectangular.
2. Every actor has an anchor point. (I don't know whether this is the correct term; I mean the point that I set with Actor.setLocation(int x, int y).). Where is this anchor point in the image of the actor? Is it allways the center of the image?
Yes -- and no. There is no exact center along a dimension of even length. So, an image with even dimension would have the point ((image_width - 1)/2, (image_height - 1)/2) at the anchor point (x, y) in the world. So, as close to the center as possible.
3. As far as I understand, getOneObjectAtOffset uses the anchor point (https://www.greenfoot.org/topics/4302). To understand this method, it would be nice if i could show the anchor points of the actors in the world (for example as red point in the image of the actor). Is this possible?
Of course it is. It involves having all your Actor subclasses extend a class like the following:
import greenfoot.*;

public class Aktor extends Actor
{
    public void act()
    {
    }

    public void setImage(String imageName)
    {
        super.setImage(imageName);
        dressImage();
    }

    public void setImage(GreenfootImage image)
    {
        super.setImage(image);
        dressImage();
    }

    private void dressImage()
    {
        GreenfootImage image = getImage();
        int width = image.getWidth();
        int height = image.getHeight();
        image.setColor(Color.RED);
        image.drawRect(0, 0, width-1, height-1);
        image.fillOval(width/2-5, height/2-5, 10, 10);
        image.setColor(Color.WHITE);
        image.drawRect(1, 1, width-3, height-3);
        image.fillOval(width/2-3, height/2-3, 6, 6);
        image.setColor(Color.RED);
        image.drawRect(2, 2, width-5, height-5);
        image.fillOval(width/2-1, height/2-1, 2, 2);
    }
}
You need to login to post a reply.