-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtentions.cs
More file actions
72 lines (63 loc) · 2.87 KB
/
Extentions.cs
File metadata and controls
72 lines (63 loc) · 2.87 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProgGame
{
public static class Extentions
{
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);
}
public static Vector2 GetOriginByRotation(Direction direction, Texture2D texture)
{
switch (direction)
{
case Direction.Left:
return new Vector2(0, texture.Height / 2);
case Direction.Right:
return new Vector2(texture.Width, texture.Height / 2);
case Direction.Up:
return new Vector2(texture.Width / 2, texture.Height);
case Direction.Down:
return new Vector2(texture.Width / 2, 0);
default:
return Vector2.Zero;
}
}
public static void DrawRectangle(Rectangle rectangle, SpriteBatch spriteBatch, Color color)
{
Texture2D _texture;
_texture = new Texture2D(spriteBatch.GraphicsDevice, 1, 1);
_texture.SetData(new Color[] { Color.White });
spriteBatch.Draw(_texture, new Rectangle(rectangle.Left, rectangle.Top, rectangle.Width, 1), color);
spriteBatch.Draw(_texture, new Rectangle(rectangle.Right, rectangle.Top, 1, rectangle.Height), color);
spriteBatch.Draw(_texture, new Rectangle(rectangle.Left, rectangle.Bottom, rectangle.Width, 1), color);
spriteBatch.Draw(_texture, new Rectangle(rectangle.Left, rectangle.Top, 1, rectangle.Height), color);
}
}
}