Working on the collision classes.
This commit is contained in:
parent
63bb31e728
commit
54e8a27709
9 changed files with 330 additions and 1 deletions
|
@ -58,11 +58,13 @@ add_executable(${PROJECT_NAME}
|
|||
src/drawable.cpp
|
||||
src/sizeratio.cpp
|
||||
src/sizenotifiable.cpp
|
||||
src/horzcollisionbar.cpp
|
||||
src/platform.cpp
|
||||
src/vectormath.cpp
|
||||
src/platformsystem.cpp
|
||||
src/movers/moverworld.cpp
|
||||
src/line.cpp
|
||||
src/collider.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
|
|
59
src/collider.cpp
Normal file
59
src/collider.cpp
Normal file
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
Copyright 2014 Michele "King_DuckZ" Santullo
|
||||
|
||||
This file is part of CloonelJump.
|
||||
|
||||
CloonelJump is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
CloonelJump is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "collider.hpp"
|
||||
#include <vector>
|
||||
#include <ciso646>
|
||||
#include <cassert>
|
||||
#include <algorithm>
|
||||
|
||||
#if defined(WITH_VERBOSE_COLLIDER) && !defined(NDEBUG)
|
||||
#define VERBOSE_COLLIDER
|
||||
#include <iostream>
|
||||
#endif
|
||||
|
||||
namespace cloonel {
|
||||
namespace {
|
||||
} //unnamed namespace
|
||||
|
||||
///--------------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------------
|
||||
Collider::Collider() {
|
||||
}
|
||||
|
||||
///--------------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------------
|
||||
Collider::~Collider() noexcept {
|
||||
}
|
||||
|
||||
///--------------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------------
|
||||
void Collider::RunCollisionTests() {
|
||||
#if defined(VERBOSE_COLLIDER)
|
||||
std::cout << "Collider::RunCollisionTests() starting\n";
|
||||
#endif
|
||||
#if defined(VERBOSE_COLLIDER)
|
||||
std::cout << "Collider::RunCollisionTests() done\n";
|
||||
#endif
|
||||
}
|
||||
} //namespace cloonel
|
||||
|
||||
#if defined(VERBOSE_COLLIDER)
|
||||
#undef VERBOSE_COLLIDER
|
||||
#endif
|
40
src/collider.hpp
Normal file
40
src/collider.hpp
Normal file
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
Copyright 2014 Michele "King_DuckZ" Santullo
|
||||
|
||||
This file is part of CloonelJump.
|
||||
|
||||
CloonelJump is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
CloonelJump is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef id7E6024372BF34999A913A36B6EAB736B
|
||||
#define id7E6024372BF34999A913A36B6EAB736B
|
||||
|
||||
#include "observersmanager.hpp"
|
||||
#include <memory>
|
||||
|
||||
namespace cloonel {
|
||||
class HorzCollisionBar;
|
||||
|
||||
class Collider {
|
||||
public:
|
||||
Collider ( void );
|
||||
~Collider ( void ) noexcept;
|
||||
|
||||
void RunCollisionTests ( void );
|
||||
|
||||
private:
|
||||
};
|
||||
} //namespace cloonel
|
||||
|
||||
#endif
|
|
@ -35,6 +35,7 @@ namespace cloonel {
|
|||
for (auto itMover : m_movers) {
|
||||
itMover->Update(parDelta);
|
||||
}
|
||||
m_collider.RunCollisionTests();
|
||||
}
|
||||
|
||||
///--------------------------------------------------------------------------
|
||||
|
@ -44,4 +45,14 @@ namespace cloonel {
|
|||
itDrawable->Draw();
|
||||
}
|
||||
}
|
||||
|
||||
///--------------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------------
|
||||
void GameplayScene::Destroy() noexcept {
|
||||
}
|
||||
|
||||
///--------------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------------
|
||||
void GameplayScene::OnPrepareDone() {
|
||||
}
|
||||
} //namespace cloonel
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#include <vector>
|
||||
#include <cassert>
|
||||
#include "gamebase.hpp"
|
||||
#include "collider.hpp"
|
||||
|
||||
namespace cloonel {
|
||||
class Mover;
|
||||
|
@ -39,10 +40,14 @@ namespace cloonel {
|
|||
virtual void Destroy ( void ) noexcept;
|
||||
|
||||
protected:
|
||||
Collider* GetCollider ( void );
|
||||
|
||||
private:
|
||||
virtual void OnRender ( void );
|
||||
virtual void OnUpdate ( float parDelta );
|
||||
virtual void OnPrepareDone ( void );
|
||||
|
||||
Collider m_collider;
|
||||
std::vector<Mover*> m_movers;
|
||||
std::vector<const Drawable*> m_drawables;
|
||||
};
|
||||
|
|
117
src/horzcollisionbar.cpp
Normal file
117
src/horzcollisionbar.cpp
Normal file
|
@ -0,0 +1,117 @@
|
|||
/*
|
||||
Copyright 2014 Michele "King_DuckZ" Santullo
|
||||
|
||||
This file is part of CloonelJump.
|
||||
|
||||
CloonelJump is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
CloonelJump is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "horzcollisionbar.hpp"
|
||||
#include "line_helpers.hpp"
|
||||
#include "maths.hpp"
|
||||
#include <cassert>
|
||||
#include <utility>
|
||||
#include <ciso646>
|
||||
|
||||
namespace cloonel {
|
||||
namespace {
|
||||
typedef Line<float, 2> Line2D;
|
||||
|
||||
float calculateOverlappingTime ( float parDeltaT, float parAY, float parBY, float parDeltaA, float parDeltaB ) __attribute__((pure));
|
||||
void DoNothing ( void ) __attribute__((pure));
|
||||
std::pair<bool, Line2D> getOverlap ( float parDeltaT, const Line2D& parA, const Line2D& parB, const float2& parDeltaA, const float2& parDeltaB ) __attribute__((pure));
|
||||
|
||||
///----------------------------------------------------------------------
|
||||
///Calculate the time t at which the two segments (which are assumed to
|
||||
///be horizontal) will lie on the same line. parDeltaT is the duration
|
||||
///of the time frame being considered, parAY and parBY are the starting
|
||||
///y position of the two segments, parDeltaA and parDeltaB are the
|
||||
///distance traveled by segment A and segment B respectively in the
|
||||
///reference time frame.
|
||||
///----------------------------------------------------------------------
|
||||
float calculateOverlappingTime (float parDeltaT, float parAY, float parBY, float parDeltaA, float parDeltaB) {
|
||||
const float deltaDiff = std::max(parDeltaA, parDeltaB) - std::min(parDeltaA, parDeltaB);
|
||||
assert(deltaDiff >= 0.0f);
|
||||
|
||||
if (deltaDiff <= 0.00001f)
|
||||
return 0.0f;
|
||||
else
|
||||
return (parDeltaT * (parBY - parAY)) / (parDeltaA - parDeltaB);
|
||||
}
|
||||
|
||||
///----------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------
|
||||
std::pair<bool, Line2D> getOverlap (float parDeltaT, const Line2D& parA, const Line2D& parB, const float2& parDeltaA, const float2& parDeltaB) {
|
||||
assert(parDeltaT > 0.0f);
|
||||
const float overlapTime = calculateOverlappingTime(parDeltaT, parA.Start().y(), parB.Start().y(), parDeltaA.y(), parDeltaB.y());
|
||||
if (overlapTime < 0.0f or overlapTime > parDeltaT)
|
||||
return std::make_pair<bool, Line2D>(false, Line2D());
|
||||
|
||||
assert(overlapTime <= parDeltaT);
|
||||
const auto midpointA(LerpRange(parA, parDeltaA, overlapTime / parDeltaT));
|
||||
const auto midpointB(LerpRange(parB, parDeltaB, overlapTime / parDeltaT));
|
||||
|
||||
if (midpointA.Start().x() >= midpointB.End().x() or midpointB.Start().x() >= midpointA.End().x())
|
||||
return std::make_pair<bool, Line2D>(false, Line2D());
|
||||
|
||||
const auto& retStart = (midpointA.Start().x() > midpointB.Start().x() ? midpointA.Start() : midpointB.Start());
|
||||
const auto& retEnd = (midpointA.End().x() < midpointB.End().x() ? midpointA.End() : midpointB.End());
|
||||
return std::make_pair(true, Line2D(retStart, retEnd));
|
||||
}
|
||||
|
||||
///----------------------------------------------------------------------
|
||||
///no-op
|
||||
///----------------------------------------------------------------------
|
||||
void DoNothing() {
|
||||
}
|
||||
} //unnamed namespace
|
||||
|
||||
///--------------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------------
|
||||
HorzCollisionBar::HorzCollisionBar (const float2& parFrom, float parLength) :
|
||||
Placeable(parFrom),
|
||||
m_segment(parFrom, float2(1.0f, 0.0f), parLength),
|
||||
m_callback(&DoNothing)
|
||||
{
|
||||
assert(parLength != 0.0f);
|
||||
}
|
||||
|
||||
///--------------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------------
|
||||
void HorzCollisionBar::SetCallback (std::function<void()> parCallback) {
|
||||
m_callback = parCallback;
|
||||
}
|
||||
|
||||
///--------------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------------
|
||||
float2 HorzCollisionBar::To() const {
|
||||
return this->GetPos() + len(m_segment);
|
||||
}
|
||||
|
||||
///--------------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------------
|
||||
bool Collide (float parDeltaT, const HorzCollisionBar& parA, const HorzCollisionBar& parB, Line<float, 2>& parOut) {
|
||||
const auto offsetA = parA.GetPos() - parA.m_segment.Start();
|
||||
const auto offsetB = parB.GetPos() - parB.m_segment.Start();
|
||||
const auto overlap(getOverlap(parDeltaT, parA.m_segment, parB.m_segment, offsetA, offsetB));
|
||||
|
||||
if (overlap.first) {
|
||||
parOut = overlap.second;
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} //namespace cloonel
|
50
src/horzcollisionbar.hpp
Normal file
50
src/horzcollisionbar.hpp
Normal file
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
Copyright 2014 Michele "King_DuckZ" Santullo
|
||||
|
||||
This file is part of CloonelJump.
|
||||
|
||||
CloonelJump is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
CloonelJump is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef id202E39A912AA43E5A224AC77D676F6CA
|
||||
#define id202E39A912AA43E5A224AC77D676F6CA
|
||||
|
||||
#include "vector.hpp"
|
||||
#include "placeable.hpp"
|
||||
#include "line.hpp"
|
||||
#include <functional>
|
||||
|
||||
namespace cloonel {
|
||||
class HorzCollisionBar;
|
||||
|
||||
class HorzCollisionBar : public Placeable {
|
||||
friend bool Collide ( float parDeltaT, const HorzCollisionBar& parA, const HorzCollisionBar& parB, Line<float, 2>& parOut );
|
||||
public:
|
||||
HorzCollisionBar ( const float2& parFrom, float parLength );
|
||||
~HorzCollisionBar ( void ) noexcept = default;
|
||||
|
||||
float2 From ( void ) const { return this->GetPos(); }
|
||||
float2 To ( void ) const;
|
||||
|
||||
void SetCallback ( std::function<void()> parCallback );
|
||||
|
||||
private:
|
||||
Line<float, 2> m_segment;
|
||||
std::function<void()> m_callback;
|
||||
};
|
||||
|
||||
bool Collide ( float parDeltaT, const HorzCollisionBar& parA, const HorzCollisionBar& parB, Line<float, 2>& parOut ) __attribute__((pure));
|
||||
} //namespace cloonel
|
||||
|
||||
#endif
|
|
@ -30,7 +30,7 @@ namespace cloonel {
|
|||
typedef Vector<T, S> Point;
|
||||
typedef T Scalar;
|
||||
|
||||
Line ( void ) = default;
|
||||
Line ( void ) {}
|
||||
Line ( const Line& parOther );
|
||||
Line ( const Point& parStart, const Point& parEnd );
|
||||
Line ( const Point& parStart, const Point& parDirection, Scalar parLength ) : Line(parStart, parDirection * parLength) { }
|
||||
|
|
45
src/maths.hpp
Normal file
45
src/maths.hpp
Normal file
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
Copyright 2014 Michele "King_DuckZ" Santullo
|
||||
|
||||
This file is part of CloonelJump.
|
||||
|
||||
CloonelJump is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
CloonelJump is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef id44C452F5B87A4993B127EFE654837C7D
|
||||
#define id44C452F5B87A4993B127EFE654837C7D
|
||||
|
||||
namespace cloonel {
|
||||
template <typename T, typename U>
|
||||
T Lerp ( const T& parStart, const T& parEnd, const U& parPercent ) __attribute__((pure));
|
||||
template <typename T, typename R, typename U>
|
||||
T LerpRange ( const T& parStart, const R& parRange, const U& parPercent ) __attribute__((pure));
|
||||
|
||||
///--------------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------------
|
||||
template <typename T, typename U>
|
||||
T Lerp (const T& parStart, const T& parEnd, const U& parPercent) {
|
||||
return parStart + parPercent * (parEnd - parStart);
|
||||
}
|
||||
|
||||
///--------------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------------
|
||||
template <typename T, typename R, typename U>
|
||||
T LerpRange (const T& parStart, const R& parRange, const U& parPercent) {
|
||||
return parStart + parPercent * parRange;
|
||||
}
|
||||
} //namespace cloonel
|
||||
|
||||
#endif
|
Loading…
Reference in a new issue