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?
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)
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.
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.
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.