#include "stdafx.h" #include <vector> #include <cmath> #include <cassert> using namespace std; #define interface class class DrawArgs { public: DrawArgs(){} void Debug(){} private: float m_TargetWidth; float m_TargetHeight; }; interface IRenderable { public: virtual void Initialize(const DrawArgs& drawArgs) = 0; virtual void Update(const DrawArgs& drawArgs) = 0; virtual void Render(const DrawArgs& drawArgs) = 0; }; class RenderableObject : public IRenderable { public: bool IsInitialized() const { return m_bInitialized; } private: bool m_bInitialized; }; const float EPSILON = 1.0e-4; class point3d { friend point3d CrossMulti(const point3d& p1, const point3d& p2); public: point3d() : x(0.0), y(0.0), z(0.0) {} point3d(const point3d& p) : x(p.x), y(p.y), z(p.z) {} point3d(float x, float y, float z) { this->x = x; this->y = y; this->z = z; } point3d operator=(const point3d& p) { if (&p == this) return *this; x = p.x; y = p.y; z = p.z; return *this; } point3d operator+(const point3d& p) const { point3d r; r.x = x + p.x; r.y = y + p.y; r.z = z + p.z; return r; } point3d operator-(const point3d& p) const { point3d r; r.x = x - p.x; r.y = y - p.y; r.z = z - p.z; return r; } point3d operator*(float f) const { point3d r; r.x = x * f; r.y = y * f; r.z = z * f; return r; } point3d operator/(float f) const { assert(f < -EPSILON || f > EPSILON); point3d r; r.x = x / f; r.y = y / f; r.z = z / f; return r; } point3d& operator+=(const point3d& p) { this->x += p.x; this->y += p.y; this->z += p.z; return *this; } point3d& operator-=(const point3d& p) { this->x -= p.x; this->y -= p.y; this->z -= p.z; return *this; } point3d& operator*=(float f) { x *= f; y *= f; z *= f; return *this; } point3d& operator/=(float f) const { assert(f < -EPSILON || f > EPSILON); x /= f; y /= f; z /= f; return *this; } bool operator==(const point3d& p) const { float tx, ty, tz; tx = x - p.x; ty = y - p.y; tz = z - p.z; return tx > -EPSILON && tx < EPSILON && ty > -EPSILON && ty < EPSILON && tz > -EPSILON && tz < EPSILON; //return x == p.x && y == p.y && z == p.z; } bool operator!=(const point3d& p) const { //return x != p.x || y != p.y || z != p.z; return !(*this == p); } void normalize() { float len = GetLength(); if (len > -EPSILON || len < EPSILON) { x /= len; y /= len; z /= len; } } float GetLength() const { return sqrt(x * x + y * y + z * z); } public: float x; float y; float z; }; typedef point3d vector3d; vector3d CrossMulti(const vector3d& p1, const vector3d& p2) { vector3d v; v.x = p1.y * p2.z - p1.z - p2.y; v.y = p1.z * p2.x - p1.x * p2.z; v.z = p1.x * p2.y - p1.y * p2.x; return v; } class BoundingBox { /* the sequence of the vertexs are defined as this: 0 1 /|----------------/| / | / | /3_|_____________2/ | | | | | | | | | | /4-------------|--/5 | / | / |/7_______________|/6 */ friend BoundingSphere ComputeBoundingSphere(const BoundingBox& b); public: BoundingBox(const point3d& v0, const point3d& v1, const point3d& v2, const point3d& v3, const point3d& v4, const point3d& v5, const point3d& v6, const point3d& v7) { corners[0] = v0; corners[1] = v1; corners[2] = v2; corners[3] = v3; corners[4] = v4; corners[5] = v5; corners[6] = v6; corners[7] = v7; } // BoundingBox(float bottom, float top, float left, float right, float radius1, float radius2) // { // } point3d CalculateCenter() { return (corners[0] + corners[6]) * 0.5; } private: point3d corners[8]; }; class BoundingSphere { public: BoundingSphere(const point3d& c, float r) { center = c; radius = r; } private: point3d center; float radius; }; BoundingSphere ComputeBoundingSphere(const BoundingBox& b) { point3d c = (b.corners[0] + b.corners[6]) * 0.5; // float r = max((b.corners[0] - b.corners[6]).GetLength(), // (b.corners[3] - b.corners[5]).GetLength()); float r = (b.corners[0] - b.corners[6]).GetLength() / 2; return BoundingSphere(c, b); } class rectangle {//水平放置,非倾斜放置 friend bool IsPointInRect(const point3d& p, const rectangle& r); public: point3d lt; float width; float height; }; bool IsPointInRect(const point3d& p, const rectangle& r) { return p.x > r.lt.x && p.x < (r.lt.x + width) && p.y > r.lt.y && p.y < (r.lt.y + height); }; class RenderableObjectList : RenderableObject { public: void Render(const DrawArgs& drawArgs) { for (vector<RenderableObject*>::const_iterator iter = m_vRenderList.begin(); iter != m_vRenderList.end(); ++iter) { if ((*iter)->IsInitialized()) (*iter)->Render(drawArgs); } } void Update(const DrawArgs& drawArgs) { for (vector<RenderableObject*>::const_iterator iter = m_vRenderList.begin(); iter != m_vRenderList.end(); ++iter) { if ((*iter)->IsInitialized()) (*iter)->Update(drawArgs); } } void Initialize(const DrawArgs& drawArgs) { for (vector<RenderableObject*>::const_iterator iter = m_vRenderList.begin(); iter != m_vRenderList.end(); ++iter) { if ((*iter)->IsInitialized()) (*iter)->Initialize(drawArgs); } } private: vector<RenderableObject*> m_vRenderList; }; bool PointInRectangle(point3d p, rectangle r) { return (p.x > r.lt.x) && (p.x < r.lt.x + r.width) && (p.y > r.lt.y) && (p.y < r.lt.y + r.height); } class atomParcle { public: void Update(float deltaTime){ pos += velocity * deltaTime; } bool Hit(const atomParcle& a) { /* point3d t = a.pos - pos; if (t.GetLength() < EPSILON) return TRUE; return FALSE;*/ return pos == a.pos; } bool Left() { } bool Right() { } bool Up() { } bool Down() { } private: point3d pos; vector3d velocity; };