Commit 983662da authored by Jonas Johan Solsvik's avatar Jonas Johan Solsvik 🎮
Browse files

Add intersect and distance functions

parent 9ab1b141
#include <cstdio>
#include <cmath>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#define FLOAT_MAX 3.40282347e+38F
struct Thing
{
sf::Vector2f velocity{0, 0};
......@@ -225,6 +228,108 @@ auto physicsTestCollision(
const auto impTopRight = sf::Vector2f{impPos.x + impHalfSize.x, impPos.y - impHalfSize.y};
const auto impBottomLeft = sf::Vector2f{impPos.x - impHalfSize.x, impPos.y + impHalfSize.y};
const auto impBottomRight = sf::Vector2f{impPos.x + impHalfSize.x, impPos.y + impHalfSize.y};
/**
* Now we are ready to test if any of the dt points are within the impacter
*
* |
* | [impTopLeft]---------------------------
* | |
* | |
* | |
* ---[botRight] ---------------|-------->> [dtBotRight] Inside?
* |
* |
* |
* [impBotLeft]--------------------------
*/
const bool insideBottomRight = dtBottomRight.x > impTopLeft.x &&
dtBottomRight.x < impTopRight.x &&
dtBottomRight.y > impTopLeft.y &&
dtBottomRight.y < impBottomLeft.y;
const bool insideBottomLeft = dtBottomLeft.x > impTopLeft.x &&
dtBottomLeft.x < impTopRight.x &&
dtBottomLeft.y > impTopLeft.y &&
dtBottomLeft.y < impBottomLeft.y;
const bool insideTopRight = dtTopRight.x > impTopLeft.x &&
dtTopRight.x < impTopRight.x &&
dtTopRight.y > impTopLeft.y &&
dtTopRight.y < impBottomLeft.y;
const bool insideTopLeft = dtTopLeft.x > impTopLeft.x &&
dtTopLeft.x < impTopRight.x &&
dtTopLeft.y > impTopLeft.y &&
dtTopLeft.y < impBottomLeft.y;
/**
* Now we are ready to find the intersection point
*
* |
* | [D]---------------------------
* | |
* | \ | /
* | \|/
* ------[A] <<<----------------x---------->>> [B]
* /|\
* / | \
* |
* [C]--------------------------
*/
const auto getIntersectionPoint = [](
const sf::Vector2f A,
const sf::Vector2f B,
const sf::Vector2f C,
const sf::Vector2f D) -> sf::Vector2f {
// Line AB represented as a1x + b1y = c1
float a = B.y - A.y;
float b = A.x - B.x;
float c = a * (A.x) + b * (A.y);
// Line CD represented as a2x + b2y = c2
float a1 = D.y - C.y;
float b1 = C.x - D.x;
float c1 = a1 * (C.x) + b1 * (C.y);
// Special case with parallell lines
double det = a * b1 - a1 * b;
if (det == 0)
{
return sf::Vector2f{FLOAT_MAX, FLOAT_MAX};
}
// Intersection x,y
float x = (b1 * c - b * c1) / det;
float y = (a * c1 - a1 * c) / det;
return sf::Vector2f{x, y};
};
// Function to calculate distance
const auto distance = [](const sf::Vector2f A, const sf::Vector2f B) -> float {
// Calculating distance
return std::sqrt(std::pow(B.x - A.x, 2) +
std::pow(B.y - A.y, 2) * 1.0);
};
const float MIN = FLOAT_MAX;
if (insideBottomRight)
{
// @see https://www.tutorialspoint.com/program-for-point-of-intersection-of-two-lines-in-cplusplus
const auto intersect = getIntersectionPoint(bottomRight,
dtBottomRight,
impBottomLeft,
impTopLeft);
}
if (insideBottomLeft)
{
}
if (insideTopRight)
{
}
if (insideTopLeft)
{
}
}
auto animationUpdate(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment