Topic:   Mouse Following   (Read 20757 times)


0 Members and 1 Guest are viewing this topic.

Connors


  • ^ This guy is amazing.

  • ****


  • Posts: 2374

  • It's a secret to everyone...
Mouse Following
« 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)

« Last Edit: December 08, 2010, 11:52:19 PM by Connors »
Warning: The above post may have been modified multiple times.

"In a great game, the character must never perfectly obey the user's command"
 - Tim Rogers

http://connorspuzzles.tumblr.com/

Tireas Dragon


  • GMG Extraordinaire

  • ***


  • Posts: 1626

  • Trying to recover from my shattered screen.
Re: Mouse Following
« Reply #1 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.
« Last Edit: December 09, 2010, 12:58:51 AM by Tireas_Dragon »
I must be dreaming (wake up me wake up) How could this have happened. Tireas' cry when he found his computer fallen over in his chair with it's screen shattered.

Connors


  • ^ This guy is amazing.

  • ****


  • Posts: 2374

  • It's a secret to everyone...
Re: Mouse Following
« Reply #2 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.
« Last Edit: December 09, 2010, 10:32:23 PM by Connors »
Warning: The above post may have been modified multiple times.

"In a great game, the character must never perfectly obey the user's command"
 - Tim Rogers

http://connorspuzzles.tumblr.com/

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: Mouse Following
« Reply #3 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.

Tireas Dragon


  • GMG Extraordinaire

  • ***


  • Posts: 1626

  • Trying to recover from my shattered screen.
Re: Mouse Following
« Reply #4 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
I must be dreaming (wake up me wake up) How could this have happened. Tireas' cry when he found his computer fallen over in his chair with it's screen shattered.

Connors


  • ^ This guy is amazing.

  • ****


  • Posts: 2374

  • It's a secret to everyone...
Re: Mouse Following
« Reply #5 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.
« Last Edit: December 10, 2010, 04:47:22 PM by Connors »
Warning: The above post may have been modified multiple times.

"In a great game, the character must never perfectly obey the user's command"
 - Tim Rogers

http://connorspuzzles.tumblr.com/

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: Mouse Following
« Reply #6 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.

Connors


  • ^ This guy is amazing.

  • ****


  • Posts: 2374

  • It's a secret to everyone...
Re: Mouse Following
« Reply #7 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? :(
Warning: The above post may have been modified multiple times.

"In a great game, the character must never perfectly obey the user's command"
 - Tim Rogers

http://connorspuzzles.tumblr.com/

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: Mouse Following
« Reply #8 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.

Connors


  • ^ This guy is amazing.

  • ****


  • Posts: 2374

  • It's a secret to everyone...
Re: Mouse Following
« Reply #9 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.
« Last Edit: February 15, 2011, 09:19:10 PM by Connors »
Warning: The above post may have been modified multiple times.

"In a great game, the character must never perfectly obey the user's command"
 - Tim Rogers

http://connorspuzzles.tumblr.com/

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: Mouse Following
« Reply #10 on: February 15, 2011, 09:33:36 PM »
It's really easy, Sc just lacks the functionality.

GMG Mike


  • Administrator

  • GMG-er

  • *****

  • no avatar

  • Posts: 536
    • mikerichardson.name
Re: Mouse Following
« Reply #11 on: February 16, 2011, 06:06:25 PM »
SC v2.0b3 has arrow keys support, you check the booleans like UPARROW, DOWNARROW etc.

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: Mouse Following
« Reply #12 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.

Connors


  • ^ This guy is amazing.

  • ****


  • Posts: 2374

  • It's a secret to everyone...
Re: Mouse Following
« Reply #13 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.
Warning: The above post may have been modified multiple times.

"In a great game, the character must never perfectly obey the user's command"
 - Tim Rogers

http://connorspuzzles.tumblr.com/