cosmetix
[k8-jellyphysics.git] / src / jelly / ClosedShape.cpp
blobf4320260685f1de6e37c7fafe06d8a54ab776b46
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) {
20 if (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];
25 center /= (float)c;
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;
34 Vector2 v;
35 unsigned int c = ret.size();
36 for (unsigned int i = 0; i < c; ++i) {
37 // rotate the point, and then translate
38 v = ret[i]*scale;
39 v = JellyPhysics::VectorTools::rotateVector(v, angleInRadians);
40 v += worldPos;
41 ret[i] = v;
43 return ret;
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);
55 v += worldPos;
56 (*out) = v;