Raycasting
Raycasting via the physics world is a calculation that shoots an imaginary line from one point to another, and reports back with the distance, each encountered fixture, and the normal vector of each surface hit. Raycasts can be used for anything from lasers and vision cones to determining what an imaginary bullet hit. In this recipe, we will demonstrate raycasting within our physics world.
Getting ready...
Follow the steps in the Introduction to the Box2D physics extension section at the beginning of the chapter to create a new activity that will facilitate our use of raycasting in the physics world.
How to do it...
Follow these steps to create a raycasting demonstration:
Place the following definitions at the beginning of the activity:
Body BoxBody; Line RayCastLine; Line RayCastLineHitNormal; Line RayCastLineHitBounce; float[] RayCastStart = {cameraWidth/2f,15f}; float RayCastAngle = 0f; float RayCastNormalAngle = 0f; float RayCastBounceAngle = 0f; float RaycastBounceLineLength...