1 #include "ClosedShape.h"
3 #include "VectorTools.h"
6 namespace JellyPhysics
{
8 void ClosedShape::begin () {
9 mLocalVertices
.clear();
13 int ClosedShape::addVertex (const Vector2
&vec
) {
14 mLocalVertices
.push_back(vec
);
15 return mLocalVertices
.size();
19 void ClosedShape::finish (bool recenter
) {
21 // find the average location of all of the vertices, this is our geometrical center
22 Vector2 center
= Vector2::Zero
;
23 unsigned int c
= mLocalVertices
.size();
24 for (unsigned int i
= 0; i
< c
; ++i
) center
+= mLocalVertices
[i
];
26 // now subtract this from each element, to get proper "local" coordinates
27 for (unsigned int i
= 0; i
< c
; ++i
) mLocalVertices
[i
] -= center
;
32 Vector2List
ClosedShape::transformVertices (const Vector2
&worldPos
, float angleInRadians
, const Vector2
&scale
) const {
33 Vector2List ret
= mLocalVertices
;
35 unsigned int c
= ret
.size();
36 for (unsigned int i
= 0; i
< c
; ++i
) {
37 // rotate the point, and then translate
39 v
= JellyPhysics::VectorTools::rotateVector(v
, angleInRadians
);
47 void ClosedShape::transformVertices (const Vector2
&worldPos
, float angleInRadians
, const Vector2
&scale
, Vector2List
&outList
) const {
48 float c
= cosf(angleInRadians
);
49 float s
= sinf(angleInRadians
);
50 Vector2List::iterator out
= outList
.begin();
51 for (Vector2List::const_iterator it
= mLocalVertices
.begin(); it
!= mLocalVertices
.end(); ++it
, ++out
) {
52 // rotate the point, and then translate
53 Vector2 v
=(*it
)*scale
;
54 v
= JellyPhysics::VectorTools::rotateVector(v
, c
, s
);