[java] Mouse follow without easing

Started by
3 comments, last by ogreblue 19 years, 7 months ago
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
Advertisement
Ok... no response.

If it is such a stupid question can someone give an URL or something.
I don't know if people know what your problem is. I don't. What you are doing looks fine to me.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
objectx=mousex;
objecty=mousey;

^ no easing there ;]
_______________________________ ________ _____ ___ __ _`By offloading cognitive load to the computer, programmers are able to design more elegant systems' - Unununium OS regarding Python
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;
}
else
{
float scale = speed / l;
objectx += vx * scale;
objecty += vy * scale;
}

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

This topic is closed to new replies.

Advertisement