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 Hanako-Seishin · Feb 21, 2016 at 04:25 PM · physics2djointsforces

Question on 2D joints and reaction force

Hello. I'm new to Unity and for starters making a simple 2D game where a player controls a bat. I don't want it to be too simple though, so I made controls like this: first a sum of external forces safe for drag is calculated, which are gravity and lift, where lift is as simple as k*(horizontal velocity)^2). Then if the input is (0,0), the bat applies a force to compensate these forces, which added with drag makes it stay in place. And if the input is not (0,0), it sees how much force it can spare to fly in the needed direction, added to the compensation force. There's some math there, but it's irrelevant, since the problem I have is with keeping the bat in place. So the impotrant part is this:

 Vector2 externalForces = totalMass * Physics2D.gravity + lift;

where totalMass is just the bat's Rigidbody2D's mass, whuch equals 1. I've also set the gravity to exactly -10 for simplicity of calculation.

It worked perfect for just the bat, but I also want it to be able to pick up and carry objects like stones. So I created a stone object with mass of 0.25 and attached it to the bat with a fixed joint. They stay connected all right, so now I only have to compensate for the stone mass too. For that I set totalMass to 1.25. But that didn't work. With no input the bat slowly goes down, which means actual force pulling it down must be more than just gravity*1.25, so it can't compensate enough, at least that's how I see it. I tried different kinds of joints with different parameters, but the result is the same. Next thing I tries is instead of setting totalMass I used Joint2D.reactionForce. Now my code is like this:

 Vector2 rf = joint.reactionForce;
 Debug.Log (rf);
 Vector2 externalForces = totalMass * Physics2D.gravity + lift - rf;

where totalMass equals 1 and as Debug.Log shows reactionForce returns (0, 2.4) when applying no input (also it's -rf and not +rf because it gives me a force pointing up while it really poins down, maybe it shows the force that affects the stone, not the bat?). And the bat still slowly goes down (must be even slightly faster than before, as it now trying to compensate 2.4 instead of 0.25*gravity which would be 2.5). I tried joint.GetReactionForce(Time.fixedDeltaTime) or joint.GetReactionForce(Time.deltaTime), but the result is the same.

Is there something I am missing? Some other force in action?

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 Hanako-Seishin · Feb 23, 2016 at 09:21 AM 0
Share

Experimenting with this matter futher I've tried something totally random like:

 Vector2 externalForces = total$$anonymous$$ass * Physics2D.gravity + lift - 1.1f * rf;

and, to my surprise, it actually worked. At least as many digits of position.y as Debug.Log gives me stay the same over time. It also works with different masses for both the bat and the stone. The only problem remaining is that sometimes after collisions, reaction force gets stuck on numbers that bigger than just gravity (like (0, 2.6) for a 0.25 massed stone) which makes the bat try to compensate for more than there really is and as a result go up when it is supposed to stay in place. It fixes itself after another more lucky collision where it doesn't get stuck and returns to usual (0, 2.4).

With how surprisingly good it works, It does look like a workaround, and I would still like to know what would be the right solution. And if somehow this IS the right solution, I'd like to know why it is, where does the 1.1 coefficient come from?

avatar image luislodosm · Apr 26, 2018 at 12:37 PM 0
Share

Could you simplify your question?

0 Replies

· Add your reply
  • Sort: 

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

Adding a force to RigidBody2d - Velocity returns 0 1 Answer

Set targetjoint2d anchor position at mouse position in 2d 0 Answers

Set connected Rigidbody 2D to a WheelJoint2D via script. 0 Answers

How to make rigidbody move with another rigidbody respecting physics? 1 Answer

How to prevent colliders from intertwining when using DistanceJoint2D 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