Slow moving sprites

If i click somewhere on the screen, I want the Sprite to move there slowly…
Have you got any ideas on this problem?

Thanks
kroedler

Kroedler wrote:

If i click somewhere on the screen, I want the Sprite to move there slowly…
Have you got any ideas on this problem?

Thanks
kroedler

Sure, it’s not that difficult.
Basically, you just need to store the sprite’s destination and its
current position. Then, every frame, you change the current position to
be a bit closer to the destination.
Do you want a constant or a varying velocity?

Sebastian

If i click somewhere on the screen, I want the Sprite to move there slowly…
Have you got any ideas on this problem?

Basically you need to grab your mouse’s coords,
then find the difference from the sprite’s then make sure it goes at a
specified speed

What I do is find the distance between the two points, then put that
into a unit circle sort of thing, which will give you a max speed of 1
pixel/update i guess, then you can multiply that by some speed to make
it faster. This may not be the best way, and i’m not a master of math so
I can’t explain it the best either

int mx, my; // mouse coords
int diffx, diffy; // Difference between mouse and sprite
float h; // the length between sprite
double vx, vy; // velocity of the sprite

SDL_GetMouseState(&mx, &my);

diffx = Sprite.x - mx;
diffy = Sprite.x - my;

// remember math class on a right triangle a^2+b^2 = c^2
h = sqrt( pow(diffx,2) + pow(diffy,2) );

/* put that length in the unit circle thinger (i forget why I had to do
the -diffx, but it works(for me at least)) */
vx = -diffx/h;
vy = diffy/h;

So, once you have vx and vy, just update the sprite’s x and y
coordinates by vx and vy for each frame and it will slowly move towards
where you clicked(if all goes well). If you want to make it go faster
just multiply vx and vy by some number of pixels/frame, if you wanted 5
pixels/frame you could do
vx = -diffx/h*5

Hopefully I didn’t screw something up when I typed this, but that seems
like what you want to do.On Sun, 2003-12-07 at 09:57, Kroedler wrote:

Thanks
kroedler


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl