mirror of
https://github.com/HowestDAE/dae16-VerhulstBram.git
synced 2026-02-04 13:29:19 +01:00
Remove Point2f, replace with Vector2f
This commit is contained in:
@@ -7,9 +7,9 @@
|
||||
namespace Collision
|
||||
{
|
||||
|
||||
TileCollisionRect::TileCollisionRect(const Point2f& pos, const Point2f& size, WorldTile* tile): pos(pos), size(size), tile(tile)
|
||||
TileCollisionRect::TileCollisionRect(const Vector2f& pos, const Vector2f& size, WorldTile* tile): pos(pos), size(size), tile(tile)
|
||||
{}
|
||||
bool PointVsRect(const Point2f p, const Collision::CollisionRect& r) {
|
||||
bool PointVsRect(const Vector2f 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 );
|
||||
}
|
||||
|
||||
@@ -17,16 +17,16 @@ namespace Collision
|
||||
return ( r1.pos.x < r2.pos.x + r2.size.x && r1.pos.x + r1.size.x > r2.pos.x && r1.pos.y < r2.pos.y + r2.size.y && r1.pos.y + r1.size.y > r2.pos.y );
|
||||
}
|
||||
|
||||
bool RayVsRect(const Point2f& rayOrigin, const Point2f& rayDirection, const Collision::CollisionRect target, Point2f& contactPoint, Point2f& contactNormal,
|
||||
bool RayVsRect(const Vector2f& rayOrigin, const Vector2f& rayDirection, const Collision::CollisionRect target, Vector2f& contactPoint, Vector2f& contactNormal,
|
||||
float& t_HitNear) {
|
||||
contactNormal = Point2f { 0, 0 };
|
||||
contactPoint = Point2f { 0, 0 };
|
||||
contactNormal = Vector2f { 0, 0 };
|
||||
contactPoint = Vector2f { 0, 0 };
|
||||
|
||||
const Point2f inverseDirection = 1.0f / rayDirection;
|
||||
const Vector2f inverseDirection = 1.0f / rayDirection;
|
||||
|
||||
// Calculate intersections with rectangle bounding axes
|
||||
Point2f t_Near = Point2f{ target.pos.x - rayOrigin.x, target.pos.y - rayOrigin.y } * inverseDirection;
|
||||
Point2f t_Far = Point2f{ target.pos.x + target.size.x - rayOrigin.x, target.pos.y + target.size.y - rayOrigin.y } * inverseDirection;
|
||||
Vector2f t_Near = Vector2f{ target.pos.x - rayOrigin.x, target.pos.y - rayOrigin.y } * inverseDirection;
|
||||
Vector2f t_Far = Vector2f{ target.pos.x + target.size.x - rayOrigin.x, target.pos.y + target.size.y - rayOrigin.y } * inverseDirection;
|
||||
|
||||
if (std::isnan(t_Far.y) || std::isnan(t_Far.x))
|
||||
return false;
|
||||
@@ -57,18 +57,18 @@ namespace Collision
|
||||
|
||||
if (t_Near.x > t_Near.y) {
|
||||
if (inverseDirection.x < 0) {
|
||||
contactNormal = Point2f { 1, 0 };
|
||||
contactNormal = Vector2f { 1, 0 };
|
||||
}
|
||||
else {
|
||||
contactNormal = Point2f { -1, 0 };
|
||||
contactNormal = Vector2f { -1, 0 };
|
||||
}
|
||||
}
|
||||
else if (t_Near.x < t_Near.y) {
|
||||
if (inverseDirection.y < 0) {
|
||||
contactNormal = Point2f { 0, 1 };
|
||||
contactNormal = Vector2f { 0, 1 };
|
||||
}
|
||||
else {
|
||||
contactNormal = Point2f { 0, -1 };
|
||||
contactNormal = Vector2f { 0, -1 };
|
||||
}
|
||||
}
|
||||
// If t_Near == t_Far, collision is diagonal so pointless
|
||||
@@ -77,7 +77,7 @@ namespace Collision
|
||||
}
|
||||
|
||||
bool DynamicRectVsRect(const Collision::CollisionRect& dynamicRectangle, float ElapsedTime, const Collision::CollisionRect& staticRectangle,
|
||||
Point2f& contactPoint, Point2f& contactNormal, float& contactTime) {
|
||||
Vector2f& contactPoint, Vector2f& contactNormal, float& contactTime) {
|
||||
// Check if dynamic rectangle is actually moving - we assume rectangles are NOT in collision to start
|
||||
if (dynamicRectangle.vel.x == 0 && dynamicRectangle.vel.y == 0) {
|
||||
return false;
|
||||
@@ -85,10 +85,10 @@ namespace Collision
|
||||
|
||||
// Expand target rectangle by source dimensions
|
||||
Collision::CollisionRect expandedTarget;
|
||||
expandedTarget.pos = Point2f{staticRectangle.pos.x - (dynamicRectangle.size / 2).x, staticRectangle.pos.y - (dynamicRectangle.size / 2).y};
|
||||
expandedTarget.pos = Vector2f{staticRectangle.pos.x - (dynamicRectangle.size / 2).x, staticRectangle.pos.y - (dynamicRectangle.size / 2).y};
|
||||
expandedTarget.size = staticRectangle.size + dynamicRectangle.size;
|
||||
|
||||
Point2f RayOrigin = dynamicRectangle.pos + dynamicRectangle.size / 2;
|
||||
Vector2f RayOrigin = dynamicRectangle.pos + dynamicRectangle.size / 2;
|
||||
|
||||
if (RayVsRect(RayOrigin, dynamicRectangle.vel * ElapsedTime, expandedTarget, contactPoint, contactNormal, contactTime)) {
|
||||
return ( contactTime >= 0.0f && contactTime < 1.0f );
|
||||
@@ -100,7 +100,7 @@ namespace Collision
|
||||
}
|
||||
|
||||
bool ResolveDynamicRectVsRect(Collision::CollisionRect& dynamicRectangle, float ElapsedTime, Collision::CollisionRect* staticRectangle) {
|
||||
Point2f contactPoint, contactNormal;
|
||||
Vector2f contactPoint, contactNormal;
|
||||
float contact_time = 0.0f;
|
||||
if (DynamicRectVsRect(dynamicRectangle, ElapsedTime, *staticRectangle, contactPoint, contactNormal, contact_time)) {
|
||||
if (contactNormal.y > 0) {
|
||||
@@ -116,7 +116,7 @@ namespace Collision
|
||||
dynamicRectangle.ContactMap[CollisionDirection::Left] = staticRectangle;
|
||||
}
|
||||
|
||||
//dynamicRectangle.vel = dynamicRectangle.vel + contactNormal * Point2f(std::abs(dynamicRectangle.vel.x), std::abs(dynamicRectangle.vel.y)) * ( 1 - contact_time );
|
||||
//dynamicRectangle.vel = dynamicRectangle.vel + contactNormal * Vector2f(std::abs(dynamicRectangle.vel.x), std::abs(dynamicRectangle.vel.y)) * ( 1 - contact_time );
|
||||
dynamicRectangle.vel = dynamicRectangle.vel + contactNormal * -utils::DotProduct(dynamicRectangle.vel, contactNormal) * ( 1 - contact_time );
|
||||
return true;
|
||||
}
|
||||
@@ -126,7 +126,6 @@ namespace Collision
|
||||
bool ResolvePlayerVsRect(Player& player, float ElapsedTime, Collision::CollisionRect* staticRectangle) {
|
||||
CollisionRect rect = player.GetCollisionRect();
|
||||
Collision::ResolveDynamicRectVsRect(rect, ElapsedTime, staticRectangle);
|
||||
|
||||
|
||||
player.SetPosition(rect.pos);
|
||||
player.SetVelocity(rect.vel);
|
||||
|
||||
Reference in New Issue
Block a user