mirror of
https://github.com/HowestDAE/dae16-VerhulstBram.git
synced 2026-02-04 09:19:19 +01:00
Collision Rework, started adding directions to the collision (WIP)
This commit is contained in:
@@ -4,6 +4,9 @@
|
||||
|
||||
namespace Collision
|
||||
{
|
||||
|
||||
TileCollisionRect::TileCollisionRect(const Point2f& pos, const Point2f& size, WorldTile* tile): pos(pos), size(size), tile(tile)
|
||||
{}
|
||||
bool PointVsRect(const Point2f p, const Collision::CollisionRect& r) {
|
||||
return ( p.x >= r.pos.x && p.y >= r.pos.y && p.x < r.pos.x + r.size.x && p.y < r.pos.y + r.size.y );
|
||||
}
|
||||
@@ -99,16 +102,16 @@ namespace Collision
|
||||
float contact_time = 0.0f;
|
||||
if (DynamicRectVsRect(dynamicRectangle, ElapsedTime, *staticRectangle, contactPoint, contactNormal, contact_time)) {
|
||||
if (contactNormal.y > 0) {
|
||||
dynamicRectangle.contact[0] = staticRectangle;
|
||||
dynamicRectangle.ContactMap[CollisionDirection::Bottom] = staticRectangle;
|
||||
}
|
||||
if (contactNormal.x < 0) {
|
||||
dynamicRectangle.contact[1] = staticRectangle;
|
||||
dynamicRectangle.ContactMap[CollisionDirection::Left] = staticRectangle;
|
||||
}
|
||||
if (contactNormal.y < 0) {
|
||||
dynamicRectangle.contact[2] = staticRectangle;
|
||||
dynamicRectangle.ContactMap[CollisionDirection::Top] = staticRectangle;
|
||||
}
|
||||
if (contactNormal.x > 0) {
|
||||
dynamicRectangle.contact[3] = staticRectangle;
|
||||
dynamicRectangle.ContactMap[CollisionDirection::Right] = staticRectangle;
|
||||
}
|
||||
|
||||
//dynamicRectangle.vel = dynamicRectangle.vel + contactNormal * Point2f(std::abs(dynamicRectangle.vel.x), std::abs(dynamicRectangle.vel.y)) * ( 1 - contact_time );
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <map>
|
||||
|
||||
#include "structs.h"
|
||||
#include "utils.h"
|
||||
@@ -10,6 +11,13 @@ class WorldTile;
|
||||
namespace Collision
|
||||
{
|
||||
|
||||
enum CollisionDirection {
|
||||
Top,
|
||||
Bottom,
|
||||
Left,
|
||||
Right
|
||||
};
|
||||
|
||||
struct CollisionRect
|
||||
{
|
||||
|
||||
@@ -17,19 +25,25 @@ namespace Collision
|
||||
Point2f size;
|
||||
Point2f vel;
|
||||
|
||||
std::array<Collision::CollisionRect*, 4> contact;
|
||||
std::map<CollisionDirection, CollisionRect*> ContactMap;
|
||||
};
|
||||
|
||||
struct TileCollisionRect : public CollisionRect
|
||||
struct TileCollisionRect
|
||||
{
|
||||
TileCollisionRect(const Point2f& pos, const Point2f& size, WorldTile* tile)
|
||||
: CollisionRect{ pos, size, Point2f{ 0, 0 }, { nullptr, nullptr, nullptr, nullptr } }
|
||||
, tile{ tile }
|
||||
{}
|
||||
Point2f pos;
|
||||
Point2f size;
|
||||
Point2f vel;
|
||||
TileCollisionRect(const Point2f& pos, const Point2f& size, WorldTile* tile);
|
||||
WorldTile* tile;
|
||||
std::map<CollisionDirection, TileCollisionRect*> ContactMap;
|
||||
|
||||
bool Contains(Point2f point2_f) {
|
||||
return utils::IsPointInRect(point2_f, Rectf{ pos.x, pos.y, size.x, size.y });
|
||||
}
|
||||
|
||||
Collision::CollisionRect getCollisionRect() {
|
||||
return Collision::CollisionRect{ pos, size, vel };
|
||||
}
|
||||
};
|
||||
|
||||
bool PointVsRect(const Point2f p, const CollisionRect& r);
|
||||
|
||||
Reference in New Issue
Block a user