Flatland

Documentation

SourceForge.net Logo

shapes.hpp

Go to the documentation of this file.
00001 // Summary: Declares Quad, Wall, Block, Circle, Line, Terrain.
00002 // Copyright: 2007  Philip Rideout.  All rights reserved.
00003 // License: see source/bsd-license.txt
00004 
00005 #pragma once
00006 #include <flatland/flatland.hpp>
00007 #include <flatland/vector.hpp>
00008 #include <vector>
00009 #include <list>
00010 #include <map>
00011 
00012 namespace Flatland
00013 {
00014     /// abstract rectangular shape; cannot be instantiated
00015     class Quad : public Geometry
00016     {
00017       public:
00018         Quad(float width, float height);
00019         void UpdateBounds();
00020         void SetMass(Body body, float density) const;
00021         Shape GetShape() const { return Shape::Quad; }
00022         virtual float Extent(int i) const { return extent[i]; }
00023         vec2 EAxis(int i) const { return Axis(i) * extent[i]; } // TODO try caching in UpdateBounds and see if it helps
00024         const vec2* GetCorners() const { return corners; }
00025         bool Contains(const vec2 &v) const;
00026       protected:
00027         float extent[2];
00028         vec2 corners[4];
00029     };
00030 
00031     /// TODO merge with Quad (after you make a line collider)
00032     class Block : public Quad
00033     {
00034       public:
00035         Block(vec2 topleft, vec2 bottomright);
00036         Block(vec2 center, float width, float height);
00037         Block(float left, float top, float right, float bottom);
00038     };
00039 
00040     /// finite line segment
00041     class Line : public Quad
00042     {
00043       public:
00044         Line(vec2 origin, vec2 end);
00045         vec2 Origin() const { return center - axis * extent[0]; } // TODO this should be cached in static segments
00046         vec2 End() const { return center + axis * extent[0]; }
00047     };
00048 
00049     typedef std::vector<vec2> VList;
00050     typedef std::vector<Line> LineStrip;
00051     typedef std::map<float, int> SpanMap;
00052 
00053     /// static strip of line segments
00054     //
00055     /// Terrains must be built strictly west-to-east; each x value must be greater than the previous x value.
00056     class Terrain : public Geometry
00057     {
00058       public:
00059         Terrain(const vec2 &start);
00060         void UpdateBounds();
00061         void Finalize() { UpdateBounds(); }
00062         Shape GetShape() const { return Shape::Terrain; }
00063         bool GetIndexRange(float left, float right, int &lower, int &upper) const;
00064 
00065         // container methods
00066         typedef VList::const_iterator const_iterator;
00067         const_iterator begin() const { return vertices.begin(); }
00068         const_iterator end() const { return vertices.end(); }
00069         const vec2 &front() const { return vertices.front(); }
00070         const vec2 &back() const { return vertices.back(); }
00071         void push_back(const vec2 &v);
00072         size_t size() const { return lines.size(); }
00073         bool empty() const { return lines.empty(); }
00074         Line &operator[](int i) { return lines[i]; }
00075         const Line &operator[](int i) const { return lines[i]; }
00076 
00077       private:
00078         vec2 previous;
00079         LineStrip lines;
00080         SpanMap spans;
00081         VList vertices;
00082     };
00083 
00084     /// ball shape
00085     class Circle : public Geometry
00086     {
00087       public:
00088         Circle(const vec2 &center, float radius);
00089         void UpdateBounds();
00090         void SetMass(Body body, float density) const;
00091         Shape GetShape() const { return Shape::Circle; }
00092         float Radius() const { return radius; }
00093       private:
00094         float radius;
00095     };
00096 
00097     typedef std::list<Geometry*> GList;
00098 
00099     /// dynamic shape composed of other shapes
00100     class Composite : public Geometry
00101     {
00102       public:
00103         Composite(vec2 centroid);
00104         ~Composite();
00105         void UpdateBounds();
00106         void Finalize() { UpdateBounds(); }
00107         Shape GetShape() const { return Shape::Composite; }
00108         void SetCenter(const vec2 &center);
00109         void SetAxis(const vec2 &axis);
00110         void SetMass(Body body, float density) const;
00111 
00112         // container methods
00113         typedef GList::const_iterator const_iterator;
00114         typedef GList::iterator iterator;
00115         const_iterator begin() const { return geometries.begin(); }
00116         const_iterator end() const { return geometries.end(); }
00117         const Geometry &front() const { return *geometries.front(); }
00118         const Geometry &back() const { return *geometries.back(); }
00119         void push_back(Geometry *geometry);
00120         size_t size() const { return geometries.size(); }
00121 
00122       private:
00123         GList geometries;
00124     };
00125 }

Generated on Sat Jan 13 17:20:21 2007 for Flatland by doxygen 1.5.1