PATH EDITOR

BlitzPlus Forums/BlitzPlus Programming/PATH EDITOR

Ash_UK(Posted 2003) [#1]
Hi guys, i was wondering if anyone could tell me how to write a path editor function, one that would allow you to click where you want the path points. If someone could maybe show me some very basic code, (just to allow the user to click points and have something following the path) i would be very grateful.

Thank for any help

BLUE
:)


Ash_UK(Posted 2003) [#2]
This is what i have tried so far but somethings wrong, i can't quite think what it might be :(


.start
Global new_id=1			; stores a new id
Global current_id=1		; holds the current target id




	Type path

		Field x
		Field y
		Field id
	
	End Type



	
 



	Function create_point()			; create a path point

		p.path	=	New path
	
		p\x=Int(MouseX())			; store x position
		p\y=Int(MouseY())			; store y position
		p\id=new_id
		new_id=new_id+1
	
	End Function







	Function update_path(x,y)			; use objects co-ordinates to calculate the path

		For p.path	=	Each path			; cycle through each point

			If p\id=current_id 				; until id matches current id
			
				If x<p\x Then x=x+1
				If x>p\x Then x=x-1
				If y<p\y Then y=y+1
				If y>p\y Then y=y-1			
		
				If x=p\x And y=p\y Then current_id=current_id+1
			
			
			EndIf
		
			Plot p\x,p\y
			
		Next
	
	End Function

	





		
		
		
		
		



	
	

	



Ross C(Posted 2003) [#3]
Hey again. Here's some code i put together that should do what you want. :D

Graphics 800,600
SetBuffer BackBuffer()

Global max_path_points=100

Dim pathx#(max_path_points-1); set up 101 path points
Dim pathy#(max_path_points-1); -1 because arrays start at zero

Global path_counter=-1
Global current_path=0
Global cx#=0
Global cy#=0
Global movement_started=0


While Not KeyHit(1)
	Cls
	
	If MouseHit(1) Then
			setpath(MouseX(),MouseY()); set path point with the mouses current coords
	End If
	
	If KeyHit(57) And movement_started=0 And path_counter>0 Then; if spacebar pressed then move rectangle
		current_path=0; reset the current path to 0
		cx=pathx(0); set the rectangles coords to the first path point
		cy=pathy(0)
		movement_started=1; set flag for movement started
	End If
	If KeyHit(28) Then; if the enter key is pressed then stop movement
		movement_started=0; clear flag for movement
		cx=pathx(0); set rectangle coords to the first path point
		cy=pathy(0)
		current_path=-1; set current path to none exsistant
	End If

	
	
	
	draw_path_points(); function for drawing the pathpoints
	If movement_started=1 Then move(); draw the moving rectangle and move it
	Color 200,200,200; set drawing color
	Rect MouseX()-1,MouseY()-1,2,2; draw mouse location with a rectangle
	Text 0,0,"Click mouse to set path point. Press spacebar to start animation. Press enter to stop."
	Text 0,10,"movement started="+movement_started
	Flip
Wend
End

Function move()
	ang#=0
	If current_path<path_counter Then; make sure the current path point is less than the highest
		ang=ATan2(pathy(current_path+1)-pathy(current_path),pathx(current_path+1)-pathx(current_path))
		;/\ basically means find the angle between the current path point and the next one /\
		cx=cx+Cos(ang); increase the x coord of the rect following the path points based on the angle
		cy=cy+Sin(ang); increase the y coord of the rect following the path points based on the angle
		Color 100,200,100; change drawing color
		Rect cx-3,cy-3,6,6; draw the rectangle central to it's coords
		If Abs(cx-pathx(current_path+1))<1 And Abs(cy-pathy(current_path+1))<1 Then
			; /\ if the rects coords are with a 1 pixel distance of the path point then /\
			current_path=current_path+1; increase the current path point number
			cx=pathx(current_path); set the rectangles coords to that of the next path point
			cy=pathy(current_path)
		End If
	End If
End Function

Function setpath(x,y)
	path_counter=path_counter+1; increase counter
        If path_counter>max_path_points-1 Then; if counter is higher than the available number of path points then set it back
               path_counter=max_path_points-1
        Else; else 
	       pathx(path_counter)=x; record the x and y positions
	       pathy(path_counter)=y
        End If
End Function

Function draw_path_points()
	For loop=0 To path_counter; loop through all the path points
		Color 100,100,200
		Rect pathx(loop),pathy(loop),2,2
		If loop<path_counter Then; do this loop while the loop value is less than the highest path value
			Rect pathx(loop+1)-1,pathy(loop+1)-1,2,2; draw a rectangle at the path point so its central
			Color 50,50,150; change drawing color
			Line pathx(loop),pathy(loop),pathx(loop+1),pathy(loop+1); draw a line between the current point and the next one
		End If
	Next
End Function




Ross C(Posted 2003) [#4]
Updated it. wasn't using a float for the angle so it would sometimes run straight off the pathpoints :o)


Ash_UK(Posted 2003) [#5]
Thankyou so much joker, i am ever so grateful for your response and kind help. Thank god theres people like you around :)

Thanks again joker

BLUE
:)


Ross C(Posted 2003) [#6]
anytime :)


Ash_UK(Posted 2003) [#7]
Oh, ;-), just one more thing joker, could explain to me exactly what this line please? (just to help me in my understanding of why it works):

If Abs(cx-pathx(current_path+1))<1 And Abs(cy-pathy(current_path+1))<1 Then



If you could just explain to me what each part does and why.

Thanks joker

BLUE
:)


Ross C(Posted 2003) [#8]
If Abs(cx-pathx(current_path+1))<1

Abs means make the number positive regardless.

cx is the current x coord of the rectangle.

pathx(0) is the array that holds the x locations of the path points.

current_path+1 is the current waypoint the rectangle is on. The plus one means the bit of code will compare the rectangles x coord with the x coords of the next waypoint.

So, let cx=100 and let current_path=5 and let pathx(5)=103

if Abs(cx-pathx(current_path))<1

if abs(cx-pathx(5))<1

if abs(100-103)<1

if abs(3)<1


If the distance is less than one pixel then, current_path is incremented.

And Abs(cy-pathy(current_path+1))<1


same as before, except compare the y coord of the rectangle with the y coord of the next path point.

If you need anymore help, plz post back :)


Ash_UK(Posted 2003) [#9]
Thanks every so much joker, i'm your biggest fan right now ;) lol.
Seriously though, i really really appriciate all your kind wisdom to get me through this :)

I'm gonna go and experiment with this command now.

Thanks again m8

BLUE
:)


