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 hellojbb1234 · Nov 02, 2017 at 07:52 PM · mathprocedural

I am having trouble with the math for a rogue like dungeon generator

Two rooms keep intersecting and nothing I do to the collision sensor code fixes it.

This is the code for the rooms

 public class DRoom
 {
 
     public Bridge PrimaryBridge;
     public bool Monsters = false;
     public string Build;
     public int Biome;
     public int ActualHeight;
     public int left;
     public int top;
 
     public Node PathWayStart;
     public Node PathWayEnd;
     public int width;
     public int height;
     public int order;
 
     public GameObject TowerVar;
 
     public bool isConnected = false;
     public int right
     {
         get { return left + width + 1; }
     }
     public int bottom
     {
         get { return top + height + 1; }
     }
     public int CenterX
     {
         get { return left + width / 2; }
     }
     public int CenterY
     {
         get { return top + height / 2; }
     }
 
 
     public float Distance(DRoom r2)
     {
 
         float Dist = Vector2.Distance(new Vector2(CenterX, CenterY), new Vector2(r2.CenterX, r2.CenterY));
 
         return Dist;
     }
     public bool CollidesWith(DRoom other)
     {
         if(left > other.right)
         {
             Debug.Log("Same");
         }
        
        
        if(top > other.bottom)
         {
             return false;
         }
       if(right < other.left)
         {
             return false;
         }
         
         if (bottom < other.top)
         {
             Debug.Log("FalseLast  :" + order );
             return false;
         }
 
         Debug.Log(order + " : " + "Ture");
         return true;
     }


Here is some Pseudo Code for two different Rooms

 DRoom 1
 left = 42
 top = 37
 width = 19
 height  =13
 
 DRoom 2
 left = 48
 top = 34
 width = 18
 height = 13
Comment
Add comment · Show 1
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 pako · Nov 04, 2017 at 06:09 PM 0
Share

@hellojbb1234 I tried to figure this out, but unfortunately, I just can't see the logic. I would expect:

 public int bottom
      {
          get { return top - height - 1; } //not plus
      }
 
 and:

      public int CenterY
      {
          get { return top - height / 2; }
      }

Also, I cannot follow the code in CollidesWith e.g.

          if(left > other.right)
          {
              Debug.Log("Same");
          }
 
 //returns true, but it seems that it should return false

         if(top > other.bottom)
          {
              return false; //it seems this should be true
          }


So, it's either I'm right about the above, and that's why you're getting the overlaps, or you are not explaining your generation logic, and it's very hard to see what you mean.

It would help if you posted some more details, an image with clarifications, or anything else that might be helpful.

Your post is very confusing.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MaxGuernseyIII · Nov 04, 2017 at 07:28 PM

So, it seems like what you want is to know whether or not two boxes collide.

Here's a generic solution. You can adapt it into your game and others can adapt it into theirs.

 public struct IntVector2D
 {
   public IntVector2D(int x, int y)
   {
     X = x;
     Y = y;
   }
 
   public int X;
   public int Y;
 }
 
 public class Box
 {
   public IntVector2D TopLeft;
   public IntVector2D Size;
   public IntVector2D BottomRight
   {
     get
     {
       return new IntVector2D(TopLeft.X + Size.X, TopLeft.Y + Size.Y);
     }
   }
 
   public bool HasIntersection(Box other)
   {
     return
       DimensionsOverlap(TopLeft.X, BottomRight.X, other.TopLeft.X, other.BottomRight.X) &&
       DimensionsOverlap(TopLeft.Y, BottomRight.Y, other.TopLeft.Y, other.BottomRight.Y);
   }
 
   bool DimensionsOverlap(int lower1, int upper1, int lower2, int upper2)
   {
     return Math.Min(upper1, upper2) >= Math.Max(lower1, lower2);
   }
 }

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

81 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

Related Questions

Struggling with procedural mesh math 0 Answers

Hint joint how to configure 0 Answers

Cross chunk details 0 Answers

Procedural mesh creation issue 1 Answer

Best Solution For Procedural Match-3 Board? 0 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