- Home /
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
@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.
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);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612142426im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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