View unanswered posts | View active topics It is currently Sun Jan 05, 2025 1:51 am



Reply to topic  [ 11 posts ] 
 How to calculate the trajectory of a particle 
Author Message
User avatar

Joined: Sat Jun 19, 2010 5:02 pm
Posts: 331
Location: Mekkan
Reply with quote
Post How to calculate the trajectory of a particle
I want to make a turret that fires grenades. Though throwing works just fine, I don't want it to. Besides, I wouldn't learn anything. I want it to correctly aim at enemies when shooting particles at slow velocities via Lua. To do this, I want to create a function that returns the angle to fire at when given:

1. The speed to fire
2. A vector containing the starting position
3. A vector containing the end position

You could replace 2 and 3 with the difference between the positions.

I have only experience in Algebra 1 and simple trigonometry. Though I know how to find where parabolas intersect the x axis, I am not sure how to do much else. This problem is too much for me... I need help. Could someone either post code with well commented math that explains everything that an advanced 14 year old could understand, or guide me through it?

Thank you so much.


Wed Sep 08, 2010 11:45 pm
Profile
User avatar

Joined: Tue Jun 12, 2007 11:52 pm
Posts: 13144
Location: Here
Reply with quote
Post Re: How to calculate the trajectory of a particle
I believe PhantomAGN did something like this before.

I can give you the basic idea on how to figure out the position of something after X time period, but the velocity is defined as a vector, not speed + angle of launch.


Thu Sep 09, 2010 12:35 am
Profile
User avatar

Joined: Sat Jun 19, 2010 5:02 pm
Posts: 331
Location: Mekkan
Reply with quote
Post Re: How to calculate the trajectory of a particle
Conventionally it is in CC, but could we do it differently?


Thu Sep 09, 2010 1:26 am
Profile
User avatar

Joined: Tue Jun 12, 2007 11:52 pm
Posts: 13144
Location: Here
Reply with quote
Post Re: How to calculate the trajectory of a particle
I need to rephrase that:

The method I'm think of, you input the velocity and set a time. The code gives you a position.

I haven't figured a way to do the opposite yet (input position, get velocity/time).


Thu Sep 09, 2010 1:29 am
Profile
User avatar

Joined: Fri Dec 22, 2006 4:20 am
Posts: 4772
Location: Good news everyone!
Reply with quote
Post Re: How to calculate the trajectory of a particle
Actually, I think piipu did something like this... you just had to input the velocity of the projectile.


Thu Sep 09, 2010 1:38 am
Profile WWW
User avatar

Joined: Sat Jun 19, 2010 5:02 pm
Posts: 331
Location: Mekkan
Reply with quote
Post Re: How to calculate the trajectory of a particle
Well, at least could you show me the forward? Then I might be able to figure out the reverse myself.


Thu Sep 09, 2010 2:43 am
Profile
User avatar

Joined: Tue Jun 12, 2007 11:52 pm
Posts: 13144
Location: Here
Reply with quote
Post Re: How to calculate the trajectory of a particle
Something like:

Pf = ( (G*T) + Vi ) + Pi

pf - final position of projectile
pi - initial position of projectile
G - gravitational pull
T - time (in seconds)
Vi - initial velocity

I think.

So, let's say your projectile's velocity is (20,-50), and gravitational pull is 10 m/s downwards. The initial position of your projectile is (0,0), and you wan to see the final position after a time one 1 second.

Pf = ( (Vector(0,10)*1) + Vector(20,-50) ) + Vector(0,0)
V
Pf = ( Vector(0,10) + Vector(20,-50) ) + Vector(0,0)
V
Pf = Vector(20,-40) + Vector(0,0)
V
Pf = Vector(20,-40)

Same conditions, but you want to see what the final position is after 5 seconds:

Pf = ( (Vector(0,10)*5) + Vector(20,-50) ) + Vector(0,0)
V
Pf = ( Vector(0,50) + Vector(20,-50) ) + Vector(0,0)
V
Pf = Vector(20,0) + Vector(0,0)
V
Pf = Vector(20,0)


Edit: The VED GL uses this technique, if you want to see how this would be implemented using Lua.


Thu Sep 09, 2010 5:17 am
Profile
User avatar

Joined: Wed Jan 07, 2009 10:26 am
Posts: 4074
Location: That quaint little British colony down south
Reply with quote
Post Re: How to calculate the trajectory of a particle
You could probably use basic calculus to find trajectory based on the velocity. Though you'd have to have the Age value of the projectile be accurate, and gravity might be a bit screwy when deltatime's different. It would be necessary to have a good understanding of the exact program mechanics of both to make a decently reliable script.


Thu Sep 09, 2010 2:23 pm
Profile WWW
DRL Developer
DRL Developer

Joined: Tue Aug 11, 2009 5:09 am
Posts: 395
Reply with quote
Post Re: How to calculate the trajectory of a particle
I used this Wikipedia article as a template for my automated mortar, but it assumes a constant muzzle velocity.

It is not perfect but works fairly well even if the difference in Pos.Y between actor and target is a few hundred pixels.


Thu Sep 09, 2010 3:39 pm
Profile
User avatar

Joined: Fri Feb 16, 2007 8:43 pm
Posts: 1695
Location: AH SHIT FUCK AUGH
Reply with quote
Post Re: How to calculate the trajectory of a particle
Hopping on to Abdul's bandwagon, i think this is the part you're interested in. Writing it in Lua would probably give something like:
Code:
local g = SceneMan.GlobalAcc;
local v = <your firing velocity goes here>
local shotAngle = math.atan((v^2 + math.sqrt(v^4 - g * (g * endPos.X^2 + 2 * endPos.Y * v^2)))/g * endPos.X);

Not sure if it's what Abdul used though, and i can't guarantee functionality either. If it fires downwards, you might have to smack a minus in front of either g or Y, as CC thinks that negative Y is upwards.


Fri Sep 10, 2010 8:30 am
Profile WWW
User avatar

Joined: Mon Jun 29, 2009 2:40 am
Posts: 610
Location: Deep below The Map of Mars
Reply with quote
Post Re: How to calculate the trajectory of a particle
Here's some code, partly commented.
Attachment:
Script Aim.txt [3.29 KiB]
Downloaded 154 times
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.


Fri Sep 10, 2010 5:07 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 11 posts ] 

Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.045s | 14 Queries | GZIP : Off ]