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

Author Topic: The Vector Functions  (Read 5707 times)

0 Members and 2 Guests are viewing this topic.

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
The Vector Functions
« on: January 23, 2010, 10:31:41 pm »
I see they haven't been implemented. Will they be added soon?
Some Vector functions you probably should do(and code for some of them) are:
Vector2
Code: [Select]
#include <cmath>

T Direction(){// May want to use double or float instead
    return atan2(y, x);
}

T Dot(Vector2<T>& Other){//May want to use double or float instead
    return x*Other.x + y*Other.y;
}

T Length(){
    return sqrt(x*x + y*y);
}

void Normalize(){
    T Length = Length();
    x /= Length;
    y /= Length;
}


Vector3
Code: [Select]
#include <cmath>

Vector3<T>& Cross(Vector3<T>& Other){
    return Vector3<T>(y*Other.z - Other.y*z, z*Other.x - x*Other.z, x*Other.y - y*Other.x);
}

T Dot(Vector3<T>& Other){
    return x*Other.x + y*Other.y + z*Other.z;
}

T Length(){
    return sqrt(x*x + y*y + z*z);
}

void Normalize(){
    T Length = Length();
    x /= Length;
    y /= Length;
    z /= Length;
}


I'll add more functions and examples as I think of them.
I use the latest build of SFML2

K-Bal

  • Full Member
  • ***
  • Posts: 104
    • View Profile
    • pencilcase.bandcamp.com
    • Email
The Vector Functions
« Reply #1 on: January 23, 2010, 11:07:03 pm »
Why are you doing a normalization in your dot-calculation? ;) That should be a user-side decision.
Listen to my band: pencilcase.bandcamp.com

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
The Vector Functions
« Reply #2 on: January 23, 2010, 11:09:34 pm »
Quote from: "K-Bal"
Why are you doing a normalization in your dot-calculation? ;) That should be a user-side decision.
Dot product requires that the lengths are equal, so I needed a way to make them equal. I'm probably going to rewrite it a little to normalize B to the length of A.

EDIT: Done, but I normalize A to the length of B instead.
EDIT: Ah, I misread the Wikipedia article, the lengths don't have to be equal. Fixing...
I use the latest build of SFML2

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
The Vector Functions
« Reply #3 on: January 27, 2010, 05:41:27 am »
Added Cross-Product to Vector3. Laurent, you gonna add these soon?
I use the latest build of SFML2

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
The Vector Functions
« Reply #4 on: January 27, 2010, 08:45:17 am »
Dunno :wink:
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
The Vector Functions
« Reply #5 on: January 28, 2010, 01:59:03 pm »
This discussion isn't new. ;)
http://www.sfml-dev.org/forum/viewtopic.php?t=1937
http://www.sfml-dev.org/forum/viewtopic.php?t=1488
http://www.sfml-dev.org/forum/viewtopic.php?t=1232
http://www.sfml-dev.org/forum/viewtopic.php?t=651

I think it's better to wait with the integration of vector algebra functions into SFML. Several discussions in the past showed that it's not easy to implement corresponding functionality, which is intuitive to use, powerful and yet generic. Especially the last point is an important one, in my opinion – most of the proposals didn't take genericity and extensibility into account. Yours is one of them.

By the way, your code isn't correct C++ (for example you shouldn't return references to local objects, templates don't work like this and variables must be declared).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything