[Add] Adding axis aligned bounding boxs for collision detection. Needed to commit to pull.
This commit is contained in:
parent
51502ccd3a
commit
1e1149aabc
@ -18,7 +18,8 @@ HEADERS += ../src/Actor/Player.h \
|
|||||||
../src/Sprite/Sprite.h \
|
../src/Sprite/Sprite.h \
|
||||||
../src/System/Debug.h \
|
../src/System/Debug.h \
|
||||||
../src/Texture/Texture.h \
|
../src/Texture/Texture.h \
|
||||||
../src/Global/Constants.h
|
../src/Global/Constants.h \
|
||||||
|
../src/Collision/AABB.h
|
||||||
SOURCES += ../src/Actor/Player.cpp \
|
SOURCES += ../src/Actor/Player.cpp \
|
||||||
../src/Global/Globals.cpp \
|
../src/Global/Globals.cpp \
|
||||||
../src/IO/Input.cpp \
|
../src/IO/Input.cpp \
|
||||||
@ -30,5 +31,6 @@ SOURCES += ../src/Actor/Player.cpp \
|
|||||||
../src/Math/FPS.cpp \
|
../src/Math/FPS.cpp \
|
||||||
../src/Sprite/Sprite.cpp \
|
../src/Sprite/Sprite.cpp \
|
||||||
../src/System/Debug.cpp \
|
../src/System/Debug.cpp \
|
||||||
../src/Texture/Texture.cpp
|
../src/Texture/Texture.cpp \
|
||||||
|
../src/Collision/AABB.cpp
|
||||||
OTHER_FILES +=
|
OTHER_FILES +=
|
||||||
|
@ -15,7 +15,7 @@ void Player::Prepare(void) {
|
|||||||
_playerTexture->Load("../Data/Img/Player.png");
|
_playerTexture->Load("../Data/Img/Player.png");
|
||||||
_player->SetTexture(_playerTexture);
|
_player->SetTexture(_playerTexture);
|
||||||
_player->SetPosition(Vec2(800/2, 600/2));
|
_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) {
|
void Player::Render(void) {
|
||||||
@ -24,8 +24,8 @@ void Player::Render(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Player::ProcessEvents(void) {
|
void Player::ProcessEvents(void) {
|
||||||
float x = _player->GetX();
|
x = _player->GetX();
|
||||||
float y = _player->GetY();
|
y = _player->GetY();
|
||||||
if(KeyStillDown(SDLK_w)) {
|
if(KeyStillDown(SDLK_w)) {
|
||||||
y -= PLAYER_SPEED;
|
y -= PLAYER_SPEED;
|
||||||
_player->SetY(y);
|
_player->SetY(y);
|
||||||
|
@ -17,6 +17,8 @@ public:
|
|||||||
void ProcessEvents(void);
|
void ProcessEvents(void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
float PLAYER_SPEED;
|
float PLAYER_SPEED;
|
||||||
Sprite* _player;
|
Sprite* _player;
|
||||||
Texture* _playerTexture;
|
Texture* _playerTexture;
|
||||||
|
73
src/Collision/AABB.cpp
Normal file
73
src/Collision/AABB.cpp
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
42
src/Collision/AABB.h
Normal file
42
src/Collision/AABB.h
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user