r/Basic • • Nov 07 '22

Can someone help with an algorithm?

I have tried several things but this is not working very well for me.

My ship is at SX, SY, The enemy is at TX, TY . Currently, at each step, I adjust Px and Py by + or - 1 depending on the difference between SX and TX and The difference between SY and TY.

This leads to a diagonal while both coordinates differ - then a straight line after one coordinate is satisfied. It's not bad, but I would prefer a more direct route. After each step, I check to see if the weapon (px, py) has collided with a non-target object. So I would like something that moves the weapon incrementally until it reaches TX, TY.

Thanks!

3 Upvotes

8 comments sorted by

View all comments

Show parent comment

2

u/SparrowhawkOfGont Nov 08 '22

Use single precision variables for all the interim values but set the pixels using INT(X+.5). That will get you a line.