I have a game where an object is being launched from a specific height and i want the object to pass through the point that the user tapped on the screen. Does anyone know a good way to calculate this?

[TOPIC: topicViewTemplate]

[GLOBAL: userSmallPhoto]

Started by
JoshuaNovak919
Jul 11 2013 12:30 PM

15 replies to this topic

box2d physics trajectory velocity angle
[TOPIC CONTROLS]

[/TOPIC CONTROLS]

[modOptionsDropdown]

[/modOptionsDropdown]

[reputationFilter]

[TOPIC: post.html]

#1

11 July 2013 - 12:30 PM

[TOPIC: post.html]

#2

11 July 2013 - 01:04 PM

Hi @JoshuaNovak919,

This tutorial should help...

http://www.coronalabs.com/blog/2013/04/09/physics-radial-gravity-and-predicting-trajectory/

Regards,

Brent

[TOPIC: post.html]

#3

11 July 2013 - 01:07 PM

Brent,

Thanks, but i've actually already been using the info on that page, but that page doesn't have the trajectory pass through where the user touches. Any ideas what equation i would need to figure this out? I've been stuck on this for weeks.

Thanks,

-Josh

[TOPIC: post.html]

#4

11 July 2013 - 01:25 PM

Hi Josh,

So is the "cannon" supposed to fire at a set velocity each time? What about the angle? If the user touches "above" the cannon or "right" of the cannon, how do you decide what the launch angle will be? I suppose the solution really depends on your "constants", which are clearly different than the constants that most trajectory plotters use.

Brent

[TOPIC: post.html]

#5

11 July 2013 - 01:36 PM

Brent,

I would say that the velocity would be what i would make constant so it's the angle required to hit the target at the constant velocity that i would need. I've find a ton of different equations and i can't seem to find the one that works correctly.

Thanks,

-Josh

[TOPIC: post.html]

#6

11 July 2013 - 02:33 PM

The closest i've found so far is the equation on this page, but for some reason i can't get it to translate properly to the physics in Corona. I'm not sure if i need to convert gravity or the points or something.

http://nial.me/2012/09/calculating-bullet-trajectories/

[TOPIC: post.html]

#7

11 July 2013 - 03:00 PM

[TOPIC: post.html]

#8

11 July 2013 - 04:33 PM

Joshua,

the trajectory is described by a second order equation of the form y=ax^2+bx+c (1)

where a,b and c are constants that depend on position and velocity. that means you have to solve 3 equations for 3 unknowns to find a, b and c. example: say we want the object to start at point {100,200}, end at point {300,150} and also pass through point {200,50}. substituting these values for x and y into (1) we get

10000*a+100*b+c=200

90000*a+300*b+c=150

40000*a+200*b+c=50

solving these 3 simultaneous equations for a,b,c we get a=0.0125, b=-5.25, c=600

google solving simultaneous linear equations to solve them online

create an x increment

local timeSteps=100 local dx=(300-100)/timeSteps

and use a timer to adjust x and y

--inside the timer function object.x=object.x+dx object.y=0.0125*math.pow(object.x,2)+(-5.25)*object.x+600

if instead of an end point condition we had an initial upward velocity of 50, we'd replace the second equation above by 200*a+b=-50 (2ax+b is the velocity at any point) and solve the equations again for a,b,c

[TOPIC: post.html]

#9

11 July 2013 - 04:56 PM

[TOPIC: post.html]

#10

12 July 2013 - 12:47 AM

Pretty sure I developed some code for this back when I was researching the projectile trajectory code on the exchange. I'll try to dig it out later, but here's some references for reading:

http://answers.unity3d.com/questions/49195/trajectory-of-a-projectile-formula-does-anyone-kno.html

http://answers.unity3d.com/questions/242624/mathematicians-wanted-trajectory-target-hitting-qu.html

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

Sorry I can't be any more help right now, but the day job calls. I'll see if I can find my working code later.

[TOPIC: post.html]

#11

12 July 2013 - 12:51 AM

[TOPIC: post.html]

#12

12 July 2013 - 11:46 AM

In case it's any help, I'll see if I can elaborate on what I posted, in particular the stuff in this chunk

local dx, dy, y = target[1] - point[1], target[2] - point[2], target[3] - point[3] local x2 = dx * dx + dy * dy local a1, a2, t1, t2 -- If the target is above or below, do special vertical case. if x2 < 1e-5 then a1, a2, t1, t2 = VerticalLaunch(y, gravity, v0, get_times) -- x(t) = v0 * cos(alpha) * t -- y(t) = v0 * sin(alpha) * t - g * t^2 / 2 -- t = x / (v0 * cos(alpha)) -- y = v0 * sin(alpha) * x / (v0 * cos(alpha)) - g * x^2 / (2 * v0^2 * cos^2(alpha)) -- phi = angle of sight to target -- Apply various transformations to get: -- sin(2 * alpha - phi) = right-hand side (RHS) -- If RHS > 1, the sine argument was invalid: the shot will miss.

*target* is the point you're clicking, and *point* where you're starting from. It may be relevant (i.e. it's a reasonable oversight to make) to note that *y* is a relative position, not absolute.

In the above, you can just pretend *dy* isn't there; this was ported from 3D code (a ball game). Anyhow, that first check is just how far in the "x" direction we go... if it's 0 we're firing straight up and can fall back to VerticalLaunch (just special-cased version of what follows).

Then you'll have those two x and y equations (note again these are relative to *point*), which you'll see in trajectory formulae when you use the angle *alpha* (which we don't know). The first one can be arranged in terms of *t*, and can then be substituted into the y equation where we have *t*.

*phi*, the "line of sight" angle, is the easy one to find. It's the one formed by the x-axis and the *point* -> *target* segment. For the rest of the process, see the excerpt in the Mechanics book (pp. 188-9, if the link didn't quite hit it). It looks like anything else I'd say is in there.

[TOPIC: post.html]

#13

12 July 2013 - 11:31 PM

Ok, well, I think it's fair to say that I've failed, I'm afraid. I've kept various versions of my code as I read around the internet and have put all the links in the file. You can download it if you want to read up or try to rearrange some of my math, just in case anything jumps out at you:

https://www.dropbox.com/s/kiicnw8agunfhfn/main.lua

I have noticed that I got solutions which appear to be close, but that converting between pixels and Box2D units was required. I eventually used the fromMKS and toMKS functions (physics.* library) to do this, but still wound up with incorrect values.

Sorry that I couldn't help more. I would love to solve this problem and if you do it I would very much encourage you to post the code here, to the Code Exchange (with a link in this thread) or even write a tutorial on it and let Brent/Rob know (so they can put it on the blog.)

I will keep thinking about this problem (as a code dog, I can't stop chasing solution cars) and if I figure it out I'll post here, of course.

Good luck.

[TOPIC: post.html]

#14

15 July 2013 - 05:35 PM

[TOPIC: post.html]

#15

21 July 2013 - 11:46 PM

[TOPIC: post.html]

[topic_controls]

[/topic_controls]