I'm thinking something like

if(velocity.x > 0 && x >= mouse.x) { velocity.x = 0; x = mouse.x; } if(velocity.y > 0 && y >= mouse.y) { velocity.y = 0; y = mouse.y; }

then something similar for if velocity.x/y < 0, etc. etc.