Jump to content

  • Log In with Google      Sign In   
  • Create Account


SDL move an image to specific coordinates


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 PauliusM   Members   -  Reputation: 128

Like
0Likes
Like

Posted 03 February 2012 - 11:54 AM

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?

Sponsor:

#2 zalzane   Members   -  Reputation: 191

Like
0Likes
Like

Posted 03 February 2012 - 12:11 PM

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.

#3 PauliusM   Members   -  Reputation: 128

Like
0Likes
Like

Posted 03 February 2012 - 12:42 PM

didnt work, because some times y or x becomes smaller than 1.

#4 jonbonazza   Members   -  Reputation: 121

Like
0Likes
Like

Posted 03 February 2012 - 01:50 PM

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.
Co-founder/Lead Programmer
Bonafide Software, L.L.C.
Fairmont, WV 26554 US

#5 PauliusM   Members   -  Reputation: 128

Like
0Likes
Like

Posted 04 February 2012 - 04:22 AM

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.

#6 sc2slash   Members   -  Reputation: 131

Like
0Likes
Like

Posted 04 February 2012 - 10:30 AM

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.

#7 sc2slash   Members   -  Reputation: 131

Like
0Likes
Like

Posted 04 February 2012 - 11:41 AM

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.

#8 PauliusM   Members   -  Reputation: 128

Like
0Likes
Like

Posted 04 February 2012 - 12:25 PM

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 ;
	  }


#9 jonbonazza   Members   -  Reputation: 121

Like
0Likes
Like

Posted 04 February 2012 - 10:50 PM

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.
Co-founder/Lead Programmer
Bonafide Software, L.L.C.
Fairmont, WV 26554 US




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS