From 1e1149aabccdf0e2f908ba3fe73cb046640528d7 Mon Sep 17 00:00:00 2001 From: Rtch90 Date: Wed, 11 Apr 2012 23:30:22 +0100 Subject: [PATCH] [Add] Adding axis aligned bounding boxs for collision detection. Needed to commit to pull. --- LibDQt/LibDQt.pro | 6 ++-- src/Actor/Player.cpp | 6 ++-- src/Actor/Player.h | 2 ++ src/Collision/AABB.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++ src/Collision/AABB.h | 42 ++++++++++++++++++++++++ 5 files changed, 124 insertions(+), 5 deletions(-) create mode 100644 src/Collision/AABB.cpp create mode 100644 src/Collision/AABB.h diff --git a/LibDQt/LibDQt.pro b/LibDQt/LibDQt.pro index 5dcb4b4..30c59c4 100644 --- a/LibDQt/LibDQt.pro +++ b/LibDQt/LibDQt.pro @@ -18,7 +18,8 @@ HEADERS += ../src/Actor/Player.h \ ../src/Sprite/Sprite.h \ ../src/System/Debug.h \ ../src/Texture/Texture.h \ - ../src/Global/Constants.h + ../src/Global/Constants.h \ + ../src/Collision/AABB.h SOURCES += ../src/Actor/Player.cpp \ ../src/Global/Globals.cpp \ ../src/IO/Input.cpp \ @@ -30,5 +31,6 @@ SOURCES += ../src/Actor/Player.cpp \ ../src/Math/FPS.cpp \ ../src/Sprite/Sprite.cpp \ ../src/System/Debug.cpp \ - ../src/Texture/Texture.cpp + ../src/Texture/Texture.cpp \ + ../src/Collision/AABB.cpp OTHER_FILES += diff --git a/src/Actor/Player.cpp b/src/Actor/Player.cpp index c76c97a..2f49344 100644 --- a/src/Actor/Player.cpp +++ b/src/Actor/Player.cpp @@ -15,7 +15,7 @@ void Player::Prepare(void) { _playerTexture->Load("../Data/Img/Player.png"); _player->SetTexture(_playerTexture); _player->SetPosition(Vec2(800/2, 600/2)); - _player->SetScale(Vec2(4.5f, 4.5f)); + _player->SetScale(Vec2(3.0f, 3.0f)); } void Player::Render(void) { @@ -24,8 +24,8 @@ void Player::Render(void) { } void Player::ProcessEvents(void) { - float x = _player->GetX(); - float y = _player->GetY(); + x = _player->GetX(); + y = _player->GetY(); if(KeyStillDown(SDLK_w)) { y -= PLAYER_SPEED; _player->SetY(y); diff --git a/src/Actor/Player.h b/src/Actor/Player.h index 382f0aa..69950e4 100644 --- a/src/Actor/Player.h +++ b/src/Actor/Player.h @@ -17,6 +17,8 @@ public: void ProcessEvents(void); private: + float x; + float y; float PLAYER_SPEED; Sprite* _player; Texture* _playerTexture; diff --git a/src/Collision/AABB.cpp b/src/Collision/AABB.cpp new file mode 100644 index 0000000..2d31e10 --- /dev/null +++ b/src/Collision/AABB.cpp @@ -0,0 +1,73 @@ +#include "AABB.h" + + +typedef unsigned int DWORD; + +AABB::AABB(void) { + _sprite = 0; + // _max represents the top left. + _max = Vec2(0,0); + // _min is the bottom left. + _min = Vec2D(0,0); +} + +AABB::AABB(Vec2 &min, Vec2 &max) { + _max = max; + _min = min; + // static represent the initial position of the max and min + // so we can position the AABB and retain it's initial values. + _staticMin = min; + _staticMax = max; +} + +AABB::~AABB(void) { + if(_sprite) + delete _sprite; +} + +void AABB::SetRelativePosition(float x, float y) { + _max.x += x; + _min.x += x; + _max.y += y; + _min.y += y; +} + +void AABB::SetPositionOffset(float x, float y) { + // Allow the AABB to be repositioned. + _max.x = _staticMax.x + x; + _min.x = _staticMin.x + x; + _max.y = _staticMax.y + y; + _min.y = _staticMin.y + y; +} + +bool AABB::InCollision(AABB* otherAABB) { + if(_max.x < otherAABB->GetMin().x || _min.x > otherAABB->GetMax().x) { + return false; + } + if(_max.y < otherAABB->GetMin().y || _min.y > otherAABB->GetMax().y) { + return false; + } + return true; +} + +void AABB::CreateAABBFromSprite(const char* filename) { + string tempName; + tempName = filename; + tempName.conatenate(".png"); + _sprite = new Sprite(); + _sprite->LoadSprite(" ", tempName); + + // I have no methods here, hopefully KonoM will have it + // implemented real soon... + float spriteWidth = _sprite->GetWidth(); + float spriteHeight = _sprite->GetHeight(); + + // Find the min, look through until we find a first instance of a white color. + bool found = false; + int color = 0; + for(int width = 0; width < _sprite->GetWidth(); width++) { + for(int height = 0; height < _sprite->GetHeight; height++) { + DWORD offset; + } + } +} diff --git a/src/Collision/AABB.h b/src/Collision/AABB.h new file mode 100644 index 0000000..bd2d029 --- /dev/null +++ b/src/Collision/AABB.h @@ -0,0 +1,42 @@ +#pragma once +#include + +#include "../Math/Vec2.h" +#include "../Sprite/Sprite.h" + +/* + * The axis aligned bounding box contains + * two vectors to represent the top left + * and bottom right of a box. This can + * then be used to calculate collision + * against other bounding box's. + */ + +class AABB { +public: + AABB(void); + AABB(Vec2 &min, Vec2 &max); + ~AABB(void); + + Vec2 GetMax(void) { return _max; } + void SetMax(Vec2 &max) { SetMax(max.x, max.y); } + void SetMax(float maxX, float maxY) { _max.x = maxX; _max.y = maxY; _staticMax = _max; } + + Vec2 GetMin(void) { return _min; } + void SetMin(Vec2 &min) { SetMin(min.x, min.y); } + void SetMin(float minX, float minY) { _min.x = minX; _min.y = minY; _staticMin = _min; } + + void SetRelitivePosition(float x, float y); + void SetPositionOffset(float x, float y); + bool InCollision(AABB* otherAABB); + void CreateAABBFromSprite(const char* filename); + +private: + Vec2 _min; + Vec2 _max; + + Vec2 _staticMin; + Vec2 _staticMax; + + Sprite* _sprite; +};