Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vector2 class in C++

Tags:

c++

vector

In C++, is there a Vector2 class and if so, what do I need to include to use it?

I want to use this to store 2-dimensional vectors such as position or velocity of a particle.

like image 600
Ben Avatar asked Jan 18 '23 16:01

Ben


1 Answers

Here you go.

struct Vector2
{
  float x;
  float y;
};

Or alternatively you can use std::pair<float, float>.

Then you'll want to learn more about Structure Of Arrays (SOA) vs Arrays of Structures (AOS) and how it impacts the performance of your code.

Particle systems would typically go SOA.

Finally here is a series of blog posts on AOS & SOA applied to the implementation of a particle system.

EDIT: there are nice math libraries out there like Eigen or glm that would define such types for you along with many useful algorithms (with performant implementations).

like image 166
Gregory Pakosz Avatar answered Jan 28 '23 18:01

Gregory Pakosz