SFML community forums

Help => General => Topic started by: Voroz on October 24, 2015, 11:34:47 pm

Title: Help with some trig math
Post by: Voroz on October 24, 2015, 11:34:47 pm
Hi. I'm trying to render some stuff faster with help of a vertexarray. Problem is that when i get the 4 vertexes of my sf::RectangleShapes i get the positions without rotation applied. Also the positions are given without accounting for my setOrigin. So i did some trigonometry to try and get what i wanted: This is the code:

class cVertexRendering{
    public:
    void addRect(sf::RectangleShape Rect){
        for (int i = 0; i < Rect.getPointCount(); i++){
            float x = Rect.getPoint(i).x - Rect.getSize().x/2;
            float y = Rect.getPoint(i).y - Rect.getSize().y/2;
            float length = sqrt(powf(x, 2) + powf(y, 2));

            float actualVertexAngle;
            actualVertexAngle = atan2f(y, x);
            //Convert to 0-360 from -180 +180
            if (actualVertexAngle < 0){
                actualVertexAngle += 2*PI;
            }
            //Add rect rotation
            actualVertexAngle += DEGTORAD*Rect.getRotation();

            sf::Vector2f actualVertexPosition;
            actualVertexPosition.x = length*cosf(actualVertexAngle);
            actualVertexPosition.y = length*sinf(actualVertexAngle);
            sf::Vertex vertex = sf::Vertex(sf::Vector2f(Rect.getPosition().x + actualVertexPosition.x,
                                                        Rect.getPosition().y + actualVertexPosition.y),
                                                        Rect.getFillColor());
            vertexArray.append(vertex);
        }
    };
    sf::VertexArray getVertexArray(){
        return vertexArray;
    }
    void clear(){
        vertexArray.clear();
    }
    private:
    sf::VertexArray vertexArray = sf::VertexArray(sf::Quads);
}vertexRendering;

What happens is that i get almost a triangular shape instead of the rectangle shape that's supposed to happen. Also the rotation doesn't match. Any idea where i go wrong? been looking at this for hours.
Title: Re: Help with some trig math
Post by: G. on October 25, 2015, 12:04:06 am
I think getTransform (http://www.sfml-dev.org/documentation/2.3.2/classsf_1_1Transformable.php#a3b48c3362e3e2c14fef7551252deb7bb) and transformPoint (http://www.sfml-dev.org/documentation/2.3.2/classsf_1_1Transform.php#af20913c6a27087c26192c116397ab40a) could help you.
Title: Re: Help with some trig math
Post by: Voroz on October 25, 2015, 01:05:28 am
I think getTransform (http://www.sfml-dev.org/documentation/2.3.2/classsf_1_1Transformable.php#a3b48c3362e3e2c14fef7551252deb7bb) and transformPoint (http://www.sfml-dev.org/documentation/2.3.2/classsf_1_1Transform.php#af20913c6a27087c26192c116397ab40a) could help you.
Ok. I'm not sure how i'm supposed to use those but even if i did i'd still like to know what's wrong with my code. I edited my post with some updated code, but it's still not working.
Title: Re: Help with some trig math
Post by: Voroz on October 25, 2015, 01:39:33 am
I found something with debugger.

actualVertexPosition.x = length*cosf(actualVertexAngle);
actualVertexPosition.y = length*sinf(actualVertexAngle);

These give me the wrong values.
for example length*sinf(actualVertexAngle); gives me ~7.75 as answer when length is 87 and degree is 135. Calculator gives me ~62 as answer which, if you think about it, should be the correct one.
Title: Re: Help with some trig math
Post by: Voroz on October 25, 2015, 01:45:01 am
Alright it works now. I didn't realize that cos and sin takes radians in c++. On the calculator it takes degrees.
Edited first post with working code.
Title: Re: Help with some trig math
Post by: Voroz on October 25, 2015, 02:57:41 am
Another question. This code calls tan, cos, sin 500 times per frame which is a bit costly. Will the Transformpoint work better performance wise? Or should i attempt to implement a sine / cosine table, or is there a better fast and accurate way?
Title: Re: Help with some trig math
Post by: mkalex777 on October 25, 2015, 02:08:08 am
Another question. This code calls tan, cos, sin 500 times per frame which is a bit costly. Will the Transformpoint work better performance wise? Or should i attempt to implement a sine / cosine table, or is there a better fast and accurate way?

calculating sine and cosine needs some time to be processed, you can replace it with single calculation before loop and then apply rotation transform in the loop, see this example which renders arc: http://en.sfml-dev.org/forums/index.php?topic=19150.0  (GeometryHelper.CreateArcGeometry)