Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a moving actor named ball and I want to move it in the opposite direction of my mouse's position.

In my code in Move To behaviour for X-axis location I write

-game.mouse.position.X

and for Y-axis location I write

-game.mouse.position.Y

But it's not working properly.

share|improve this question

2 Answers 2

The answer to this question is exactly the same as my answer to your other question, except you multiply the resulting per frame vector by -1.

share|improve this answer

the formula would be more or less like this (I don't know how GameSalad works):

finaldirection.X = - (game.mouse.position.X - ball.position.X);
finaldirection.Y = - (game.mouse.position.Y - ball.position.Y);

because if you substract a vector (v1=ball position) from other vector (v2=mouse position) you get a third vector in the direction from v1 to v2 (i think) then you multiply the vector by -1 because you want to go the opposite direction (which you were doing already)

You may want to make this a unit vector for then be able to multiply by the distance you want to move the ball (or the velocity/force to apply to it), you then divide it by its magnitude like this:

// find the magnitude of this vector
length = sqrt( (finaldir.x*finaldir.x) + (finaldir.y*finaldir.y) );
if( length != 0 )
{ // divide the vector by its length
   finaldir.x /= length;
   finaldir.y /= length;
} 
else
{
   finaldir.x = 0;
   finaldir.y = 0    
} 

then you will have a final vector of length 1 so if you want to move it for example 15 units far in that direction you can do:

   ball.position.X += finaldir.x*15;
   ball.position.Y += finaldir.y*15;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.