Page 1 of 1

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

Posted: 08 Mar 2013, 14:31
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.

Re: what does the "Vector diff" mean ?

Posted: 08 Mar 2013, 15:50
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.

Re: what does the "Vector diff" mean ?

Posted: 08 Mar 2013, 17:02
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

Re: what does the "Vector diff" mean ?

Posted: 08 Mar 2013, 17:04
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

Re: what does the "Vector diff" mean ?

Posted: 08 Mar 2013, 18:13
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

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

Posted: 09 Mar 2013, 11:52
by dlqingxi
I think now I understand it. Thank you for your help.