PDA

View Full Version : Making an FFC move to a point



Joe123
10-14-2007, 06:15 PM
So, to make an ffc move to a point, I need to set its vertical and horizontal speed to that which will move it to the point.

My assumption on how to accomplish this was thus that V=S/t, therefore the speed that I want to give it for horizontal and vertical motion seperately will both be the distance I want it to travel, divided by the time I want it to get there in.

So I have this:

while(true){
x = Link->X;
y = Link->Y;
a = x - Link->X;
b = y - Link->Y;
c = a/20;
d = b/20;
if(this->X < x){this->Vx = c;}
if(this->Y < y){this->Vy = d;}
if(this->X == x){this->Vx = 0;}
if(this->Y == y){this->Vy = 0;}
Waitframe();
}

I know it's a bit convuluted, but that's how I've done it. Why should this not work? It's just occured to me that the time that I've set for it to take has no units, so that may be a problem but I was assuming it shouldn't really be.

Gleeok
10-15-2007, 12:08 AM
if(this->X < x){this->Vx = c;}
if(this->Y < y){this->Vy = d;}

That should work fine, and just add negative speed for >. ie:



if(this->X > x){this->Vx = -c;}
if(this->Y > y){this->Vy = -d;}

I'm no math wiz, but

x = Link->X;
y = Link->Y;
a = x - Link->X;
b = y - Link->Y;

...isn't that like saying

a = 0
b = 0

...:scared:

Joe123
10-15-2007, 02:56 AM
now that's why it's not working. Thankyou Gleeok >_<

while(true){
x = Link->X;
y = Link->Y;
a = x - this->X;
b = y - this->Y;
c = a/20;
d = b/20;
if(this->X < x){this->Vx = c;}
if(this->Y < y){this->Vy = d;}
if(this->X == x){this->Vx = 0;}
if(this->Y == y){this->Vy = 0;}
Waitframe();
}

That's what I'm looking for.

ShadowMancer
10-15-2007, 08:20 PM
Here is an altrenate solution for you. If you dont' want to mess with Vx and Vy you can use this formula:


ffc script fetch{
void run(int spd,int nX,int nY){
int dx; int dy; int dist;
dx = nX-this->X;
dy = nY-this->Y;
dist = Sqrt(dx*dx + dy*dy);
while(this->X != nX && this->Y != nY){
this->X += (dx / dist) * spd;
this->Y += (dy / dist) * spd;
Waitframe();
}
}
}

Argument0 is the speed the FFC will travel
Argument1 is the X destination
Argument2 is the Y destination

Just some food for thought

Joe123
10-16-2007, 02:49 AM
So you're using pythagoras there? Why's that? I can't really work out what you're doing.

C-Dawg
10-16-2007, 11:50 AM
Here is an altrenate solution for you. If you dont' want to mess with Vx and Vy you can use this formula:


ffc script fetch{
void run(int spd,int nX,int nY){
int dx; int dy; int dist;
dx = nX-this->X;
dy = nY-this->Y;
dist = Sqrt(dx*dx + dy*dy);
while(this->X != nX && this->Y != nY){
this->X += (dx / dist) * spd;
this->Y += (dy / dist) * spd;
Waitframe();
}
}
}

Argument0 is the speed the FFC will travel
Argument1 is the X destination
Argument2 is the Y destination

Just some food for thought

Remember though, if you move an ffc directly using X and Y instead of Vx and Vy, there are two things to bear in mind:

(1) Movement may be less fluid depending on your code; and
(2) FFCs that are set to "follow" the scripted FFC WILL NOT FOLLOW changes in X and Y, only Vx and Vy.

It's find to use X Y, just don't run afoul of these limitations.

ShadowMancer
10-16-2007, 09:57 PM
(1) Movement may be less fluid depending on your code; and
(2) FFCs that are set to "follow" the scripted FFC WILL NOT FOLLOW changes in X and Y, only Vx and Vy.

Thank you, I was not aware of this (Mostly #2) As far as fluid movement, this is a chopped up version of the 'rapid fire enemy script' the part that aims at Link, it should work in the same way only aimed at nX, nY. Although if what you are aiming at is also moveing I could see a problem with fulid movement.
I have not messed with Vx and Vy much at all, alot of my techniques come from my experience with GameMaker 5, I got so used to some built in varibles bieng either choppy , hard to use, or nonexistient so, force of habbit to use the most 'raw' functions possible :)