From 876510bc5285487dbbb31c6849840deae55e8786 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 9 Jul 2014 01:01:39 +1000 Subject: [PATCH] The collision check is working --- src/character.hpp | 11 ++--- src/in_world.cpp | 11 +++-- src/utilities/bounding_box.hpp | 77 ++++++++++++++++++++++++++++++++++ src/utilities/check_bounds.cpp | 40 ------------------ src/utilities/check_bounds.hpp | 30 ------------- 5 files changed, 89 insertions(+), 80 deletions(-) create mode 100644 src/utilities/bounding_box.hpp delete mode 100644 src/utilities/check_bounds.cpp delete mode 100644 src/utilities/check_bounds.hpp diff --git a/src/character.hpp b/src/character.hpp index fbf9ba4..76cff19 100644 --- a/src/character.hpp +++ b/src/character.hpp @@ -24,6 +24,7 @@ //components #include "vector2.hpp" +#include "bounding_box.hpp" //graphics #include "sprite_sheet.hpp" @@ -35,10 +36,6 @@ constexpr double CHARACTER_WALKING_SPEED = 140.0; constexpr double CHARACTER_WALKING_MOD = 1.0/sqrt(2.0); -//the bounding boxes for the characters -constexpr double CHARACTER_BOUNDS_WIDTH = 32.0; -constexpr double CHARACTER_BOUNDS_HEIGHT = 32.0; - class Character { public: Character() = default; @@ -56,8 +53,8 @@ public: Vector2 GetOrigin() const { return origin; } Vector2 SetMotion(Vector2 v) { return motion = v; } Vector2 GetMotion() const { return motion; } - Vector2 SetBounds(Vector2 v) { return bounds = v; } - Vector2 GetBounds() const { return bounds; } + BoundingBox SetBoundingBox(BoundingBox b) { return bounds = b; } + BoundingBox GetBoundingBox() const { return bounds; } private: //graphics @@ -66,7 +63,7 @@ private: //position Vector2 origin = {0.0,0.0}; Vector2 motion = {0.0,0.0}; - Vector2 bounds = {CHARACTER_BOUNDS_WIDTH,CHARACTER_BOUNDS_HEIGHT}; + BoundingBox bounds; }; #endif diff --git a/src/in_world.cpp b/src/in_world.cpp index 83b00ca..27db90c 100644 --- a/src/in_world.cpp +++ b/src/in_world.cpp @@ -22,7 +22,6 @@ #include "in_world.hpp" #include "utility.hpp" -#include "check_bounds.hpp" #include "region_pager_api.hpp" #include "tile_sheet_api.hpp" @@ -83,6 +82,10 @@ InWorld::InWorld(lua_State* L): lua(L) { //entities character.GetSprite()->LoadSurface(sprites + "elliot2.bmp", 4, 4); + character.SetBoundingBox({0, 0, + character.GetSprite()->GetImage()->GetClipW(), + character.GetSprite()->GetImage()->GetClipH() + }); //setup the camera camera.margin.x = GetScreen()->w / 2 - character.GetSprite()->GetImage()->GetClipW() / 2; @@ -128,8 +131,10 @@ void InWorld::Update(double delta) { continue; } - if (checkOverlap(character.GetOrigin(), character.GetBounds(), wallPoint, {32.0, 32.0})) { - std::cout << "1"; + if ( (character.GetOrigin() + character.GetBoundingBox()).CheckOverlap(wallPoint + BoundingBox{0,0,32,32})) { + character.SetOrigin(character.GetOrigin() - (character.GetMotion() * delta)); + character.SetMotion({0,0}); + character.CorrectSprite(); } } } diff --git a/src/utilities/bounding_box.hpp b/src/utilities/bounding_box.hpp new file mode 100644 index 0000000..1d32887 --- /dev/null +++ b/src/utilities/bounding_box.hpp @@ -0,0 +1,77 @@ +/* Copyright: (c) Kayne Ruse 2013, 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef BOUNDINGBOX_HPP_ +#define BOUNDINGBOX_HPP_ + +#include +#include + +class BoundingBox { +public: + //This is explicitly a POD + int x, y; + int w, h; + + BoundingBox() = default; + BoundingBox(int i, int j, int k, int l): x(i), y(j), w(k), h(l) {}; + ~BoundingBox() = default; + BoundingBox& operator=(BoundingBox const&) = default; + + int Size() { + return std::max(w*h,0); + } + + bool CheckOverlap(BoundingBox rhs) { + return !( + x >= rhs.x + rhs.w || + y >= rhs.y + rhs.h || + rhs.x >= x + w || + rhs.y >= y + h); + } + + BoundingBox CalcOverlap(BoundingBox rhs) { + if (!CheckOverlap(rhs)) { + return {0, 0, 0, 0}; + } + BoundingBox ret; + ret.x = std::max(x, rhs.x); + ret.y = std::max(y, rhs.y); + ret.w = std::min(x+w, rhs.x+rhs.w) - ret.x; + ret.h = std::min(y+h, rhs.y+rhs.h) - ret.y; + return ret; + } +}; + +//This is explicitly a POD +static_assert(std::is_pod::value, "BoundingBox is not a POD"); + +#include "vector2.hpp" + +//operators +inline BoundingBox operator+(BoundingBox b, Vector2 v) { + return {b.x + (int)v.x, b.y + (int)v.y, b.w, b.h}; +} +inline BoundingBox operator+(Vector2 v, BoundingBox b) { + return b + v; +} + +#endif diff --git a/src/utilities/check_bounds.cpp b/src/utilities/check_bounds.cpp deleted file mode 100644 index 0f17948..0000000 --- a/src/utilities/check_bounds.cpp +++ /dev/null @@ -1,40 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2014 - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * - * 3. This notice may not be removed or altered from any source - * distribution. -*/ -#include "check_bounds.hpp" - -bool checkPoint(Vector2 const& origin, Vector2 const& bound, Vector2 const& point) { - return !( - point.x < origin.x || - point.y < origin.y || - point.x >= origin.x + bound.x || - point.y >= origin.y + bound.y - ); -} - -bool checkOverlap(Vector2 const& originOne, Vector2 const& boundOne, Vector2 const& originTwo, Vector2 const& boundTwo) { - return !( - originOne.x >= originTwo.x + boundTwo.x || - originOne.x + boundOne.x < originTwo.x || - originOne.y >= originTwo.y + boundTwo.y || - originOne.y + boundOne.y < originTwo.y - ); -} diff --git a/src/utilities/check_bounds.hpp b/src/utilities/check_bounds.hpp deleted file mode 100644 index e02d6cd..0000000 --- a/src/utilities/check_bounds.hpp +++ /dev/null @@ -1,30 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2014 - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * - * 3. This notice may not be removed or altered from any source - * distribution. -*/ -#ifndef CHECKBOUNDS_HPP_ -#define CHECKBOUNDS_HPP_ - -#include "vector2.hpp" - -bool checkPoint(Vector2 const& origin, Vector2 const& bound, Vector2 const& point); -bool checkOverlap(Vector2 const& originOne, Vector2 const& boundOne, Vector2 const& originTwo, Vector2 const& boundTwo); - -#endif