-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRectangle.cs
109 lines (90 loc) · 3.16 KB
/
Rectangle.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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
namespace MmorpgServer
{
public class Rectangle : Shape
{
public override Vector2 HalfSize
{
get;
set;
}
public Rectangle(in Vector2 position, in Vector2 halfSize) : base(in position)
{
this.HalfSize = halfSize;
}
public double X1
{
get
{
return Position.X - HalfSize.X;
}
}
public double X2
{
get
{
return Position.X + HalfSize.X;
}
}
public double y1
{
get
{
return Position.Y - HalfSize.Y;
}
}
public double Y2
{
get
{
return Position.Y + HalfSize.Y;
}
}
public override void ResolveCollision(Circle other, out CollisionResult result)
{
other.ResolveCollision(this, out result);
result.Normal.X = -result.Normal.X;
result.Normal.Y = -result.Normal.Y;
}
public override void ResolveCollision(Shape other, out CollisionResult result)
{
other.ResolveCollision(this, out result);
result.Normal.X = -result.Normal.X;
result.Normal.Y = -result.Normal.Y;
}
public override void ResolveCollision(Rectangle other, out CollisionResult result)
{
throw new NotImplementedException();
}
public override bool IntersectsWith(AreaOfInterest areaOfInterest)
{
var nearestX = Math.Max(this.Position.X, Math.Min(areaOfInterest.Position.X, areaOfInterest.Position.X + this.HalfSize.X));
var nearestY = Math.Max(this.Position.Y, Math.Min(areaOfInterest.Position.Y, areaOfInterest.Position.Y + this.HalfSize.Y));
var dist = new Vector2(areaOfInterest.Position.X - nearestX, areaOfInterest.Position.Y - nearestY);
var sqrLength = dist.X * dist.X + dist.Y * dist.Y;
return areaOfInterest.Radius * areaOfInterest.Radius > sqrLength;
}
public override BoundingBox GetClusters()
{
return new BoundingBox(new Vector2i(
(Int32)Math.Floor((Position.X - HalfSize.X) / 3),
(Int32)Math.Floor((Position.Y - HalfSize.Y) / 3)
), new Vector2i(
(Int32)Math.Ceiling((Position.X + HalfSize.X) / 3),
(Int32)Math.Ceiling((Position.Y + HalfSize.Y) / 3)
));
}
public override double Raycast(in Ray ray)
{
double tx1 = (X1 - ray.Position.X) * ray.InvDirection.X;
double tx2 = (X2 - ray.Position.X) * ray.InvDirection.X;
double tmin = Math.Min(tx1, tx2);
double tmax = Math.Max(tx1, tx2);
double ty1 = (y1 - ray.Position.Y) * ray.InvDirection.Y;
double ty2 = (Y2 - ray.Position.Y) * ray.InvDirection.Y;
tmin = Math.Max(tmin, Math.Min(ty1, ty2));
tmax = Math.Min(tmax, Math.Max(ty1, ty2));
return tmax >= tmin ? tmin : Double.PositiveInfinity;
}
}
}