# Sprite Movement Towards Cursor On MouseClick

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

## Recommended Posts

I am having a problem with implementing a sprite that walks towards the cursor when the mouse is clicked. I have been looking all over the internet and gamedev.net but have failed to find a solution to my problem. It works in genreal but it only works sometimes, doesnt go to point, or shakes before it gets there and stops moving. Here is the following code I am working with...
int mouseX = 512;
int mouseY = 384;

//Inside directInput.cpp
//Set Cursor Position To Center
SetCursorPos( mouseX, mouseY );

//Game Loop
if (BUTTONDOWN(mouse_state, 0))
{
spriteStruct.destX = mouseX;
spriteStruct.destY = mouseY;
}

int valid_area = 1;
if( spriteStruct.posX <= ( spriteStruct.destX + valid_area ) && spriteStruct.posX >= ( spriteStruct.destX - valid_area ) && spriteStruct.posY <= ( spriteStruct.destY + valid_area ) && spriteStruct.posY >= ( spriteStruct.destY - valid_area ) )
{
spriteStruct.destX = spriteStruct.posX;
spriteStruct.destY = spriteStruct.posY;
}
else
{
move_sprite( );
}
update_sprite( );
mouseX += mouse_state.lX;
mouseY += mouse_state.lY;

//How I am initializing and moving sprite
bool init_sprite( void )
{
spriteSurface = directG->getSurfaceFromBitmap("sprite.bmp");
if (spriteSurface == NULL)
return false;

spriteStruct.srcRect.top    = 0;
spriteStruct.srcRect.left   = 0;
spriteStruct.srcRect.right  = SPRITE_WIDTH;
spriteStruct.srcRect.bottom = SPRITE_HEIGHT;
spriteStruct.posX = 512;
spriteStruct.posY = 384;
spriteStruct.destX = spriteStruct.posX;
spriteStruct.destY = spriteStruct.posY;
spriteStruct.speed = SPRITE_SPEED;

return true;
}
void move_sprite( void )
{
double adjacent = abs( spriteStruct.destX - spriteStruct.posX );
double opposite = abs( spriteStruct.destY - spriteStruct.posY );

double moveX = sin( radians ) * SPRITE_SPEED;
double moveY = cos( radians ) * SPRITE_SPEED;

//Move X Direction
if( spriteStruct.destX < spriteStruct.posX )
{
spriteStruct.posX -= moveX;
}
else
{
spriteStruct.posX += moveX;
}

//Move Y Direction
if( spriteStruct.destY < spriteStruct.posY )
{
spriteStruct.posY -= moveY;
}
else
{
spriteStruct.posY += moveY;
}
}


##### Share on other sites
Well, first of all, have you found any sort of pattern of when it does this?

If I'm correct, it's not moving when the mouse is directly above, below, to the left, or to the right of the sprite, it's doing that shaking and stopping thing at angles nearing the four cardinal directions, and it's moving correctly at angles close to 45 degrees. Does that sound about right?

I think that the problem is with your sine and cosine logic in the method "move_sprite". You have "moveX" set to the sine and "moveY" set to the cosine of "radians". Try swapping those, so "moveX" is the cosine and "moveY" is the sine, and let me know if it works after that.

##### Share on other sites
Thanks for the reply... I looked into what you said about switching the sine and cosine around, and you were right. But it is still behaving oddly. Like when I click some place for instance, the sprite will move along a slope/direction off a little from the where the mouse was click then change it direct later on and move towards the mouse. I have no idea what is causing this problem, is it possible that I am not getting the absolute coordinates of the cursor correctly? I am setting them to the center of the screen with SetCursorPos and then every game loop I get the relative from the device and add them to the total. I appreciate any articles or help anyone can provide me with because this simple problem is haulting my development. Thanks.

##### Share on other sites
I noticed a lot of redundant computations in your code (converting from X/Y to angle and then immediately back. Using the absolute value when you do a signed/unsigned comparison later). Try this instead:

// Step 1: Get vector difference.double diffX = spriteStruct.destX - spriteStruct.posX;double diffY = spriteStruct.destY - spriteStruct.posY;// Step 2: Normalize vector.double len = sqrt(diffX*diffX + diffY*diffY);if (len < 0.001){  return;}diffX /= len;diffY /= len;// Step 3: Vector addition.spriteStruct.posX += diffX;spriteStruct.posY += diffY;

Edit: Wow, typoville.

##### Share on other sites
Thanks for the reply, I tried what you said and it work somewhat. The sprite will move to a location where the mouse clicks but as soon as it gets close to it, it will be blitted to (0,0) on the screen. Also like before it still has erratic behaviour and will move in a certain direction then shift and move in another direction when the mouse was only clicked once. Also it will not work for clicking in the bottom right of the screen. Any other suggestions?