raycast

General discussion about Box2D tutorials
Post Reply
vkreal
Posts: 66
Joined: Sun Jan 13, 2013 7:29 pm

raycast

Post by vkreal »

I am having trouble detecting distance using raycast when its behind another body. Is it possible to raycast to figure out distance where the body is behind another?


Thanks!
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: raycast

Post by iforce2d »

Yes. If you are using b2World::RayCast, you can return 1 from the ReportFixture function to make it keep going until you get a report for the fixture you are interested in.

But if you already know which fixture you want, you can use b2Fixture::RayCast, so that the ray does not even consider other fixtures in the first place - that would probably be faster too.
vkreal
Posts: 66
Joined: Sun Jan 13, 2013 7:29 pm

Re: raycast

Post by vkreal »

Awesome! Thanks for quick reply and solutions. I will try out b2Fixture::RayCast, but i did try b2World::RayCast return 1 in ReportFixture to find correct fixture but didn't seem to work, maybe i didn't do it right.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: raycast

Post by iforce2d »

When you return 1 from the ReportFixture it will continue to loop through all of the fixtures that the ray hits. You will also need to check that the fixture being reported is the one you want, and the order the fixtures are reported can be any order. If you just record the first hit, or the last hit etc, it may be some other fixture.
Post Reply