(solved) projectile motion formula in zcript

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 a reply

Smilies
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :geek: :ugeek: :!: :?: :idea: :arrow: :| :mrgreen: :3: :wub: >:( :blergh:
View more smilies

BBCode is OFF
Smilies are ON

Topic review
   

Expand view Topic review: (solved) projectile motion formula in zcript

Re: (solved) projectile motion formula in zcript

by Razumen » Sat Apr 02, 2022 6:56 am

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;
}

Re: projectile motion formula in zcript

by Jekyll Grim Payne » Thu Jan 17, 2019 10:45 am

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;
	}

Re: projectile motion formula in zcript

by Jekyll Grim Payne » Wed Jan 16, 2019 11:30 am

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.

Re: projectile motion formula in zcript

by Cherno » Wed Jan 16, 2019 9: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).

Re: projectile motion formula in zcript

by Graf Zahl » Wed Jan 16, 2019 7: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.

Re: projectile motion formula in zcript

by Apeirogon » Wed Jan 16, 2019 7:14 am

vel.z = cos(pitch) * speed;
No?

(solved) projectile motion formula in zcript

by Jekyll Grim Payne » Wed Jan 16, 2019 4: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?

Top