#include <math.h>
#include "math/vector.h"
#include "math/aatriangle.h"
-#include "math/rectangle.h"
+#include "math/rect.h"
#include "collision_hit.h"
static const float DELTA = .0001;
bool
-Collision::rectangle_rectangle(CollisionHit& hit, const Rectangle& r1,
- const Vector& movement, const Rectangle& r2)
+Collision::rectangle_rectangle(CollisionHit& hit, const Rect& r1,
+ const Vector& movement, const Rect& r2)
{
if(r1.p2.x < r2.p1.x || r1.p1.x > r2.p2.x)
return false;
}
bool
-Collision::rectangle_aatriangle(CollisionHit& hit, const Rectangle& rect,
+Collision::rectangle_aatriangle(CollisionHit& hit, const Rect& rect,
const Vector& movement, const AATriangle& triangle)
{
- if(!rectangle_rectangle(hit, rect, movement, (const Rectangle&) triangle))
+ if(!rectangle_rectangle(hit, rect, movement, (const Rect&) triangle))
return false;
Vector normal;
#define __COLLISION_H__
class Vector;
-class Rectangle;
+class Rect;
class AATriangle;
class CollisionHit;
/** does collision detection between 2 rectangles. Returns true in case of
* collision and fills in the hit structure then.
*/
- static bool rectangle_rectangle(CollisionHit& hit, const Rectangle& r1,
- const Vector& movement, const Rectangle& r2);
+ static bool rectangle_rectangle(CollisionHit& hit, const Rect& r1,
+ const Vector& movement, const Rect& r2);
/** does collision detection between a rectangle and an axis aligned triangle
* Returns true in case of a collision and fills in the hit structure then.
*/
- static bool rectangle_aatriangle(CollisionHit& hit, const Rectangle& rect,
+ static bool rectangle_aatriangle(CollisionHit& hit, const Rect& rect,
const Vector& movement, const AATriangle& triangle);
};
objects.push_back(wrapper);
wrapper->id = objects.size()-1;
- const Rectangle& bbox = object->bbox;
+ const Rect& bbox = object->bbox;
for(float y = bbox.p1.y; y < bbox.p2.y; y += cell_height) {
for(float x = bbox.p1.x; x < bbox.p2.x; x += cell_width) {
int gridx = int(x / cell_width);
}
#endif
- const Rectangle& bbox = wrapper->dest;
+ const Rect& bbox = wrapper->dest;
for(float y = bbox.p1.y; y < bbox.p2.y; y += cell_height) {
for(float x = bbox.p1.x; x < bbox.p2.x; x += cell_width) {
int gridx = int(x / cell_width);
{
// FIXME not optimal yet... should leave the gridcells untouched that don't
// need to be changed.
- const Rectangle& obbox = wrapper->dest;
+ const Rect& obbox = wrapper->dest;
for(float y = obbox.p1.y; y < obbox.p2.y; y += cell_height) {
for(float x = obbox.p1.x; x < obbox.p2.x; x += cell_width) {
int gridx = int(x / cell_width);
}
}
- const Rectangle& nbbox = wrapper->object->bbox;
+ const Rect& nbbox = wrapper->object->bbox;
for(float y = nbbox.p1.y; y < nbbox.p2.y; y += cell_height) {
for(float x = nbbox.p1.x; x < nbbox.p2.x; x += cell_width) {
int gridx = int(x / cell_width);
{
iterator_timestamp++;
- const Rectangle& bbox = wrapper->object->bbox;
+ const Rect& bbox = wrapper->object->bbox;
for(float y = bbox.p1.y - cell_height; y < bbox.p2.y + cell_height; y += cell_height) {
for(float x = bbox.p1.x - cell_width; x < bbox.p2.x + cell_width; x += cell_width) {
int gridx = int(x / cell_width);
MovingObject* object1 = wrapper->object;
MovingObject* object2 = wrapper2->object;
- Rectangle dest1 = object1->get_bbox();
+ Rect dest1 = object1->get_bbox();
dest1.move(object1->get_movement());
- Rectangle dest2 = object2->get_bbox();
+ Rect dest2 = object2->get_bbox();
dest2.move(object2->get_movement());
Vector movement = object1->get_movement() - object2->get_movement();
struct ObjectWrapper
{
MovingObject* object;
- Rectangle dest;
+ Rect dest;
/** (pseudo) timestamp. When reading from the grid the timestamp is
* changed so that you can easily avoid reading an object multiple times
* when it is in several cells that you check.
#ifndef __COLLISION_GRID_ITERATOR_H__
#define __COLLISION_GRID_ITERATOR_H__
-#include "math/rectangle.h"
+#include "math/rect.h"
class CollisionGrid;
class CollisionGridIterator
{
public:
- CollisionGridIterator(CollisionGrid& newgrid, const Rectangle& bbox)
+ CollisionGridIterator(CollisionGrid& newgrid, const Rect& bbox)
: grid(newgrid)
{
start_x = int(bbox.p1.x / grid.cell_width) - 2;
#ifndef __AATRIANGLE_H__
#define __AATRIANGLE_H__
-#include "rectangle.h"
+#include "rect.h"
/**
* An axis aligned triangle (ie. a triangle where 2 sides are parallel to the x-
* and y-axis.
*/
-class AATriangle : public Rectangle
+class AATriangle : public Rect
{
public:
/** Directions:
{
}
AATriangle(const Vector& v1, const Vector& v2, int newdir)
- : Rectangle(v1, v2), dir(newdir)
+ : Rect(v1, v2), dir(newdir)
{
}
--- /dev/null
+#ifndef __RECT_H__
+#define __RECT_H__
+
+#include <assert.h>
+#include "vector.h"
+
+/** This class represents a rectangle.
+ * (Implementation Note) We're using upper left and lower right point instead of
+ * upper left and width/height here, because that makes the collision dectection
+ * a little bit efficienter.
+ */
+class Rect
+{
+public:
+ Rect()
+ { }
+
+ Rect(const Vector& np1, const Vector& np2)
+ : p1(np1), p2(np2)
+ {
+ }
+
+ Rect(float x1, float y1, float x2, float y2)
+ : p1(x1, y1), p2(x2, y2)
+ {
+ assert(p1.x <= p2.x && p1.y <= p2.y);
+ }
+
+ float get_width() const
+ { return p2.x - p1.x; }
+
+ float get_height() const
+ { return p2.y - p1.y; }
+
+ Vector get_middle() const
+ { return Vector((p1.x+p2.x)/2, (p1.y+p2.y)/2); }
+
+ void set_pos(const Vector& v)
+ {
+ move(v-p1);
+ }
+
+ void set_height(float height)
+ {
+ p2.y = p1.y + height;
+ }
+ void set_width(float width)
+ {
+ p2.x = p1.x + width;
+ }
+ void set_size(float width, float height)
+ {
+ set_width(width);
+ set_height(height);
+ }
+
+ void move(const Vector& v)
+ {
+ p1 += v;
+ p2 += v;
+ }
+
+ bool inside(const Vector& v) const
+ {
+ return v.x >= p1.x && v.y >= p1.y && v.x < p2.x && v.y < p2.y;
+ }
+ bool inside(const Rect& other) const
+ {
+ if(p1.x >= other.p2.x || other.p1.x >= p2.x)
+ return false;
+ if(p1.y >= other.p2.y || other.p1.y >= p2.y)
+ return false;
+
+ return true;
+ }
+
+ // leave these 2 public to safe the headaches of set/get functions for such
+ // simple things :)
+
+ /// upper left edge
+ Vector p1;
+ /// lower right edge
+ Vector p2;
+};
+
+#endif
+
+++ /dev/null
-#ifndef __RECTANGLE_H__
-#define __RECTANGLE_H__
-
-#include <assert.h>
-#include "vector.h"
-
-/** This class represents a rectangle.
- * (Implementation Note) We're using upper left and lower right point instead of
- * upper left and width/height here, because that makes the collision dectection
- * a little bit efficienter.
- */
-class Rectangle
-{
-public:
- Rectangle()
- { }
-
- Rectangle(const Vector& np1, const Vector& np2)
- : p1(np1), p2(np2)
- {
- }
-
- Rectangle(float x1, float y1, float x2, float y2)
- : p1(x1, y1), p2(x2, y2)
- {
- assert(p1.x <= p2.x && p1.y <= p2.y);
- }
-
- float get_width() const
- { return p2.x - p1.x; }
-
- float get_height() const
- { return p2.y - p1.y; }
-
- Vector get_middle() const
- { return Vector((p1.x+p2.x)/2, (p1.y+p2.y)/2); }
-
- void set_pos(const Vector& v)
- {
- move(v-p1);
- }
-
- void set_height(float height)
- {
- p2.y = p1.y + height;
- }
- void set_width(float width)
- {
- p2.x = p1.x + width;
- }
- void set_size(float width, float height)
- {
- set_width(width);
- set_height(height);
- }
-
- void move(const Vector& v)
- {
- p1 += v;
- p2 += v;
- }
-
- bool inside(const Vector& v) const
- {
- return v.x >= p1.x && v.y >= p1.y && v.x < p2.x && v.y < p2.y;
- }
- bool inside(const Rectangle& other) const
- {
- if(p1.x >= other.p2.x || other.p1.x >= p2.x)
- return false;
- if(p1.y >= other.p2.y || other.p1.y >= p2.y)
- return false;
-
- return true;
- }
-
- // leave these 2 public to safe the headaches of set/get functions for such
- // simple things :)
-
- /// upper left edge
- Vector p1;
- /// lower right edge
- Vector p2;
-};
-
-#endif
-
#include "game_object.h"
#include "collision_hit.h"
#include "math/vector.h"
-#include "math/rectangle.h"
+#include "math/rect.h"
class Sector;
class CollisionGrid;
}
/** returns the bounding box of the Object */
- const Rectangle& get_bbox() const
+ const Rect& get_bbox() const
{
return bbox;
}
/** The bounding box of the object (as used for collision detection, this
* isn't necessarily the bounding box for graphics)
*/
- Rectangle bbox;
+ Rect bbox;
/** The movement that will happen till next frame
*/
Vector movement;
* layer where it should be drawn and a texture.
* The coordinate system used here is a virtual one. It would be a bad idea to
* populate whole levels with particles. So we're using a virtual rectangle
- * here that is tiled onto the level when drawing. This rectangle has the size
+ * here that is tiled onto the level when drawing. This rect.has the size
* (virtual_width, virtual_height). We're using modulo on the particle
* coordinates, so when a particle leaves left, it'll reenter at the right
* side.
#include "collision_grid_iterator.h"
#include "object_factory.h"
#include "collision.h"
-#include "math/rectangle.h"
+#include "math/rect.h"
#include "math/aatriangle.h"
#include "object/coin.h"
#include "object/block.h"
camera->reset(player->get_pos());
}
-Rectangle
+Rect
Sector::get_active_region()
{
- return Rectangle(
+ return Rect(
camera->get_translation() - Vector(1600, 1200),
camera->get_translation() + Vector(1600, 1200));
}
int max_y = int(y2+1);
CollisionHit temphit, hit;
- Rectangle dest = object->get_bbox();
+ Rect dest = object->get_bbox();
dest.move(object->movement);
hit.time = -1; // represents an invalid value
for(int x = starttilex; x*32 < max_x; ++x) {
hit = temphit;
}
} else { // normal rectangular tile
- Rectangle rect(x*32, y*32, (x+1)*32, (y+1)*32);
+ Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
if(Collision::rectangle_rectangle(temphit, dest,
object->movement, rect)) {
if(temphit.time > hit.time)
Sector::collision_object(MovingObject* object1, MovingObject* object2)
{
CollisionHit hit;
- Rectangle dest1 = object1->get_bbox();
+ Rect dest1 = object1->get_bbox();
dest1.move(object1->get_movement());
- Rectangle dest2 = object2->get_bbox();
+ Rect dest2 = object2->get_bbox();
dest2.move(object2->get_movement());
Vector movement = object1->get_movement() - object2->get_movement();
}
bool
-Sector::inside(const Rectangle& rect) const
+Sector::inside(const Rect& rect) const
{
if(rect.p1.x > solids->get_width() * 32
|| rect.p1.y > solids->get_height() * 32
class Writer;
}
-class Rectangle;
+class Rect;
class Sprite;
class GameObject;
class Player;
{ return name; }
/// tests if a given rectangle is inside the sector
- bool inside(const Rectangle& rectangle) const;
+ bool inside(const Rect& rectangle) const;
void play_music(MusicType musictype);
MusicType get_music_type();
typedef std::vector<SpawnPoint*> SpawnPoints;
SpawnPoints spawnpoints;
- Rectangle get_active_region();
+ Rect get_active_region();
private:
void fix_old_tiles();
if(cur->get_type() == lisp::Lisp::TYPE_STRING) {
std::string file;
cur->get(file);
- imagespecs.push_back(ImageSpec(file, Rectangle(0, 0, 0, 0)));
+ imagespecs.push_back(ImageSpec(file, Rect(0, 0, 0, 0)));
} else if(cur->get_type() == lisp::Lisp::TYPE_CONS &&
cur->get_car()->get_type() == lisp::Lisp::TYPE_SYMBOL) {
const lisp::Lisp* ptr = cur->get_cdr();
ptr->get_car()->get(y); ptr = ptr->get_cdr();
ptr->get_car()->get(w); ptr = ptr->get_cdr();
ptr->get_car()->get(h);
- imagespecs.push_back(ImageSpec(file, Rectangle(x, y, x+w, y+h)));
+ imagespecs.push_back(ImageSpec(file, Rect(x, y, x+w, y+h)));
} else {
std::cerr << "Expected string or list in images tag.\n";
continue;
#include <vector>
#include "video/surface.h"
-#include "math/rectangle.h"
+#include "math/rect.h"
#include "lisp/lisp.h"
/**
unsigned int id;
struct ImageSpec {
- ImageSpec(const std::string& newfile, const Rectangle& newrect)
+ ImageSpec(const std::string& newfile, const Rect& newrect)
: file(newfile), rect(newrect)
{ }
std::string file;
- Rectangle rect;
+ Rect rect;
};
std::vector<ImageSpec> imagespecs;
std::vector<Surface*> images;
message_displayed = false;
}
-SecretAreaTrigger::SecretAreaTrigger(const Rectangle& area)
+SecretAreaTrigger::SecretAreaTrigger(const Rect& area)
{
bbox = area;
message = "You found a secret area!";
{
public:
SecretAreaTrigger(const lisp::Lisp& reader);
- SecretAreaTrigger(const Rectangle& area);
+ SecretAreaTrigger(const Rect& area);
~SecretAreaTrigger();
void write(lisp::Writer& writer);
#define SUPERTUX_TRIGGER_BASE_H
#include "moving_object.h"
-#include "math/rectangle.h"
+#include "math/rect.h"
#include "sprite/sprite.h"
class Player;