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 Karsnen_2 · Dec 05, 2011 at 03:06 AM · javascriptmathprojectilemathfformula

Distance in a Trajectory of a projectile

Hello,

I am trying to implement the below formula.

Distance

In the equations on this page, the following variables will be used: g: the gravitational acceleration—usually taken to be 9.81 m/s2 near the Earth's surface θ: the angle at which the projectile is launched v: the velocity at which the projectile is launched y0: the initial height of the projectile d: the total horizontal distance traveled by the projectile

Code:

I scripted that formula as

 distance = (((cannonScript.velocity*Mathf.Cos(cannonScript.angle))/gravity)*(cannonScript.velocity*Mathf.Sin(cannonScript.angle))+(Mathf.Sqrt(Mathf.Pow(cannonScript.velocity*Mathf.Sin(cannonScript.angle),2)+(2*gravity*ini_height))));

Code Explanation:

I have just parsed the formula using a machine readable, Javascript. I have used Mathf functions from the engine. I have double checked for the closing and opening braces. I have done O/P the result, distance in the console window. The angle is degrees and I think I just receive it as a float value and pass it. I hope it is degrees in the right way. The initial height is 0 and gravity is 9.81.

GameObjects:

I have placed this script on a turret model from which the cannon prefab is launched every time.

Problem:

I get even negative values in the console window. For example if the angle is 31 and velocity is 11 my value (distance) is -0.1142434. I think it makes no sense.

I/P Variables:

My usage of angle and initial velocity are given by the following script.

http://codepad.org/bTUVFdc3

I am using the same angle and velocity.

Request:

My humble request is could someone help me in this. I have been working & not sure where I am wrong. I do also believe that all the values I have given as an input correspond to their respective real world values (metrics).

THANK you in advance

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

1 Reply

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

Answer by syclamoth · Dec 05, 2011 at 04:11 AM

How about this- try expanding that out into about 5 lines to see everything more clearly, and then make it one line again when it's all working properly. While it's shorter in code to compress mathematical functions that way, it's way harder to troubleshoot!

A few things to look out for- Mathf.Sin and Mathf.Cos take radians- use Mathf.Deg2Rad to fix the values before passing them into those functions.

So, an expanded version would go like this-

 var vCosTheta : float = (cannonScript.velocity * Mathf.Cos(cannonScript.angle * Mathf.Deg2Rad));
 var firstPart : float = vCosTheta / gravity;

 var 2gyo : float = 2 * gravity * ini_height;
 var vSinTheta : float = cannonScript.velocity * Mathf.Sin(cannonScript.angle * Mathf.Deg2Rad);
 var bitsUnderRoot : float = Mathf.Pow(vSinTheta, 2) + 2gyo;
 var secondPart : float = vSinTheta + Mathf.Sqrt(bitsUnderRoot);

 var result : float = firstPart * secondPart;

 distance = result;
Comment
Add comment · Show 3 · 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 Karsnen_2 · Dec 05, 2011 at 04:29 AM 0
Share

Wow. I got it. I never converted to Radians. The code now looks neat. I hope that I split big statements in the future. Could you please enlighten me the purpose of converting to Radians. Thank you.

avatar image syclamoth · Dec 05, 2011 at 05:41 AM 0
Share

Radians make more sense as a unit- and the Sin and Cos functions work with them natively. The conversion from degrees is as simple as multiplying by a constant, but you have to make sure you're aware of what units you are working in when you are doing rotational arithmetic.

avatar image ennnceee · Jul 03, 2014 at 06:14 AM 0
Share

thanks my friend.. i was entering the angle in degrees.. good on ya.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Trajectory of a projectile formula. Does anyone know maths? 5 Answers

Casting-related error: "^ cannot be applied to double and int" 1 Answer

getting the lowest point of a sine wave in javascript 3 Answers

Help with Mathf.PingPong 2 Answers

Shooting Projectiles 1 Answer


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