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

Author Topic: Normalization of Vectors  (Read 22349 times)

0 Members and 1 Guest are viewing this topic.

phear-

  • Jr. Member
  • **
  • Posts: 64
    • MSN Messenger - LOApokalypse@hotmail.com
    • View Profile
    • http://gtproductions.org
Normalization of Vectors
« on: August 01, 2009, 05:24:35 pm »
It would be cool if we could have a member function within sf::Vector2f that takes a Vector2 parameter and normalizes the member data x/y and the paramaters x/y. Here's what i'm using, it can be adapted to sf::Vector2f pretty easily.

Vector2.h
Code: [Select]

namespace gtp
{
struct Vector2
{
Vector2();
Vector2(float x, float y);
Vector2& operator=(const Vector2& param);
float x;
float y;
};

Vector2 normalize(Vector2 Target, Vector2 Current);
}


Vector2.cpp
Code: [Select]

namespace gtp
{
Vector2 gtp::normalize(Vector2 Target, Vector2 Current)
{
Vector2 _Normal;
float SlopeX = Target.x - Current.x;
float SlopeY = Target.y - Current.y;
if ((SlopeY == 1) || (SlopeY == -1))
{
_Normal.y = SlopeY;
_Normal.x = SlopeX;
}
else
{
_Normal.y = (SlopeY / SlopeY);
_Normal.x = (SlopeX / SlopeY);
}
return _Normal;
}
}


Normalization is basically finding the delta between 2 coords and making it a unit of one (for unit moving).
Eugene Alfonso
GTP | Twitter

K-Bal

  • Full Member
  • ***
  • Posts: 104
    • View Profile
    • pencilcase.bandcamp.com
    • Email
Normalization of Vectors
« Reply #1 on: August 01, 2009, 05:54:39 pm »
You know that there can be other things in an Vector2 than floats? :D
Listen to my band: pencilcase.bandcamp.com

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Normalization of Vectors
« Reply #2 on: August 01, 2009, 06:08:25 pm »
This is not vector normalization (google/wikipedia it if you don't know what is it exactly).

This function is a very specific one that may be useful to you, but not a good candidate for being generic and integrated into SFML.
Laurent Gomila - SFML developer

phear-

  • Jr. Member
  • **
  • Posts: 64
    • MSN Messenger - LOApokalypse@hotmail.com
    • View Profile
    • http://gtproductions.org
Normalization of Vectors
« Reply #3 on: August 02, 2009, 02:20:53 am »
It's used within the XNA framework in their Vector2 class and I thought it might be used by other people who have to find the delta of 2 coords in a 1 unit distance. (I was told its called normalization and thats what it is called in that framework so my apologies if thats not what its called)

Edit: I just checked Wiki and it is called normalization:
Quote
A unit vector is any vector with a length of one; normally unit vectors are used simply to indicate direction. A vector of arbitrary length can be divided by its length to create a unit vector. This is known as normalizing a vector. A unit vector is often indicated with a hat as in รข.
Eugene Alfonso
GTP | Twitter

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Normalization of Vectors
« Reply #4 on: August 02, 2009, 01:31:14 pm »
This definition is right, but it doesn't match your function ;)

Normalizing is dividing the vector's components by its length so that its new length is 1.
Code: [Select]
Vector2 normalize(const Vector2& source)
{
    float length = sqrt((source.x * source.x) + (source.y * source.y));
    if (length != 0)
        return Vector2(source.x / length, source.y / length);
    else
        return source;
}


Your function is doing something totally different: it computes the delta between 2 points, and then scale it so that its Y component is 1.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Normalization of Vectors
« Reply #5 on: August 13, 2009, 03:21:05 pm »
We have already had a discussion about vector functions (-> Link)...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything