PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

How to control an object smoothly using stylus?

I want to control my plane using stylus. The idea is if my plane is in location X,Y and I tap a different loc X1,Y1 - i want to move my plane from X,Y --> X1,Y1. From there it will follow the Stylus move.

Any algo, code or idea will be a big help. Thanks.
You should decide how long it should take the plane to move that far... ie, will it take 1 second, regardless of distance? If that's the case, you divide the distance by the number of frames for that one second, and move the plane. If the plane has a maximum travel speed, you could make it take longer too.

-John
calc a delta x and y and normalize.


Code:
float dist = sqrt( (X1 - X)^2 + (Y1 - Y)^2 );
float speed = 1.0f;
if(dist > 1)// avoid div by zero or small number
{
float dx = ( X1 - X ) / dist * speed;
float dy = ( Y1 - Y ) / dist * speed;

X += dx;
Y += dy;
}


and you could calculate a speed dependant on distance, or ramp up/down, whatever.
Thanks Fast and Presto. Will try that out. Cheers.
fast_rx wrote:
Code:
float dist = sqrt( (X1 - X)^2 + (Y1 - Y)^2 );



The ^ operator is not exponent in C, it is bitwise exclusive OR. That will not even compile, because you cannot perform bit operators on floats. So you will have to square the two deltas explicitely:

Code:
float dist = sqrt( (X1 - X)*(X1 - X) + (Y1 - Y)*(Y1 - Y) );


Dan East
Thanks Dan,

In this code.. I need to check for negative right? That means if X1>X or X>X1 before doing the computations.
denthorq wrote:
I need to check for negative right? That means if X1>X or X>X1 before doing the computations.


The sign is maintained in the dx/dy calcs, so in the case where x1>x you'll be adding a negative, and moving to the right.
Thanks Dan, Fast, Presto & Smirk :-)

The code works smoothly and perfectly. You'll see this in action with my xfighter game.

Again thanks.