#ifndef _PHYSICS_POLYGON_ #define _PHYSICS_POLYGON_ #include "body.h" #include namespace wgd { class Vector2D; }; class Polygon : public Body { public: Polygon (const wgd::InstanceID &); void update(); void draw(); float getArea () const { checkVertexCache(); return area; } float getMomentOfInertia () const { checkVertexCache(); return momentOfInertia * getDensity(); } float getMinX () { checkCache(); return vertices[xMin].x; } float getMaxX () { checkCache(); return vertices[xMax].x; } float getMinY () { checkCache(); return vertices[yMin].y; } float getMaxY () { checkCache(); return vertices[yMax].y; } bool contains (const wgd::Vector2D& p); void addVertex (float x, float y); int getVertexCount () const { return count; } wgd::Vector2D getVertex (int i) const { checkCache(); i %= getVertexCount(); return vertices[i]; } wgd::Vector2D getNormal (int i) const { checkCache(); wgd::Vector2D side = getVertex(i+1) - getVertex(i); wgd::Vector2D axis = wgd::Vector2D(-side.y, side.x); axis = axis.normalise(); return axis; } wgd::Vector2D getExtremePoint (const wgd::Vector2D& direction, int& index) const; INSTANCE(Body, Polygon, "polygon"); DOSTE_BEGINEVENTS_INHERIT(Body); DOSTE_EVENT(102, positionChanged); DOSTE_EVENT(103, positionChanged); DOSTE_EVENT(104, positionChanged); DOSTE_EVENT(100, vertexAdded); DOSTE_EVENT(101, vertexChanged); DOSTE_ENDEVENTS; private: void vertexAdded (WGD_HANDLER); void vertexChanged (WGD_HANDLER); void positionChanged (WGD_HANDLER); void remakeCOM (); bool m_verticesChanged; bool m_moved_polygon; void checkCache () const { if (m_moved_polygon) { const_cast(this)->generateCache(); } } void checkVertexCache () const { if (m_verticesChanged) { const_cast(this)->remakeCOM(); } } void generateCache (); std::vector vertices; int xMin; int xMax; int yMin; int yMax; int count; float area; float momentOfInertia; }; #endif