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

2011/8/25

Touching other Objects

K_O_P K_O_P

2011/8/25

#
Is there any possibility to check, if the Image of an Object touchs another image? I don't mean "getOneIntersectingObject(Whatever.class)". I mean, if that, what is drawed on the Image, touches anything, that is drawed on another Objects Image?
kiarocks kiarocks

2011/8/25

#
well, thats exactly what you use, unless you want this link.
K_O_P K_O_P

2011/8/25

#
NO that's the point! I don't mean this Method! I wont a Method, that checks if that, what is drawed on the Object really hits another object... And that the invisible parts will not be considered.
mjrb4 mjrb4

2011/8/25

#
If you're talking about more detailed collision detection (per pixel as oppose to just a box round the image) it's not the default method of collision quite deliberately because of performance reasons, it's notably slower to check each pixel for overlap and the vast majority of the time that's overkill. You'd have to implement it yourself (or find another scenario that has) but it shouldn't be hugely difficult if you're comfortable with working in Java. If you Google around for 2D collision detection algorithms there's plenty you could choose and implement depending on what you require.
davmac davmac

2011/8/25

#
K_O_P, do you really need the collision detection to be pixel-perfect or do you just need it a bit "tighter" than what getOneIntersectingObject() etc provide? There are some pretty simply ways to refine the collision detection without making it pixel-perfect.
kiarocks kiarocks

2011/8/26

#
so, like if i had a one pixel cell and a 25 by 25 image, check to see if the image touches or if the pixel at center does?
Representation based Sometimes it is not precise enough to use the cell location to determine collisions. Greenfoot has a few methods that allow you to to check whether the graphical representations of actors overlap. Actor leaf = getOneIntersectingObject(Leaf.class); List leaves = getIntersectingObjects(Leaf.class); Be aware that these method calls require more computation than the cell based methods and might slow down your program if it contains many actors.
GameCode GameCode

2011/8/26

#
You could use the getOneObjectAtOffset(...) Method of Actor. Then you could check some/every Pixel with for-loops. This would be very detaild:
public boolean checkTouch(Actor a)
{
     if(checkOneTouch(-20, 20, -40, a)) return true;
     if(checkOneTouch(-17, 17, -39, a)) return true;
     if(checkOneTouch(-18, 18, -38, a)) return true;
     if(checkOneTouch(-14, 14, -37, a)) return true;
     if(checkOneTouch(-15, 15, -36, a)) return true;
                    //...
     if(checkOneTouch(-14, 14, 40, a)) return true;

     return false;
 
}

public boolean checkOneTouch(int xStart, int xEnd, y, Actor a)
{
    for(int x=start; x<end; x++)
    {
        if(getOneObjectAtOffset(x,y, a.class)!=null)
            return true;
    }
    return false;
}
in the checkTouch() Method you can check all Pixels of your drawn Objects
K_O_P K_O_P

2011/8/26

#
Thank you GameCode... I tried it... But your solution was very compute-intensive... I'll search for another and easier solution... Should I find a solution, I'll tell you.
Busch2207 Busch2207

2011/8/26

#
I've found a solution and it's not very compute-intensive. :) Try it:
    public List getTouchedObjects(Class clss)
    {
        List<Actor> list = getIntersectingObjects(clss);
        List<Actor> list2 = getIntersectingObjects(clss);
        list2.clear();
        GreenfootImage i=new GreenfootImage(getImage());
        i.rotate(getRotation());
        int w=i.getWidth(),h=i.getHeight(),x=getX(),y=getY();
        for(Actor A : list)
        {
            GreenfootImage Ai = new GreenfootImage(A.getImage()), i2 = new GreenfootImage(w,h);
            Ai.rotate(A.getRotation());
            int Aw=Ai.getWidth(),Ah=Ai.getHeight(),Ax=A.getX(),Ay=A.getY();
            i2.drawImage(Ai,Ax-x-(Aw/2-w/2),Ay-y-(Ah/2-h/2));
            boolean b = true;
            for(int yi = 0; yi<h && b; yi++)
                for(int xi = 0; xi<w && b; xi++)
                    if(i2.getColorAt(xi,yi).getAlpha()>0 && i.getColorAt(xi,yi).getAlpha()>0)
                    {
                        list2.add(A);
                        b=false;
            }
        }
        return list2;
    }

    public Actor getOneTouchedObject(Class clss)
    {
        List<Actor> list = getIntersectingObjects(clss);
        List<Actor> list2 = getIntersectingObjects(clss);
        list2.clear();
        GreenfootImage i=new GreenfootImage(getImage());
        i.rotate(getRotation());
        int w=i.getWidth(),h=i.getHeight(),x=getX(),y=getY();
        for(Actor A : list)
        {
            GreenfootImage Ai = new GreenfootImage(A.getImage()), i2 = new GreenfootImage(w,h);
            Ai.rotate(A.getRotation());
            int Aw=Ai.getWidth(),Ah=Ai.getHeight(),Ax=A.getX(),Ay=A.getY();
            i2.drawImage(Ai,Ax-x-(Aw/2-w/2),Ay-y-(Ah/2-h/2));
            for(int yi = 0; yi<h; yi++)
                for(int xi = 0; xi<w; xi++)
                    if(i2.getColorAt(xi,yi).getAlpha()>0 && i.getColorAt(xi,yi).getAlpha()>0)
                        return A;
        }
        return null;
    }
you have to import the class of Lists with:
import java.util.List;
You have to write this under:
import greenfoot.*;
K_O_P K_O_P

2011/8/26

#
Busch! you're really a genius! :D Thank you very very much! :D
You need to login to post a reply.