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

Author Topic: Facing in the direction of velocity vector  (Read 4939 times)

0 Members and 1 Guest are viewing this topic.

Student555

  • Guest
Facing in the direction of velocity vector
« on: September 14, 2016, 06:12:14 am »
Currently have an ai agent that moves along a certain path. But currently have no idea how to rotate the agent to face the direction it is moving in.  The heading vector is calculated the following way (along with Side vector )

Code: [Select]

if(MagnitudeOfVector(m_Velocity) > 0.00000001)
{
CurrentHeading = NormalizeVector(m_Velocity);

Side = Perp(CurrentHeading);
}


Code: [Select]
const sf::Vector2f vMath::Perp(const sf::Vector2f & vec)
{
return sf::Vector2f(-vec.y, vec.x);
}


The agent is an SFML drawable entity, and the above code is in an Update function that calculates the steering behavior.  Below are images of what I'm talking about. Where the green vector is the direction in which the agent is heading and the orange is where the agent is facing.

If anyone can point me in the right direction with pseudo code that would be helpful.

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Facing in the direction of velocity vector
« Reply #1 on: September 14, 2016, 06:42:42 am »
Remember that a (2D) vector can be thought of as a triangle on a plane.

y
|\
|  \
|    \  -> vector
|     \
|___\
x     ^
        angle

angle = arctan(y / x)

Gotta remember your trigonometry. Turns out it's useful. Sometimes. :)

(click to show/hide)
« Last Edit: September 14, 2016, 06:48:14 am by dabbertorres »

Student555

  • Guest
Re: Facing in the direction of velocity vector
« Reply #2 on: September 14, 2016, 08:52:31 am »
dabbertorres, works great for diagonal movement! But it doesn't work for horizontal and vertical.  The agent moves along a varied path (Like the image attached). If I remember correctly from trig, I would have to use asin and acos (y and x respectively). I have tried that and does not seem to work properly, especially since the built in functions only have one parameter.

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Facing in the direction of velocity vector
« Reply #3 on: September 14, 2016, 07:54:49 pm »
The following code is rotating a shape to 45 deg and 90 deg correctly.

(click to show/hide)

Can you make a minimal example (ie, similar to the code I put above) of what you are doing?

Student555

  • Guest
Re: Facing in the direction of velocity vector
« Reply #4 on: September 16, 2016, 04:52:25 am »
Sorry, I managed to erase it and came up with another technique. I then managed to see the code that you posted and wanted to implement that in my code. Only in mine, I'm trying to have the line align in the direction of movement. That way I can subtract one point with another to get the difference, then normalize it as a vector, then apply the atan2 function. Problem is I'm having trouble getting there.

(Agent Update)

Code: [Select]
if(MagnitudeOfVector(m_Velocity) > 0.00000001)
{
CurrentHeading = NormalizeVector(m_Velocity);

lineHeadingVector[1].position = sf::Vector2f(getPosition().x * CurrentHeading.x, getPosition().y * CurrentHeading.y);
lineHeadingVector[1].color    = sf::Color::Red;

Side = Perp(CurrentHeading);
}


I know this is incorrect, but I cannot for the life of me think of a better way
Code: [Select]
sf::Vector2f(getPosition().x * CurrentHeading.x, getPosition().y * CurrentHeading.y);
What do you think?

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Facing in the direction of velocity vector
« Reply #5 on: September 16, 2016, 06:47:30 am »
Ah, I think I see what you're trying to do.

Instead of this:
sf::Vector2f(getPosition().x * CurrentHeading.x, getPosition().y * CurrentHeading.y);

You're looking for this:
getPosition() + CurrentHeading * DesiredLengthOfFacingArrow;

You may want to brush up on some of your vector math.

Side note: you can do "code=cpp" instead of just "code" to get syntax highlighting.

Student555

  • Guest
Re: Facing in the direction of velocity vector
« Reply #6 on: September 22, 2016, 06:36:24 am »
Thanks dabbertorres, have it working.

I only have one more question what is the difference between setRotation and rotate functions. From the documentation, setRotation is to set the orientation, but aren't you doing the same thing when you use the rotate function (Rotate the object)? 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Facing in the direction of velocity vector
« Reply #7 on: September 22, 2016, 07:49:13 am »
They can have the same effect, but setRotation() takes an absolute value (set the rotation to e.g. 45°), while rotate() uses a relative value (add e.g. 45° to the current rotation).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/