### Author Topic: Moving between two sets of coordinates  (Read 8948 times)

• Guest
##### Moving between two sets of coordinates
« on: 2009-Jan-21 »
This code allows you to calculate the X/Y step values between two sets of coordinates, allowing you to move between the two.

Code: (glbasic) [Select]
`// --------------------------------- //// Project: TestLineMove// Start: Monday, January 19, 2009// IDE Version: 6.138TYPE tMoveAlongLine ax ay ex ey bx by ix iy numSteps maxDistanceENDTYPEFUNCTION _dummy:ENDFUNCTIONINLINE #include "math.h" extern double atan2 (      double y,      double x );ENDINLINEFUNCTION setTo:x,y moveAlongLine.ax=x moveAlongLine.ay=yENDFUNCTIONFUNCTION changeTo:bx,by moveTo(moveAlongLine.ax,moveAlongLine.ay,bx,by)ENDFUNCTIONFUNCTION angleBetweenPoints:ax,ay,ex,eyINLINE double angle; angle=atan2(ex-ax,ey-ay); if (angle<0.0) { angle=180.0+(180.0+angle); } return angle;ENDINLINEENDFUNCTIONFUNCTION moveTo:sx,sy,fx,fy,mDistance=-1.0LOCAL dxLOCAL dy moveAlongLine.ax=sx moveAlongLine.ay=sy moveAlongLine.ex=fx moveAlongLine.ey=dy dx=fx-sx dy=fy-sy 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.numStepsENDFUNCTIONFUNCTION 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 TRUEENDFUNCTION`
The angle between two points code needs changing as its inaccurate, but the rest works fine.

The test code :

Code: (glbasic) [Select]
`GLOBAL moveAlongLine AS tMoveAlongLineSETSCREEN 1024,768,0moveTo(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

#### Kitty Hello

• code monkey
• Prof. Inline
• Posts: 10839
• here on my island the sea says 'hello'
##### Re: Moving between two sets of coordinates
« Reply #1 on: 2009-Jan-22 »
why inline? Why ATAN2? The GLBasic ATAN _is_ atan2.

#### Moru

• Prof. Inline
• Posts: 1793
##### Re: Moving between two sets of coordinates
« Reply #2 on: 2009-Jan-22 »
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.

• Guest
##### Re: Moving between two sets of coordinates
« Reply #3 on: 2009-Jan-22 »
I had thought ATAN2 would compile, but alas, its not found.

#### Schranz0r

• Premium User :)
• Prof. Inline
• Posts: 5116
• O Rly?
##### Re: Moving between two sets of coordinates
« Reply #4 on: 2009-Jan-22 »
ATAN == ATAN2 !

And works fine
I DGArray's

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

#### Kitty Hello

• code monkey
• Prof. Inline
• Posts: 10839
• here on my island the sea says 'hello'
##### Re: Moving between two sets of coordinates
« Reply #5 on: 2009-Jan-22 »
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.

• Guest
##### Re: Moving between two sets of coordinates
« Reply #6 on: 2009-Jan-22 »
Thats true   Should have relaised that

#### Hemlos

• To boldy go where no pixel has gone before!
• Global Moderator
• Prof. Inline
• Posts: 1621
• Particle Hawk
##### Re: Moving between two sets of coordinates
« Reply #7 on: 2009-Jun-19 »
Old topic yes..
but theres another way i think, much easier for me to understand...

where: a2+b2=c2
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

« Last Edit: 2009-Jun-25 by Hemlos »
Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)

#### Schranz0r

• Premium User :)
• Prof. Inline
• Posts: 5116
• O Rly?
##### Re: Moving between two sets of coordinates
« Reply #8 on: 2009-Jun-20 »
Hey Hemlos, great work!

did you have ICQ installed?
you can PM your number

I have some great ideas
I DGArray's

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

#### Hemlos

• To boldy go where no pixel has gone before!
• Global Moderator
• Prof. Inline
• Posts: 1621
• Particle Hawk
##### Re: Moving between two sets of coordinates
« Reply #9 on: 2009-Jun-20 »
Thanks Schranz0r

For some reason the smtp server is timing out, is it because im in FLorida?
« Last Edit: 2009-Jun-25 by Hemlos »
Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)

#### Schranz0r

• Premium User :)
• Prof. Inline
• Posts: 5116
• O Rly?
##### Re: Moving between two sets of coordinates
« Reply #10 on: 2009-Jun-20 »

... is it because im in Florida?

Thats not fair!
I DGArray's

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

#### Hemlos

• To boldy go where no pixel has gone before!
• Global Moderator
• Prof. Inline
• Posts: 1621
• Particle Hawk
##### Re: Moving between two sets of coordinates
« Reply #11 on: 2009-Jun-25 »
This topic went from making example of movement math,
to stirring up a particle engine system function set.

ps. check the beta section for the latest screenshot..it is in alpha testing still
Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)

#### Schranz0r

• Premium User :)
• Prof. Inline
• Posts: 5116
• O Rly?
##### Re: Moving between two sets of coordinates
« Reply #12 on: 2009-Jun-25 »
little test (my solution):

Code: (glbasic) [Select]
`DRAWRECT 0,0,10,10, RGB(0xff, 0x00, 0x00)DRAWLINE 5,5,10,5, RGB(0x00, 0x00, 0x00)GRABSPRITE 0,0,0,10,10LOCAL x1=400,y1=100,x2=300,y2=250WHILE TRUE LOCAL winkel = ATAN(y1-y2,x1-x2)+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+180SHOWSCREENWENDEND`
« Last Edit: 2009-Jun-25 by Schranz0r »
I DGArray's

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

#### Hemlos

• To boldy go where no pixel has gone before!
• Global Moderator
• Prof. Inline
• Posts: 1621
• Particle Hawk
##### Re: Moving between two sets of coordinates
« Reply #13 on: 2009-Jun-25 »
Magic with Math! Beautiful
Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)