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

Author Topic: adding sf::Vector2f ?  (Read 2958 times)

0 Members and 1 Guest are viewing this topic.

battosaijenkins

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
adding sf::Vector2f ?
« 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!

G.

  • Hero Member
  • *****
  • Posts: 1590
    • View Profile

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: adding sf::Vector2f ?
« Reply #2 on: September 15, 2019, 11:41:07 am »
In addition you can always add your own overloads if SFML does not provide them.
Failing to succeed does not mean failing to progress!

battosaijenkins

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: adding sf::Vector2f ?
« Reply #3 on: September 16, 2019, 06:06:46 am »
Oh that's right overload functions. I completely forgot about that, ok thank you.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: adding sf::Vector2f ?
« Reply #4 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
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

battosaijenkins

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: adding sf::Vector2f ?
« Reply #5 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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: adding sf::Vector2f ?
« Reply #6 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.
Laurent Gomila - SFML developer

battosaijenkins

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: adding sf::Vector2f ?
« Reply #7 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: adding sf::Vector2f ?
« Reply #8 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
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

battosaijenkins

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: adding sf::Vector2f ?
« Reply #9 on: September 17, 2019, 05:51:48 pm »
@Nexus nice! I'll check these out cheers!

 

anything