tags:

views:

69

answers:

3

Hey guys. I'm trying to make a simple game for Android in which the player drags a space ship with one thumb and fires at enemy ships by tapping empty space with the other thumb. Thing is, to animate the bullets traveling across space, I need the list of points between the user's spaceship, and the end of the screen, the line passing through the place the user tapped.

I know I have to parametrize the line, and have read some about the subject, but I can't quite grasp it, especially on how to convert it to code. The way I see it, I need to convert the line segment between the user's ship (P1) and the place the user taps (P2) the smallest possible piece, the rate of change between x and y, the slope, I guess. Once I have that I can just multiply the rate of change by the distance from P1 to get the desired point.

But it ain't quite working. Any help would be appreciated. Thanks.

EDIT: This is what I was doing. This is all to calculate the rate of change, which I use on the Beam's update method to update it's position by multiplying it by speed...

Beam(Renderer r, float OX, float OY, float TX, float TY)
{
    super(r);

    p.x = (int) OX;
    p.y = (int) OY;

    paint = new Paint();
    paint.setColor(Color.BLUE);


    float X = TX - OX;
    float Y = TY - OY;

    boolean xPositive = X >= 0;
    boolean yPositive = Y >= 0;

    if(X < Y)
    {
        RateOfChangeX = Math.abs(X) / Math.abs(Y);
        RateOfChangeY = 1;
    }
    else
    {
        RateOfChangeX = 1;
        RateOfChangeY = Math.abs(Y) / Math.abs(X);
    }
    if(RateOfChangeX < 0 & xPositive)
    {
        RateOfChangeX = RateOfChangeX * -1;
    }
    if(RateOfChangeY < 0 & yPositive)
    {
        RateOfChangeY = RateOfChangeY * -1;
    }
}
+1  A: 

This is an example of how to interpolate which points lie between two points. The code to draw a line is very different and much simpler

This example is in C, but the code may be useful. im is the image, stride is the stride of the image, sx and sy are the amount you are moving up/down or left/right.

float x=sx, y=sy;
  for ( int i=1; i < int(totalLength); i++ ) { 
    int  top = stride*int(y)    + int(x),
         bot = stride*int(y+1)  + int(x);
    float xr = x-int(x),
          xl = 1-xr,
          yt = y-int(y),
          yb = 1-yt;
    newLine[i]= im[top]*xr*yt   +
                im[top-1]*xl*yt + 
                im[bot]*xr*yb   +
                im[bot-1]*xl*yb;
    x+=xgain;
    y+=ygain;
  }

Getting sx/sy (x1,y1) (x2,y2) are the points you are drawing between... Code is python:

totalLength = math.sqrt(math.pow(x2-x1,2) + math.pow(y2-y1,2))

if x1 < x2:
  sx,sy,ex,ey=(x1,y1,x2,y2)
else:
  sx,sy,ex,ey=(x2,y2,x1,y1)

xgain = float(ex-sx)/totalLength
ygain = float(ey-sy)/totalLength

The point to note is that you need to figure out the x and y movement for each step, and then interpolate as shown. To think about it, just write a grid on a piece of paper, and look where the dot should be for each step. Draw a 1px by 1px box around that point.

This algorithm simplifies slightly by drawing a 1px by 1px box backwards from that point, using interpolation between the four grid points touched by that box. Note that the first pixel is skipped as it is a special case. The first pixel represents a problem as it could be outside the image, and you would need to check that you could make the computation before actually performing the computation.

Bear
+1  A: 

Basically you can use Slope–intercept form of a linear equation. To do this without using floating point arithmetic you might use on of these algorithms:

stacker
A: 

Bezier Curve is the best way to do this.....link for this

http://en.wikipedia.org/wiki/B%C3%A9zier_curve