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.