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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Shedex

Pages: [1]
1
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;
}
 

2
System / Re: Creating a sf::Thread inside a class
« on: June 22, 2016, 04:56:03 pm »
You should have a look at the following sf::Thread tutorial sections:
- "How to create a thread" -> member functions
- "Common mistakes"
I declared it now as a pointer in the private section and created a new instance inside the function, but I still get the same error, even when not declaring it locally:
'&' : illegal operation on bound member function expression
'sf::Thread' : no appropriate default constructor available

Class test {
private:
sf::Thread* thread;
public:
void b() {
//do stuff...
}
void a() {
thread = new sf::Thread(&b)
thread.launch();
}
};
 
I may be stupid but I still dont see the mistake...can you give me a short piece of code that explains it better or that works?

3
System / Re: AW: Creating a sf::Thread inside a class
« on: June 22, 2016, 04:14:12 pm »
Check the documentation for how to use sf::Thread
I did, but I didnt find the mistake. A code example would be nice, because Im a beginner

4
System / Creating a sf::Thread inside a class
« on: June 22, 2016, 04:06:07 pm »
I have a class with function a and function b. If function a is called, it should create a Thread to function b and then launch it, but I got the error "no appropiate default constructor available". Heres my code:

Class test {
public:
void b() {
//do stuff...
}
void a() {
sf::Thread thread(&b);
thread.launch();
}
};
 

Pages: [1]
anything