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 BluShine · Jul 03, 2015 at 01:53 PM · c#physicsrigidbodyaddforceforce

Get result (force & torque) of AddForceAtPosition?

Here's my scenario:

I've got a hovercraft with four "thrusters" that lift it off the ground. Each thruster uses AddForceAtPosition to push the hovercraft, and the player controls the craft by increasing or decreasing the amount of force that each thruster applies.

I want to be able to automatically make the hovercraft hover to a stop in mid-air. What I want to do is this:

  1. AddForceAtPosition for each thruster.

  2. Figure out the total force and torque being applied by the thrusters, plus the force of gravity.

  3. Slowly apply an opposite force to bring the hovercraft to a halt.

Step 2 is the part that I can't figure out how to do with Unity.

My current work-around gets the velocity, and then applies an opposite force. This almost brings it to a stop, but not quite. When I calculate force from velocity, but the velocity doesn't update immediately when you apply force. This code demonstrates issue is:

 FixedUpdate() {
   Debug.log(rigidB.velocity);                     
   //output: (0, 0, 0)
   rigidB.AddForceAtPosition(new Vector3(100, 0, 0), new Vector3(10, 0, 0));
   Debug.log(rigidB.velocity);                     
   //output: (0, 0, 0)
 }


I wish I could do something like this:

 FixedUpdate() {
   rigidB.AddForce(new Vector3(100, 0, 0));
   rigidB.AddForce(new Vector3(100, 0, 0));
   Debug.log(rigidB.forces);                     
   //output: (200, 0, 0)
 }

But I don't think there's any way to access the current force being applied to a rigidbody.

If I was just using AddForce, I could do this

 FixedUpdate() {
   Debug.log(rigidB.velocity);                     
   //output: (0, 0, 0)
   Vector3 totalForce = Vector3.zero;
   totalForce += new Vector3(100, 0, 0));
   totalForce += new Vector3(100, 0, 0));
   Debug.log(totalForce );                     
   //output: (200, 0, 0)
 }

But that won't work when I use AddForceAtPosition. So what I'm asking for is a "ForceAtPosition to Force" that can be broken-down into force and torque. Something like this:

 ForceAtPosition f = new ForceAtPosition(new Vector3(0, 10, 0), new Vector3(5, 0, 0), rigidB);
 Debug.log(f.force);
 //output: (3.2, 3.2, 0)
 Debug.log(f.torque);
 //output: (5, 0, 0)

So, does anyone know how ForceAtPosition is calculated and turned into a force and a torque?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by markefus · Jul 08, 2015 at 09:55 PM

Rather than trying to nullify the velocity with real physics (this is unfortunately extremely tedious due to race conditions for every frame, as you mentioned), manually set it to zero; gradually.

Here's a quick example:

 //Really can be any number, you can tune the times
 float LERP_TIME = 1.0f;
 
 FixedUpdate()
 {
     if(whatever_condition_to_trigger_hover)
     {
         zeroVelocity();
     }
 }
 
 void zeroVelocity()
 {
     //Current Velocity
     Vector3 v =  GetComponent<Rigidbody>().velocity;
 
     //Gradually set the velocity
     GetComponent<Rigidbody>().velocity = new Vector3(Mathf.Lerp(v.x, 0, LERP_TIME), 
                                           Mathf.Lerp(v.y, 0, LERP_TIME), 
                                           Mathf.Lerp(v.z, 0, LERP_TIME));
 }
 

That's the general idea.

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 BluShine · Jul 09, 2015 at 12:14 AM 0
Share

This doesn't work with gravity, though.

You can try this really easily: create a cube, and attach a script that sets velocity to 0 on FixedUpdate. The cube will slowly fall downwards, because gravity applies a force after FixedUpdate, but before movement/collision simulation.

I could disable gravity, but there's another problem: the thursters on the hovercraft are also keeping the hovercraft in level flight. If I disable gravity and lerp velocity to 0, the hovercraft will still be spinning freely.

To fix that issue, I could just also lerp the rotation towards 0. But this looks ugly and unconvincing. Normally, the hovercraft will level itself with a little bit of swaying, but lerp will just rotate and stop. Also, doing this will cancel out all angular velocity, which looks really weird.

avatar image
1

Answer by PaulMakesThings · Jul 09, 2015 at 06:31 AM

I would suggest using a single application of force and torque to get the effect of the multiple thrusters that you are seeking. The combined force would be the sum of the force of all three thrusters applied to the whole object, the combined torque could be obtained by using the cross product of the offset vector (between the point of application and the center of gravity) and the force vector.

τ(torque)=r⃗ ×F⃗

Calculate these and then apply them with one application. Then you will know what the total is, your physics will be more stable and less CPU consuming, and if you want to apply an opposite force you can subtract it from the vector before you apply it, same goes for damping the torque.

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

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

Why wont my character jump? (using rigidbody and addForce) 1 Answer

Set Velocity at relative position. 2 Answers

add force to object that has 2 different rigid bodies 0 Answers

Why is force only being added in the same direction? 1 Answer

C# script not working 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