GLBasic forum
Codesnippets => 2Dsnippets => Topic started by: MrTAToad on 2009Jan21

This code allows you to calculate the X/Y step values between two sets of coordinates, allowing you to move between the two.
//  //
// Project: TestLineMove
// Start: Monday, January 19, 2009
// IDE Version: 6.138
TYPE tMoveAlongLine
ax
ay
ex
ey
bx
by
ix
iy
numSteps
maxDistance
ENDTYPE
FUNCTION _dummy:
ENDFUNCTION
INLINE
#include "math.h"
extern double atan2 ( double y, double x );
ENDINLINE
FUNCTION setTo:x,y
moveAlongLine.ax=x
moveAlongLine.ay=y
ENDFUNCTION
FUNCTION changeTo:bx,by
moveTo(moveAlongLine.ax,moveAlongLine.ay,bx,by)
ENDFUNCTION
FUNCTION angleBetweenPoints:ax,ay,ex,ey
INLINE
double angle;
angle=atan2(exax,eyay);
if (angle<0.0)
{
angle=180.0+(180.0+angle);
}
return angle;
ENDINLINE
ENDFUNCTION
FUNCTION moveTo:sx,sy,fx,fy,mDistance=1.0
LOCAL dx
LOCAL dy
moveAlongLine.ax=sx
moveAlongLine.ay=sy
moveAlongLine.ex=fx
moveAlongLine.ey=dy
dx=fxsx
dy=fysy
moveAlongLine.bx=ABS(dx)
moveAlongLine.by=ABS(dy)
IF moveAlongLine.bx>moveAlongLine.by
moveAlongLine.numSteps=moveAlongLine.bx
ELSE
moveAlongLine.numSteps=moveAlongLine.by
ENDIF
moveAlongLine.maxDistance=mDistance
moveAlongLine.ix=dx/moveAlongLine.numSteps
moveAlongLine.iy=dy/moveAlongLine.numSteps
ENDFUNCTION
FUNCTION process%:speed
IF moveAlongLine.numSteps<=0.0
RETURN FALSE
ELSE
INC moveAlongLine.ax,moveAlongLine.ix*speed
INC moveAlongLine.ay,moveAlongLine.iy*speed
DEC moveAlongLine.numSteps,speed
IF moveAlongLine.maxDistance>0.0
DEC moveAlongLine.maxDistance,speed
IF moveAlongLine.maxDistance<=0.0
moveAlongLine.numSteps=0.0
moveAlongLine.maxDistance=0.0
RETURN FALSE
ENDIF
ENDIF
ENDIF
RETURN TRUE
ENDFUNCTION
The angle between two points code needs changing as its inaccurate, but the rest works fine.
The test code :
GLOBAL moveAlongLine AS tMoveAlongLine
SETSCREEN 1024,768,0
moveTo(0,0,200,100)
DEBUG "Angle : "+angleBetweenPoints(100,100,10,20)+"\n"
WHILE TRUE
DEBUG "X:"+moveAlongLine.ax+"\n"
DEBUG "Y:"+moveAlongLine.ay+"\n"
DEBUG moveAlongLine.numSteps+"\n"
DRAWLINE moveAlongLine.ax,moveAlongLine.ay,1,1,RGB(255,255,255)
SHOWSCREEN
process(0.25)
WEND
If anyone has a better routine for calculating the angle between two coordinates, let me know :)

why inline? Why ATAN2? The GLBasic ATAN _is_ atan2.

Mabe this should be in the manual, MrTAToad here isn't the first to ask this. I was too slow to answer him on IRC though.

I had thought ATAN2 would compile, but alas, its not found.

ATAN == ATAN2 !
And works fine ;)

See, the atan function just takes one argument, which is: dy/dx. Now, if you have atan2, you can have the atan behaviour: atan2(dy/dx, 1.0), as well as the (much better) atan2(dy,dx) version, which offers you 360 degree angles, whereas atan() dis not deciding on the quadrant:
dy,dx is the same as dy,dx.
Anyway.

Thats true :) Should have relaised that :whistle:

Old topic yes..
but theres another way i think, much easier for me to understand...
where: a^{2}+b^{2}=c^{2}
distance x from origin to destination=Xdistance
distance y from origin to destination=Ydistance
hypotenuese=SQR(POW(Xdistance,2)+POW(Ydistance,2))
angle of origin x to destination x is CosAngle=ACOS(Xdistance/hypotenuese)
angle of origin y to destination y is SinAngle=ASIN(Ydistance/hypotenuese)
Results:
RELX=x+COS(CosAngle)*movesize
RELY=y+SIN(SinAngle)*movesize

Hey Hemlos, great work!
did you have ICQ installed?
you can PM your number ;)
I have some great ideas :P

Thanks Schranz0r :)
For some reason the smtp server is timing out, is it because im in FLorida? :P

... is it because im in Florida? :P
Thats not fair! :D

This topic went from making example of movement math,
to stirring up a particle engine system function set. :good:
ps. check the beta section for the latest screenshot..it is in alpha testing still

little test (my solution):
DRAWRECT 0,0,10,10, RGB(0xff, 0x00, 0x00)
DRAWLINE 5,5,10,5, RGB(0x00, 0x00, 0x00)
GRABSPRITE 0,0,0,10,10
LOCAL x1=400,y1=100,x2=300,y2=250
WHILE TRUE
LOCAL winkel = ATAN(y1y2,x1x2)+180
INC x1,COS(winkel)
INC y1,SIN(winkel)
IF KEY(203) THEN DEC x2,3
IF KEY(205) THEN INC x2,3
IF KEY(200) THEN DEC y2,3
IF KEY(208) THEN INC y2,3
DRAWLINE x1+5,y1+5,x2+5,y2+5, RGB(0xff, 0xff, 0xff)
ROTOSPRITE 0,x1,y1, winkel
ROTOSPRITE 0,x2,y2, winkel+180
SHOWSCREEN
WEND
END

Magic with Math! Beautiful :enc: