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 destructivArts · May 29, 2014 at 10:32 PM · c#vector3collision-detectioncollisionhandling

Collision Response Algorithm for 3D Axis-Aligned Box Collider

Hello!

I'm writing a game that doesn't need complex physics. For that reason I am giving any object that needs physics enacted on it a box collider and a rigidbody with all constraints turned on. This way it still fires the onCollisionEnter and onCollisionStay methods which give much more information about collisions than the onTrigger equivalents.

The problem I'm having is with how I'm handling the collisions. I'll write the method out in English and include the relevant code just to cover everything. What happens when I'm using this algorithm is that the player kind of bounces back and forth just overlapping the object its colliding with. What I want to happen is for the collision handling to prevent any overlap.

My movement algorithm is: In the update function

  1. Based on player input, set a movement vector.

  2. Based on collisions modify the movement vector

  3. Apply the movement vector to the transform of the player object

  4. Clear all logged collisions

OnCollisionEnter and OnCollisionStay

  1. Add the collision object to an arrayList of collision objects

My collision handling (#2) works like this:

  1. Get the current center of the collider (collider.center + collider.transform)

  2. Get the distance to the collision and store it in a Vector3

  3. Scale the distanceToCollision vector by the collision normal (leaves just the distance to the collision along the direction of the collision)

  4. Get the distance to the edge of the collider closest to the collision (Take a vector3 containing half the width, height and depth, multiply it by the collision normal)

  5. If the distanceToCollision (#3) magnitude is less or equal to the distanceToEdge (#4) magnitude (ie. the player collider is inside the other collider) take the difference, and create a vector of that distance along the normal.

  6. Apply this new vector to the movement vector (from the update function. (The movement vector is then applied to the transform of the object)

Ok and here's the code:

 void Update(){
 //Set Input Velocity
         if (Input.GetKey (KeyCode.W))
         {
             velocity += (Vector3.forward * speed * Time.deltaTime);
         }
         if (Input.GetKey (KeyCode.S))
         {
             velocity += (Vector3.forward * -speed * Time.deltaTime);
         }
         if (Input.GetKey (KeyCode.A))
         {
             velocity += (Vector3.right * -speed * Time.deltaTime);
         }
         if (Input.GetKey (KeyCode.D))
         {
             velocity += (Vector3.right * speed * Time.deltaTime);
         }
 
         //Rectify Velocity with any collisions
         for (int i=0;i<collisionLog.Count;i++){
             HandleCollision((Collision)(collisionLog[i]));
         }
 
         //Move Entity
         transform.Translate (velocity);
 }


 void HandleCollision(Collision col){
         Vector3 correctiveMoveVector = Vector3.zero;
         
         Vector3 colliderCenterPos = collider.center + collider.transform.position;
         
         //1. Determine Distance To Collision
         Vector3 distToCollision = new Vector3(col.contacts[0].point.x - colliderCenterPos.x,
                                               col.contacts[0].point.y - colliderCenterPos.y,
                                               col.contacts[0].point.z - colliderCenterPos.z);
         
         distToCollision = Vector3.Scale(distToCollision, col.contacts[0].normal);
         
         Vector3 distToEdgeAlongColNormal = Vector3.Scale (col.contacts[0].normal, distToEdges);
 
         //Debug.Break();
         
         if (distToCollision.magnitude <= distToEdgeAlongColNormal.magnitude){
             float backtrackDist = distToEdgeAlongColNormal.magnitude - distToCollision.magnitude;
             Vector3 backtrackVector = new Vector3(backtrackDist,backtrackDist,backtrackDist);
             backtrackVector = Vector3.Scale(backtrackVector, col.contacts[0].normal);
             correctiveMoveVector += backtrackVector;
         }
         
         velocity += correctiveMoveVector;
     }

 void OnCollisionEnter(Collision col){
         LogCollision(col);
     }
     void OnCollisionStay(Collision col){
         LogCollision(col);
     }
 
     void LogCollision(Collision col){
         collisionLog.Add(col);
     }
 
     void resetCollisionLog(){
         collisionLog.Clear();
     }
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

1 Reply

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

Answer by destructivArts · May 31, 2014 at 04:34 AM

I realized that this was an order of operations problem. I hadn't accounted for what Unity was doing outside of the update loop.

In effect I was:

  1. Taking Input

  2. Resolving Collisions

  3. Moving the Entity

  4. Checking for Collisions

What I ended up doing was taking the input commands and storing the result in a moveVector, doing my own collision testing with a rigidbody.sweeptest and then resolving the collision it returned, and applying the needed change to the moveVector before finally applying that moveVector to the player object.

Thanks for being here! A lot of the time all I need is to say the problem out loud or write it down to figure out whats wrong.

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

20 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to Check if any object is at an specific Vector3? 1 Answer

Writing Policy class for handling Vector2s and 3s. 1 Answer

How can i change a childs local position with a variable? 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