• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by irgendeine · Apr 20, 2012 at 03:05 PM · trajectory

Mathematicians wanted = trajectory target hitting question

I should really be able to figure this one out myself but it is driving me mad - my maths knowledge seems to be just too rusty.

I basically want to throw a ball from any height (position x,y,z in 3d space) onto a target point in 3D space. On top of this, I am checking that the ball with the calculated parabola does not hit a net with a certain height. I came up with the correct calculations to hit the target point based on this site: http://hyperphysics.phy-astr.gsu.edu/hbase/traj.html

So I am defining a velocity and calculating the appropriate angle. But now I am checking whether it hits the net, and if it does want to change the angle and/or velocity so it goes higher.

But I am having a real problem changing the equation from the "where will it land" part so that I can adjust the height to go over the net height. How can I reverse the equation from x and time? I have the start x and y, the target x and y and need to determine the angle and velocity (I can always make up one of them, just need to get the velocity). The one I came up with is obviously wrong, I am missing the x and y part - I can only adjust the throwing velocity correctly if the start and end point are on the same y level.

Any help appreciated!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by aldonaletto · Apr 20, 2012 at 03:54 PM

If both objects are at the same height, things are easier. If they have different heights, however, the equation becomes too complicated, so complicated that I was not able to get the inverse function. But I used a simple trick to correct the trajectory when the height difference is relatively small compared to the horizontal distance (how much is "small" depends on the elevation angle). The idea is to aim closer or farther than the target distance in such a way that the trajectory passes through its actual position:

alt text

This trick works fine for relatively small differences, as I said, but since it relies on a straight line approximation of the trajectory near to the zero height point, the error grows with the height difference.
You can see my answer with the complete ballistic calculation function in this question: http://answers.unity3d.com/questions/148399/shooting-a-cannonball.html

EDITED: The equation you have is interesting because it takes the precise height difference into account, while mine just approximates it. Unfortunately, it returns the angle, not the velocity, and this complicate things a lot!
My function does the opposite - returns a velocity vector for a given angle - but isn't so precise when the heights are different. Maybe you could combine both and try a numeric solution: find an initial angle based on the fence height and distance from the target, use my function to find the necessary velocity, refine the angle with your equation (the second one - the first always returns the higher angle) and check if the fence is being hit: if yes, increase the initial angle (multiply it by 1.10, for instance) and repeat the process.
Just as a convenience, I posted the function BallisticVel below:

function BallisticVel(target: Transform, angle: float): Vector3 {
    var dir = target.position - transform.position;  // get target direction
    var h = dir.y;  // get height difference
    dir.y = 0;  // retain only the horizontal direction
    var dist = dir.magnitude ;  // get horizontal distance
    var a = angle * Mathf.Deg2Rad;  // convert angle to radians
    dir.y = dist * Mathf.Tan(a);  // set dir to the elevation angle
    dist += h / Mathf.Tan(a);  // correct for small height differences
    // calculate the velocity magnitude
    var vel = Mathf.Sqrt(dist * Physics.gravity.magnitude / Mathf.Sin(2 * a));
    // return the complete velocity vector
    return vel * dir.normalized;
}
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Apr 20, 2012 at 03:57 PM 0
Share

I guess I can't compete...

avatar image irgendeine · Apr 20, 2012 at 04:02 PM 0
Share

I already got so far and I can actually check whether it will clear the fence. $$anonymous$$y problem starts just there - I need to get a new angle and velocity in order to clear the net but still hit my target. Currently I am changing the velocity (vel--) and calculate my angle again, because I got the formula for the angle:

 float angle1 = $$anonymous$$athf.Atan((vel*vel + $$anonymous$$athf.Sqrt($$anonymous$$athf.Pow(vel,4) -gravity*(gravity * xDist * xDist + 2*yDist*vel*vel)))/(gravity * xDist))*$$anonymous$$athf.Rad2Deg;
 float angle2 = $$anonymous$$athf.Atan((vel*vel - $$anonymous$$athf.Sqrt($$anonymous$$athf.Pow(vel,4) -gravity*(gravity * xDist * xDist + 2*yDist*vel*vel)))/(gravity * xDist))*$$anonymous$$athf.Rad2Deg;

But this is not quite working, I'd need a way to actually get the velocity needed for a certain angle.

With each angle calculation I get two possible angles. I do not want anything over ca. 45 degrees because it just doesn't look right so I am kind of stuck with guessing...

I need to change the equation to something like that:

newVel(if hitting the net) = some formula with newAngle, xDistance, landing Y, $$anonymous$$Height at net point

$$anonymous$$aybe I just have to change my overall logic and ins$$anonymous$$d of applying an Impulse Force use some iTween Paths to hit the target...

avatar image aldonaletto · Apr 21, 2012 at 01:13 PM 0
Share

I think you could try a numeric solution: I edited the answer to propose something like this - more a hack than a real numeric algorithm; a real numeric solution could be found from a 3 equation non-linear system, but it's way too complex for my taste.

avatar image Kleptomaniac · Apr 21, 2012 at 02:27 PM 0
Share
  • for an excellent pictorial representation of the problem Aldo!

avatar image irgendeine · Apr 23, 2012 at 11:26 AM 0
Share

Thanks for the help, looks like it is just kind of a guessing game. I am just plaing around with a solution that looks like it is going to work taking your suggestions into account. If it all fails after some testing I guess I'll get rid of the net :) Cheers

Show more comments
avatar image
2

Answer by fafase · Apr 20, 2012 at 03:37 PM

I think I found something that could help you out.

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

Go down the middle of the page to "Will it clear the fence?"

I thought that would be easier for you to read and understand than writing some (vel^2*sin^2(alpha))/2g = height

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

how to predict physics trajectory of a sphere 2 Answers

Using lookat for a specific direction (not an object) 1 Answer

Draw an orbital trajectory of a ship 1 Answer

Having trouble using a line renderer to show a projectile's predicted trajectory. 1 Answer

Show trajectory of a bouncing ball 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges