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 dustxx · Jan 08, 2018 at 07:24 PM · movementmathgrid

How can I calculate a list of points where a line intersects a grid?

I am making a somewhat unusual character controller that requires me to do some strange math. Basically, I want to use the line drawn below in red to calculate a list of the points where the line intersects grid lines. Any hints as to what sort of approach I should use to accomplish this?

alt text

movement2.png (81.8 kB)
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
3
Best Answer

Answer by Bunny83 · Jan 08, 2018 at 09:26 PM

Well there are a lot of unknown left over. I assume that we talk about a 2d problem here, right?


You should notice that there are two different kinds of intersections with the grid:

  • intersections with horizontal grid lines

  • intersections with vertical grid lines


Those two has to be handled seperately. Lets further assume the line is defined by the starting point at the lower left and a direction vector. Also the grid passes through the origin and has a "stepsize" of "G".


To find the x positions of the intersections with the vertical lines you start at the starting point and if the direction goes to the right you round the staring position up to the next higher grid position. If the direction goes to the left you find the next lower grid position. For this you just divide the x staring position by "G" and either Ceil or Floor the value. Multiply the result by G again and you get the x coordinate of an intersetion. To get the y position of an intersection we just use the slope of our line equation. Now just increase / decrease "x" by "G" to get the next vertical gridline. Continue as far as you need.


The same thing can be done for the horizontal grid lines. We just determine the y coordinate first and use the line equation to get the x coordinate. Finally you may want to sort all the points based on the distance from the starting point.


It's a tedious process with several cases but nothing too complicated.


edit

If you have trouble understanding what i said, here's a method that does what you want based on the assumptions i made above:

 public static void FindGridIntersections(Vector2 aLineStart, Vector2 aDir, float aDistance, Vector2 aGridSize, ref List<Vector2> aResult)
 {
     aResult.Clear();
     aDistance *= aDistance;

     // vertical grid lines
     float x = aLineStart.x / aGridSize.x;
     if (aDir.x > 0.0001f)
     {
         Vector2 v = new Vector2((Mathf.Ceil(x) * aGridSize.x) - aLineStart.x, 0f);
         v.y = (v.x / aDir.x) * aDir.y;
         while (v.sqrMagnitude < aDistance)
         {
             aResult.Add(v);
             v.x += aGridSize.x;
             v.y = (v.x / aDir.x) * aDir.y;
         }
     }
     else if (aDir.x < -0.0001f)
     {
         Vector2 v = new Vector2((Mathf.Floor(x) * aGridSize.x) - aLineStart.x, 0f);
         v.y = (v.x / aDir.x) * aDir.y;
         while (v.sqrMagnitude < aDistance)
         {
             aResult.Add(v);
             v.x -= aGridSize.x;
             v.y = (v.x / aDir.x) * aDir.y;
         }
     }

     // horizontal grid lines
     float y = aLineStart.y / aGridSize.y;
     if (aDir.y > 0.0001f)
     {
         Vector2 v = new Vector2(0f,(Mathf.Ceil(y) * aGridSize.y) - aLineStart.y);
         v.x = (v.y / aDir.y) * aDir.x;
         while (v.sqrMagnitude < aDistance)
         {
             aResult.Add(v);
             v.y += aGridSize.y;
             v.x = (v.y / aDir.y) * aDir.x;
         }
     }
     else if (aDir.y < -0.0001f)
     {
         Vector2 v = new Vector2(0f, (Mathf.Floor(y) * aGridSize.y) - aLineStart.y);
         v.x = (v.y / aDir.y) * aDir.x;
         while (v.sqrMagnitude < aDistance)
         {
             aResult.Add(v);
             v.y -= aGridSize.y;
             v.x = (v.y / aDir.y) * aDir.x;
         }
     }
     aResult.Sort((a,b)=> a.sqrMagnitude.CompareTo(b.sqrMagnitude));
 }


You have to pass a List of Vector2 which will be filled by the method. The points in the list are relative to the starting point. So to get the actual final point you have to add "aLineStart" to each point.

Here's another implementation that uses a "Ray" as line definition. So the ray origin is the same as aLineStart and the ray direction is the same as aDir. Instead of returning Vector2s we just return the distance along the ray so we can simply use ray.GetPoint with that distance to get the actual point:

 public static void FindGridIntersections(Ray aRay, float aDistance, Vector2 aGridSize, ref List<float> aResult)
 {
     aResult.Clear();
     Vector2 pos = aRay.origin;
     Vector2 dir = aRay.direction;
     // vertical grid lines
     float x = pos.x / aGridSize.x;
     if (dir.x > 0.0001f)
     {
         x = (Mathf.Ceil(x) * aGridSize.x) - pos.x;
         float v = x / dir.x;
         while (v < aDistance)
         {
             aResult.Add(v);
             x += aGridSize.x;
             v = x / dir.x;
         }
     }
     else if (dir.x < -0.0001f)
     {
         x = (Mathf.Floor(x) * aGridSize.x) - pos.x;
         float v = x / dir.x;
         while (v < aDistance)
         {
             aResult.Add(v);
             x -= aGridSize.x;
             v = x / dir.x;
         }
     }

     // horizontal grid lines
     float y = pos.y / aGridSize.y;
     if (dir.y > 0.0001f)
     {
         y = (Mathf.Ceil(y) * aGridSize.y) - pos.y;
         float v = y / dir.y;
         while (v < aDistance)
         {
             aResult.Add(v);
             y += aGridSize.y;
             v = y / dir.y;
         }
     }
     else if (dir.y < -0.0001f)
     {
         y = (Mathf.Floor(y) * aGridSize.y) - pos.y;
         float v = y / dir.y;
         while (v < aDistance)
         {
             aResult.Add(v);
             y -= aGridSize.y;
             v = y / dir.y;
         }
     }
     aResult.Sort((a, b) => a.CompareTo(b));
 }


Example:

 List<float> points = new List<float>();
 
 Ray ray = new Ray(pos, dir);
 FindGridIntersections(ray, 10f, new Vector2(1f, 1f), ref points);
 foreach(float dist in points)
 {
     Vector2 p = ray.GetPoint(dist);
     // do something with p
 }

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

118 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 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

Implement the equation as a code? 1 Answer

How to Get Velocity Based Movement With Character Controller 0 Answers

3D grid movement, like in fire emblem radiant dawn 0 Answers

Finding the axis of a curving pipe 0 Answers

Hexagon-Grid Distance 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