## Obtaining the Surface Normal of a Touched Wall

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)

### Obtaining the Surface Normal of a Touched Wall

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?

Pyhrrous

Joined: 14 Jul 2020

### Re: Obtaining the Surface Normal of a Touched Wall

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

Apeirogon
I have a strange sense of humour

Joined: 12 Jun 2017

### Re: Obtaining the Surface Normal of a Touched Wall

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.

Pyhrrous

Joined: 14 Jul 2020

### Re: Obtaining the Surface Normal of a Touched Wall

Replace all test_line.delta.-y and test_line.delta.-x to -test_line.delta.y and -test_line.delta.x.

Apeirogon
I have a strange sense of humour

Joined: 12 Jun 2017

### Re: Obtaining the Surface Normal of a Touched Wall

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?

Pyhrrous

Joined: 14 Jul 2020

### Re: Obtaining the Surface Normal of a Touched Wall

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:

Pyhrrous

Joined: 14 Jul 2020