Move A Sprite To A New X/y Position At A Constant Rate


mac-10

Still Fresh
Joined
Oct 27, 2006
Messages
56
Age
45
Website
Visit site
Hi There

I am in need of some help again :(

last week I was thinking about point and click adventure games and how the character moves around the environment. I decided to try and have a go at doing it myself.

I am really struggling with moving the player from one node to the next. I have seen some many
tutorials show me how to move a sprite using key presses but none on how to move a sprite to a new x/y position at a constant rate.

Any help you can give me with sprite movement would be greatly appreciated.

Thanks

Mac

Here are some pics of what I have done so far.
The red box represent to pointer.
The green box is the player.
Orange lines it the selected path.
This last pic is rough sketch of how I plan to implement it. But the node will be hidden.

image1.png

image2.png

image3.png

image4.png


Here is the link to the exe.
http://http://webpoint-servers.com/sdl/pathfinder/pathfinder.rar
 
Do you mean you want it to move evenly, rather than, for example just:

if (y<y1) y++;
if (x<x1) x++;

Which would result is something of a 'amateurish' movement?

You want it to be proportional to the distance? This if it has to go a long way in the X but not far in the Y you want it to gently drop down on the Y but go quickly along the X?

Is that what you want? I'm tired and having trouble understanding!

Assuming it is, check http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm

It's worth using this method as it is very fast, good for hand helds.
 
Hi Craigix

thanks for getting back to me

I just tried the x++ example but the player move along the x axis first the the y.

I think the I need it to the proportional to the distance basiclly following the line
to the next node.

thanks for your help
 
Thanks again for the help craigix really appreciate it

I ended up b*stardizing a line drawing function I've been using. It uses same algorithm
but storing the X and Y Values in vectors instead of putting pixels on the screen

Code:
void CPlayer::SetPath(int x1, int y1, int x2, int y2)
{
 
int i, deltax, deltay, posnum;
int d, dinc1, dinc2;
int x, xinc1, xinc2;
int y, yinc1, yinc2;
//calculate deltaX and deltaY
deltax = abs(x2 - x1);
deltay = abs(y2 - y1);
    //initialize
    if(deltax >= deltay)
    {
    //If x is independent variable
        posnum = deltax + 1;
        d = (2 * deltay) - deltax;
        dinc1 = deltay << 1;
        dinc2 = (deltay - deltax) << 1;
        xinc1 = 1;
        xinc2 = 1;
        yinc1 = 0;
        yinc2 = 1;
    }
    else
    {
    //if y is independent variable
        posnum = deltay + 1;
        d = (2 * deltax) - deltay;
        dinc1 = deltax << 1;
        dinc2 = (deltax - deltay) << 1;
        xinc1 = 0;
        xinc2 = 1;
        yinc1 = 1;
        yinc2 = 1;
    }
    
    //move the right direction
    if(x1 > x2)
    {
        xinc1 = -xinc1;
        xinc2 = -xinc2;
    }
    
    if(y1 > y2)
    {
        yinc1 = -yinc1;
        yinc2 = -yinc2;
    }
    
    x = x1;
    y = y1;
    
    //draw the pixels
    for(i = 1; i < posnum; i++)
    {

    PATH_X.push_back(x);
    PATH_Y.push_back(y);
   
        if(d < 0)
        {
            d = d + dinc1;
            x = x + xinc1;
            y = y + yinc1;
        }
        else
        {
            d = d + dinc2;
            x = x + xinc2;
            y = y + yinc2;
        }
    }   
    
}
 
As it has been my dream for many years now to create an adventure game, I have spent
quite some time thinking about it (and doing nothing to reach the goal ;)).
If you want to chat about it, I am your man.
But regarding the movement from A to B, its just as Craig says, you may use the line
algorithm or you can simply look at it another way. You can compare the distances
on the x and y axis, then inc/decrease the shorter by one and the longer by the length
of the longer divided by the length of the shorter. As I have written it, it is probably
incomprehensible, so lets take an example:

Code:
S.........
..........
..........
..........
.........E

You want to get from S to E here. DX (the distance on the X axis from S to E) = 10, DY = 5;
DX is smaller so we create variables SX and SY (the step on the X and Y axis separately)

Code:
SX = 1
SY = DX / DY

so your loop then does this

Code:
X = X + SX
Y = Y + SY

All the variables need to be real numbers obviously. Cast them to integer later.

Hope I am correct with this simple approach. It would probably work even when more generalized,
I mean just make either SX or SY = 1 and then count the other one.
 
Back
Top