Re: How to calculate the trajectory of a particle
Here's some code, partly commented.
This is actually Lua, but it made me have to rename it for upload.
And I can highlight the really important bits for you here.
I've attached the script to the projectile fired for simplicity, as each of mine had a different function that I wanted to code in.
So first, I get the parent actor, and get the aim angle.
Then, I raycast for obstacles in that direction for a distance equal to a bit more than the maximum range I can hit.
Then, a pile of calculus you can mostly ignore so long as you feed it inputs.
I separated the X and Y velocities for the final trajectory to make it easier to read, so you have this:
self.mortarshot.Vel.X = (self.targetx*math.sqrt((self.firevel^2 - self.gravacc*self.targety + self.arcmode * math.sqrt(self.rangetest))/(self.targetx^2 + self.targety^2)))/math.sqrt(2);
self.mortarshot.Vel.Y = (self.gravacc*self.targetx^2 + self.targety*(self.firevel^2 + self.arcmode * math.sqrt(self.rangetest)))/(math.sqrt(2)*math.sqrt(self.arcmode*(self.targetx^2 + self.targety^2)*(self.arcmode*(self.firevel^2 - self.gravacc*self.targety) + math.sqrt(self.rangetest))));
This passes you the X and Y components of a new aiming vector, which will launch a projectile to land at its target in one second. All you have to do is pass that vector on to the projectile you want to fling, be it a grenade, bullet, or even actor.
This is the most flexible way I have so far found, as you can dynamically change gravitational acceleration if need be. I have not put that stuff in yet, but I probably should given the fun that can be had with slow-motion nowadays.
This was all made so long ago that I have no idea if I will ever really use it. Anyone's allowed to use this.
I hope that helps a little, you might get more from reading the code outright and tracing where the variables go.