Game Maker's Garage Forum

Game Creation => SilverCreator => Topic started by: Connors on December 08, 2010, 11:32:51 PM

Title: Mouse Following
Post by: Connors on December 08, 2010, 11:32:51 PM
OK guys, I know how to use mousex and mousey, so I got a question:
How can I make the ship always move in a straight line towards the mouse? making it go to the mouse x and y is easy, and I tried having X and Y velocities but that's awkward and floaty.
Gan, I bet you know this one! I NEED some help here!
Simpler version:
How to move sprite A a certain distance straight towards a point on the screen.

EDIT: Here's one example of the math (in Action Script). Both programs move the object at a fixed speed towards a point.
http://www.gamedev.net/community/forums/topic.asp?topic_id=564343

There's 2 ways to do it. In #1 I can re-create everything but the function "atan2" because I'm not sure how it uses the 2 values.

If I can't emulate that perhaps there's a chance I can use the second version?

EDIT #2:
I used this code and the ship follows the mouse, but ONLY if the mouse x and y are greater than the ship's x and y.
Still, pretty cool that it works when the mouse is below and to the right of the ship.
How can I make it work in any direction?

Code: [Select]
LET speed=44

LET fY = mouseY-shipY
LET fX = mouseX-shipX

LET dist = sqrt( fX*fX + fY*fY )

LET step = speed/dist

LET shipX=shipX+(fX*step)
LET shipY=shipY+(fY*step)

Title: Re: Mouse Following
Post by: Tireas Dragon on December 09, 2010, 12:45:56 AM
This worked for me

x is object x
y is object y
speedX is the speed the object moves in the x direction
speedY is the speed the object moves in the y direction

  angle = atan2 (mouseX - x, mouseY - y);
  speedX = sin (angle) * SPEED;
  speedY = cos (angle) * SPEED;
  x = x + speedX;
  y = y + speedY;

this was in processing but I am sure it wouldn't be too difficult to adapt it for SC

The Idea is you move the object at an angle and forget the distance.

P.S. You may have to flip the sin and cos depending on how your atan2 works (atan2 takes a coordinate and measures its angle to the origin AKA 0,0)

EDIT:
This works too

  xchange = mouseX - x;
  ychange = mouseY - y;
  distance = sqrt( xchange*xchange + ychange*ychange );
  movement = SPEED/distance;
  x += xchange*movement;
  y += ychange*movement;

EDIT 2:
The only reason I can see for it not working is if the program doesn't accept negative numbers.
Title: Re: Mouse Following
Post by: Connors on December 09, 2010, 10:05:27 PM
OK so here's my problem: It's supposed to follow the mouse, at a constant speed, and it has to be able to do it in any direction, because is a shooter game it shouldn't just always be at the mouse location or you could move really fast and go through sprites and not collide. With this thing set up it does go towards the mouse but the speed seems to change based on the angle, and it can't go up or to the left. Oh yeah and SC has no atan2 function so I can't test the other version.
The following doesn't work because it still moves down and to the right if the mouse x/y is lower so I don't know what I want to do.
Code: [Select]
LET speed=44

IF mouseY > shipY then
   LET fY = mouseY-shipY
ELSE
   LET fy = shipY-mouseY
END IF
IF mouseX > shipX then
   LET fX = mouseX-shipX
ELSE
   LET fX = shipX-mouseX
END IF


LET dist = sqrt( fX*fX + fY*fY )

LET step = speed/dist

LET shipX=shipX+(fX*step)
LET shipY=shipY+(fY*step)

I'd love to be able to use the arrow keys in this. Oh well, I'll get back to making enemies work.
Title: Re: Mouse Following
Post by: Gan on December 10, 2010, 12:46:08 AM
Arrow keys would be fantastic...

Somehow in my mind I keep thinking of something dealing with slopes * speed to move toward the mouse. Ah plus conditional statements if one of em is 0.
Maybe I can help tomorrow when I'm more lucid.
Title: Re: Mouse Following
Post by: Tireas Dragon on December 10, 2010, 12:29:04 PM
So you don't want the object to move up and down at the same time moving left and right?

Xdif = mouseX - manX
if (Xdif < 0)
  Xdif = Xdif * -1

Ydif = mouseY - manY
if (Ydif < 0)
  Ydif = Ydif * -1

if (Xdif > Ydif)
  move left or right
else
  move up or down
Title: Re: Mouse Following
Post by: Connors on December 10, 2010, 04:45:29 PM
Code: [Select]
LET speed=44

IF mouseY > shipY then
   LET fY = mouseY-shipY
ELSE
   LET fy = shipY-mouseY
END IF
IF mouseX > shipX then
   LET fX = mouseX-shipX
ELSE
   LET fX = shipX-mouseX
END IF


LET velX = speed * ( fx / (sqrt( fx*fx  + fy*fy ) ) )
LET velY = speed * ( fy / (sqrt( fx*fx + fy*fy ) ) )

IF mousex < shipx then
   LET velx=-velx
END IF
IF mousey < shipy then
   LET vely=-vely
END IF

LET shipx = shipx+velX
LET shipy = shipy+velY


GOT IT! This makes it go towards mouse x/y.
It works in any direction. Now can anybody explain why it goes faster when it's moving closer to a multiple of 90 degrees?

EDIT: Hey Gan, let's go bug someone about the SC update together, so it's even more annoying.
Title: Re: Mouse Following
Post by: Gan on December 10, 2010, 05:11:25 PM
Awesome.

Quote
EDIT: Hey Gan, let's go bug someone about the SC update together, so it's even more annoying.
Sounds like a fantastic idea.
Title: Re: Mouse Following
Post by: Connors on February 15, 2011, 07:52:34 PM
Matt, you've used similar math recently enough right? I still wanna try and make this work. Remember what I did before? If you try that code you'll notice it does not move at the same speed every direction. Is is because of degrees vs radian again? :(
Title: Re: Mouse Following
Post by: Gan on February 15, 2011, 08:34:40 PM
I think it's because of the formula.

It sure would be nice to have an atan2 function in Sc. That'd solve all the world's problems.
Title: Re: Mouse Following
Post by: Connors on February 15, 2011, 09:18:56 PM
Are there other ways? I've played plenty of games that push the player at the mouse why is this so hard aaaaggh.
Title: Re: Mouse Following
Post by: Gan on February 15, 2011, 09:33:36 PM
It's really easy, Sc just lacks the functionality.
Title: Re: Mouse Following
Post by: GMG Mike on February 16, 2011, 06:06:25 PM
SC v2.0b3 has arrow keys support, you check the booleans like UPARROW, DOWNARROW etc.
Title: Re: Mouse Following
Post by: Gan on February 16, 2011, 06:19:25 PM
I think he specifically wants to make a ship to fly directly to the mouse's location. Need atan2 to make the best results.
Title: Re: Mouse Following
Post by: Connors on February 18, 2011, 07:56:14 AM
Yes but I should also try some things with the smooth arrow keys, because that's what started this whole topic.