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

Author Topic: Transforming own Vertex Arrays --> std::vector  (Read 2521 times)

0 Members and 1 Guest are viewing this topic.

Benjaniniooo

  • Guest
Transforming own Vertex Arrays --> std::vector
« on: October 24, 2020, 07:01:48 pm »
I am currently trying to code a physics engine based on SFML's abilities to handle Graphics and Windows. Because i want to give my objects an unknown amount of instances of sf::Vertex to let them be able to accept various types of shapes like circles and polygons, i used std::vectors instead of sf::VertexArrays. The  code that does this looks like that: 

std::vector<sf::Vertex> m_vertices;                //the container that contains all instances of sf::Vertex

template<typename T, typename... Ts>
void setVertices(T vertex, Ts... vertex2){        //this is a variadic function to let the program accept various amounts of instances
m_vertices.push_back((sf::Vertex) vertex);   //this adds the first argument to the vector every time the function is called by itself

setVertices(vertex2...);
}

void setVertices(void){}                                 //this function is needed when there are all arguments processed
 

The question which occurs is, if i am still able to use sf::Transform in combination with the vector full of sf::Vertex instances to move and rotate all vertices arround an origin and how it could possibly work.

Because i yet couldn't find the answer to my question, i am asking in this forum.

Any help that could answer this is very appreciated.
« Last Edit: October 24, 2020, 11:21:33 pm by Benjaniniooo »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Transforming own Vertex Arrays --> std::vector
« Reply #1 on: October 25, 2020, 06:51:31 am »
To draw your vertex array, you'll use this function:
https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1RenderTarget.php#a976bc94057799eb9f8a18ac5fdfd9b73

The last argument, sf::RenderStates, allows to pass the transform, texture, shader, etc.

If you want all this stuff nicely wrapped into a SFML-like class, you can inherit from sf::Drawable and sf::Transformable -- see the dedicated tutorial for more details.

By the way, it's uncommon to use a variadic template function to pass an array (this is usually meant for arguments of different types). Using an initializer_list, for example, would be simpler and slightly more efficient at runtime.

And avoid C-style casts, especially when the cast itself is not needed. If the argument is not a sf::Vertex or something implicitly convertible to it, you won't magically make it compatible with a C-style cast ;)
« Last Edit: October 25, 2020, 06:53:37 am by Laurent »
Laurent Gomila - SFML developer

Benjaniniooo

  • Guest
Re: Transforming own Vertex Arrays --> std::vector
« Reply #2 on: October 25, 2020, 11:12:27 am »
 This is the perfect approach for me. I need the
window.draw
to accept both the render states and the primitive type.

The main problem for me is that i yet don't know how i can implement SFML's abilities with transforming my vertex array. I tried to make a small demo program, but i don't know if it's correct.

class CObject{
std::vector<sf::Vertex> m_vertices;
sf::RenderStates m_renderStates;
};

class CFramework{
void draw(CObject object){
  window.draw(&object.m_vertices[0], object.m_vertices.get_size(), sf::Trianglefan, object.m_renderStates);
}
};

main(){
CObject object;
object.m_vertices.push_back(sf::Vertex(sf::Vector2f(10.f, 10.f), sf::Color::Red));

CFramework framework;
framework.draw(object);

//this demo is missing many parts and should only be used to demonstrate a posssible solution
}
 

If i am correct, depending on what i pass the render state, the object gets rotated, upscaled, downscaled, moved. But does that effect the vector's instances of sf::Vertex? 
« Last Edit: October 25, 2020, 12:58:54 pm by Benjaniniooo »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Transforming own Vertex Arrays --> std::vector
« Reply #3 on: October 25, 2020, 08:51:12 pm »
Quote
If i am correct, depending on what i pass the render state, the object gets rotated, upscaled, downscaled, moved.
Correct.

Quote
But does that effect the vector's instances of sf::Vertex?
No, render-states only change how an object is drawn, they can't modify it. At low-level, it just translates to OpenGL states and commands.
Moreover, window.draw takes a pointer to const sf::Vertex, so... ;)
Laurent Gomila - SFML developer

Benjaniniooo

  • Guest
Re: Transforming own Vertex Arrays --> std::vector
« Reply #4 on: October 25, 2020, 10:05:40 pm »
If i understand properly, the vertex array is not affected by the renderstates, but the way it is drawn on the Windows is. That would also mean that if i rotate the object, my collision detection would use the values provided before the rotation and according to that, the whole thing would get broken.

To prevent that from happening, it would be useful to have some functions given by SFML that does all the trigonomeric functions and that affects the vertex array directly by changing the values in it.

Are these functions available anywhere in the library?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Transforming own Vertex Arrays --> std::vector
« Reply #5 on: October 26, 2020, 08:09:47 am »
Somehow, yes.

The sf::Transform that you pass to your render-states is all you need to compute collisions. Although not the most efficient solution, you can use it to transform all your vertices one by one.

However that's all you get, since SFML is not a physics engine ;)
Laurent Gomila - SFML developer

Benjaniniooo

  • Guest
Re: Transforming own Vertex Arrays --> std::vector
« Reply #6 on: October 27, 2020, 10:30:33 pm »
For those who are interested, i searched a bit and found some interesting pieces of code to rotate points around points:

std::vector<sf::Vertex> vertices;

sf::Vector2f origin(10.f, 10.f);  //the center of the object, where you want to rotate arround
int degree = 10;  //rotate object arround origin for 10 degrees

for(int i = 0; i < vertices.size(); i++){
  double c = cos((PI / 180) * degree);
  double s = sin((PI / 180) * degree);

double x = c * (vertices[i].position.x - origin.x) - s * (vertices[i].position.y - origin.y) + origin.x;
double y = s * (vertices[i].position.x - origin.x) + c * (vertices[i].position.y - origin.y) + origin.y;
}
 

Find more information here https://stackoverflow.com/questions/2259476/rotating-a-point-about-another-point-2d.

Thanks for your help!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Transforming own Vertex Arrays --> std::vector
« Reply #7 on: October 28, 2020, 07:57:45 am »
Have you looked at sf::Transform? Operations such as translating, rotating, scaling (and combining transforms) are already implemented there.
Laurent Gomila - SFML developer

 

anything