Ross C(Posted 2003) [#10]
Again, no problem :D


Ash_UK(Posted 2003) [#11]
Ok (cough) i'm sorry to bother you again joker but i was just wondering if you could check this code and help me out here.
Basically, i have tried to modify the code in order for me to get a better understanding of it, but for some reason it doesn't seem to be working and i can't see why :(

Heres the code:




Global pathx#=0		;these are used to position the object on the path
Global pathy#=0

Global id_counter=-1	; keeps track of current point created
Global current_point=0		; current point to follow

Global max_points=100		; maximum allowed number of points

Dim point_x(max_points-1)
Dim point_y(max_points-1)





;******************************************
;MAIN CODE
;******************************************

move=0
While Not KeyHit(1)
	Cls
	
	If MouseHit(1) Then
			create_point(MouseX(),MouseY()); set path point with the mouses current coords
	End If
	
	If KeyHit(57) And move=0 And id_counter>0 Then; if spacebar pressed then move rectangle
		current_point=0; reset the current path to 0
		pathx=point_x(0); set the rectangles coords to the first path point
		pathy=point_y(0)
		move=1; set flag for movement started
	End If
	
	If KeyHit(28) Then; if the enter key is pressed then stop movement
		move=0; clear flag for movement
		pathx=point_x(0); set rectangle coords to the first path point
		pathy=point_y(0)
		current_point=-1; set current path to none exsistant
	End If

	
	
	
	follow_path(); function for drawing the pathpoints
	If move=1 Then update_path(); draw the moving rectangle and move it
	Color 200,200,200; set drawing color
	Rect MouseX()-1,MouseY()-1,2,2; draw mouse location with a rectangle
	Text 0,0,"Click mouse to set path point. Press spacebar to start animation. Press enter to stop."
	Text 0,10,"movement started="+movement_started
	Flip
Wend
End




;*************************************************
;*************************************************








Function create_point(x,y)
	
		id_counter=id_counter+1
		
		If id_counter>max_points-1
			id_counter=max_points-1
		Else
			point_x(id_counter)=x		; assign give values to x and y fields
			point_y(id_counter)=y
		EndIf
		
End Function








Function update_path()

	angle#=0
	
	If current_point<id_counter
	
		angle=ATan2(point_y(current_point+1)-point_y(current_point),point_x(curent_point+1)-point_x(current_point))
	
		pathx=pathx+Cos(angle)
		pathy=pathy+Sin(angle)
	
		Rect pathx-3,pathy-3,6,6

	
		If Abs(pathx-point_x(current_point+1))<1 And Abs(pathy-point_y(current_point+1))<1 Then
		
			current_point=current_point+1; increase the current path point number
			pathx=point_x(current_point); set the rectangles coords to that of the next path point
			pathy=point_y(current_point)
		
		End If
	
	EndIf
	
	
	
End Function








Function follow_path()

	For a=0 To id_counter
		
		Rect point_x(a),point_y(a),2,2
		
		If a<id_counter
		
			Rect point_x(a+1)-1,point_y(a+1)-1,2,2
			Line point_x(a),point_y(a),point_x(a+1),point_y(a+1)
			
		EndIf
		
	Next

End Function


		






Thanks for your kind help joker

:)
BLUE


Ross C(Posted 2003) [#12]
hehe, perfect coding except from you spelt current wrong :)

In function update_path

angle=ATan2(point_y(current_point+1)-point_y(current_point),point_x(curent_point+1)-point_x(current_point))


one of the currents is spelled curent :D
Function update_path()

	angle#=0
	
	If current_point<id_counter
	
;***************************************
		angle=ATan2(point_y(current_point+1)-point_y(current_point),point_x(curent_point+1)-point_x(current_point)); <<<**** current spelt wrong :)
;***************************************
	
		pathx=pathx+Cos(angle)
		pathy=pathy+Sin(angle)
	
		Rect pathx-3,pathy-3,6,6

	
		If Abs(pathx-point_x(current_point+1))<1 And Abs(pathy-point_y(current_point+1))<1 Then
		
			current_point=current_point+1; increase the current path point number
			pathx=point_x(current_point); set the rectangles coords to that of the next path point
			pathy=point_y(current_point)
		
		End If
	
	EndIf
	
	
	
End Function


It's wee mistakes like that, that sometimes throw me off to :)


Ash_UK(Posted 2003) [#13]
:0 OOPS!!!! lol, my bad.

I wouldn't have ever found that problem,lol
I really hate it when that happens :)
I could have sworn that the code was right hehe :)
Ah well.

Thanks ever so much joker for you help
You are the most patient person i have ever had the honour of recieving help from.

Thanks again
:)
BLUE


Ash_UK(Posted 2003) [#14]
:0 FINALLY, IT WORKS!!!!!!!! :) :) :)

Thankyou very much joker for your patience and kind wisdom, i can finally sleep well tonight.

:)

BLUE


Ross C(Posted 2003) [#15]
:D