• 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 SeaLeviathan · Mar 14, 2021 at 05:22 AM · gridalgorithmfovroguelike

2D grid base Rogue style FOV acting funky

WHAT I WANT: More than anything, is a functioning FOV system for a tile based Rogue game I'm making. The question at hand is what is causing my current algorithm to suck so hard?

THE SETUP:

 void Scan(Row row, Queue<TileLogger> logger)
             {
                 Vector2Int prevTile = new Vector2Int(10000,10000);
                 foreach(Vector2Int tilePos in row.Tiles())
                 {
                     Debug.Log(row.depth);
                     TileLogger newLog = new TileLogger(quadrant.TransformCoords(tilePos), "NotInside",0.5f,row.depth);
                     if(CheckTransform(tilePos, mapCols, mapRows))
                     {
                         newLog.type = $"depth: {newLog.curdepth} status: OnlyInside";
                         if(Mathf.Abs(tilePos.x) > radius || Mathf.Abs(tilePos.y) > radius)
                         {
                             newLog.type = $"depth: {newLog.curdepth} status: OutOfBounds";
                             return;
                         }
                         if(IsWall(tilePos) || IsSymmetric(row, tilePos))
                         {
                             Reveal(tilePos);
                             newLog.type = $"depth: {newLog.curdepth} status: WallOrSymmetric";
                             newLog.lightLevel = 1f;
                         }
                         if(IsWall(prevTile) && IsFloor(tilePos))
                         {
                             row.startSlope = Slope(tilePos);
                             newLog.type = $"depth: {newLog.curdepth} status: LastTileWallThisIsFloor. setting row.startslope to {row.startSlope}";
                         }
                         if(IsFloor(prevTile) && IsWall(tilePos))
                         {
                             Row nextRow = row.Next();
                             nextRow.endSlope = Slope(tilePos);
                             newLog.type = 
                             $"depth: {newLog.curdepth} status: LastTileFloorThisIsWall. nextRow.endSlope = {nextRow.endSlope}, begin scan next row.";
                             Scan(nextRow, logger);
                         }
                         prevTile = tilePos;
                     }
                     logger.Enqueue(newLog);
                 }
                 logger.Enqueue(new TileLogger(Vector2Int.zero, 
                 $"depth: {row.depth} Nontile event: Row finished, scanning next row. Else, returning up a row.",0.25f,row.depth));
                 if(IsFloor(prevTile))
                 {
                     Scan(row.Next(),logger);
                 }
             }

(Please excuse the logging code, it makes it all a bit messy) There is a lot of code to my current implementation, and overall, its essentially a translation of the following: https://www.albertford.com/shadowcasting/ I followed the above and implemented a version in python that works beautifully, and as such I thought if I just converted it to c# for unity it'd work great. There is however, one big problem im encountering right now, and I have no idea why.

THE PROBLEM: alt text

alt text

It should scan left to right, and there are ONLY TWO reasons it should change to the next row for scanning:

A) The current scanned tile is a wall tile, and the last scanned one is a floor tile

B) The row has reached capacity for the quadrant

But for some reason, my code skips from revealing the first tile in the second row, straight to the first tile of the third row. I have no idea how this is even possible. If anybody has any further help or questions relating to the code, please feel free to ask. Im also open to an entirely new approach, but so far this symmetric shadowcasting has been the best looking one. I will also paste some of the methods used in this scan.

 Quadrant quadrant = new Quadrant(direction, origin);
 
             void Reveal(Vector2Int tilePos)
             {
                 Vector2Int transformedTilePos = quadrant.TransformCoords(tilePos);
                 SetVisible(transformedTilePos, visibilityQueue, map2D);
             }
 
             bool IsWall(Vector2Int tilePos)
             {
                 if(tilePos == new Vector2Int(10000,10000))
                 {
                     return false;
                 }
                 Vector2Int transformedTilePos = quadrant.TransformCoords(tilePos);
                 return IsBlocking(transformedTilePos, map2D);
             }
 
             bool IsFloor(Vector2Int tilePos)
             {
                 if(tilePos == new Vector2Int(10000,10000))
                 {
                     return false;
                 }
                 Vector2Int transformedTilePos = quadrant.TransformCoords(tilePos);
                 return !IsBlocking(transformedTilePos, map2D);
             }
 
             bool CheckTransform(Vector2Int tilePos,int xLength, int yLength)
             {
                 Vector2Int transformedTilePos = quadrant.TransformCoords(tilePos);
                 bool viable = true;
                 if(transformedTilePos.x > xLength)
                     viable = false;
                 if(transformedTilePos.x < 0)
                     viable = false;
                 if(transformedTilePos.y > yLength)
                     viable = false;
                 if(transformedTilePos.y < 0)
                     viable = false;
                 return viable;
             }

probpart1.png (58.8 kB)
problempart2.png (1.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
0
Best Answer

Answer by SeaLeviathan · Mar 17, 2021 at 06:51 PM

Oy vey, I figured it out now. It just does things a little out of order, and I fixed my whole algorithm by floatcasting my integer divides for view slope!!! I missed that bit, but it works now so sweet. If you're a curious passerby, @ me if you want the working algorithm.

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

115 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

Related Questions

Algorithm for Finding Shapes on a Grid 1 Answer

Tileset, large maps and game object 0 Answers

Improving Grid Algorithm 1 Answer

Grid Shape Detection 3 Answers

Texture grid displayed oddly when width =/= height 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