[Resolved]what does the "Vector diff" mean ?

Ask for help regarding any technical issue or report any bug or OS independent issues.
Post Reply
dlqingxi
Newly Registered User
Posts: 7
Joined: 04 Jan 2013, 08:44

[Resolved]what does the "Vector diff" mean ?

Post by dlqingxi »

I read the mana client's source code recently.
There is a code snippet in src/being.cpp (about line 823th)

Code: Select all

  // The deplacement of a point along a vector is calculated
            // using the Unit Vector (â) multiplied by the point speed.
            // â = a / ||a|| (||a|| is the a length.)
            // Then, diff = (dir/||dir||) * speed.
            const Vector normalizedDir = dir.normalized();
            Vector diff(normalizedDir.x * mSpeedPixelsPerTick.x,
                        normalizedDir.y * mSpeedPixelsPerTick.y);

            // Test if we don't miss the destination by a move too far:
            if (diff.length() > distance)
            {
                setPosition(mPos + dir);
My question is : what is the vector diff use for ?
From the context , I thought it should record the movement distance per tick.
But why "normalizedDir.x * mSpeedPixelsPerTick.x "?
Shouldn't it be mSpeedPixelsPerTick.x ?

Can someone tell me why "diff = (dir/||dir||) * speed" ?
Thank you very much.
Last edited by dlqingxi on 09 Mar 2013, 11:51, edited 1 time in total.
User avatar
Crush
TMW Adviser
TMW Adviser
Posts: 8046
Joined: 25 Aug 2005, 16:08
Location: Germany

Re: what does the "Vector diff" mean ?

Post by Crush »

The vector "diff" is the x and y value the being position changes per game tick.

mSpeedPixelsPerTick.x and .y are always positive. But when the being isn't moving down and right, the value of diff.x and diff.y might be 0 or negative.

I do not understand that comment either.
  • former Manasource Programmer
  • former TMW Pixel artist
  • NOT a game master

Please do not send me any inquiries regarding player accounts on TMW.


You might have heard a certain rumor about me. This rumor is completely false. You might also have heard the other rumor about me. This rumor is 100% accurate.
Ablu
Manasource
Manasource
Posts: 288
Joined: 23 Jul 2011, 08:31
Location: Germany

Re: what does the "Vector diff" mean ?

Post by Ablu »

The being has a current position and a destination.
Lets call the current pos vpos and the destionation vector vdest

So the direction vdir is:

Code: Select all

vdir = vdest - vpos
However this vdir only points into the direction we want to move. But we want the vector we need move per tick.

So we first make sure that vdir has the length (|vdir|) of 1 by normalizing it:

Code: Select all

const Vector normalizedDir = dir.normalized();
This means a vector (20/0) would become (1/0) or a vector of (1/1) would become (0.707, 0.707).
Now we can calculate the actual vector to move for this tick by multiplying the normalized vector with the x and y speed:

Code: Select all

Vector diff(normalizedDir.x * mSpeedPixelsPerTick.x,
            normalizedDir.y * mSpeedPixelsPerTick.y);
For questions like this btw it is the best to go to #mana on freenode irc since that is where we mana developers hang around.

Regards
Ablu
User avatar
Nard
Knight
Knight
Posts: 1113
Joined: 27 Jun 2010, 12:45
Location: France, near Paris

Re: what does the "Vector diff" mean ?

Post by Nard »

Code: Select all

  // The deplacement of a point along a vector is calculated
            // using the Unit Vector (â) multiplied by the point speed.
            // â = a / ||a|| (||a|| is the a length.)
            // Then, diff = (dir/||dir||) * speed.
            const Vector normalizedDir = dir.normalized();
            Vector diff(normalizedDir.x * mSpeedPixelsPerTick.x,
                        normalizedDir.y * mSpeedPixelsPerTick.y);

            // Test if we don't miss the destination by a move too far:
            if (diff.length() > distance)
            {
                setPosition(mPos + dir);
dlqingxi wrote:My question is : what is the vector diff use for ?
apparently it is used to compute the length of a displacement between to ticks
dlqingxi wrote:Shouldn't it be mSpeedPixelsPerTick.x ?
Indeed, it should be:

Code: Select all

            Vector diff( mSpeedPixelsPerTick.x,
                        mSpeedPixelsPerTick.y);
or (complication, but may be required if dir is not actual speed direction ):

Code: Select all

 const Vector normalizedDir = dir.normalized();
            Speed =mSpeedPixelsPerTick.length()
            Vector diff(normalizedDir.x * Speed
                        normalizedDir.y * Speed);
or the (algebraic) distance run along a direction (different from speed one) is scalar product of speed vector and direction unit vector.

Code: Select all

            DiffLength =abs(normalizedDir.x*mSpeedPixelsPerTick.x+normalizedDir.y*mSpeedPixelsPerTick.y)
dlqingxi wrote:Can someone tell me why "diff = (dir/||dir||) * speed" ?.
if speed is the (absolute) scalar speed
speed= ||SpeedVector|| =SQRT( SpeedVector.x^2+SpeedVector.y^2) from Pythagoras theorem;
Thus:
UnitDIrectionVector=SpeedVector /||SpeedVector|| is the unit vector to characterize speed direction,
and obviously:
SpeedVector= (SpeedVector /||SpeedVector||)*||SpeedVector||=UnitDIrectionVector*speed
"The language of everyday life is clogged with sentiment, and the science of human nature has not advanced so far that we can describe individual sentiment in a clear way." Lancelot Hogben, Mathematics for the Million.
“There are two motives for reading a book; one, that you enjoy it; the other, that you can boast about it.” Bertrand Russell, Conquest of Happiness.
"If you optimize everything, you will always be unhappy." Donald Knuth.
User avatar
Nard
Knight
Knight
Posts: 1113
Joined: 27 Jun 2010, 12:45
Location: France, near Paris

Re: what does the "Vector diff" mean ?

Post by Nard »

I just discussed with Ablu about that. The mSpeedPixelsPerTick.x and mSpeedPixelsPerTick.y include display scale factor to take into account rectangular tiles. Thus it is the same as if you are in an orthogonal-non - normal system of coordinates.

They are useless in TMW: mSpeedPixelsPerTick.x = mSpeedPixelsPerTick.y = speed
"The language of everyday life is clogged with sentiment, and the science of human nature has not advanced so far that we can describe individual sentiment in a clear way." Lancelot Hogben, Mathematics for the Million.
“There are two motives for reading a book; one, that you enjoy it; the other, that you can boast about it.” Bertrand Russell, Conquest of Happiness.
"If you optimize everything, you will always be unhappy." Donald Knuth.
dlqingxi
Newly Registered User
Posts: 7
Joined: 04 Jan 2013, 08:44

Re: [Resolved]what does the "Vector diff" mean ?

Post by dlqingxi »

I think now I understand it. Thank you for your help.
Post Reply