Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dunno if you want my stuff? #11

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 95 additions & 51 deletions Assets/AABB2D.cs
Original file line number Diff line number Diff line change
@@ -1,59 +1,103 @@
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using PlasticGui.WorkspaceWindow.Items;
using Unity.Mathematics;
using UnityEngine;

namespace NativeQuadTree
{
[Serializable]
public struct AABB2D {
public float2 Center;
public float2 Extents;

public float2 Size => Extents * 2;
public float2 Min => Center - Extents;
public float2 Max => Center + Extents;

public AABB2D(float2 center, float2 extents)
{
Center = center;
Extents = extents;
}

public bool Contains(float2 point) {
if (point[0] < Center[0] - Extents[0]) {
return false;
}
if (point[0] > Center[0] + Extents[0]) {
return false;
}

if (point[1] < Center[1] - Extents[1]) {
return false;
}
if (point[1] > Center[1] + Extents[1]) {
return false;
}

return true;
}

public bool Contains(AABB2D b) {
return Contains(b.Center + new float2(-b.Extents.x, -b.Extents.y)) &&
Contains(b.Center + new float2(-b.Extents.x, b.Extents.y)) &&
Contains(b.Center + new float2(b.Extents.x, -b.Extents.y)) &&
Contains(b.Center + new float2(b.Extents.x, b.Extents.y));
}

public bool Intersects(AABB2D b)
{
//bool noOverlap = Min[0] > b.Max[0] ||
// b.Min[0] > Max[0]||
// Min[1] > b.Max[1] ||
// b.Min[1] > Max[1];
[Serializable, DebuggerDisplay("Center: {Center}, Extents: {Extents}")]
public struct AABB2D
{
public float2 Center;
public float2 Extents;

public float2 Size => Extents * 2;
public float2 Min => Center - Extents;
public float2 Max => Center + Extents;

public AABB2D(float2 center, float2 extents)
{
Center = center;
Extents = extents;
}

public AABB2D(RectTransform rect)
{
Center = new float2(rect.position.x, rect.position.y);
Extents = new float2((rect.rect.max - rect.rect.min) / 2f);
}

public bool Contains(float2 point)
{
if(point.x < Center.x - Extents.x)
return false;

if(point.x > Center.x + Extents.x)
return false;

if(point.y < Center.y - Extents.y)
return false;

if(point.y > Center.y + Extents.y)
return false;

return true;
}

public bool Contains(AABB2D b)
{
return Contains(b.Center + -b.Extents) &&
Contains(b.Center + new float2(-b.Extents.x, b.Extents.y)) &&
Contains(b.Center + new float2(b.Extents.x, -b.Extents.y)) &&
Contains(b.Center + b.Extents);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(Circle2D b)
{
if(Contains(b.Center))
{
// inside box
float2 squareEdgePoint = math.clamp(b.Center, Center - Extents, Center + Extents);
float distance = math.distance(squareEdgePoint, Center);

if(distance + b.Radious <= math.max(Extents.x, Extents.y))
{
return true;
}
else
{
// this could mean that the point is in the very corner of the square
float BL = math.distance(b.Center, Center + -Extents);
float TL = math.distance(b.Center, Center + new float2(-Extents.x, Extents.y));
float BR = math.distance(b.Center, Center + new float2(Extents.x, -Extents.y));
float TR = math.distance(b.Center, Center + Extents.x);
float closestCornerDistance = math.min(math.min(BL, TL), math.min(BR, TR));
return closestCornerDistance > b.Radious;
}
}
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Intersects(Circle2D b)
{
return Circle2D.Intersects(this, b);
}

public bool Intersects(AABB2D b)
{
//bool noOverlap = Min[0] > b.Max[0] ||
// b.Min[0] > Max[0]||
// Min[1] > b.Max[1] ||
// b.Min[1] > Max[1];
//
//return !noOverlap;
//return !noOverlap;

return (math.abs(Center[0] - b.Center[0]) < (Extents[0] + b.Extents[0])) &&
(math.abs(Center[1] - b.Center[1]) < (Extents[1] + b.Extents[1]));
}
}
return (math.abs(Center.x - b.Center.x) < (Extents.x + b.Extents.x)) &&
(math.abs(Center.y - b.Center.y) < (Extents.y + b.Extents.y));
}
}
}
62 changes: 62 additions & 0 deletions Assets/Circle2D.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
using NativeQuadTree;
using Unity.Mathematics;

namespace NativeQuadTree
{
public struct Circle2D
{
public float2 Center;
public float Radious;

public Circle2D(float2 center, float radious)
: this()
{
Center = center;
Radious = radious;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(float2 point)
{
return math.distance(point, Center) <= Radious;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(AABB2D b)
{
// check that all 4 points are inside circle
return Contains(b.Center + -b.Extents) &&
Contains(b.Center + new float2(-b.Extents.x, b.Extents.y)) &&
Contains(b.Center + new float2(b.Extents.x, -b.Extents.y)) &&
Contains(b.Center + b.Extents);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Intersects(AABB2D a)
{
return Intersects(a, this);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool Intersects(AABB2D a, Circle2D b)
{
float2 squareEdgePoint = math.clamp(b.Center, a.Center - a.Extents, a.Center + a.Extents);
float distance = math.distance(squareEdgePoint, b.Center);

if(a.Contains(b.Center))
{
// inside box
/*float length = math.max(a.Extents.x, a.Extents.y);
return distance > b.Radious || length < b.Radious;*/
return true;
}
else
{
// outside box
return distance < b.Radious;
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading