Skip to content

Commit 42b04f3

Browse files
author
RandyGaul
committed
Finished up circle vs polygon, added license
1 parent 359fecf commit 42b04f3

19 files changed

+478
-3
lines changed

Body.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
Copyright (c) 2013 Randy Gaul http://RandyGaul.net
3+
4+
This software is provided 'as-is', without any express or implied
5+
warranty. In no event will the authors be held liable for any damages
6+
arising from the use of this software.
7+
8+
Permission is granted to anyone to use this software for any purpose,
9+
including commercial applications, and to alter it and redistribute it
10+
freely, subject to the following restrictions:
11+
1. The origin of this software must not be misrepresented; you must not
12+
claim that you wrote the original software. If you use this software
13+
in a product, an acknowledgment in the product documentation would be
14+
appreciated but is not required.
15+
2. Altered source versions must be plainly marked as such, and must not be
16+
misrepresented as being the original software.
17+
3. This notice may not be removed or altered from any source distribution.
18+
*/
19+
120
#include "Precompiled.h"
221

322
Body::Body( Shape *shape_, uint32 x, uint32 y )

Body.h

+19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
Copyright (c) 2013 Randy Gaul http://RandyGaul.net
3+
4+
This software is provided 'as-is', without any express or implied
5+
warranty. In no event will the authors be held liable for any damages
6+
arising from the use of this software.
7+
8+
Permission is granted to anyone to use this software for any purpose,
9+
including commercial applications, and to alter it and redistribute it
10+
freely, subject to the following restrictions:
11+
1. The origin of this software must not be misrepresented; you must not
12+
claim that you wrote the original software. If you use this software
13+
in a product, an acknowledgment in the product documentation would be
14+
appreciated but is not required.
15+
2. Altered source versions must be plainly marked as such, and must not be
16+
misrepresented as being the original software.
17+
3. This notice may not be removed or altered from any source distribution.
18+
*/
19+
120
#ifndef BODY_H
221
#define BODY_H
322

Clock.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
Copyright (c) 2013 Randy Gaul http://RandyGaul.net
3+
4+
This software is provided 'as-is', without any express or implied
5+
warranty. In no event will the authors be held liable for any damages
6+
arising from the use of this software.
7+
8+
Permission is granted to anyone to use this software for any purpose,
9+
including commercial applications, and to alter it and redistribute it
10+
freely, subject to the following restrictions:
11+
1. The origin of this software must not be misrepresented; you must not
12+
claim that you wrote the original software. If you use this software
13+
in a product, an acknowledgment in the product documentation would be
14+
appreciated but is not required.
15+
2. Altered source versions must be plainly marked as such, and must not be
16+
misrepresented as being the original software.
17+
3. This notice may not be removed or altered from any source distribution.
18+
*/
19+
120

221
#include "Precompiled.h"
322

Clock.h

+19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
Copyright (c) 2013 Randy Gaul http://RandyGaul.net
3+
4+
This software is provided 'as-is', without any express or implied
5+
warranty. In no event will the authors be held liable for any damages
6+
arising from the use of this software.
7+
8+
Permission is granted to anyone to use this software for any purpose,
9+
including commercial applications, and to alter it and redistribute it
10+
freely, subject to the following restrictions:
11+
1. The origin of this software must not be misrepresented; you must not
12+
claim that you wrote the original software. If you use this software
13+
in a product, an acknowledgment in the product documentation would be
14+
appreciated but is not required.
15+
2. Altered source versions must be plainly marked as such, and must not be
16+
misrepresented as being the original software.
17+
3. This notice may not be removed or altered from any source distribution.
18+
*/
19+
120
class Clock
221
{
322
public:

Collision.cpp

+111-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
Copyright (c) 2013 Randy Gaul http://RandyGaul.net
3+
4+
This software is provided 'as-is', without any express or implied
5+
warranty. In no event will the authors be held liable for any damages
6+
arising from the use of this software.
7+
8+
Permission is granted to anyone to use this software for any purpose,
9+
including commercial applications, and to alter it and redistribute it
10+
freely, subject to the following restrictions:
11+
1. The origin of this software must not be misrepresented; you must not
12+
claim that you wrote the original software. If you use this software
13+
in a product, an acknowledgment in the product documentation would be
14+
appreciated but is not required.
15+
2. Altered source versions must be plainly marked as such, and must not be
16+
misrepresented as being the original software.
17+
3. This notice may not be removed or altered from any source distribution.
18+
*/
19+
120
#include "Precompiled.h"
221

322
CollisionCallback Dispatch[Shape::eCount][Shape::eCount] =
@@ -48,12 +67,101 @@ void CircletoCircle( Manifold *m, Body *a, Body *b )
4867

4968
void CircletoPolygon( Manifold *m, Body *a, Body *b )
5069
{
70+
Circle *A = reinterpret_cast<Circle *> (a->shape);
71+
PolygonShape *B = reinterpret_cast<PolygonShape *>(b->shape);
72+
5173
m->contact_count = 0;
74+
75+
// Transform circle center to Polygon model space
76+
Vec2 center = a->position;
77+
center = B->u.Transpose( ) * (center - b->position);
78+
79+
// Find edge with minimum penetration
80+
// Exact concept as using support points in Polygon vs Polygon
81+
real separation = -FLT_MAX;
82+
uint32 faceNormal = 0;
83+
for(uint32 i = 0; i < B->m_vertexCount; ++i)
84+
{
85+
real s = Dot( B->m_normals[i], center - B->m_vertices[i] );
86+
87+
if(s > A->radius)
88+
return;
89+
90+
if(s > separation)
91+
{
92+
separation = s;
93+
faceNormal = i;
94+
}
95+
}
96+
97+
// Grab face's vertices
98+
Vec2 v1 = B->m_vertices[faceNormal];
99+
uint32 i2 = faceNormal + 1 < B->m_vertexCount ? faceNormal + 1 : 0;
100+
Vec2 v2 = B->m_vertices[i2];
101+
102+
// Check to see if center is within polygon
103+
if(separation < EPSILON)
104+
{
105+
m->contact_count = 1;
106+
m->normal = -(B->u * B->m_normals[faceNormal]);
107+
m->contacts[0] = m->normal * A->radius + a->position;
108+
m->penetration = A->radius;
109+
return;
110+
}
111+
112+
// Determine which voronoi region of the edge center of circle lies within
113+
real dot1 = Dot( center - v1, v2 - v1 );
114+
real dot2 = Dot( center - v2, v1 - v2 );
115+
m->penetration = A->radius - separation;
116+
117+
// Closest to v1
118+
if(dot1 <= 0.0f)
119+
{
120+
if(DistSqr( center, v1 ) > A->radius * A->radius)
121+
return;
122+
123+
m->contact_count = 1;
124+
Vec2 n = v1 - center;
125+
n = B->u * n;
126+
n.Normalize( );
127+
m->normal = n;
128+
v1 = B->u * v1 + b->position;
129+
m->contacts[0] = v1;
130+
}
131+
132+
// Closest to v2
133+
else if(dot2 <= 0.0f)
134+
{
135+
if(DistSqr( center, v2 ) > A->radius * A->radius)
136+
return;
137+
138+
m->contact_count = 1;
139+
Vec2 n = v2 - center;
140+
v2 = B->u * v2 + b->position;
141+
m->contacts[0] = v2;
142+
n = B->u * n;
143+
n.Normalize( );
144+
m->normal = n;
145+
}
146+
147+
// Closest to face
148+
else
149+
{
150+
Vec2 n = B->m_normals[faceNormal];
151+
if(Dot( center - v1, n ) > A->radius)
152+
return;
153+
154+
n = B->u * n;
155+
m->normal = -n;
156+
m->contacts[0] = m->normal * A->radius + a->position;
157+
m->contact_count = 1;
158+
}
52159
}
53160

54161
void PolygontoCircle( Manifold *m, Body *a, Body *b )
55162
{
56-
m->contact_count = 0;
163+
CircletoPolygon( m, b, a );
164+
m->normal = -m->normal;
57165
}
58166

59167
real FindAxisLeastPenetration( uint32 *faceIndex, PolygonShape *A, PolygonShape *B )
@@ -257,6 +365,8 @@ void PolygontoPolygon( Manifold *m, Body *a, Body *b )
257365
m->penetration = -separation;
258366
++cp;
259367
}
368+
else
369+
m->penetration = 0;
260370

261371
separation = Dot( refFaceNormal, incidentFace[1] ) - refC;
262372
if(separation <= 0.0f)

Collision.h

+19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
Copyright (c) 2013 Randy Gaul http://RandyGaul.net
3+
4+
This software is provided 'as-is', without any express or implied
5+
warranty. In no event will the authors be held liable for any damages
6+
arising from the use of this software.
7+
8+
Permission is granted to anyone to use this software for any purpose,
9+
including commercial applications, and to alter it and redistribute it
10+
freely, subject to the following restrictions:
11+
1. The origin of this software must not be misrepresented; you must not
12+
claim that you wrote the original software. If you use this software
13+
in a product, an acknowledgment in the product documentation would be
14+
appreciated but is not required.
15+
2. Altered source versions must be plainly marked as such, and must not be
16+
misrepresented as being the original software.
17+
3. This notice may not be removed or altered from any source distribution.
18+
*/
19+
120
#ifndef COLLISION_H
221
#define COLLISION_H
322

IEMath.h

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
Copyright (c) 2013 Randy Gaul http://RandyGaul.net
3+
4+
This software is provided 'as-is', without any express or implied
5+
warranty. In no event will the authors be held liable for any damages
6+
arising from the use of this software.
7+
8+
Permission is granted to anyone to use this software for any purpose,
9+
including commercial applications, and to alter it and redistribute it
10+
freely, subject to the following restrictions:
11+
1. The origin of this software must not be misrepresented; you must not
12+
claim that you wrote the original software. If you use this software
13+
in a product, an acknowledgment in the product documentation would be
14+
appreciated but is not required.
15+
2. Altered source versions must be plainly marked as such, and must not be
16+
misrepresented as being the original software.
17+
3. This notice may not be removed or altered from any source distribution.
18+
*/
19+
120
#ifndef IEMATH_H
221
#define IEMATH_H
322

@@ -87,7 +106,7 @@ struct Vec2
87106
y += rhs.y;
88107
}
89108

90-
Vec2 operator-( const Vec2& rhs )
109+
Vec2 operator-( const Vec2& rhs ) const
91110
{
92111
return Vec2( x - rhs.x, y - rhs.y );
93112
}
@@ -231,6 +250,12 @@ inline real Dot( const Vec2& a, const Vec2& b )
231250
return a.x * b.x + a.y * b.y;
232251
}
233252

253+
inline real DistSqr( const Vec2& a, const Vec2& b )
254+
{
255+
Vec2 c = a - b;
256+
return Dot( c, c );
257+
}
258+
234259
inline Vec2 Cross( const Vec2& v, real a )
235260
{
236261
return Vec2( a * v.y, -a * v.x );

License.txt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Copyright (c) 2013 Randy Gaul http://RandyGaul.net
2+
3+
This software is provided 'as-is', without any express or implied
4+
warranty. In no event will the authors be held liable for any damages
5+
arising from the use of this software.
6+
7+
Permission is granted to anyone to use this software for any purpose,
8+
including commercial applications, and to alter it and redistribute it
9+
freely, subject to the following restrictions:
10+
1. The origin of this software must not be misrepresented; you must not
11+
claim that you wrote the original software. If you use this software
12+
in a product, an acknowledgment in the product documentation would be
13+
appreciated but is not required.
14+
2. Altered source versions must be plainly marked as such, and must not be
15+
misrepresented as being the original software.
16+
3. This notice may not be removed or altered from any source distribution.

Manifold.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
Copyright (c) 2013 Randy Gaul http://RandyGaul.net
3+
4+
This software is provided 'as-is', without any express or implied
5+
warranty. In no event will the authors be held liable for any damages
6+
arising from the use of this software.
7+
8+
Permission is granted to anyone to use this software for any purpose,
9+
including commercial applications, and to alter it and redistribute it
10+
freely, subject to the following restrictions:
11+
1. The origin of this software must not be misrepresented; you must not
12+
claim that you wrote the original software. If you use this software
13+
in a product, an acknowledgment in the product documentation would be
14+
appreciated but is not required.
15+
2. Altered source versions must be plainly marked as such, and must not be
16+
misrepresented as being the original software.
17+
3. This notice may not be removed or altered from any source distribution.
18+
*/
19+
120
#include "Precompiled.h"
221

322
void Manifold::Solve( void )

Manifold.h

+19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
Copyright (c) 2013 Randy Gaul http://RandyGaul.net
3+
4+
This software is provided 'as-is', without any express or implied
5+
warranty. In no event will the authors be held liable for any damages
6+
arising from the use of this software.
7+
8+
Permission is granted to anyone to use this software for any purpose,
9+
including commercial applications, and to alter it and redistribute it
10+
freely, subject to the following restrictions:
11+
1. The origin of this software must not be misrepresented; you must not
12+
claim that you wrote the original software. If you use this software
13+
in a product, an acknowledgment in the product documentation would be
14+
appreciated but is not required.
15+
2. Altered source versions must be plainly marked as such, and must not be
16+
misrepresented as being the original software.
17+
3. This notice may not be removed or altered from any source distribution.
18+
*/
19+
120
#ifndef MANIFOLD_H
221
#define MANIFOLD_H
322

Precompiled.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,21 @@
1+
/*
2+
Copyright (c) 2013 Randy Gaul http://RandyGaul.net
3+
4+
This software is provided 'as-is', without any express or implied
5+
warranty. In no event will the authors be held liable for any damages
6+
arising from the use of this software.
7+
8+
Permission is granted to anyone to use this software for any purpose,
9+
including commercial applications, and to alter it and redistribute it
10+
freely, subject to the following restrictions:
11+
1. The origin of this software must not be misrepresented; you must not
12+
claim that you wrote the original software. If you use this software
13+
in a product, an acknowledgment in the product documentation would be
14+
appreciated but is not required.
15+
2. Altered source versions must be plainly marked as such, and must not be
16+
misrepresented as being the original software.
17+
3. This notice may not be removed or altered from any source distribution.
18+
*/
19+
120
#pragma message( "Compiling Precompiled Header File..." )
221
#include "Precompiled.h"

0 commit comments

Comments
 (0)