views:

146

answers:

1

How would you achieve a certain slope for a moving object ? I'm trying to get a enemy to spawn of the screen at point x,y and fly by itself across the screen to a new x,y while aslo adding a slope so it looks like it's swooping downward,straight, or upwards.. something like that. Right now I only have the knowledge of spawning and moving my enemies in a certain direction and don't know what to look up from here.

+1  A: 

If you want to move in a strait line between to points then you could calculate the delta X and delta Y as:

DX = X2-X1;
DY = Y2-Y1;

Then if you want to move a specific distance (speed/frame), normalize the length using the Pythagorian theorem:

HYP = DX*DX + DY*DY;
HYP = sqrt(HYP);
DX = MoveD * DX/HYP;
DY = MoveD * DY/HYP;

Then:

// Move Distance (MoveD) From X1,Y1 to X2,X2 each screen update.
if ((abs(X2-X)<DX)&&(abs(Y2-Y)<DY))
{
  X = X + DX;
  Y = Y + DY;
} else
// STOP

"Swooping" can be done by changing the endpoint dynamically. Start with the end point in front of the "Target" and Move through the target and up to create a "swooping" motion. Add a "Speed bias" such that it accelerates moving down and decelerates moving up.

This can also be addapted to integer math using a variation on Bresenham's line drawing algorthm or DDA.

NoMoreZealots
You mean "*Bresenham's* line algorithm"
tpdi
Also you mean "*Accelerates* moving down and *decelerates* moving up."
jilles de wit