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 KingNeil · Mar 17, 2016 at 10:28 PM · physicsrigidbodyvelocitymathcalculate

Calculating force

I have two points: A launch point, and a target. I have a script that calculates the force i need to apply to my object, so it lands on my target.

Formula where v0 is initial velocity, R is the range, g is the acceleration of gravity, and θ is the angle.

This works fine, but only if the y-axis of my launch point and target are the same. How can i calculate the force i need, but also being able to hit my target, even if it is above or below me.

Code:`using UnityEngine; using System.Collections;

public class LobbTest : MonoBehaviour {

 public int roundTo;
 public Transform target;

 private Rigidbody rb;
 private float range;
 private float gravity;
 private float angle;
 private float velocity;
 private float angleRad;

 public void Lobb () 
 {
     rb = GetComponent<Rigidbody> ();

     range = target.position.x - transform.position.x;
     Debug.Log ("range = " + range);
     gravity = -Physics.gravity.y;
     Debug.Log ("gravity = " + gravity);
     angle = transform.eulerAngles.z;
     Debug.Log ("angle = " + angle);

     velocity = (Mathf.Sqrt ((range * gravity) / CalculateSine(angle, 2)));


     Debug.Log ("velocty = " + velocity);


     rb.velocity = transform.right * velocity;


 }


 float CalculateSine(float angle, float multiplier)
 {
     float radAngle = angle * Mathf.PI / 180f;
     float radSine = Mathf.Sin (multiplier * radAngle);

     radSine = Round (radSine, roundTo);

     return radSine;
 }



 float Round(float f, int roundTo)
 {
     return Mathf.Round (f * Mathf.Pow(10, roundTo)) / Mathf.Pow(10, roundTo); 
 }`

}

Comment
Add comment · Show 3
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 jebemti · Mar 17, 2016 at 11:03 PM -1
Share

I really doubt there is someone mathematically skilled around here. You'll be better off trying on your own :D

avatar image elenzil · Mar 18, 2016 at 12:10 AM 1
Share

so the launch position and the launch target are fixed, as well as the launch angle, and you need to calculate the launch speed ?

avatar image elenzil · Mar 18, 2016 at 12:53 AM 1
Share

here's my approach.

the key is that you know the formula for the X & Y position of the projectile over time as a function of the initial velocity, and the initial velocity (X & Y) is a function of the angle and the speed. You can use all that to work backwards to get the speed.

alt text

img-8389.jpg (370.5 kB)

1 Reply

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

Answer by elenzil · Mar 18, 2016 at 05:46 AM

yes. slightly different derivation than above, but:

 // speed = +/- sqrt( GX^2 / (2C * (SX - CY)) )
 // where
 // X, Y = location of target
 // C, S = cosine & sine of theta
 // G = gravity

 float speed = float.NaN;

 float C2_times_SX_minus_CY = 2f * C * ((S * X) - (C * Y));
 float G_times_X_SQUARED    = G * X * X;

 if (Mathf.Approximately(C2_times_SX_minus_CY, 0f)) {
   // divide by zero
 }
 else if (C2_times_SX_minus_CY < 0f) {
   // imaginary answer
 }
 else {
   // hokay
   speed = Mathf.Sqrt(G_times_X_SQUARED / C2_times_SX_minus_CY);
 }

you'll need to modify this to deal with the fact that your launch point is not at the origin (or reconfigure scene so that is), and also this formula gives you the launch speed, but you're asking for the launch force. i'm not sure how to compute the force from the speed, but hopefully that's relatively simple. also, if you allow the launch angle to be pointing in the opposite direction from the target, this formula will produce a wrong result, because it's assuming the "+" portion of "+/-" sqrt().

i put up a live demo here

fun stuff !

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 KingNeil · Mar 18, 2016 at 01:44 PM 0
Share

Thank you very much for your help!
Looks like this formula might work, but it gives me the distance, and not the velocity, and i don't know how to reverse it.

Anyways, i will see if i can figure it out.

avatar image KingNeil · Mar 18, 2016 at 03:30 PM 1
Share

This works super well! You can just set the velocity to speed, and it will work! Thank you so much!

avatar image elenzil KingNeil · Mar 18, 2016 at 04:25 PM 0
Share

right on! glad this worked for you.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Rigidbody character controller can't walk on stairs 0 Answers

How to calculate desire angular velocity? 1 Answer

strange unrealisticly physic problems about velocity/gravity acceleration 2 Answers

Character Joints and Animations results in strange physic animation 1 Answer

Gravitational Object Creation for 3D Game 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