August 27th, 2011, 02:56 AM

Moving 2D Vectors at specified angle
I've got the following code which calculates the distance between two vectors (one the starting point and one the target point), the angle and the direction from the starting point to the target point.
I now want to move from the starting point to the target coordinates at the specified x,y angle. My question is, how do I do that?
If I have a specified velocity, how do I get my sprite to move from the starting point in the direction of the target point at the calculated angle?
Code:
'calculate distance between the two vectors
Horizontal = Abs(X2  X1)
Vertical = Abs(Y2  Y1)
DistanceBetween = Sqrt((Horizontal * Horizontal) + (Vertical * Vertical))
'calculate the angle between position and target position
angle = Atan2(Y2  Y1, X2  X1) * 180 / Pi
'direction
x = Sin(angle)
y = Cos(angle)
Thanks for any help given
August 27th, 2011, 11:41 AM

I've been trying a few things could anyone confirm that what I'm doing is accurate (it looks fine to me but I'm not 100% sure)?
I've been using this:
DirectionX = Velocity * Cos(anglex)
DirectionY = Velocity * Sin(angley)
(and additionally the Cos and Sin where appropriate)
Then:
X1 = X1 + directionx
Y1 = Y1 + directiony
August 30th, 2011, 08:23 AM

Moving 2D Vectors at specified angle
I've been trying a few things could anyone confirm that what I'm doing is accurate (it looks fine to me but I'm not 100% sure)?
August 30th, 2011, 08:56 AM

Originally Posted by funchilliguy
I've been trying a few things could anyone confirm that what I'm doing is accurate (it looks fine to me but I'm not 100% sure)?
Should I take it that you are embarrassed by my dreadful efforts to get to grips with this?
August 30th, 2011, 12:41 PM

sines and cosines not required.
Code:
Note 0
. Program to draw a sprite ($) between 2 random positions using
ANSI escape sequences with a terminal window.
. This version does not erase the sprite.
. Run this program in a command window with ANSI support
. xterm, for instance, or DOS command window with correct set up.
. install j from www.jsoftware.com
. save this program in current directory as sprite.ijs
. run command window.
. start jconsole.
. issue sentence load'sprite.ijs'
)
ANSI=: '[',~27{a.
embed=: >@{.@[ , ] , >@}.@[
goto=: (ANSI;'H')embed' '&=`(,:&';')}@":
CLS=:ANSI,'2J'
HOM=:goto 0 0
START=: ? 0 0
END=: ? 0 0
position=: 2 : 'm+y*nm'
smoutput CLS
smoutput @ ('$' ,~ [: goto [: >. 40 80 * START position END)"0 (i. % >:) 10000
j notation let's us think at level higher than "(x1,y1)".
The algorithm is:
Position at time t is some fraction of the way from start to finish.
The fraction is (the distance traveled) divided by (the separation).
You've already computed the separation using Mr.Pythagorus's idea.
The distance traveled is
(current_time  time_of_traversal_start)*speed
Thus "fraction" depends on time.
The current position is the
(start position) + (fraction)*((end position)  (start position))
You'll need to understand that "positions" are straight old Euclidean
(x,y) coordinate pairs, that "fraction" is a scalar. I can't think of
a reason to spell it out any deeper than this.