Calculating Trajectory

Posted by horacebury, Posted on December 5, 2011

4 votes

This code calculates every point (to the granularity you want) of an object travelling along a trajectory, starting with a given angle and velocity.

I wanted to provide some very simply functions developers can call and have the most simple demonstration code - because that's all I can deal with.

I have to say that I did not work this stuff out myself, but just went looking for a really good tutorial, which I found at:

http://hyperphysics.phy-astr.gsu.edu/hbase/traj.html

Thanks to those guys - some really awesome work!

Anyway, here is the single file you will need - just rip out the bits you want, it will work as is:

main.lua:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
--[[ references ]]--
 
-- The code in this listing was derived from this first URL:
-- http://hyperphysics.phy-astr.gsu.edu/hbase/traj.html
-- http://en.wikipedia.org/wiki/Trajectory_of_a_projectile
 
 
--[[ support functions ]]--
 
function vxf(v, a)
        return v * math.cos(a * math.pi / 180)
end
 
function vyt(v, a, t)
        return v * math.sin(a * math.pi / 180) - 9.8 * t
end
 
function yt(v, a, t)
        return v * math.sin(a * math.pi / 180) * t - 0.5 * 9.8 * t * t
end
 
 
--[[ worker functions ]]--
 
function totalRangeHeightFlightTime(v, ag)
        local h = vyt(v, ag, 0) * vyt(v, ag, 0) / (2 * 9.8)
        local t = 2 * vyt(v, ag, 0) / 9.8
        local r = v * v * math.sin( 2 * ag * math.pi / 180 ) / 9.8
        return r, h, t
end
 
function positionAtTime(v, ag, t)
        local vx = vxf(v,ag) -- horizontal velocity
        local x = vxf(v,ag) * t -- horizontal distance
        local vy = vyt(v, ag, t) -- vertical velocity
        local y = yt(v, ag, t) -- height at time 't'
        
        return x, y, vx, vy
end
 
 
--[[ environment setup ]]--
 
local offsetX, offsetY = 50, 400
local iteration = 0.1
 
local v = 70 -- launch velocity v/ms
local ag = 80 -- launch angle (theta) degrees
local r, h, f = totalRangeHeightFlightTime(v, ag) -- total flight time
 
print( 'range: ', r )
print( 'height: ', h )
print( 'flight time: ', f )
 
 
--[[ trajectory plotting ]]--
 
for t=0, f, iteration do
        local x, y, vx, vy = positionAtTime(v, ag, t)
        print( math.round(x), math.round(y) )
        display.newCircle( x + offsetX, y * -1 + offsetY, (vx+vy)*.1 )
end


Replies

canupa.com's picture
canupa.com
User offline. Last seen 5 hours 50 min ago. Offline
Joined: 20 Jun 2011

nice one, thanks!

horacebury's picture
horacebury
User offline. Last seen 9 hours 4 min ago. Offline
Joined: 17 Aug 2010

No problem. It's a lot of fun to work on this. My only concern is that it is very difficult to calculate a reliable trajectory for a physics object in the Box2D engine in Corona, because of the lack of a GetMass() function. If we had that we'd be able to apply it to the trajectory calculations and get a more accurate path.

krystian6
User offline. Last seen 11 hours 30 min ago. Offline
Joined: 6 Dec 2011

This is just amazing.
I was just going to create some projectile calculations and I was looking for some basic functions in lua to do so and I stumbled upon your code.
Man... you just made my day!

horacebury's picture
horacebury
User offline. Last seen 9 hours 4 min ago. Offline
Joined: 17 Aug 2010

Well, I've been working over the last week to make a function to calculate the mass of an object in place of the missing getMass() function. Once I've got that I'll apply it to the trajectory code.

clay
User offline. Last seen 2 weeks 6 hours ago. Offline
Joined: 18 May 2011

Is there any way to use this to calculate the angle needed to shoot the object a certain distance (given the distance and power)?

This would be a huge help.. Thanks a lot!

- Clay

horacebury's picture
horacebury
User offline. Last seen 9 hours 4 min ago. Offline
Joined: 17 Aug 2010

Hi @clay, yes - if you look at the link I provide in my post you'll see the math for that. The cool thing is that the javascript to demonstrate the equations they provide is very easily turned into lua.