LIBGDX RayCast: how do i stop the ray? (How to return just the wanted fixture in the world)

774 views Asked by At

Can you please help me to understand,

How can i STOP a raycast, when it hit a wall?

Let's say that :

$==player, #==wall, %==Enemy, RayCast == ------.

And i have this level:

___________________________


       #
       #
% -----#---$---
___________________________

How can i stop the enemy shooting at me in this kind of situation?

How can i just stop the raycast for "to see what there is after the wall fixture"?

For now I just get them both:

     RayCastCallback callback= new RayCastCallback() {
        @Override
        public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {

            if (fixture.getFilterData().categoryBits == Application.PLAYER){
                return fraction;
            }


            if (fixture.getFilterData().categoryBits == Application.ENEMY){
                return fraction;
            }

            return 0;
        }
    };


    world.rayCast(callback, p1, p2);

So it's the fraction that can achieve that? if so, how?

Thanks so much!

1

There are 1 answers

0
adam On BEST ANSWER

with this approach, it's works for what i need:

private static final int NOTHING = 0;
private static final int WALL = 1;
private static final int PLAYER = 2;
private int type = NOTHING;
private Vector2 position;

@Override
public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {

    if (fixture.getFilterData().categoryBits == Application.WALL){
        type = WALL;
    }
    if (fixture.getFilterData().categoryBits == Application.PLAYER){
        type = PLAYER;
    }

    return fraction;
}

So when i print the type:

System.out.println(this.rayCastStatus);

The result will be to STOP a raycast, when it hit a wall.

Well, not really to stop, in other words, just to get the result that i need which in this case is NOT to print the player, when the wall comes first.

From the docs, about the return:

public interface RayCastCallback {
/** Called for each fixture found in the query. You control how     the ray cast proceeds by returning a float: return -1: ignore
* this fixture and continue return 0: terminate the ray cast return fraction: clip the ray to this point return 1: don't clip
* the ray and continue.
* 
* The {@link Vector2} instances passed to the callback will be reused for future calls so make a copy of them!
* 
* @param fixture the fixture hit by the ray
* @param point the point of initial intersection
* @param normal the normal vector at the point of intersection
* @return -1 to filter, 0 to terminate, fraction to clip the ray for closest hit, 1 to continue **/
public float reportRayFixture (Fixture fixture, Vector2 point, Vector2 normal, float fraction);
}