This mini tutorial, well example source, shows one a way how you can move an object between two known points.
The demo lets the user select the start & end point with the mouse, then moves the object (a circle in this case) between these. When complete, the demo lets you repeat this process over and over.
[pbcode]
Speed#=2
; init the variables used
gosub Reset_Variables
; start of main loop
Do
; clear the screen
cls 0
;Draw the start and end points (which default to be being off screen)
Circlec StartPoint_X,StartPoint_Y,5,0,rgb(255,0,255)
Circlec EndPoint_X,EndPoint_Y,5,0,rgb(155,255,055)
; Draw the MOve Objects position
Circlec CurrentPoint_X#,CurrentPoint_Y#,5,1,rgb(255,255,255)
; Select what mode the demo is in.
select MoveMent_Mode
; -----------------------------------------------------
; mode0 = select Starting point of objects path
; -----------------------------------------------------
case 0
Print "Select Start point"
While MouseButton()=1
StartPoint_X=MouseX()
StartPoint_Y=MouseY()
FlushMouse
inc MoveMent_Mode
endwhile
; -----------------------------------------------------
; mode1 = select strart point and calc movement vectors
; -----------------------------------------------------
case 1
Print "Select End point"
While MouseButton()=1
EndPoint_X=MouseX()
EndPoint_Y=MouseY()
FlushMouse
inc MoveMent_Mode
; calc the X & Y steps
Dist#=getdistance2d(StartPoint_X,StartPoint_Y,EndPoint_X,EndPointY)
; Calc the time it'll take to move this distance at this speed
; ie. the number of steps
Movement_Time=Dist#/speed#
; calc movement vector (change in X & Y over this time peroid)
Movement_StepX#=(EndPOint_X-StartPoint_X)/Float(Movement_Time)
Movement_StepY#=(EndPOint_Y-StartPoint_Y)/Float(Movement_Time)
; Init the current Positions X& Y to the starting position
CurrentPoint_X#=StartPoint_X
CurrentPoint_Y#=StartPoint_Y
endwhile
; -----------------------------------------------------
; mode2 = Move the object between the two points
; -----------------------------------------------------
case 2
print "Moving Object"
; Calc the time it'll take
dec Movement_Time
if Movement_time<0
; On the last move, force the End point as the current position
CurrentPoint_X#=EndPoint_X
CurrentPoint_Y#=EndPoint_Y
inc MoveMent_Mode
else
; Init the current POsition from the starting position
CurrentPoint_X#=CurrentPoint_X#+MoveMent_StepX#
CurrentPoint_Y#=CurrentPoint_Y#+MoveMent_StepY#
endif
; -----------------------------------------------------
case 3
; -----------------------------------------------------
print "DONE - Press mouse to restart"
While MouseButton()=1
StartPoint_X=MouseX()
StartPoint_Y=MouseY()
FlushMouse
gosub Reset_Variables
endwhile
endselect
Sync
loop
Reset_Variables:
; INit the Mode variable to zero. This varibale is used
; select what mode the demo is in. I.e. it's accepting input
; from the user, or moving the ball object
MoveMent_Mode=0
; Set the Time Varibale. This Holds the number of frames
; it'll take for our object to move between the two points
MoveMent_Time=-1
; CurretPOint X & Y are used as the moving objects current position
CurrentPOint_X#=-10
CurrentPOint_Y#=0
; StartPoint X& Y are used to record the objects starting position
StartPOint_X=-10
StartPOint_Y=0
; EndPoint X& Y are used to record the objects end position
EndPoint_X=-0
EndPoint_Y=0
; Movement Step X +Y hold the change in objects Move along the X and Y axis
Movement_StepX#=0
Movement_StepY#=0
return
[/pbcode]