[ACS]Convert X/Y velocity into single variable?

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!)
User avatar
Xane123
Posts: 165
Joined: Tue Nov 24, 2015 1:58 pm
Graphics Processor: nVidia (Modern GZDoom)
Location: Inwood, WV

[ACS]Convert X/Y velocity into single variable?

Post by Xane123 »

As the title says, I wonder how to convert X/Y velocities into a single "speed" variable so I can do things based on how fast the player is moving. At the moment, I check whichever velocity is the highest number, but then diagonal angles have lower numbers. I bet it's just trigonometry but the only things I do with sine/cosine is making things smoothly move in circles and spawn coins in a circle.

In my game, this causes running on the water to be harder when not going at a relatively grid-aligned direction, and an attack I'm going to add will not work when running diagonally through my maps.
User avatar
AFADoomer
Posts: 1337
Joined: Tue Jul 15, 2003 4:18 pm

Re: [ACS]Convert X/Y velocity into single variable?

Post by AFADoomer »

Try this:

Code: Select all

Function int GetSpeed(int tid)
{
	int x = GetActorVelX(tid);
	int y = GetActorVelY(tid);
	int z = GetActorVelZ(tid);

	int speed = (FixedMul(x, x) + FixedMul(y, y) + FixedMul(z, z)) >> 16;

	return speed;
}
User avatar
Xane123
Posts: 165
Joined: Tue Nov 24, 2015 1:58 pm
Graphics Processor: nVidia (Modern GZDoom)
Location: Inwood, WV

Re: [ACS]Convert X/Y velocity into single variable?

Post by Xane123 »

That seems to work! Thanks, AFADoomer.

Return to “Scripting”