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

Author Topic: Help with some trig math  (Read 2337 times)

0 Members and 1 Guest are viewing this topic.

Voroz

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Help with some trig math
« 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.
« Last Edit: October 25, 2015, 12:37:26 pm by Voroz »

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: Help with some trig math
« Reply #1 on: October 25, 2015, 12:04:06 am »
I think getTransform and transformPoint could help you.

Voroz

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Re: Help with some trig math
« Reply #2 on: October 25, 2015, 01:05:28 am »
I think getTransform and transformPoint 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.

Voroz

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Re: Help with some trig math
« Reply #3 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.

Voroz

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Re: Help with some trig math
« Reply #4 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.
« Last Edit: October 25, 2015, 01:51:15 am by Voroz »

Voroz

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Re: Help with some trig math
« Reply #5 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?

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: Help with some trig math
« Reply #6 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)
« Last Edit: October 25, 2015, 02:12:53 am by mkalex777 »

 

anything