Jump to content
  • Advertisement
Sign in to follow this  
Polas22

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!