I have a Rectangle class, of which I have 5-6 vectors for each instance. The main vectors are the center and color of the object, while the secondary vectors represent more or less the north, south, east, and west directions of the rectangle itself. These vectors are used to detect collision by "pointing" outside of the sides of the rectangle to detect collision. I may end up creating more fields of the NW, SW, NE, SE, equivalents as well, but I'd like to know if this is actually a good implementation before I do this. The only Vec2f I have allocated as a pointer is the mWidthHeight vector, which of course is just a representation of the width and the height (x = width, y = height) of the rectangle.
Will this allocate too much memory if there are thousands of these on the screen, or is this an OK (at least) implementation?
Rect.h
Note - I'm debating with the idea of making the N, S, E, W vectors pointers to memory. Is that a good idea, or no?
#pragma once
#include <QGLWidget>
#include <GL/glext.h>
#include <cmath>
#include <QDebug>
#include "Shape.h"
#include "Vec3f.h"
#include "rand.h"
const int DEFAULT_SQUARE_WIDTH = 5;
const int DEFAULT_SQUARE_HEIGHT = 5;
typedef enum {
V2D_NORTH,
V2D_SOUTH,
V2D_EAST,
V2D_WEST
} V2D_DIRECTION;
class Rectangle : public Shape
{
public:
Rectangle(
Vec2f center = Vec2f(),
Vec2f widthheight = Vec2f(DEFAULT_SQUARE_WIDTH, DEFAULT_SQUARE_HEIGHT),
float radius = 0,
Vec3f color = Vec3f()
);
~Rectangle();
inline Vec2f* getWidthHeight() const {
return mWidthHeight;
}
inline Vec2f getDirection(V2D_DIRECTION dir) const {
switch(dir) {
case V2D_NORTH:
return mNorth;
case V2D_SOUTH:
return mSouth;
case V2D_EAST:
return mEast;
case V2D_WEST:
return mWest;
}
}
virtual void Collide( Shape &s );
virtual void Collide( Rectangle &r );
virtual void Collide ( Circle &c );
virtual bool Intersects( const Shape& s ) const;
virtual bool Intersects( const Rectangle& s ) const;
virtual bool IsAlive( void ) const;
virtual float Mass( void ) const;
protected:
virtual void Draw( void ) const;
Vec2f* mWidthHeight;
Vec3f mColor;
private:
Vec2f mNorth;
Vec2f mSouth;
Vec2f mEast;
Vec2f mWest;
void InitDirections();
};