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

2012/12/3

Checking for Objects in Range (kind of)

TheKidChameleon TheKidChameleon

2012/12/3

#
Does anyone know a good way for an Actor to check for other actors in a range, but only in one direction? For instance, I don't want the entire radius around the actor, but just a line or cone-shape along a single axis. I tried using a for-loop in conjunction with getOneObjectAtOffset(), but it didn't pan out quite like I'd hoped.
Kyle273 Kyle273

2012/12/3

#
I'm assuming you're using this for line of sight. One of the easier ways to do it is to create an invisible actor, and check collisions on that. Otherwise, you can use getObjectsInRange(), and reference x and y coordinates, and do a bit of math to see if it falls within your check boundary.
danpost danpost

2012/12/3

#
You can still use the list of actors within range about the entire radius of the actor and narrow it down from there by getting the angle of the line between said actor and any potentials around it. If said actor is facing the general direction, then the adjusted difference between the two angles must be equal to or less than the range of the angle allowable. The general idea of how the list returned can be narrowed down is demonstrated in the code below; be advised, however, that this code is not plug-n-play:
int angleFacing = getRotation();
for (Object obj : getObjectsInRange(rangeValue, null)
{
    Actor actor = (Actor) obj;
    int angleToActor = (int) (Math.atan2(actor.getY()-getY(), actor.getX()-getX())*180/Math.PI);
    boolean validActor = (360+angleToActor-angleFacing)%360 <= maxAngleDifference;
}
You need to login to post a reply.