0
\$\begingroup\$

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.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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;
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .