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

2015/6/26

Help with detecting intersecting actors in world class.

jorge89 jorge89

2015/6/26

#
So I am having trouble with trying to detect if an actor is intersecting another one of itself in the world class. If you see on the code I am making several of them with respects to I. The only problem is that I am getting the error can not find sym
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public AnimalWorld()
    {   
     super(600, 400, 1);
     for(int i = 0; i < 30; i++)
     {
         xPosition = Greenfoot.getRandomNumber(580) + 10;
         yPosition = Greenfoot.getRandomNumber(360) + 10;
    if (i==0)
           {
               Actor animal = new Animal();
               addObject(animal, xPosition, yPosition);
            }
            else if(i > 0 && i < 15)
            {
                Actor leaf = new Leaf();
                addObject(leaf, xPosition, yPosition);
                            }
            else if(i > 15 && i < 25)
            {
                Actor obstacle = new Obstacle();
                addObject(obstacle, xPosition, yPosition);
                
               Greenfoot.getOneIntersectingObjects(Obstacle.class);
                if (getOneInterectingObject == null)
                {
                }
                else
                {
                 getWorld().removeObject(this);
                getworld().addObject(obstacle, xPosition, yPosition);
            }
             }
             else
             {
                 Actor bomb = new Bomb();
                 addObject(bomb, xPosition, yPosition);
                }
    }
    }
bol method getoneintersectingobject. I do not understand what is wrong.
davmac davmac

2015/6/26

#
For a start, please indent your code properly. You can use ctrl+shift+I if you haven't changed the keybindings. Your problem is here, I think:
1
2
Greenfoot.getOneIntersectingObjects(Obstacle.class);
 if (getOneInterectingObject == null)
Two issues: First, 'getOneIntersectingObjects' is not a member of the Greenfoot class, so 'Greenfoot.getOneIntersectingObjects(...)' is not right. There are two methods with a similar name in the Actor class: 'getOneIntersectingObject' and 'getIntersectingObjects'. I think it's the first one that you really want; it checks whether one actor is interesting any other actors (and returns a reference to one of the intersecting actors). Because it's in the Actor class, and is not a static method, you need a reference to the one actor in order to call the method. In this case I think that 'obstacle' would probably do, i.e 'obstacle.getOneIntersectingObject(...)'. Second, you call the method, but then separately try to use the value that it returned - 'if (getOneInterectingObject == null)'. You can't just use the method name as if it was a variable. You need to either define a variable, and save the method return value in that variable, or put the method call directly inside the 'if' condition, i.e. 'if (obstacle.getOneIntersectingObject(Obstacle.class) == null)'. I think the correct version of the code above is, therefore:
1
if (obstacle.getOneInterectingObject(Obstacle.class) == null)
davmac davmac

2015/6/26

#
Also, on line 29:
1
getWorld().removeObject(this);
... this tries to remove the world from itself. I think you probably wanted:
1
getWorld().removeObject(obstacle);
danpost danpost

2015/6/26

#
You cannot use the 'if' statement given at the end of the first post davmac wrote in your AnimalWorld class. The method 'getOneIntersectingObject' has protected access and can only be called from within an Actor subclass. I believe the lines 23 through 30 are misplaced and should be in your Obstacle class. There is still one other thing of concern with that block of code. Line 29 removes the object from the world and then line 30 uses 'getWorld', which will now return a 'null' value, to add a new Obstacle object into the world. This will fail as you cannot execute a method on a 'null' reference. Swapping the order of the two lines should solve that issue. After further investigation into your code, it appears what you wanted to do was randomly add a number of Obstacle objects into the world so that none of them intersected each other. The check for intersection still needs to be done in the Obstacle class. It would be better to have the Obstacle object being added find its own place in the world where it is not intersecting another of its kind. In other words -- all your World subclass would be doing is adding the object into the world (it could be added at location (0, 0) for all that mattered). Use the 'addedToWorld' method of the Actor class to place the code that repeatedly gets a new random location, sets itself there and checks for intersectors until none are found.
davmac davmac

2015/6/26

#
The method 'getOneIntersectingObject' has protected access and can only be called from within an Actor subclass.
That's true. I always seem to forget this...
wabuilderman wabuilderman

2015/6/26

#
One thing you could do, if adding random non-intercepting objects is all you want, try using a different algorithm. There are algorithms designed specifically for this. I am only saying this because it is probably more efficient to do a different algorithm.
fejfo fejfo

2015/7/2

#
I think the easest solution is to put this code in your Obstacle class:
1
2
3
protected void addedToWorld(World w) {
    removeTouching(Obstacle.class);
}
You need to login to post a reply.