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 mhughson · Jun 27, 2016 at 12:38 AM · collisionphysicscollision detectionmath

How to calculate magnitude when resolving collision events?

I am trying to write my own 3D collision resolving logic. I want to write this myself for learning purposes.

 void OnCollisionStay(Collision col)
 {
          Vector3 HitVector = transform.position - col.contacts[0].thisCollider.ClosestPointOnBounds(transform.position);
         
         transform.Translate(col.contacts[0].normal * CurrentSpeed, Space.World);
 }

It kind of works. The object hitting the wall is pushed back by the normal of the thing it hit, but the distance it is moving is wrong, causing jerky movement:

alt text

alt text

The multicolored line is the history of positions of the car driving North. You can see it zig zags a bit as it is pushed off the wall further than its collision bounds, causing it drive forward for a few frames before hitting the wall again.

What is the correct way to calculate the offset require the put the collider against the edge of the wall?

untitled.png (68.7 kB)
untitled.png (387.6 kB)
Comment
Add comment · Show 1
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 JedBeryll · Jun 27, 2016 at 06:10 AM 0
Share

It's a pretty complex thing as you can see it in Unity they added physic materials. You need to consider friction and bounciness too. In a collision like this, friction would probably make the car turn around (and maybe flip over because the ground has friction too)

1 Reply

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

Answer by fafase · Jun 27, 2016 at 06:52 AM

You may be aiming a bit high with this. Consider the physics engine was developed by hundreds of devs with high level degrees and experience.

But for the sake of learning, you could use the dot product of the direction of the car and the normal of the hit. The dot of two perpendicular vectors will return 0, so if forward and normal are perpendicular, aka the car is hitting on the side and should not bounce too much, then your value is multiplied by something close to 0 and affect just a little:

  void OnCollisionStay(Collision col)
  {
           float dot = Mathf.Abs(Vector3.Dot(transform.forward, col.contacts[0].normal));    
           transform.Translate(col.contacts[0].normal * CurrentSpeed * dot, Space.World);
  }
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 Bunny83 · Jun 27, 2016 at 11:26 AM 1
Share

You probably should invert one of the vectors in your Dot product as they usually point in the opposite direction. So "dot" will have a negative value and in result you're pushing the object further into the wall.

avatar image fafase Bunny83 · Jun 27, 2016 at 05:35 PM 1
Share

Shouldn't $$anonymous$$athf.Abs take care of that?

avatar image mhughson · Jun 28, 2016 at 05:44 AM 0
Share

That worked great @fafase!

I'm not trying to create the physics system from scratch or anything like that. Just learning some 3D math through program$$anonymous$$g games :)

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

81 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Continuous collision detection - unnatural behaviour 0 Answers

Every time my character hits or touches the walls he gets stuck or acts as if the object the character hit was a magnet. 0 Answers

BoxCollider get stuck when dragging over the border of two BoxColliders 1 Answer

OnCollisionEnter not working with CharacterController 1 Answer

Detect collider collision ,identify and access colliding colliders 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