(solved) projectile motion formula in zcript

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.

(solved) projectile motion formula in zcript

Postby Jekyll Grim Payne » Wed Jan 16, 2019 5:12 am

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 3:02 pm, edited 1 time in total.
User avatar
Jekyll Grim Payne
 
Joined: 21 Jul 2008

Re: projectile motion formula in zcript

Postby Apeirogon » Wed Jan 16, 2019 8:14 am

vel.z = cos(pitch) * speed;
No?
Apeirogon
I have a strange sense of humour
 
Joined: 12 Jun 2017

Re: projectile motion formula in zcript

Postby Graf Zahl » Wed Jan 16, 2019 8:36 am

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.
User avatar
Graf Zahl
Lead GZDoom Developer
 
Joined: 19 Jul 2003
Location: Germany

Re: projectile motion formula in zcript

Postby Cherno » Wed Jan 16, 2019 10:05 am

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).
User avatar
Cherno
 
Joined: 06 Dec 2016

Re: projectile motion formula in zcript

Postby Jekyll Grim Payne » Wed Jan 16, 2019 12:30 pm

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.
User avatar
Jekyll Grim Payne
 
Joined: 21 Jul 2008

Re: projectile motion formula in zcript

Postby Jekyll Grim Payne » Thu Jan 17, 2019 11:45 am

Thanks to a huge help from Gutawer, the following formula is now working:
Code: Select allExpand view
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;
   }
User avatar
Jekyll Grim Payne
 
Joined: 21 Jul 2008


Return to Scripting

Who is online

Users browsing this forum: No registered users and 1 guest