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 Alsun · Mar 31, 2012 at 12:03 PM · 2dcollisionphysics

Smooth box to curve collision in 2D

I'm starting my game development trials with a top-down, tile-based 2D racing game like the old Super Sprint.

I've mapped a car sprite onto a box (which rotates and moves forward/back based on input), and made 2 different track planes, one straight and one corner. The corner tile is a curve, basically one quarter of a circle. I'm having trouble confining the car to the inside of the curve.

I am doing the following.. If the car is over a corner tile, I get the origin of the circle, which is the corner of the tile in world space based on the rotation of the corner (so the north-east corner has the bottom left corner as the origin). For each of the corners of the rotated car, I check which is the furthest away from the origin. I then check if the distance of that corner from the origin is larger than the radius of the circle. If so, I want to move the box back towards the origin by the amount it exceeds the radius, so that it stays inside the curve.

This works to some extent, but every time I trigger the collision code, the car jerks all over the place, sometimes even getting stuck or jumping outside the tile. I don't know how to make it smoothly traverse the inside of the curve.

Here is the relavant code.

Inside the car's FixedUpdate() is this:

 if (roadtile.IsCorner())
 {
     // Get the corner of the tile that is the origin of the circle
     Vector2 origin = roadtile.GetOrigin();
     // The car box is 1x1, so the top right corner in local space is at (0.5, 0.5)
     Vector2 topright = new Vector2(0.5f, 0.5f);
     // Cheap way of getting the 4 corners in world space
     Vector2[] corners = {
         new Vector2(transform.TransformPoint(-topright.x, +topright.y, 0).x, transform.TransformPoint(-topright.x, +topright.y, 0).y), // top left
         new Vector2(transform.TransformPoint(+topright.x, +topright.y, 0).x, transform.TransformPoint(+topright.x, +topright.y, 0).y), // top right
         new Vector2(transform.TransformPoint(+topright.x, -topright.y, 0).x, transform.TransformPoint(+topright.x, -topright.y, 0).y), // bottom right
         new Vector2(transform.TransformPoint(-topright.x, -topright.y, 0).x, transform.TransformPoint(-topright.x, -topright.y, 0).y)  // bottom left
     };
     Vector2 furthestPoint = origin;
     float distance = 0;
     // Check all 4 corners for the furthest from the origin
     foreach (Vector2 corner in corners)
     {
         if (Vector2.Distance(origin, corner) > distance)
         {
             distance = Vector2.Distance(origin, corner);
             furthestPoint = corner;
         }
     }
     // Now check if the furthest point is outside the circle
     if (distance > roadtile.GetRadius())
     {
         // Calculate the angle and distance to move
         float angle = Vector2.Angle(origin, furthestPoint);
         float amount = distance - roadtile.GetRadius();
         Vector2 moveVector = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle));
         moveVector *= amount;
         // Move the car
         transform.position = new Vector3(transform.position.x + moveVector.x, transform.position.y + moveVector.y, transform.position.z);
     }
 }
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 Owen-Reynolds · Mar 31, 2012 at 05:28 PM

Vector2.Angle returns degrees (odd, but Vector2.Angle(Vector2.right, Vector2.up) is 90, not PI/2) and Cos/Sin want radians.

Another trick for "pulling in", which doesn't need any angles, is to scale a line segment by the percentage you're off:

 Vector2 toCarCorner = furthestPoint - origin;
 float dist = ...
 if(dist too big)
   // find the point on circle nearest carCorner by scaling toCarCorner down:
   float scaleFactor = roadTile.GetRadius/dist; // will be <1
   Vector2 nearestPointOnCircle = toCarCorner*scaleFactor;
   // this will be the same angle, but on the circle
 
   Vector2 moveVector=nearestPointOnCircle-furthestPoint;
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
avatar image Alsun · Mar 31, 2012 at 06:13 PM 0
Share

Awesome! Dividing the angle by (180/PI) worked, but I'm using your alternate vector math suggestion (except the last line should have nearestPointOnCircle - toCarCorner). Thanks Owen!

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

Is there a solution to when colliders bypass? 2 Answers

Prefab instantiated by code, collision does not work 0 Answers

Unity 4.3 CharacterController collision with physics 2D not working 1 Answer

2D Collider aren't exact? 6 Answers

Script calling onCollisionEnter2D and onTriggerEnter2D while disabled 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