(solved) Get the angle of the wall from the projectile

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

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!)
Post Reply
User avatar
Jekyll Grim Payne
 
 
Posts: 1065
Joined: Mon Jul 21, 2008 4:08 am
Preferred Pronouns: He/Him
Graphics Processor: nVidia (Modern GZDoom)
Contact:

(solved) Get the angle of the wall from the projectile

Post by Jekyll Grim Payne »

I want to spread some particle effects over the surface of the wall or plane that was hit by a projectile. How can I get the necessary information about the angles (ideally the slope too) and use it like that?

(Ideally I'd like to be able to do it from a puff of a hitscan attack as well, but let's start with projectiles.)
Last edited by Jekyll Grim Payne on Sat Jan 12, 2019 5:54 am, edited 1 time in total.
User avatar
Matt
Posts: 9696
Joined: Sun Jan 04, 2004 5:37 pm
Preferred Pronouns: They/Them
Operating System Version (Optional): Debian Bullseye
Location: Gotham City SAR, Wyld-Lands of the Lotus People, Dominionist PetroConfederacy of Saudi Canadia
Contact:

Re: [ZScript] Get the angle of the wall from the puff/projec

Post by Matt »

The Line class has 2 member classes, v1 and v2, that have a member vector2 called p that holds the coordinates of the vertex, e.g. you can grab a position with "vector2 linestart=myline.v1.p;"

Then something something arctangents and trying to figure out which side of the linedef you're coming in from, in order to get the correct relative angle.

If you can get a pointer for the line, whether by checking the actor's blockingline or with some other check like [wiki]LineTrace[/wiki] (which I believe would also get you the side of the line hit), the rest is just trying to figure out the fastest way to do it without null pointer VM crashes.
User avatar
Apeirogon
Posts: 1605
Joined: Mon Jun 12, 2017 12:57 am

Re: [ZScript] Get the angle of the wall from the puff/projec

Post by Apeirogon »

Sector struct have native "Sec(tor)Plane" struct, which contain "native Vector3 Normal;" variable, which is normal to the plane of a sector. You can find angle/slope/pitch of that sector from it, using some math.
How to do so here
https://en.wikipedia.org/wiki/Normal_(geometry)
User avatar
ketmar
Posts: 160
Joined: Sat Oct 01, 2016 6:26 am
Contact:

Re: [ZScript] Get the angle of the wall from the puff/projec

Post by ketmar »

i believe that sector normals are only for floor/ceiling. as for walls, Matt is right: it is quite easy to get angles from line. something like this (quote from my engine):

Code: Select all

  TVec vec = v2-v1;
  double length = sqrt(vec.x*vec.x+vec.y*vec.y);
  if (!length) {
    angles.pitch = (vec.z > 0 ? 90 : 270);
    angles.yaw = 0;
  } else {
    angles.pitch = -RAD2DEG(atan2(vec.z, length));
    angles.yaw = RAD2DEG(atan2(vec.y, vec.x));
  }
this will give you angles for line direction. basically, `vec.z` will always be zero, so you can only use `yaw` part, and use simple arithmetics to get angle for normal, for example.

p.s.: as for determining the side, i believe that zscript has "PointOnLineSide()" API (or it can be easily exposed, as the engine has that function).
User avatar
Matt
Posts: 9696
Joined: Sun Jan 04, 2004 5:37 pm
Preferred Pronouns: They/Them
Operating System Version (Optional): Debian Bullseye
Location: Gotham City SAR, Wyld-Lands of the Lotus People, Dominionist PetroConfederacy of Saudi Canadia
Contact:

Re: [ZScript] Get the angle of the wall from the puff/projec

Post by Matt »

ZS also has a native length() function: double length=(v2.p-v1.p).length();

(Might also want to check of the two positions are identical before calculating the length, just to save the extra square root call, but I haven't checked for sure since ASMJIT was added whether that would still be faster)
User avatar
ketmar
Posts: 160
Joined: Sat Oct 01, 2016 6:26 am
Contact:

Re: [ZScript] Get the angle of the wall from the puff/projec

Post by ketmar »

it is illegal to have zero-length lines on map. not that it is impossible, but any sane editor should warn and collapse that. the code was copypasted from my doom engine, therefore it has checks there. i mostly pasted it to save OP some effort on figuring out atan2 arguments. ;-)

