Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Calculating intersection point between Ray and VertexArray-Shape  (Read 2441 times)

0 Members and 1 Guest are viewing this topic.

Shedex

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Here's the code I recently wrote, but it seems like there's something wrong. The intersection point on the upper right Quad is jittering and the intersection point of the Triangle isn't shown correctly on some placed (pictures attached). I was googling through the whole Internet and because I found no results and I am fairely new to collision-intersection-stuff, I'm hoping to get some help here  :).

#include <iostream>
#include <SFML/Graphics.hpp>
#include <cmath>

using namespace std;
using namespace sf;
 
Vector2f intersectionPoint(Vector2f p1, Vector2f p2, Vector2f p3, Vector2f p4) {
        //found that function in the Internet
        Vector2f ret(-10000, -10000);
        float x1 = p1.x, x2 = p2.x, x3 = p3.x, x4 = p4.x;
        float y1 = p1.y, y2 = p2.y, y3 = p3.y, y4 = p4.y;

        float d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
       
        if (d == 0) return ret;

        float pre = (x1*y2 - y1*x2), post = (x3*y4 - y3*x4);
        float x = (pre * (x3 - x4) - (x1 - x2) * post) / d;
        float y = (pre * (y3 - y4) - (y1 - y2) * post) / d;

        if (x < min(x1, x2) || x > max(x1, x2) ||
                x < min(x3, x4) || x > max(x3, x4)) return ret;
        if (y < min(y1, y2) || y > max(y1, y2) ||
                y < min(y3, y4) || y > max(y3, y4)) return ret;

        ret.x = x;
        ret.y = y;
        return ret;
}

float dist(Vector2f p1, Vector2f p2) {
        return sqrt(
                        (p1.x - p2.x) * (p1.x - p2.x)
                        +
                        (p1.y - p2.y) * (p1.y - p2.y)
                        );
}

bool compare(const Vector2f& toCompare1, const Vector2f& toCompare2, const Vector2f& playerPos) {
        return dist(toCompare1, playerPos) < dist(toCompare2, playerPos)
        ? true
        : false;
}
//if 1 smaller than 2 return true

Vertex& nextVertex(int current, VertexArray& v) {
        return current == v.getVertexCount() - 1
                ? v[0]
                : v[current + 1];
}

int main() {
        RenderWindow window(VideoMode(500, 500), "Lighting with SFML!");
        VertexArray triangle(PrimitiveType::TrianglesStrip), rectangle(PrimitiveType::Quads);
       
        triangle.append(Vertex(Vector2f(100, 150)));
        triangle.append(Vertex(Vector2f(300, 375)));
        triangle.append(Vertex(Vector2f(200, 100)));
       
        rectangle.append(Vertex(Vector2f(300, 50)));
        rectangle.append(Vertex(Vector2f(400, 50)));
        rectangle.append(Vertex(Vector2f(400, 150)));
        rectangle.append(Vertex(Vector2f(300, 150)));

        CircleShape c, player;
        player.setFillColor(Color::Red);
        player.setRadius(5);
        player.setOrigin(Vector2f(5, 5));
        player.setPosition(Vector2f(400, 300));
        c.setFillColor(Color::Red);
        c.setRadius(5);
        c.setOrigin(Vector2f(5, 5));
       
        window.clear(Color::Black);
        window.draw(triangle);
        window.draw(c);
        window.draw(player);
        window.display();

        vector<VertexArray> shapes;
        shapes.push_back(triangle);
        shapes.push_back(rectangle);

        Vector2f iPoint(0, 0), tmp(0, 0), tmp1(0, 0);
        Clock clock;

        while (window.isOpen()) {
                Event event;
                while (window.pollEvent(event))
                        if (event.type == event.Closed)
                                window.close();
               
                Vector2f iPoint(0, 0), tmp(0, 0);
                if (clock.getElapsedTime().asMilliseconds() >= 20) {
                        clock.restart();
                        Vector2i mPos = Mouse::getPosition(window);
                        for (auto x = shapes.begin(); x != shapes.end(); x++) {
                                for (int i = 0; i < x->getVertexCount(); i++) {
                                        tmp = intersectionPoint(
                                                (*x)[i].position,
                                                nextVertex(i, (*x)).position,
                                                player.getPosition(),
                                                Vector2f(
                                                player.getPosition().x + (1000 * (mPos.x - player.getPosition().x)
                                                / dist(Vector2f(mPos), player.getPosition())),
                                                player.getPosition().y + (1000 * (mPos.y - player.getPosition().y)
                                                / dist(Vector2f(mPos), player.getPosition()))
                                                ));
                                        if (dist(tmp, player.getPosition()) < dist(iPoint, player.getPosition()))
                                                iPoint = tmp;
                                }
                        }

                        if (Keyboard::isKeyPressed(Keyboard::W))
                                player.move(0, -1);
                        if (Keyboard::isKeyPressed(Keyboard::S))
                                player.move(0, 1);
                        if (Keyboard::isKeyPressed(Keyboard::D))
                                player.move(1, 0);
                        if (Keyboard::isKeyPressed(Keyboard::A))
                                player.move(-1, 0);

                        c.setPosition(iPoint);
                        iPoint.x = 0; iPoint.y = 0;
                        window.clear(Color::Black);
                        window.draw(triangle);
                        window.draw(rectangle);
                        window.draw(c);
                        window.draw(player);
                        window.display();
                }
        }

        system("pause");
        return 0;
}
 

 

anything