Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Yoshinator2 · Jul 22, 2017 at 06:09 AM · mathanglealgorithmcalculationparabola

Math Genius NEEDED! Algorithm problem with parabola

Hi all, I am trying to make a targeting system with an artillery turret in which there is a set distance, velocity, and gravity acceleration. This targeting system is supposed to calculate the angle in which the turret should fire at to hit the target. I am having some trouble as it is either undershooting or overshooting the target, and am wondering if I have the ratios of real world values like Unity velocity to Real World velocity mixed up or if the equation is wrong. Here is my code:

  public class test : MonoBehaviour {
   public float angle;
   public float g = Physics.gravity.y;
   public float v = 1f;
   public float d;
   public Transform firePoint;
   public Transform target;
   public TestAAT3 obj;
   // Use this for initialization
   void Start () {
     obj = FindObjectOfType<TestAAT3>();
 }
 
 // Update is called once per frame
 void Update () {
     if (Input.GetKeyDown(KeyCode.Alpha1))
     {
         CalculateAngle();
     }
 }



 //d = distance between firepoint and target
 //g = gravitational acceleration
 //v = given velocity
 //angle = theta
 public void CalculateAngle()
 {
     d = Vector3.Distance(firePoint.position, target.position);

     angle = (1/2) * Mathf.Asin((g * d) / (Mathf.Pow(v_, 2)));
     Debug.Log(d);
     obj.Fire(d);
 }
 }

Any and ALL help is appreciated. I've looked at wikipedia, khan academy videos, etc. Nothing helps!

Comment
Add comment · Show 1
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 Bobtown · Jul 22, 2017 at 08:12 PM 0
Share

Your angle seems to be calculated correctly, unless the firePoint and target are in different elevations (different z values). The formula you use is correct, but only for objects of the same elevation. $$anonymous$$aybe the problem is with your obj.Fire function?

1 Reply

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

Answer by Bunny83 · Jul 22, 2017 at 08:54 PM

Well first of all "g" should be the value of the acceleration. This should be positive. There is no direction information in a single value. You seem to use the y component of the Physics gravity vector which is usually negative.

Next thing is the formula you use only works when both your firePoint as well as your target are on the exact same height. Also keep in mind when you rely on Unity's physics engine for the actual simulation that this assumes no drag at all.

You also need to check if the term inside ASin is not lower than "-1" or greater than "1", In that case the target can't be reached at all. Also ASin would return a NaN in that case.

If the height of the two points can be different you have to use this one instead. Of course here a similar check has to be done. You can only reach the target when the root has a real value (i.e. the value inside the root has to be 0 or positive). If the root is a complex root the target can't be reached,

Finally what do you actually pass to your "Fire" method? Currently you pass the distance between you and the target. Shouldn't you somehow pass the angle?

ps: Never use "pow" for simple powers. This (Mathf.Pow(v_, 2)) should be simply
(v_ * v_)

Comment
Add comment · Show 5 · 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 ShadyProductions · Jul 22, 2017 at 11:55 PM 0
Share

Is there a disadvantage for using $$anonymous$$athf.Pow(v_, 2) ins$$anonymous$$d of (v_ * v_) for a simple power?

avatar image Bunny83 ShadyProductions · Jul 23, 2017 at 02:42 AM 1
Share

Yes, about a factor of "10". First of all $$anonymous$$athf.Pow is (like almost all methods of $$anonymous$$athf) just a wrapper for System.$$anonymous$$ath.Pow just casted to float. All System.$$anonymous$$ath methods work with double. It's literally defined as:

 public static float Pow(float f, float p)
 {
     return (float)$$anonymous$$ath.Pow((double)f, (double)p);
 }

Though the slight casting overhad is not the worst. A simple native floating point multiplication will be carried out directly inline by FPU opcodes while Pow is an actual method call. The main problem is that Pow does work with non-integer powers as well. That means the method has to decompose the power into an integer part and a fractional part.

I ran a quick test and did 1$$anonymous$$ Pow with "2" as power in a loop and it took 733k ticks on my machine. By doing a simple "manual square" (i.e. "x * x") it took only 66k ticks. It roughly translated to about 70 ms vs 6 ms.

avatar image Bunny83 Bunny83 · Jul 23, 2017 at 02:53 AM 1
Share

ps: decompose means if you have a power of "3.2" it would have to split that power into "3" and "0.2". It can then do a simple integer power with the 3 and in addition calculate the 5th root of the base ((1 / 0.2) --> 5th root). As you might know multiplying two numbers with the same base but different exponent is the same as when you just add the two exponents together. So (B^x * B^y) is the same as B^(x+y).

Show more comments
Show more comments

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

73 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Find angle between two gameobjects? 1 Answer

Angle calculation not working?? 2 Answers

Vertical Angle Between two game objects 2 Answers

Quaternion using global values instead of local. Please help :( 0 Answers

Drawing a Mandelbrot Set problem 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