Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Mendenbarr · Feb 26, 2014 at 02:36 PM · c#physicsmathtargeting

How to make targeting code for a physics based projectile

I was trying to make a catapult take the inputs of an initial speed, and the position of a target, and figuring out what angle it needs to shoot at to hit the target. Below is the answer I found.

Leaving the original question in for legacy: I'm trying to write a script to calculate the X and Y velocity required to hit a target with a catapult, but since it's a multivariable equation, c# is having none of it. Is there any way to do a min( X1/Y1 = Z and X2/Y2 = Z), where X1 and X2 are the only known variables? I've pulled the distance between the launcher and the target using transform and finding the difference, which are X1 and X2. distance/speed = time, and I want to try and find the Y1 and Y2 speeds that cause the time to be as low as possible. I realize that a pure min solution will return an insanely high value for X to try to get there instantly, so I'm also looking for a way to work in an upper and lower bound for the speed (Y1 and Y2).

Comment
Add comment · Show 8
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 perchik · Feb 26, 2014 at 02:37 PM 0
Share

Seems like a standard physics question. Why don't you calculate the trajectory and then break that into x and y components?

avatar image fafase · Feb 26, 2014 at 02:49 PM 0
Share

I don't quite get your X1, Y1 things but the little I get, you know the distance and you are looking for the force to apply?

If that is the case then look there http://en.wikipedia.org/wiki/Projectile_motion, it has all you need.

avatar image Mendenbarr · Feb 26, 2014 at 03:29 PM 0
Share

I don't want to lock it to a single angle, I want it to calculate both an angle and a launch velocity, which is basically the same as calculating an X velocity and a Y velocity. Normal algebra solves for 1 unknown in an equation, but trying to solve for angle and launch velocity is solving for 2 unknowns. This has multiple correct solutions, for example: X + Y = 10 has the solutions, X = 5, Y =5, as well as X = 1, Y = 9, and X = 3, Y = 7.
I want the script to find one of these solutions, and I'm not sure how to get it to do that.

avatar image perchik · Feb 26, 2014 at 03:33 PM 0
Share

Right. The problem is there's infinite solutions to your equation which is exactly why multivariate solving techniques are not easy to implement...

Even with constraints on y speed and x speed, its still a bear to implement. $$anonymous$$aybe you should pick 5 different y velocities and solve for the corresponding x values, but trying to implement a multivar solver isn't going to be fun... ask yourself if it's really worth it and reevaluate what you're actually trying to accomplish

avatar image Mendenbarr · Feb 26, 2014 at 03:51 PM 0
Share

And then pick a random of the 5 y velocities? Or mayhaps the one closest to the X velocity? Hmmm.

Show more comments

2 Replies

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

Answer by Mendenbarr · Feb 28, 2014 at 04:44 PM

So I found what I was looking for, and here is the code I'm using that works. It grabs the positions of itself, and it's target, and takes a public variable "maxLaunch", which is the launch velocity, and determines the angle, and through the angle the X and Y velocities required to hit the target.

 void Target (){
         xTarget = target.transform.position.x;
         yTarget = target.transform.position.y;
         xCurrent = transform.position.x;
         yCurrent = transform.position.y;
         xDistance = Math.Abs(xTarget - xCurrent);
         yDistance = yTarget - yCurrent;
         fireAngle = 1.57075f - (float)(Math.Atan((Math.Pow(maxLaunch, 2f)+ Math.Sqrt(Math.Pow(maxLaunch, 4f) - 9.8f * (9.8f * Math.Pow(xDistance, 2f) + 2f * yDistance * Math.Pow(maxLaunch, 2f) )))/(9.8f * xDistance)));
         xSpeed = (float)Math.Sin(fireAngle) * maxLaunch;
         ySpeed = (float)Math.Cos(fireAngle) * maxLaunch;
         if ((xTarget - xCurrent) < 0f){
             xSpeed = - xSpeed;
         }
         launchSpeed.x = (xSpeed);
         launchSpeed.y = (ySpeed);
     }
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
avatar image
0

Answer by Owen-Reynolds · Feb 26, 2014 at 04:39 PM

Think you've confused yourself thinking about it. If you want to get there as fast as possible, the catapult should always fire at maximum speed. So speed is a constant. There are two angles to hit any target in range -- a lofting one above 45, and a flat one below 45. Wikipedia has this. The flat one is the fastest (proof: as the angle decreases, x velocity increases.)

The only time you might need variable starting speed is if you need to loft just barely over obstacles. Normally, if the low trajectory would hit a wall, you simply switch to the high one. But, say you have a roof. Then I think you could pick a point just above the obstacle, and use the three points (start, obstacle+y, end) to solve for the parabola.

Also, it's a catapult. AFAIK, these things were aimed by dead reckoning and trial&error.

Comment
Add comment · Show 2 · 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 Mendenbarr · Feb 26, 2014 at 09:29 PM 0
Share

If you're firing at max speed Z, you can break down the X and Y components of the velocity to Z*sinθ and Z*cosθ. θ is still an unknown, as it's what I'm trying to solve for, and so is the X and Y component of the velocity. $$anonymous$$y physics is a bit rusty, and I can't think of anyway to take a given target position and max speed and find out the angle of fire required to hit it.

avatar image Owen-Reynolds · Feb 27, 2014 at 02:06 AM 1
Share

Exactly -- theta is the single unknown. You aren't trying to compute the x velocity and the y velocity and the angle. All you need is to get the angle. It's tricky, but is a known problem with known solutions, given speed, distance and change in height.

Or, it you want less math, just beep guessing angles and bracket the solution.

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.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

In what way can I calculate a predicted target destination based on force to be added? 1 Answer

Drawing a Mandelbrot Set problem 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Finding the axis of a curving pipe 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