Page 1 of 1

Obtaining the Surface Normal of a Touched Wall

Posted: Sat Nov 20, 2021 9:01 pm
by Pyhrrous
See title; this is for ZScript, just in case someone asks.

I'd like to find the surface normal of any wall that the player touches, from the side of the wall that they touched. I do understand that LineTrace exists, but I would like to know if there's a pre-existing way to get a normal vector before I try working with it.

Anyone have any pointers?

Re: Obtaining the Surface Normal of a Touched Wall

Posted: Mon Nov 22, 2021 4:17 am
by Apeirogon
Unless I forgot how to count

Code: Select all

vector2 get_line_normal_to_player(vector3 player_pos, line test_line)
{
    //normal to the vector in 2d case is (vector.-y, vector.x), simple rotation on 90 degree
    //works only in 2d case
    if(player_pos.xy dot (test_line.delta.-y, test_line.delta.x) >= 0)
        return (test_line.delta.-y, test_line.delta.x);
    else return (test_line.delta.y, test_line.delta.-x);
}
https://github.com/coelckers/gzdoom/blo ... ta.zs#L167

Re: Obtaining the Surface Normal of a Touched Wall

Posted: Mon Nov 22, 2021 2:02 pm
by Pyhrrous
Apeirogon wrote:Unless I forgot how to count

Code: Select all

vector2 get_line_normal_to_player(vector3 player_pos, line test_line)
{
    //normal to the vector in 2d case is (vector.-y, vector.x), simple rotation on 90 degree
    //works only in 2d case
    if(player_pos.xy dot (test_line.delta.-y, test_line.delta.x) >= 0)
        return (test_line.delta.-y, test_line.delta.x);
    else return (test_line.delta.y, test_line.delta.-x);
}
https://github.com/coelckers/gzdoom/blo ... ta.zs#L167
When I added this to my file with some value tracing code on it and attempted to run, I got an "Unexpected '-'" error. Were the - symbols supposed to be before their respective "test_line"s?

Making that replacement and assuming that this is the intended output, this function doesn't seem to be what I'm looking for. It returns the delta of the line length rather than the normal.

I'll make my own jab at this code and get back here with the results, once I have them.

EDIT: Err... how would I go about getting the endpoints of a line? Planning on using the signed distance of the player's XY position in relation to the line, which is what I need this for.

Re: Obtaining the Surface Normal of a Touched Wall

Posted: Mon Nov 22, 2021 2:56 pm
by Apeirogon
Replace all test_line.delta.-y and test_line.delta.-x to -test_line.delta.y and -test_line.delta.x.

Re: Obtaining the Surface Normal of a Touched Wall

Posted: Mon Nov 22, 2021 6:15 pm
by Pyhrrous
Yeah, it's still not producing the desired effect. Running this function on two walls on opposite sides of a hallway does not produce opposite-signed return values.

Gonna re-ask this again, I suppose: how do I get the start and endpoints of a line found from LineTrace?

Re: Obtaining the Surface Normal of a Touched Wall

Posted: Tue Nov 23, 2021 9:52 am
by Pyhrrous
Okay, I think I found a satisfactory way to do this. If anyone knows there's a more efficient method though, do let me know.
Spoiler: