-
Notifications
You must be signed in to change notification settings - Fork 0
/
Misc.cs
44 lines (43 loc) · 1.59 KB
/
Misc.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using Microsoft.Xna.Framework;
using System;
namespace ProjektPO
{
enum FaceDirection
{
Left = -1,
Right = 1,
}
enum TileCollision
{
Passable = 0,
Impassable = 1,
Platform = 2,
}
public static class RectangleHelp
{
public static Vector2 GetIntersectionDepth(this Rectangle rectA, Rectangle rectB)
{
float halfWidthA = rectA.Width / 2.0f;
float halfHeightA = rectA.Height / 2.0f;
float halfWidthB = rectB.Width / 2.0f;
float halfHeightB = rectB.Height / 2.0f;
Vector2 centerA = new Vector2(rectA.Left + halfWidthA, rectA.Top + halfHeightA);
Vector2 centerB = new Vector2(rectB.Left + halfWidthB, rectB.Top + halfHeightB);
float distanceX = centerA.X - centerB.X;
float distanceY = centerA.Y - centerB.Y;
float minDistanceX = halfWidthA + halfWidthB;
float minDistanceY = halfHeightA + halfHeightB;
if (Math.Abs(distanceX) >= minDistanceX || Math.Abs(distanceY) >= minDistanceY)
{
return Vector2.Zero;
}
float depthX = distanceX > 0 ? minDistanceX - distanceX : -minDistanceX - distanceX;
float depthY = distanceY > 0 ? minDistanceY - distanceY : -minDistanceY - distanceY;
return new Vector2(depthX, depthY);
}
public static Vector2 GetBottomCenter(this Rectangle rect)
{
return new Vector2(rect.X + rect.Width / 2.0f, rect.Bottom);
}
}
}