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

Author Topic: normalize vector  (Read 4852 times)

0 Members and 1 Guest are viewing this topic.

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
normalize vector
« on: October 12, 2008, 11:25:16 am »
why does this not work:

i have a movement vector telling the direction, and a speed ;
sprite should move direction mousecursor,
but in order to keep my pace i have to normalize the vector, am i right?

Code: [Select]

sf::Vector2f GetDir(sf::Vector2f x)
{
    sf::Vector2f Sprite_Mouse = GetMousePos()  - x;
    float temp =(float) sqrt((Sprite_Mouse.x * Sprite_Mouse.x + Sprite_Mouse.y * Sprite_Mouse.y));
    Sprite_Mouse = Sprite_Mouse / temp;
    return Sprite_Mouse;
}


vector Sprite_mouse should be the vector pointing from

x = Sprite_Position to
GetMousePos = into view-coords converted MousePosition

to normalize i have to calculate the lenght of my vector
this should be sqrt( x^2 + y^2)
when i divide my original vector by this above ( i called it temp)
my vector should have lenght 1;
but trying it my Sprite moves VERY slow;

thx for coming answers

__fastcall

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: normalize vector
« Reply #1 on: October 19, 2008, 06:53:10 am »
Quote from: "ravenheart"

 in order to keep my pace i have to normalize the vector, am i right?


Correct.

Quote from: "ravenheart"

when i divide my original vector by this above ( i called it temp)
my vector should have lenght 1;


Correct.

Quote from: "ravenheart"

but trying it my Sprite moves VERY slow;



It's because your movement vector is only one unit in length.  :wink:
Try placing:  Sprite_Mouse = Sprite_Mouse * 5; before returning Sprite_Mouse, and it should move 5 times faster

Leandro

  • Newbie
  • *
  • Posts: 8
    • MSN Messenger - leostera@hotmail.com
    • View Profile
    • http://www.elrincondelea.com.ar
normalize vector
« Reply #2 on: November 20, 2008, 07:33:53 pm »
hi there,

I was wondering, how should I multiply that vector so that I can use it to control de direction?

can I do something like this:

           
Code: [Select]
m_Bullets[i].Direction.x = m_Bullets[i].Speed * Sprite_Mouse.x ;
            m_Bullets[i].Direction.y = m_Bullets[i].Speed * Sprite_Mouse.y ;


The fact is that my angle is reduced from 30.0f to -30.0f (ergo, 330.0f, but counting backwards to zero and then from 360.0f to 330.0f) but doing that makes my bullets go always in the same X direction...

I tried doing something like:

Code: [Select]
m_Bullets[i].Direction.x = ( (angle>180.f || angle<0.0f)  ?m_Bullets[i].Speed : -m_Bullets[i].Speed ) * Sprite_Mouse.x ;

But it just doesnt work =/...meaning, angle is a var that actually works great that goes from 30.0f to 330.0f backwards.

Any ideas?
Leandro Ostera
My Blags
Who Am I

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
normalize vector
« Reply #3 on: November 20, 2008, 08:14:34 pm »
Your code should have worked the first time, assuming all the types are correct, but anyways, this is how I would've done it; try it and see if it works:
Code: [Select]
sf::Vector2f GetDir(sf::Vector2f x)
{
    sf::Vector2f Sprite_Mouse = GetMousePos() - x;

    Sprite_Mouse = Sprite_Mouse / sqrt(Sprite_Mouse.x * Sprite_Mouse.x + Sprite_Mouse.y * Sprite_Mouse.y) * SPEED;

    return Sprite_Mouse;
}