Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
2
Question by ReedRGale · May 07, 2018 at 11:04 AM · physics2dcollider2dcontacts

Collider2D.GetContacts() Checking One Physics Step Behind

Hello,

First question here and I can't really find anything answering this specific question. I've tried to collect all the information I can about this that might be relevant and if I'm missing anything, please let me know.

Onto the question:

Is Collider2D.GetContacts() one physics step behind the Collider2D? It seems like weird behavior and I'm wondering if something I'm doing is causing this to happen.

I am trying to glean contact information by checking the collision points contained by the Collider2D itself...

 // Get collider. We can promise that there's only one collider per unit.
             Collider2D[] colliders = new Collider2D[1];
             if (rb2d.GetAttachedColliders(colliders) != 1)
                 throw new System.InvalidOperationException();
             Collider2D collider = colliders[0];
     
             // DEBUG
             Debug.DrawLine(collider.bounds.max, collider.bounds.min, Color.cyan, 5f);
             Debug.DrawRay(collider.bounds.center, Vector2.down, Color.red, 5f);
     
             // Collect each collision point on the collider.
             ContactPoint2D[] contacts = new ContactPoint2D[20];
             int contactCount = collider.GetContacts(contacts);
     
             // Convert it to a list.
             List<ContactPoint2D> contactList = new List<ContactPoint2D>();
             for (int i = 0; i < contactCount; i++)
                 contactList.Add(contacts[i]);
     
             // DEBUG
             foreach (ContactPoint2D c in contactList) Debug.DrawRay(c.point, Vector2.up, Color.blue, 5f);
     
             return contactList;

...then using that information to:

  • ...detect the angle relative to the CollisionPoint2D

    // Assume colliding with ground initially. downToCollisionAngle = 0;

                   // Iterate through all contacts and set the collision angle to the greatest value.
                     List<ContactPoint2D> contactList = GetContactPoints();
                     foreach (ContactPoint2D c in contactList)
                     {
                         Vector2 length = c.point - prevPosition;
                         float angle = Vector2.Angle(length, Vector2.down);
                         downToCollisionAngle = angle > downToCollisionAngle ? angle : downToCollisionAngle;
    
    
  • ...detect whether the point is within additional bounding conditions for grounded

    // Presume not grounded upon checking. isGrounded = false;

                   // Iterate through all contacts and check for grounded state.
                     List<ContactPoint2D> contactList = GetContactPoints();
                     foreach (ContactPoint2D c in contactList)
                         if (c.point.y < prevPosition.y &&
                             c.point.x < prevPosition.x + (LESS_THAN_HALF * width) &&
                             c.point.x > prevPosition.x - (LESS_THAN_HALF * width))
                             isGrounded = true;
     
    

This seems to work... provided that the contacts given are in the same physics step as the Collider2D. I'd expect this to be a given. However, I kept running into odd behavior, and lo and behold, throwing some debug lines in...

alt text

Red: Center of the Collider2D, as gleaned from Debug.DrawLine() in GetContactPoints() Cyan: Cross-Sectional of Collider2D, as gleaned from Debug.DrawLine() in GetContactPoints() Blue: Detected Contact Point of Collider2D as gleaned from GetContacts() in GetContactPoints()

...You can see that the Blue contact points always line up with the previously checked center. (Note that these are called within OnCollisionStay2D, so if that's having an effect let me know.)

In the code, I am recording the previous Rigidbody's position to contrast, and the center lines up perfectly with the current contact, which would make sense.

This suggests to me that the contacts gleaned from GetContacts(), for whatever reason, is one physics step behind the Collider2D itself. Is there something I'm doing that's causing this? Is there a setting I haven't checked?

The Rigidbody2D is currently in continuous mode, if that helps.

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 Harinezumi · May 07, 2018 at 11:58 AM 0
Share

Interesting issue, and it certainly is not consistent behaviour. I know it's not a solution, but maybe decreasing the physics time step (like in this answer) could mitigate the issue?

avatar image ReedRGale Harinezumi · May 07, 2018 at 01:28 PM 1
Share

Changed the timestep, but the issue persists, just with smaller intervals.

alt text

At least it makes sense. And kind of looks pretty.

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

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

Related Questions

How do i stop rigidbody2D bounce 1 Answer

Where should I call Physics2D.Raycast ? 0 Answers

OnTriggerEnter2D working, OnColliderEnter2D not working 1 Answer

Ignore collision before instantiating 1 Answer

Problem with colliders in topdown 2d scene with many floors 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