Moving along a line

Hi,

I am trying to take an point and move it along a line. In more detail, let’s say I have the following locations on my screen: point A: (1,1) and then point B (4,4). I want to move from A to B. Let’s say that during each movement, it will move X, where X is a whole number such as 2. So the angle is around 45 degrees, and I want to move on this. I am trying to develop the method to calculate this movement.

My initial thought is to move X (2 units) along a straight line, say (1,1) to (1, 4), then rotate it by the angle: 45 degrees. This would then plot the correct point on the path.
So something of this nature:

1.) Get the distance on the Y plane, in this case 2: (1,3).
2.) Rotate by the angle, in this case 45 degrees.
3.) Save the new point.
Repeat until at the spot.

Would this work? What would happen if the location does not fit directly on a solid number, ie: (1.5, 3), would that cause any unforeseen problems with using say, contains or a transform with affinetransform?

Thanks!

You probably ought to look up (or reinvent) the basic line drawing
algorithm, which (sketched) is to first decide on a path that will
change X by 1 each time, or one which will change Y by one,
then accumulate a fraction to decide when to kick the other
axis by 1 as well. Low level graphics engines use this.

Details will vary depending on exactly the behavior you want,
for example if diagonal steps are ok or not.

If the distance is large and you don’t necessarily want the same
number of steps as pixels (or cells) then just interpolate the
number of steps you want independently in X and Y.

That’s really some weird idea, you got there. :slight_smile:

One way to do it:
-make a vector from this point to the other
-normalize it (what you got is a so called direction vector)
-add the components (multiplied with the speed) to the position

Another way to do it:
-use Math.atan2 to get the angle
-use sin/cos (multiplied with the speed) to update the position

If it’s… say… a bullet (going straight all the way), you’ll only need to calculate the deltas once (c’tor). For updating the position you simply add the deltas to the position.

eg…

turretDeg=(float)Math.toDegrees(Math.atan2(crossPos.x-tankPos.x,tankPos.y-crossPos.y));
[...]
bullets.add(new Bullet(tankPos.x,tankPos.y,turretDeg,12f));
[...]
class Bullet{
	public float x,y,dx,dy,deg;
	public Bullet(float x,float y,float deg,float speed){
		this.x=x;
		this.y=y;
		this.dx=(float)Math.sin(Math.toRadians(turretDeg))*speed;
		this.dy=-(float)Math.cos(Math.toRadians(turretDeg))*speed;
		this.deg=deg;
	}
	public void tick(){
		x+=dx;
		y+=dy;
	}
[...]

edit: Forgot the *speed there (in the c’tor). The original source looks quite different. :slight_smile:

[quote]point A: (1,1) and then point B (4,4). I want to move from A to B
[/quote]
What about linear interpolation?
C = A + [0…1] * (B - A)

It doesn’t get much faster than that.

[quote=“Riven,post:4,topic:29929”]
What about linear interpolation?
[/quote

Ah but the OP wants to move a certain distance each iteration. You need to normalise like what oNyx said.

This worked great - thank you very much!

I was wondering if someone could explain the way atan works? i didn’t really get what the API said. Thanks!

you’ll have to google for geometry definitions of sinus and cosinus for that, as atan is reverse function of tanges, and tanges is sin/cos actually (in a circle of radius 1). I can’t really explain it in english words as I don’t know the technical terms good.

here’s a wiki entery

here is another wiki entry for polar coordinates, since this corresponds to the use case you have