-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollisionManager.cs
More file actions
203 lines (173 loc) · 7.46 KB
/
CollisionManager.cs
File metadata and controls
203 lines (173 loc) · 7.46 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Open Source Initiative OSI - The MIT License (MIT):Licensing
* The MIT License (MIT)
* Copyright (c) <2012> <A* Games>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace ImagineCup2012
{
// Structure that stores the results of the PolygonCollision function
public struct PolygonCollisionResult
{
// True if the polygons will intersect forward in time.
public bool WillIntersect;
// True if the polygons currently intersects.
public bool Intersect;
// The translation to apply to the polygon to push the polygons apart.
public Vector2 MinimumTranslationVector;
}
public static class CollisionManager
{
// Check if polygon A is going to collide with polygon B for the given velocity
public static PolygonCollisionResult intersects(Polygon polygonA, Polygon polygonB, Vector2 velocity)
{
PolygonCollisionResult result = new PolygonCollisionResult();
result.Intersect = true;
result.WillIntersect = true;
int edgeCountA = polygonA.getEdges().Count;
int edgeCountB = polygonB.getEdges().Count;
float minIntervalDistance = float.PositiveInfinity;
Vector2 translationAxis = new Vector2();
Vector2 edge;
// Loop through all the edges of both polygons
for (int edgeIndex = 0; edgeIndex < edgeCountA + edgeCountB; edgeIndex++)
{
if (edgeIndex < edgeCountA)
{
edge = polygonA.getEdges()[edgeIndex];
}
else
{
edge = polygonB.getEdges()[edgeIndex - edgeCountA];
}
// ===== 1. Find if the polygons are currently intersecting =====
// Find the axis perpendicular to the current edge
Vector2 axis = new Vector2(-edge.Y, edge.X);
axis.Normalize();
// Find the projection of the polygon on the current axis
float minA = 0;
float minB = 0;
float maxA = 0;
float maxB = 0;
projectPolygon(axis, polygonA, ref minA, ref maxA);
projectPolygon(axis, polygonB, ref minB, ref maxB);
// Check if the polygon projections are currentlty intersecting
if (intervalDistance(minA, maxA, minB, maxB) > 0)
{
result.Intersect = false;
}
// ===== 2. Now find if the polygons *will* intersect =====
// Project the velocity on the current axis
// float velocityProjection = axis.Dot(velocity);
float velocityProjection = Vector2.Dot(axis, velocity);
// Get the projection of polygon A during the movement
if (velocityProjection < 0)
{
minA += velocityProjection;
}
else
{
maxA += velocityProjection;
}
// Do the same test as above for the new projection
float iDistance = intervalDistance(minA, maxA, minB, maxB);
if (iDistance > 0)
{
result.WillIntersect = false;
}
// If the polygons are not intersecting and won't intersect, exit the loop
if (!result.Intersect && !result.WillIntersect)
{
break;
}
// Check if the current interval distance is the minimum one. If so store
// the interval distance and the current distance.
// This will be used to calculate the minimum translation vector
iDistance = Math.Abs(iDistance);
if (iDistance < minIntervalDistance)
{
minIntervalDistance = iDistance;
translationAxis = axis;
Vector2 d = polygonA.getCenter() - polygonB.getCenter();
if (Vector2.Dot(d, translationAxis) < 0)
{
translationAxis = -translationAxis;
}
}
}
// The minimum translation vector can be used to push the polygons appart.
// First moves the polygons by their velocity, then move polygonA by MinimumTranslationVector.
if (result.WillIntersect)
{
result.MinimumTranslationVector = translationAxis * minIntervalDistance;
}
return result;
}
public static bool intersects(Vector2 mousePos, Vector2 objectPos, Vector2 objectOrigin)
{
bool collision = false;
float objectTop = objectPos.Y;
float objectBottom = objectPos.Y + objectOrigin.Y * 2;
float objectRight = objectPos.X + objectOrigin.X * 2;
float objectLeft = objectPos.X;
if (mousePos.Y >= objectTop && mousePos.Y <= objectBottom && mousePos.X <= objectRight && mousePos.X >= objectLeft)
{
collision = true;
}
return collision;
}
private static float intervalDistance(float minA, float maxA, float minB, float maxB)
{
if (minA < minB)
{
return minB - maxA;
}
else
{
return minA - maxB;
}
}
// Calculate the projection of a polygon on an axis and returns it as a [min, max] interval
private static void projectPolygon(Vector2 axis, Polygon polygon, ref float min, ref float max)
{
// To project a point on an axis use the dot product
float d = Vector2.Dot(axis, polygon.getPoints()[0]);
min = d;
max = d;
for (int i = 0; i < polygon.getPoints().Count; i++)
{
d = Vector2.Dot(polygon.getPoints()[i], axis);
if (d < min)
{
min = d;
}
else
{
if (d > max)
{
max = d;
}
}
}
}
}
}