Skip to content

Commit

Permalink
World:rayCastAny and rayCastClosest also return the fixture they hit.
Browse files Browse the repository at this point in the history
  • Loading branch information
slime73 committed Oct 2, 2023
1 parent 6cb0287 commit da430d5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
18 changes: 14 additions & 4 deletions src/modules/physics/box2d/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ float World::RayCastCallback::ReportFixture(b2Fixture *fixture, const b2Vec2 &po
}

World::RayCastOneCallback::RayCastOneCallback(uint16 categoryMask, bool any)
: hit(false)
: hitFixture(nullptr)
, hitPoint()
, hitNormal()
, hitFraction(1.0f)
Expand All @@ -256,7 +256,7 @@ float World::RayCastOneCallback::ReportFixture(b2Fixture *fixture, const b2Vec2
if (categoryMask != 0xFFFF && (categoryMask & fixture->GetFilterData().categoryBits) == 0)
return -1;

hit = true;
hitFixture = fixture;
hitPoint = point;
hitNormal = normal;
hitFraction = fraction;
Expand Down Expand Up @@ -652,8 +652,13 @@ int World::rayCastAny(lua_State *L)
b2Vec2 v2 = Physics::scaleDown(b2Vec2(x2, y2));
RayCastOneCallback raycast(categoryMaskBits, true);
world->RayCast(&raycast, v1, v2);
if (raycast.hit)
if (raycast.hitFixture)
{
Fixture *f = (Fixture *)(raycast.hitFixture->GetUserData().pointer);
if (f == nullptr)
return luaL_error(L, "A fixture has escaped Memoizer!");
luax_pushtype(L, f);

b2Vec2 hitPoint = Physics::scaleUp(raycast.hitPoint);
lua_pushnumber(L, hitPoint.x);
lua_pushnumber(L, hitPoint.y);
Expand All @@ -676,8 +681,13 @@ int World::rayCastClosest(lua_State *L)
b2Vec2 v2 = Physics::scaleDown(b2Vec2(x2, y2));
RayCastOneCallback raycast(categoryMaskBits, false);
world->RayCast(&raycast, v1, v2);
if (raycast.hit)
if (raycast.hitFixture)
{
Fixture *f = (Fixture *)(raycast.hitFixture->GetUserData().pointer);
if (f == nullptr)
return luaL_error(L, "A fixture has escaped Memoizer!");
luax_pushtype(L, f);

b2Vec2 hitPoint = Physics::scaleUp(raycast.hitPoint);
lua_pushnumber(L, hitPoint.x);
lua_pushnumber(L, hitPoint.y);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/physics/box2d/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class World : public Object, public b2ContactListener, public b2ContactFilter, p
virtual ~RayCastOneCallback() {};
float ReportFixture(b2Fixture* fixture, const b2Vec2& point, const b2Vec2& normal, float fraction) override;

bool hit;
b2Fixture *hitFixture;
b2Vec2 hitPoint;
b2Vec2 hitNormal;
float hitFraction;
Expand Down

0 comments on commit da430d5

Please sign in to comment.