Original Post
Heyho,
for a game I managed to get SAT-collision testing working by mixing some examples and codes I've found. My problem now is that while resolving my collision I want my object ( only 1 moving object, all others are static ) to bounce from the edge of the collided polygon. My code looks like this:
The collisions are detected correct, the resoultion is really weird, though. In some cases it works ( even if the leaving angle isn't correct imo ). In others the object just stopps moving. Here I have a pictures showing the behaviour:
[attachment=6001:UnbenannteZeichnung.png]
Is there anything I did wrong? ( The idea with multiplying the velocity by the normal I read on a yt video comment :S )
for a game I managed to get SAT-collision testing working by mixing some examples and codes I've found. My problem now is that while resolving my collision I want my object ( only 1 moving object, all others are static ) to bounce from the edge of the collided polygon. My code looks like this:
static public void ResolveCollisions()
{
GameObject currentObj;
for (int i = 0; i < _Objects.Count; i++)
{
currentObj = _Objects;
for (int x = 0; x < _Objects.Count; x++)
{
if (currentObj != _Objects[x])
{
if (!currentObj.IsStatic)
{
Vector2 Collision = CollisionCheck.Collide(currentObj.Polygon, _Objects[x].Polygon);
if (Collision != Vector2.Zero)
{
currentObj.Velocity *= Vector2.Normalize(Collision);
}
}
}
}
}
} public static Vector2 Collide(Polygon A, Polygon B)
{
if (A.Points.Count > 0 && B.Points.Count > 0)
{
List<Vector2> Axis = new List<Vector2>();
for (int i = 0; i < A.Points.Count; i++)
{
Vector2 a, b;
a = A.Points;
if (i + 1 >= A.Points.Count)
b = A.Points[0];
else
b = A.Points[i + 1];
Axis.Add(b - a);
}
for (int i = 0; i < B.Points.Count; i++)
{
Vector2 a, b;
a = B.Points;
if (i + 1 >= B.Points.Count)
b = B.Points[0];
else
b = B.Points[i + 1];
Axis.Add(b - a);
}
Vector2 CollisionNormal = Vector2.Zero;
foreach (Vector2 axis in Axis)
{
Vector2 normal = Vector2.Normalize(new Vector2(-axis.Y, axis.X));
float minA = 0, minB = 0, maxA = 0, maxB = 0;
ProjectPolygon(normal, A, ref minA, ref maxA);
ProjectPolygon(normal, B, ref minB, ref maxB);
float gap = GapWidth(minA, maxA, minB, maxB);
if (gap > 0)
{
return Vector2.Zero;
}
else
{
CollisionNormal = normal;
}
}
return CollisionNormal;
}
else
return Vector2.Zero;
}
static private float GapWidth(float minA, float maxA, float minB, float maxB)
{
if (minA < minB)
return minB - maxA;
else
return minA - maxB;
}
static private void ProjectPolygon(Vector2 Axis, Polygon Polygon, ref float min, ref float max)
{
float dotProduct = Vector2.Dot(Axis, Polygon.Points[0]);
min = dotProduct;
max = dotProduct;
for (int i = 0; i < Polygon.Points.Count; i++)
{
dotProduct = Vector2.Dot(Polygon.Points, Axis);
if (dotProduct < min)
min = dotProduct;
else
if (dotProduct > max)
max = dotProduct;
}
}The collisions are detected correct, the resoultion is really weird, though. In some cases it works ( even if the leaving angle isn't correct imo ). In others the object just stopps moving. Here I have a pictures showing the behaviour:
[attachment=6001:UnbenannteZeichnung.png]
Is there anything I did wrong? ( The idea with multiplying the velocity by the normal I read on a yt video comment :S )