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

2012/5/21

Check for objects on a line

USBest USBest

2012/5/21

#
Is there a way to check for objects on a given line?
USBest USBest

2012/5/21

#
Can nobody help me?
kartikitrak kartikitrak

2012/5/21

#
You have to explain your issue a bit better. Provide some sample code, or analogy or something to explain what you want.
USBest USBest

2012/5/21

#
Ah ok! Well, I have 2 Points: P1 and P2 And I want all objects, that are touching the line, between these two Points. And how can I get them?
darkmist255 darkmist255

2012/5/21

#
I would think something using a For loop would work: For(int x = P1, x <= P2, x++) { find objects }
iau iau

2012/5/21

#
There's nothing built-in to Greenfoot which will do this for you, but you could follow a plan like this:
  • Find all the objects which might be on the line. Easiest way to do that is to find all the objects which are in the circle with P1 at its centre and P2 on the circumference. Use getObjectsInRange()
  • From the List of objects returned by getObjectsInRange(), discard all the ones which are not between P1 and P2 (that is, the ones that might on the line, but are on the other side of the circle from P2).
  • Check the remaining objects to see if they are actually on the line. An object (P?) is on the line between P1 and P2 if the angle of the line between P1 and P? is the same as the angle of the line between P1 and P2. The function you need to do this is atan(). Neil's Sinepost blog has a description of the basic problem, but he describes it as "moving towards a point". The issue here is that if "moving towards P2" means you would run over P?, then P? and P2 are on the same line.
But then your question has already changed from "objects on the line" to "objects touching the line", and I guess that next will be "objects that would be touched if this other object moved from P1 to P2", so you might end up moving an invisible object along the line (using turnTowards() and move()) and checking for touching objects at each step (using getOneIntersectingObject()). That's going to be expensive.
darkmist255 darkmist255

2012/5/21

#
iau's idea about getting all the objects then filtering them out would also work great, but might be a bit more complex.
Busch2207 Busch2207

2012/5/21

#
This code can be used in classes, that are subclasses of Actor:
    public List getObjectsOnLine(Point P1, Point P2,Class clss)
    {
        double
        EX=P1.getX(),
        EY=P1.getY(),
        XB=P2.getX(),
        YB=P2.getY();
        int
        i_AX=getX(),
        i_AY=getY(),
        i_Rot=getRotation();

        setLocation((int)((EX+XB)/2),(int)((EY+YB)/2));

        GreenfootImage
        i = getImage(),
        ColImage = new GreenfootImage((int)(getDistance(EX,XB,EY,YB)+0.5),1);
        setImage(ColImage);
        setRotation((int)getDegreesToFrom(XB,YB,EX,EY));

        List List_Class_ = getIntersectingObjects(clss);

        setRotation(i_Rot);
        setLocation(i_AX,i_AY);
        setImage(i);
        return List_Class_;
    }

    public static double getDistance(double x1,double x2, double y1,double y2)
    {
        return Math.hypot(x2-x1,y2-y1);
    }

    public double getDegreesToFrom(double x1, double y1,double x,double y)
    {
        return Math.toDegrees(Math.atan2(y1-y,x1-x));
    }
It will return a List of all objects of the given class, that are on the line between the two given points.
iau iau

2012/5/22

#
Busch2207's idea is much better than mine: Set your Actor's image and position to match the line you want, then use getIntersectingObjects to find all the other objects that lie along it, then switch the image and position back.
danpost danpost

2012/5/22

#
You could just create an object called 'Line', with an image of thickness 1, and add it to the world at the line in question (with the proper rotation), and get its intersecting objects using a 'null' parameter (or you could specify a specific class).
You need to login to post a reply.