Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Calculating velocity and angle to hit target
Started by JoshuaNovak919 Jul 11 2013 12:30 PM

- - - - -
15 replies to this topic
box2d physics trajectory velocity angle
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

JoshuaNovak919

[GLOBAL: userInfoPane.html]
JoshuaNovak919
  • Enthusiast

  • 55 posts
  • Enterprise

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: post.html]
#2

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

[TOPIC: post.html]
#3

JoshuaNovak919

[GLOBAL: userInfoPane.html]
JoshuaNovak919
  • Enthusiast

  • 55 posts
  • Enterprise

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

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

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

JoshuaNovak919

[GLOBAL: userInfoPane.html]
JoshuaNovak919
  • Enthusiast

  • 55 posts
  • Enterprise

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

JoshuaNovak919

[GLOBAL: userInfoPane.html]
JoshuaNovak919
  • Enthusiast

  • 55 posts
  • Enterprise

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

StarCrunch

[GLOBAL: userInfoPane.html]
StarCrunch
  • Contributor

  • 842 posts
  • Corona SDK

Did you see my comments further down on that tutorial page? It got a bit butchered (didn't see any formatting in the reply part... I could fix it up here or whatever, if necessary), but you can get an angle (two, actually) out of that. Speed would be solving for "v", instead (given time, angle, etc.)



[TOPIC: post.html]
#8

tetu

[GLOBAL: userInfoPane.html]
tetu
  • Contributor

  • 166 posts
  • Corona SDK

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

JoshuaNovak919

[GLOBAL: userInfoPane.html]
JoshuaNovak919
  • Enthusiast

  • 55 posts
  • Enterprise

@StarCrunch Yeah i saw your comments, but it's a bit hard to decipher how that's supposed to work. I want to try and get a version working with the example on that page so everyone can see how it would work easier.



[TOPIC: post.html]
#10

horacebury

[GLOBAL: userInfoPane.html]
horacebury
  • Corona Geek

  • 3,070 posts
  • Corona SDK

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

JoshuaNovak919

[GLOBAL: userInfoPane.html]
JoshuaNovak919
  • Enthusiast

  • 55 posts
  • Enterprise

@horacebury Thanks that'd be great. In the mean time i'll look over the links you posted, but i believe i went through these before and still had trouble getting it to work properly.



[TOPIC: post.html]
#12

StarCrunch

[GLOBAL: userInfoPane.html]
StarCrunch
  • Contributor

  • 842 posts
  • Corona SDK

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

horacebury

[GLOBAL: userInfoPane.html]
horacebury
  • Corona Geek

  • 3,070 posts
  • Corona SDK

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

JoshuaNovak919

[GLOBAL: userInfoPane.html]
JoshuaNovak919
  • Enthusiast

  • 55 posts
  • Enterprise

Thanks for everyone's help. I'm still trying to get this to work correctly. I'm going to keep doing research and tests, I'll update when/if i get it working right.



[TOPIC: post.html]
#15

JoshuaNovak919

[GLOBAL: userInfoPane.html]
JoshuaNovak919
  • Enthusiast

  • 55 posts
  • Enterprise

Quick update guys. I was still having a lot of trouble figuring it out so i ended up hiring a physics tutor to help me out. It took us three hours, but we ended up figuring it out. Once i get a chance i will port it over to a sample app and put it up for you guys to use.



[TOPIC: post.html]
#16

horacebury

[GLOBAL: userInfoPane.html]
horacebury
  • Corona Geek

  • 3,070 posts
  • Corona SDK

Wow, cool, really glad you made it. That's quite a commitment, well done!




[topic_controls]
[/topic_controls]