-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseObject.cs
117 lines (89 loc) · 3.52 KB
/
BaseObject.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
110
111
112
113
114
115
116
117
using System;
using System.Windows;
using System.Windows.Media;
namespace Paratrooper2049
{
internal class BaseObject
{
public enum gameObject
{
Bullet, Laser, Cluster, Heli, Para, Airplane, Gift
}
protected PointCollection _vertices = new PointCollection();
protected PointCollection _battleField = new PointCollection();
protected double _direction, _speed;
protected bool _lastFrame = false;
protected gameObject _description;
public PointCollection Vertices { get => _vertices; }
public double Speed { get => _speed; set => _speed = value; }
public bool LastFrame { get => _lastFrame; set => _lastFrame = value; }
public gameObject Description { get => _description; }
protected static Point RotateObject(Point point, Point center, double angle)
{
Point temp = new Point(point.X - center.X, point.Y - center.Y);
// Rotate the point by the given angle
double newX = temp.X * Math.Cos(angle) - temp.Y * Math.Sin(angle);
double newY = temp.X * Math.Sin(angle) + temp.Y * Math.Cos(angle);
// Translate the point back to its original position
newX += center.X;
newY += center.Y;
return new Point(newX, newY);
}
public static bool IntersectObject(PointCollection verticesA, PointCollection verticesB)
{
for (int i = 0; i < verticesA.Count; i++)
{
Point va = verticesA[i];
Point vb = verticesA[(i + 1) % verticesA.Count];
Point axis = new Point(va.Y - vb.Y, vb.X - va.X);
axis = Normalize(axis);
ProjectVertices(verticesA, axis, out double minA, out double maxA);
ProjectVertices(verticesB, axis, out double minB, out double maxB);
if (minA >= maxB || minB >= maxA)
{
return false;
}
}
for (int i = 0; i < verticesB.Count; i++)
{
Point va = verticesB[i];
Point vb = verticesB[(i + 1) % verticesB.Count];
Point axis = new Point(va.Y - vb.Y, vb.X - va.X);
axis = Normalize(axis);
ProjectVertices(verticesA, axis, out double minA, out double maxA);
ProjectVertices(verticesB, axis, out double minB, out double maxB);
if (minA >= maxB || minB >= maxA)
{
return false;
}
}
return true;
}
private static void ProjectVertices(PointCollection vertices, Point axis, out double min, out double max)
{
min = double.MaxValue;
max = double.MinValue;
for (int i = 0; i < vertices.Count; i++)
{
Point p = vertices[i];
double proj = DotProduct(p, axis);
if (proj < min) { min = proj; }
if (proj > max) { max = proj; }
}
}
private static double DotProduct(Point a, Point b)
{
// a · b = ax * bx + ay * by
return a.X * b.X + a.Y * b.Y;
}
private static double Magnitude(Point p)
{
return Math.Sqrt(p.X * p.X + p.Y * p.Y);
}
private static Point Normalize(Point p)
{
double mag = Magnitude(p);
return new Point(p.X / mag, p.Y / mag);
}
}
}