Data Realms Fan Forums http://45.55.195.193/ |
|
How to calculate the trajectory of a particle http://45.55.195.193/viewtopic.php?f=1&t=19703 |
Page 1 of 1 |
Author: | Awesomeness [ Wed Sep 08, 2010 11:45 pm ] |
Post subject: | 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. |
Author: | CaveCricket48 [ Thu Sep 09, 2010 12:35 am ] |
Post subject: | 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. |
Author: | Awesomeness [ Thu Sep 09, 2010 1:26 am ] |
Post subject: | Re: How to calculate the trajectory of a particle |
Conventionally it is in CC, but could we do it differently? |
Author: | CaveCricket48 [ Thu Sep 09, 2010 1:29 am ] |
Post subject: | 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). |
Author: | CrazyMLC [ Thu Sep 09, 2010 1:38 am ] |
Post subject: | 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. |
Author: | Awesomeness [ Thu Sep 09, 2010 2:43 am ] |
Post subject: | 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. |
Author: | CaveCricket48 [ Thu Sep 09, 2010 5:17 am ] |
Post subject: | 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. |
Author: | 411570N3 [ Thu Sep 09, 2010 2:23 pm ] |
Post subject: | 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. |
Author: | Abdul Alhazred [ Thu Sep 09, 2010 3:39 pm ] |
Post subject: | 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. |
Author: | Shook [ Fri Sep 10, 2010 8:30 am ] |
Post subject: | 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. |
Author: | PhantomAGN [ Fri Sep 10, 2010 5:07 pm ] |
Post subject: | Re: How to calculate the trajectory of a particle |
Here's some code, partly commented. Attachment: 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. |
Page 1 of 1 | All times are UTC [ DST ] |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |