## (solved) projectile motion formula in zcript

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!)
Jekyll Grim Payne

Posts: 936
Joined: Mon Jul 21, 2008 4:08 am
Discord: Agent_Ash#4401
Github ID: jekyllgrim
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia (Modern GZDoom)

### (solved) projectile motion formula in zcript

I was looking at the projectile motion formula and thinking of how I could covert that to ZScript: I need to thrust an object horizontally and vertically to make it cover a specific distance. However, I don't have a lot of experience with these formulas, and it seems to always be concerned with the pitch of the motion. As far as I know, I can't directly set the pitch of a thrust, only horizontal and vertical velocities, and with that I don't know how I can set the pitch.

Could anyone help?
Last edited by Jekyll Grim Payne on Thu Jan 17, 2019 2:02 pm, edited 1 time in total.
Apeirogon
Posts: 1590
Joined: Mon Jun 12, 2017 12:57 am

### Re: projectile motion formula in zcript

vel.z = cos(pitch) * speed;
No?
Graf Zahl
Posts: 47976
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

### Re: projectile motion formula in zcript

Velocity is a 3-dimensional vector which you can set in any way you like. The standard math looks so complicated because it needs to start from a 2D vector and transform that to a 3D vector by factoring in the pitch.

Of course, if you have a 3D start pos and a 3D end pos it all becomes quite simple

vel = (endpos - startpos).Unit() * speed

So, before giving you any further advice you have to explain what values you start with - because that ultimately determines what needs to be done.
Cherno
Posts: 1279
Joined: Tue Dec 06, 2016 11:25 am

### Re: projectile motion formula in zcript

I had to find a function for a monster throwing grenades in the past and this is what gives good results:

horizontal speed: distance / 40
vertical speed: 6
spawn height of projectile: 36
This works nicely for a grenade, it gets thrown in a nice arc and lands more or less at the spot you want (not taking into account things like bouncing).
Jekyll Grim Payne

Posts: 936
Joined: Mon Jul 21, 2008 4:08 am
Discord: Agent_Ash#4401
Github ID: jekyllgrim
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia (Modern GZDoom)

### Re: projectile motion formula in zcript

Graf Zahl wrote:
So, before giving you any further advice you have to explain what values you start with - because that ultimately determines what needs to be done.
It's about a reverse gibbing animation where a gib has to fly back to its "target", i.e. the monster that initially spawned it. The gib is facing its target, and for now I'm designing it for a case when they have the same height, but ideally it should work for when they're at different heights, since the gib could have been thrown farther than the main body. I'd like it to be thrown at about 45 degrees and reach the main body within 40 tics or so.
Jekyll Grim Payne

Posts: 936
Joined: Mon Jul 21, 2008 4:08 am
Discord: Agent_Ash#4401
Github ID: jekyllgrim
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia (Modern GZDoom)

### Re: projectile motion formula in zcript

Thanks to a huge help from Gutawer, the following formula is now working:

Code: Select all

``````virtual void FlyBack() {
if (!target)
return;
bFLATSPRITE = false;
bNOGRAVITY = false;
bNOINTERACTION = false;
bRELATIVETOFLOOR = false;
bTHRUACTORS = true;
gravity = 1.0;
A_FaceTarget();

double dist = Distance2D(target);							//horizontal distance to target
double vdisp = target.pos.z - pos.z + frandom(8,32);		//height difference between this object and target + randomized height
double ftime = 20;											//time of flight

double vvel = (vdisp + 0.5 * ftime*ftime) / ftime;
double hvel = dist / ftime;

Thrust(hvel,angle);
vel.z = vvel;
}``````
Razumen
Posts: 24
Joined: Tue Mar 13, 2018 8:04 pm

### Re: (solved) projectile motion formula in zcript

Hey, thanks for this equation!
I'm trying to convert your solution into ACS for my map, and I've got it working, but I can't get it to work if the target changes position vertically.
What am I missing here? I really suck at math, and after hours of fiddling with it, my brain is fried at this point lol.

Here's the code so far:

Code: Select all

``````//TID0 is the target, TID671 the shooter

int dist = (playerd2d)>>16;   //horizontal distance to target, calculated by the function below
int vdisp = (GetActorZ(0) - GetActorZ(671))>>16;   //height difference between the spawnspot and the target

int ftime = 10;   //time of flight, seems to break at higher numbers
int vvel = ((vdisp + 0.5 * ftime*ftime) / ftime)>>16;   //Vertical speed
int hvel = dist / ftime;   //Horizontal speed
//Thrust(hvel,angle);     //Don't think I need these two
//vel.z = vvel;

while (shotcount<shots)
{
SpawnProjectile (671, "Fireball", shotangle>>8, hvel, vvel, 1, 0);
shotcount++;
Delay (firerate);
}

function int distance2d (int tid1, int tid2)
{
int y = getactory(tid1) - getactory(tid2);
int x = getactorx(tid1) - getactorx(tid2);
int len = VectorLength (x, y);
return len;
}``````