Skip to content

Commit 84a98b7

Browse files
committed
Fixed collision normal vector.
1 parent 4c785ab commit 84a98b7

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

Foundation/Public/Aurora.Math/Collision.hpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
inline namespace Math
2424
{
25-
2625
// -=(Undocumented)=-
2726
template<typename Base>
2827
static constexpr Bool Intersects(ConstRef<Math::Circle<Base>> Circle, ConstRef<Math::Circle<Base>> Other, Ptr<Math::Manifold<Base>> Manifold)
@@ -39,7 +38,7 @@ inline namespace Math
3938
Core::Clamp(Center.GetX(), Rectangle.GetLeft(), Rectangle.GetRight()),
4039
Core::Clamp(Center.GetY(), Rectangle.GetTop(), Rectangle.GetBottom()));
4140

42-
const Vector2<Base> Delta = Center - Closest;
41+
const Vector2<Base> Delta = Closest - Center;
4342
const Base SqDistance = Delta.GetLengthSquared();
4443
const Base SqRadius = Circle.GetRadius() * Circle.GetRadius();
4544

@@ -99,7 +98,13 @@ inline namespace Math
9998
template<typename Base>
10099
static constexpr Bool Intersects(ConstRef<Math::Rect<Base>> Rectangle, ConstRef<Math::Circle<Base>> Circle, Ptr<Math::Manifold<Base>> Manifold)
101100
{
102-
return Intersects<Base>(Circle, Rectangle, Manifold);
101+
const Bool Result = Intersects<Base>(Circle, Rectangle, Manifold);
102+
103+
if (Result && Manifold)
104+
{
105+
Manifold->SetNormal(-Manifold->GetNormal());
106+
}
107+
return Result;
103108
}
104109

105110
// -=(Undocumented)=-
@@ -192,7 +197,7 @@ inline namespace Math
192197
Normal = Vector2<Base>::Normalize(Rectangle.GetCenter() - Contact);
193198
}
194199

195-
Manifold->SetPenetration(Delta.GetLength() * (T1 - T0));
200+
Manifold->SetPenetration((T1 - T0) * Delta.Dot(Normal));
196201
Manifold->SetNormal(Normal);
197202
Manifold->AddPoint(Contact);
198203
}
@@ -212,13 +217,25 @@ inline namespace Math
212217
template<typename Base>
213218
static constexpr Bool Intersects(ConstRef<Math::Edge<Base>> Edge, ConstRef<Math::Circle<Base>> Circle, Ptr<Math::Manifold<Base>> Manifold)
214219
{
215-
return Intersects<Base>(Circle, Edge, Manifold);
220+
const Bool Result = Intersects<Base>(Circle, Edge, Manifold);
221+
222+
if (Result && Manifold)
223+
{
224+
Manifold->SetNormal(-Manifold->GetNormal());
225+
}
226+
return Result;
216227
}
217228

218229
// -=(Undocumented)=-
219230
template<typename Base>
220231
static constexpr Bool Intersects(ConstRef<Math::Edge<Base>> Edge, ConstRef<Math::Rect<Base>> Rectangle, Ptr<Math::Manifold<Base>> Manifold)
221232
{
222-
return Intersects<Base>(Rectangle, Edge, Manifold);
233+
const Bool Result = Intersects<Base>(Rectangle, Edge, Manifold);
234+
235+
if (Result && Manifold)
236+
{
237+
Manifold->SetNormal(-Manifold->GetNormal());
238+
}
239+
return Result;
223240
}
224241
}

0 commit comments

Comments
 (0)