Public Group

# SDL move an image to specific coordinates

This topic is 2503 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I want to move image from one coordinates to second coordinates. like from (0,0) to (485.354), but with an movement not a "teleport", should I move the image by angle? If yes, then what I need to use to do that?

##### Share on other sites
To move an image slowly, you'll need to teleport it over towards the destination just a tiny bit each frame to make it look like a smooth animation.

Depending on the speed and smoothness you want it to "move" at, it might take a few dozen to a few hundred teleports.

In order to get what coordinates you should teleport the image to for the next frame, you'll take (FinalPixelX-InitialPixelX)/totalFrames to get the number of pixels the image should move in the X direction per frame. The same formula applies in the Y direction.

##### Share on other sites
didnt work, because some times y or x becomes smaller than 1.

##### Share on other sites
Assuming no interpolation is needed/wanted, you can simply do something like this:
 currentVec += translationVec * speed * dt; 

where currentVec is the current position as a vector; translationVec is the amount to translate x and y (and possibly z if it is a 3D game) by; speed is a scalar to speed up or slow down movement; and dt is the time it took to complete the last frame--this is used to promote platform independance.

##### Share on other sites
but how do I move image by vector on x y coord?

I want the image to follow mouse coordinates. So if I press up arrow it would go to that direction.

##### Share on other sites
Ok so lets say this. you've a X value and a Y value that represent your object position. Then you've a X and Y to represent the velocity of the object. What you then do is add to the X the Xvel * time and to the Y the Yvel * time. The problem you said with it being smaller then 0 doesnt really matter that much. Just use floats, but anyways if it's moving slower then 1pixel per sec will be really really slow.

##### Share on other sites
Oh sorry didn't read the last part. So if you want it to follow your mose it's even easier. You jsut need to reasing the position everytime there's a mouse movement.
To do so, you do soemthing like this
if (SDL_PollEvent(&event))
{
if( event.type == SDL_QUIT)
quit = true;
if( event.type == SDL_MOUSEMOTION)
image.updateEndPosition(event.button.x, event.button.y);
}
In this scenario there would be a class image containing an x and y position and that method would simply update it to the current one whenever ur mouse moved. If you wanted to do it when you press arrow button just change the type of event to SDL_KEYDOWN and SDLK_UP.
That position would be the desired one to moving. So you need to calculate the direction. Knowing the starting position and the desired end position you should be able to do it with simple maths.

##### Share on other sites
found solution: Bresenham Line Drawing algorithm:

if (CU_UP == true) { double x1 = mousex - x; //mousex - mouse x coordinate; x -image x coordinate double y1 = mousey - y; //mousey - mouse y coordinate; y -image y coordinate double length = sqrt( x1*x1 + y1*y1 ); double xVel = x1 / length; double yVel = y1 / length; apply_surface( (int)x, (int)y, cube, screen, &clipsLeft[ 0 ] ); x += xVel ; y += yVel ; }

##### Share on other sites
Bresenham's line algorithm is way way overkill for what you are trying to do. You are making it much more complicated than it needs to be.

If no obstacles will exist in the map, then you can simply use sc2slash's approach. If obstacles do exist, then the line algorithm definitly will not work. instead, you need to use some path finding algorithm combined with a steering behavior.

1. 1
2. 2
3. 3
Rutin
13
4. 4
5. 5

• 20
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633694
• Total Posts
3013366
×