Finding offset position relative to actor angle

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!
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!)
User avatar
Cherno
Posts: 1339
Joined: Tue Dec 06, 2016 11:25 am

Finding offset position relative to actor angle

Post by Cherno »

My math skills are limited, so I have come here for help :)

A want to find a Vector3 that is offset from an actor's position relative to the actor's angle. So, maybe 16 units to the front and 32 units to the right.

I figured out how to find the x offset (forwards/back), but I don't know about the right-left offset maths.

Code: Select all

int offsetX = 16;//forward-backward
int offsetY = 32;//right-left
Vector3 offsetPos = (pos.x + cos(angle) * offsetX, pos.y + sin(angle) * offsetX, pos.z);
			
User avatar
KeksDose
 
 
Posts: 596
Joined: Thu Jul 05, 2007 6:13 pm
Location: my laboratory

Re: Finding offset position relative to actor angle

Post by KeksDose »

You can easily do the same thing you did forwards a second time to go sideways by adding or subtracting 90° from your angle:

Code: Select all

vector3 forwards  = (cos(angle),      sin(angle),      0);
vector3 sideways  = (cos(angle - 90), sin(angle - 90), 0);
vector3 offsetPos = pos + offsetX * forwards + offsetY * sideways;
When you get used to that, you can also use that cos(x + 90) = -sin(x) and sin(x + 90) = cos(x) to find out that you can write sideways in terms of forwards like this:

Code: Select all

vector3 sideways = (forwards.y, -forwards.x, 0);
I like to think of forwards and sideways as local x and y axes, with offsetX and offsetY being the coordinates there.
User avatar
Cherno
Posts: 1339
Joined: Tue Dec 06, 2016 11:25 am

Re: Finding offset position relative to actor angle

Post by Cherno »

Thank you very much.

Return to “Scripting”