• 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 BigRoy · Apr 08, 2014 at 09:04 PM · rigidbodybouncefriction

RigidBody2D trap inside a circle

I'm trying to trap objects (with RigidBody2D) inside a circle and use the object's physics material to calculate bouncing/friction with the circle's border.. Yet I can't figure out how to get less of a bouncy effect but somet$$anonymous$$ng that transfers the velocity into velocity along the surface (that takes into account t$$anonymous$$s friction/bounciness).

T$$anonymous$$s is basically my current solution:

 // offset = vector from center to current position
 // distanceSqr = distance from center squared
 // radius = radius of the sphere object is trapped in.
 if (distanceSqr > radius * radius)
 {
     // Reflect only if velocity is outwards
     float dot = Vector2.Dot(offset.normalized, m_rigidBody.velocity.normalized);
     if (dot > 0.0f)
     {
         // Reflect (Bounce)
         m_rigidBody.velocity = Vector3.Reflect(m_rigidBody.velocity, -offset.normalized);
         // Drag
         m_rigidBody.velocity = m_rigidBody.velocity * (1.0f - (drag));
     }

     // Trap Position inside
     m_transform.position = center + (offset.normalized * radius); // maximum distance (trap inside)
 }

Another t$$anonymous$$ng I've tried is to remove all outwards velocity of the rigidBody. But as long as it moves slightly to the side it comes to a halt very quickly because then it removes the upwards vector in sublty different directions and rather soon it removed all of it. :)

Is there a way to figure out how to calculate a similar resulting velocity vector as it would've had if moved up against a point with a normal of an object (w$$anonymous$$ch is the information I have) of a regular RigidBody2D object?

I would love to be able to control the friction and bounciness just like with the Physics2d Material.

(Pointers on how t$$anonymous$$s works in 3D space to calculate it nicely would - I t$$anonymous$$nk - also be good enough to point me in the direction of how to solve t$$anonymous$$s)

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 BigRoy · Apr 09, 2014 at 08:49 PM

So I've found a workaround that works very nicely. :)

Instead of figuring out my own physics I wanted to rather rely mostly on the PhysX engine that is already implemented in Unity (so it works with basically all of rigidBody stuff I throw at it).

The solution I thought of was using the EdgeCollider2D that just has points laid out in a circular fas$$anonymous$$on! And it seems to work beautifully.

For anyone else that wants to do somet$$anonymous$$ng similar and needs a $$anonymous$$gh quality one here's a small script I quickly put together. It's an editor script that creates a small window that allows you to apply/set EdgeCollider2D to selected gameObjects where it lays out the points automatically in a circular fas$$anonymous$$on. :) The window allows you to set how many vertices you want to have in the final circle.

 using UnityEditor;
 using UnityEngine;

 public class CircleEdgeCollider : EditorWindow
 {
     float radius = 1.0f;
     int vertices = 30;

     [MenuItem("Scripts/Colliders/CircleEdgeCollider")]
     public static void ShowWindow()
     {
         //Show existing window instance. If one doesn't exist, make one.
         EditorWindow.GetWindow(typeof(CircleEdgeCollider));
     }

     void OnGUI()
     {
         radius = EditorGUILayout.FloatField("Radius", radius);
         vertices = EditorGUILayout.IntField("Vertices", vertices);
         
         if (GUILayout.Button("Add CircleEdgeCollider")) t$$anonymous$$s.AddCircleEdgeCollider();
     
     }

     void AddCircleEdgeCollider()
     {
         // Get selection
         GameObject[] sel;
         sel = Selection.gameObjects;
         if (sel.Length == 0) return;

         if (vertices < 2){
             Debug.LogError("Can't create a circle edge collider with less than 2 points.");
             return;
         }

         // Create list of points
         Vector2[] pts = new Vector2[vertices];
         for(int i=0; i<vertices; i++)
         {
             // Points on a circle
             Vector2 pt;
             pt.x = Mathf.Cos((i / ((float)vertices - 1.0f)) * (2f * Mathf.PI)) * radius;
             pt.y = Mathf.Sin((i / ((float)vertices - 1.0f)) * (2f * Mathf.PI)) * radius;
             pts[i] = pt;
         }
             
         GameObject obj;
         for (int i = 0; i < sel.Length; i++)
         {
             obj = sel[i];
             Debug.Log(obj.name);

             EdgeCollider2D coll = obj.GetComponent<EdgeCollider2D>();
             if (coll == null)
             {
                 coll = obj.AddComponent<EdgeCollider2D>();
             }

             coll.points = pts;

         }
     }
 }

Oh yeah, t$$anonymous$$s is my first ever Editor script. ;) But I hope t$$anonymous$$s workaround can be useful to someone else as well.

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Re-Creating Unity's Friction and Bounce 0 Answers

Why Rigidbody collide with wall passes through partially? 0 Answers

How to make objects non-penetrable, inelastic 0 Answers

Player bouncing off issue 1 Answer

Rigidbody Bouncing Issue 2 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