Moving 2d object without easing

I would like to know how to make something move with respect to the mouse’s position without the use of easing. I currently use the formula:

xvelocity = (mousex - objectx)/speed;
objectx += xvelocity;

yvelocity = (mousey - objecty)/speed;
objecty += yvelocity;

Thanks for your time

I’m not quite sure what you mean by ‘easing’

Anyway, 2 points:

  1. Instead of dividing by ‘speed’, store ‘speed’ as 1/speed (the reciprocal) and multiply. Its faster that way.

Anyway:
2) The formulae you have looks like it will zoom towards the mouse and then slow down. Is this what you mean by the easing?

If so, and you want the motion to be static, you need to normalise the offset to the max speed you desire. To do this, you normalise the velocity vector and scale it before applying:


float vx, vy, l;
vx = mousex - objectx;
vy = mousey - objecty;
l = Math.sqrt( vx*vx + vy*vy );

if( l <= speed )
{ // We are so close we get there in 1 turn
    objectx = mousex;
    objecty = mousey;
}
else
{
    float scale = speed / l;
    objectx += vx * scale;
    objecty += vy * scale;
}

(Apologies for syntax errors & typos)
There are other ways but this is pretty simple & direct :slight_smile:
Hope it helps,

  • Dom

crystalsquid you are A legend!

Thanks so much mate, I was trying to over complicate things as usual, trying to use vectors and the unit circle :expressionless: lol.

By easing yes I did mean decelleration in relevance to the distance away from mouse.

Thankyou for your quick and accurate response.