Sign in to follow this  

[java] Mouse follow without easing

Recommended Posts

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

Share this post

Link to post
Share on other sites
I am sorry I don't think made myself very clear. What I wanted was an object to follow the mouse, but not aquire the mouse's position.
I wanted to have the object move topwards the mouse at a constant velocity, instead in a manner of decelleration as seen in my above example.
Anyway I have found the answer on the javagaming forums and if anyone is interested it is as follows:

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;
float scale = speed / l;
objectx += vx * scale;
objecty += vy * scale;

Thanks for your help though guys and thanks to crystal squid from

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this