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.