Question

I'm making a game in XNA. enter image description here

I'm doing a raycast from the enemy to the player to determine if the enemy can see the player. Heres the code..

 private float RayCallBack(Fixture fixture, Vector2 point, Vector2 normal, float fraction)
    {
        rayhit = fixture.Body.UserData.ToString();
        if (fixture.Body.UserData == "player")
        {
            //AIawake = true;
        }
        return 0f;
    }


     _world.RayCast(RayCallBack, _blocklist[0]._floor.Position , ConvertUnits.ToSimUnits(playerpos));

My problem is that in the situation in the picture where I have procedurally generated caves made out of blocks the rays seem to go through the blocks so the enemy can see through the walls.

--

UPDATE

Ok the following code works! but.. I have no idea why!! :/

 private float RayCallBack(Fixture fixture, Vector2 point, Vector2 normal, float fraction)
    {
        rayhit = fixture.Body.UserData.ToString();
        if (fixture.Body.UserData == "player")
        {
            return fraction;
        }
        else
        {
            return 0f;
        }
    }

and then in a seperate update statement in this class have the code to awaken the enemy.

    if (rayhit == "player") AIawake = true;

I obviously do not understand how raycast and the callback works. If someone could explain why this method works that'd be great. I am planning on doing a lot more raycasting to stop the enemies crashing into stuff and so on.

Était-ce utile?

La solution

There are 4 scenarios. If you:

  1. return -1 in your callback fixture then the fixture is going to be ignored and the ray will continue
  2. return 0 the ray will terminate
  3. return fraction then the ray will stop at this point
  4. return 1 then the ray will continue until it ends

The first scenario is used when you want to ignore certain fixtures. Second scenario is used when you want to know if your ray hit anything (not necessarily the closest object). Third scenario is used to find the nearest object. And the fourth scenario is used when you wish to know about all objects in the ray's path.

In general you callback logic would have a part that checks for objects that should be ignored and return -1 if they should be ignored which will be followed by a code block that either returns 0, fraction or 1.

The difference between second and third scenario is possibly the hardest to understand. The way I understand it is that if you return 0 none of other potential hits are going to be evaluated. But if you return a fraction (which will clip the ray) other potential hits within the new (clipped) ray length are going to be evaluated - in other words your callback will be executed for all of them and the ray might be clipped even further which will eventually return the nearest object.

See Box2D documentation about Ray Casting here.

One thing that documentation does say is that the order of evaluation is not guaranteed. In your case it might be that the player was being evaluated before the wall blocks.

tl; dr; return fractions as you did in your second code sample.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top