p.s.: and i am pretty sure that you cannot hit zero-length lines anyway, so zero check can be safely omited.
User avatar
Jekyll Grim Payne
 
 
Posts: 1065
Joined: Mon Jul 21, 2008 4:08 am
Preferred Pronouns: He/Him
Graphics Processor: nVidia (Modern GZDoom)
Contact:

Re: [ZScript] Get the angle of the wall from the puff/projec

Post by Jekyll Grim Payne »

Matt wrote:The Line class has 2 member classes, v1 and v2, that have a member vector2 called p that holds the coordinates of the vertex, e.g. you can grab a position with "vector2 linestart=myline.v1.p;"
I'll be honest, I'm too new to this to fully understand that :( First of all, do I understand correctly that v1 and v2 represent the two vertices of the line? Second, in vector2 linestart=myline.v1.p I honestly really understand only "vector2". Linestrat and myline are implied to be my variables? Sorry for the basic questions.
User avatar
Apeirogon
Posts: 1605
Joined: Mon Jun 12, 2017 12:57 am

Re: [ZScript] Get the angle of the wall from the puff/projec

Post by Apeirogon »

Jekyll Grim Payne wrote: v1 and v2 represent the two vertices of the line
Yes
Jekyll Grim Payne wrote: vector2 linestart=myline.v1.p I honestly really understand only "vector2". Linestrat and myline are implied to be my variables
No, linestart "return" position, as vector 2, of the vertex with "number" v1 of a line "myline".
User avatar
Jekyll Grim Payne
 
 
Posts: 1065
Joined: Mon Jul 21, 2008 4:08 am
Preferred Pronouns: He/Him
Graphics Processor: nVidia (Modern GZDoom)
Contact:

Re: [ZScript] Get the angle of the wall from the puff/projec

Post by Jekyll Grim Payne »

Apeirogon wrote:
Jekyll Grim Payne wrote:
Jekyll Grim Payne wrote: vector2 linestart=myline.v1.p I honestly really understand only "vector2". Linestrat and myline are implied to be my variables
No, linestart "return" position, as vector 2, of the vertex with "number" v1 of a line "myline".
That... hasn't really made things clearer for me, sorry. How do I get "myline"? Will that be blockingline?
User avatar
Apeirogon
Posts: 1605
Joined: Mon Jun 12, 2017 12:57 am

Re: [ZScript] Get the angle of the wall from the puff/projec

Post by Apeirogon »

You store to variable "linestart", which can store vector2 type of data, position "p" of the vertex "v1" from line line "myline", where "myline" is a pointer to some line in map.
User avatar
Matt
Posts: 9696
Joined: Sun Jan 04, 2004 5:37 pm
Preferred Pronouns: They/Them
Operating System Version (Optional): Debian Bullseye
Location: Gotham City SAR, Wyld-Lands of the Lotus People, Dominionist PetroConfederacy of Saudi Canadia
Contact:

Re: [ZScript] Get the angle of the wall from the puff/projec

Post by Matt »

"myline" can be either the actor's BlockingLine after a CheckMove/TryMove check, or the hitline of the FLineTraceData of [wiki]LineTrace[/wiki].

EDIT: simpler to just learn LineTrace, really. Check the example at the bottom of that wiki page.
User avatar
Jekyll Grim Payne
 
 
Posts: 1065
Joined: Mon Jul 21, 2008 4:08 am
Preferred Pronouns: He/Him
Graphics Processor: nVidia (Modern GZDoom)
Contact:

Re: [ZScript] Get the angle of the wall from the puff/projec

Post by Jekyll Grim Payne »

Code: Select all

Angle = atan2(t.HitLine.delta.Y, t.HitLine.delta.X) - 90.;
That works.
Post Reply

Return to “Scripting”