PDA

View Full Version : Smooth movement



C-Dawg
10-25-2006, 02:28 PM
I'd like an FFC that behaves as follows: It moves back and forth horizontally between two given X coordinates, but does so in a smoothly increasing and decreasing speed. That is, when it first changes direction, it will be moving slowly, and it will pick up speed until halfway, then decrease speed to zero by the time it hits the other barrier. Here's a snippet of the code I'm trying to use for such movement going right. (X_Origin stores the FFC's original X coordinate, pi is previously defined by the script.)

if (this->X >= x_origin + 32){ state = 1; }
else{
this->Vx = sin( ( 2 * pi) / 32) * (this_x - origin);
}

I can't test till I get home, but I wondered if anyone could verify that this function will mathematically perform as indicated.

EDIT - For reference, this is the rest of the code.



// ===============================================
// TAIL_WHIP : This ffc will move slowly back and forth horizontally
// over four tiles, centered on it's original location. After a set amount
// of time, it will lash out towards link' s location at a high speed. It
// will pause breifly after reaching link's old location, and then return
// to it's starting point to continue wagging. Use in conjunction with
// the tail_segment script to create a helmstar-style tail whip attack.
// Ignores walkability of combos in the way.
// ===============================================

ffc script tail_whip{

// CONSTANTS

int wait = 60; // How long the tail waits
float pi = 3.1415926; // It's pi!
int delay = 10; // How much warning Link gets before
// the tail strikes

// VARIABLES

int i = 0; // Counter

int x_origin = 0;
int y_origin = 0;

int x_target = 0;
int y_target = 0;

int x;
int y;

int dist;

int state = 0; // The state of the tail_whip.
// 0 = wagging right
// 1 = wagging left
// 2 = pulling back to strike
// 3 = striking at link
// 4 = pausing after striking
// 5 = returning to wagging mode

void run(){

x_origin = this->X;
y_origin = this->Y;

while (true){

// Wagging right

if (state ==0){

if (this->X >= x_origin + 32){ state = 1; }
else{
this->Vx = sin( ( 2 * pi) / 32) * (this_x - origin);
}

if (i >= wait) { i = 0; state = 2;}
else{ i = i + 1;}
}

// Wagging left

if (state ==1){

if (this->X >= x_origin - 32){ state = 0; }
else{
this->Vx = -sin( ( 2 * pi) / 32) * (this_x - origin);
}

if (i >= wait) { i = 0; state = 2;}
else{ i = i + 1;}
}

// Targeting and pulling back to strike

if (state ==2){

x_target = Link->X;
y_target = Link->Y;
if ( i >= delay ){ i = 0; state = 3; }
else {
this -> Vx = 0;
this->Vy = -0.1;
}
}

if (state == 3){

if ( (this->X >= x_target - 16) && (this->X <= x_target + 16) && (this->Y >= y_target - 16) && (this->Y <= y_target + 16) ){

state = 4;
}
else{

dist = distancesq(x_target; this->X, y_target, this->Y);
x = this->X;
y = this->Y;
this->Vx = 2* ((x_target - x)*Abs((x_target - x))/dist);
this->Vy = 2 * ((y_target - y)*Abs((y_target - y))/dist);
}

}

if (state == 4){

if ( i >= delay/2 ) { i = 0; state = 5;}
else{ i = i++; }

}

if (state == 5){

if ( (this->X >= x_origin - 16) && (this->X <= x_origin + 16) && (this->Y >= y_origin - 16) && (this->Y <= y_origin + 16) ){

state = 0;
}
else{

dist = distancesq(x_origin; this->X, y_origin, this->Y);
x = this->X;
y = this->Y;
this->Vx = ((x_origin - x)*Abs((x_origin - x))/dist);
this->Vy = ((x_origin - y)*Abs((x_origin - y))/dist);
}
}

} // end of while loop
} // end of void run()
} // end of ffc script