Human beings employ five distinct senses to interact with the environment. One of these, vision, is its own branch of Computer Science entitled Computer Vision. In this example, we implement basic vision testing in a Box2D environment using a Ray Cast to see if there is another object in-between the player and an enemy AI actor.
Execute the following code:
class RayCastAnyCallback : public b2RayCastCallback { public: RayCastAnyCallback() { m_hit = false; } float32 ReportFixture( b2Fixture* fixture, const b2Vec2& point, const b2Vec2& normal, float32 fraction) { b2Body* body = fixture->GetBody(); void* userData = body->GetUserData(); if (userData) { int32 index = *(int32*)userData; if (index == 0) { // filter return -1.0f; } } m_hit = true; m_point = point; m_normal...