Wall detection in Lua

Status
Not open for further replies.

742mph

My work is never done yet
Can anyone think of a reliable way to detect whether a wall or solid surface is directly in front of an object using Lua? I've tried using P_CheckPosition with the x/y coordinates shifted slightly ahead of the object, but that doesn't seem to work. Is there some trick to using that function?
 

fickleheart

ms reflec beat stan
Code:
-- P_BlockingWall: returns whether there's a wall blocking an object from moving a specified angle/distance
-- (NOTE: does NOT return WHICH wall was hit!)
-- You can put this in a separate script lump if you want! Call it from anywhere with P_BlockingWall(mobj, angle, maxdist)
local tester = nil -- Misc object used for testing
rawset(_G, "P_BlockingWall", function(mobj, angle, maxdist)
    if not (tester and tester.valid) then -- Spawn a new one!
        tester = P_SpawnMobj(mobj.x, mobj.y, mobj.z, MT_GARGOYLE) -- Has the best chance of not getting cleared on spawn
        tester.flags = $1|MF_NOGRAVITY|MF_NOCLIPTHING -- Make sure it won't move or block things
        tester.flags2 = $1|MF2_DONTDRAW -- Don't see the gargoyle! (can be commented out for testing if needed)
    end
    
    -- Match tester size to mobj size
    tester.radius = mobj.radius
    tester.height = mobj.height
    
    P_TeleportMove(tester, mobj.x, mobj.y, mobj.z) -- Move tester to mobj's position
    P_InstaThrust(tester, angle, maxdist) -- Misuse P_InstaThrust to easily get the distance to move
    return not P_TryMove(tester, tester.x+mobj.x, tester.y+mobj.y, true) -- Return the inverse of P_TryMove (since it returns true on successful move, false on blocked)
end)
I'm using a trick very similar to this for a project I'm working on. If you need the specific wall it'd need a more complicated setup, probably creating your own blockmap for Lua to avoid testing every line unnecessarily, but it'd be possible.
 
Last edited:

742mph

My work is never done yet
Thanks, Red, but there's a problem. I'm not sure what you mean when you call P_TryMove(tester, tester.x+mobj.x, tester.y+mobj.y, true), since P_TryMove uses absolute coordinates according to the wiki, and when you add those coordinates together the result could be anywhere in (or outside) the map. In practice, P_BlockingWall seems to return true even when there's nothing in the way, and the issue could probably be fixed if I knew what that was supposed to be.

EDIT: Also, the gargoyle makes pushing noises since MF_PUSHABLE, MF_SOLID, etc. weren't removed, so I changed "tester.flags = $1|MF_NOGRAVITY|MF_NOCLIPTHING" to just "tester.flags = MF_NOGRAVITY|MF_NOCLIPTHING", which seemed to fix the problem.
 
Last edited:

fickleheart

ms reflec beat stan
...My defense is it was really late at night and I wasn't focusing. It's supposed to use the tester object's momx/momy instead. :V
 
Status
Not open for further replies.

Who is viewing this thread (Total: 1, Members: 0, Guests: 1)

Top