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
1
Question by Guessmyname · Dec 12, 2013 at 03:24 PM · matharcballistic

Calculating ballistic angle to a target

I'm writing a custom BallisticArc class for... well, dealing with ballistic arcs but I'm running into a snag: one of the constructors takes in a target and a velocity, then calculates the angle to reach it, based off the equation on these two pages:

Ballistic Angle Equation

Which I have implemented as follows:

 public BallsiticArc(Vector2 start, Vector2 target, float velocity){
             float v = velocity;
             float v2 = v*v;
             this.start = start;
             g = Mathf.Abs(Physics.gravity.y);
 
             float x = target.x - start.x;
             flipped = !Sign(x); //returns a bool based on the sign: returns value >= 0
         //this is just for cases where we are aiming 'behind' us
             if(flipped) x = -x; //ensure a positive x value for calculations
             
             float y = target.y - start.y;
 
             angle = Mathf.Atan((v2 + Mathf.Sqrt(v2*v2 - g*(g*x*x + 2*y*v2))) / g*x);
 
             Debug.Log("X: " + x + ", Y: " + y + ", angle: " + angle * Mathf.Rad2Deg);
 
             sinA = Mathf.Sin(angle);
             cosA = Mathf.Cos(angle);
             
             _valid = true;
             launchVelocity = new Vector2(v * cosA, v * sinA);
         }

However, I'm getting completely inaccurate results; the angle drops only when x is extremely close; when x is further away the angle climbs to 90:

x = 6.98, angle = 89.4

x = 0.27, angle = 79.6

I've checked over the equation obsessively in case I got a bracket wrong, and I've tried the alternate form ('v2 - ...' instead of 'v2 + ...') but it just gives similar results. The other constructors work just fine; it must be the angle that's going wrong here. Whatever result it is I'm getting, it seems to have an inverse relationship with the actual angle, but it's not linear.

I'm at my wits end trying to figure this out; the other constructors calculate arcs from the angle and power and from a target and a given angle (so calculated power) - both these work just fine (the drawn debug arcs in the images are from sweeping sphere casts along the arcs, hence the red Xs when it hits something; either way the GetPoint(float time) function works as it should and is extremely simple). Can anyone help?

Comment
Add comment · Show 2
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 MarcioLiao · Jan 30, 2014 at 04:30 PM 0
Share

The X from Vector2 target, is the distance between the target and the shooter? In a 3D perspective

avatar image Guessmyname · Jan 30, 2014 at 06:12 PM 0
Share

It's been written for a 2D context, so the x value is the horizontal distance, yes.

Note this is not the literal distance; that would be the magnitude of the vector (x,y).

For a 3D context, you'd want to calculate the arc in two dimensions along the axis [start -> target], then convert to Vector3 and rotate via $$anonymous$$atrix or Quaternions to be in world 3D coordinates.

1 Reply

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

Answer by TehJustice · Dec 12, 2013 at 08:58 PM

Looks like an order of operations error, your denominator needs to be (g*x) instead of g*x.

Comment
Add comment · Show 1 · 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 Guessmyname · Dec 12, 2013 at 09:00 PM 1
Share

...

Yep, that was exactly it. Damnit and I'd thought I'd tried all the possible bracket combinations too. Please excuse me whilst I find a bag in which to scream...

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

18 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

Related Questions

How to make a circle with three known points 4 Answers

arc object's position using sin 1 Answer

Moving the player in a ballistic trajectory in 3D 0 Answers

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

how to Break a vector3 line into half and Quater 2 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