开发者

Vector2 class in C++

开发者 https://www.devze.com 2023-04-06 05:28 出处:网络
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.Here you g

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.


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).


There is no "vector2" class in the standard libraries. There is a pair class that would suit your needs, but for this scenario it would probable be best to create your own vector class(because then you get to have the variables named x and y, rather than first and second), e.g.

class Vector2
{
public:
   double x;
   double y;

   Vector2( double x, double y);
   ... etc
}

You can then overload the operator +, add functions for finding cross/dot product, etc.

The std::vector class is NOT what you need. The std::vector class is pretty much just a replacement for C malloced arrays.


There is std::pair in the header <utility>. It doesn't support vector arithmetic, though.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号