SFML community forums

Help => General => Topic started by: battosaijenkins on September 15, 2019, 05:47:04 am

Title: adding sf::Vector2f ?
Post by: battosaijenkins on September 15, 2019, 05:47:04 am
+Just a quick question,
 
is it possible to add multiple sf::Vector2f? For example I have a apos.x and apos.y in the form of sf::Vector2f apos.
If I have another sf::Vector2f bPos is there a way to .add() them like something equivalent in p5 js? Or do I just do apos.x + bpos.x  and then apos.y + bpos.y etc..? Thanks in advance!
Title: Re: adding sf::Vector2f ?
Post by: G. on September 15, 2019, 09:31:25 am
https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Vector2.php#a72421239823c38a6b780c86a710ead07
Title: Re: adding sf::Vector2f ?
Post by: Geheim on September 15, 2019, 11:41:07 am
In addition you can always add your own overloads (https://en.cppreference.com/w/cpp/language/operators) if SFML does not provide them.
Title: Re: adding sf::Vector2f ?
Post by: battosaijenkins on September 16, 2019, 06:06:46 am
Oh that's right overload functions. I completely forgot about that, ok thank you.
Title: Re: adding sf::Vector2f ?
Post by: Nexus on September 16, 2019, 08:15:12 am
More overloaded operators ;)

Overloaded functions are functions with the same name that can be invoked with different arguments, like this:
sf::Sprite s;
s.setPosition(2.5f, 7.f);               // call with float, float
s.setPosition(sf::Vector2f(2.5f, 7.f)); // call with sf::Vector2f
Title: Re: adding sf::Vector2f ?
Post by: battosaijenkins on September 17, 2019, 04:43:02 am
I apologize, that's what I meant to say overload operators not functions :D. By the way, after much experimentation I can't seem to find a multiplication for vectors.

For example:
Code: [Select]
float a = 5.f,
      b = 6.f,
      x = 8.f,
      y = 9.f;
sf::Vector2f apos(a, b);
sf::Vector2f bpos(x, y);
sf::Vector2f cpos;

cpos = apos + bpos; // 13 and 15
cpos = apos - bpos; // -3 and -3
// however
cpos = apos * bpos; // error no match for operator*
// But a scalar value works such as
cpos = apos * 4.f // output is 20 and 24
Title: Re: adding sf::Vector2f ?
Post by: Laurent on September 17, 2019, 06:32:02 am
Vector multiplication can have multiple mathematical meanings (component-wise, or cross product). So it is typically implemented as functions with explicit names rather than overloaded operator.
Title: Re: adding sf::Vector2f ?
Post by: battosaijenkins on September 17, 2019, 07:47:49 am
Ok thank you. I suppose I don't really need to multiply vectors but thought I'd ask since I was wondering why addition and subtractions were there. I don't really have any use for vector multiplication, at least not yet.
Title: Re: adding sf::Vector2f ?
Post by: Nexus on September 17, 2019, 07:53:44 am
In case you need more vector operations, I've implemented a bunch of them in Thor: :)
http://www.bromeon.ch/libraries/thor/documentation/v2.1/_vector_algebra2_d_8hpp.html
Title: Re: adding sf::Vector2f ?
Post by: battosaijenkins on September 17, 2019, 05:51:48 pm
@Nexus nice! I'll check these out cheers!