How to Follow a Contour?

BlitzPlus Forums/BlitzPlus Beginners Area/How to Follow a Contour?

Normsthename(Posted 2008) [#1]
I need to write a routine where the Image drops down the left hand of the background and then goes right and follows the contour until it gets to the right hand side of the screen.
I have the following code that draws the background etc, and the object moves but the Object just bounces up and down at the moment.....
I need the object to move at one pixel resolution and follow the contour perfectly.
I am assuming that I could use Imagescollide for the Collision Detection?

Graphics 800,600,0,2 
SetBuffer BackBuffer()

AI = CreateImage(100,20)
background = CreateImage(800,600) 

Xflag=0
Yflag=0

xpos=44
ypos=250

;Draw Test Background
Line 43,200,755,200
Line 43,200,43,537
Line 755,200,755,400
Line 43,537,143,450
Line 143,450,200,450
Line 200,450,200,400
Line 200,400,150,350
Line 150,350,150,300
Line 150,300,300,300
Line 300,300,250,350
Line 250,350,350,400
Line 350,400,450,425
Line 450,425,375,450
Line 375,450,375,500
Line 375,500,475,475
Line 475,475,550,475
Line 550,475,671,475
Line 671,475,755,400
GrabImage background,0,0 

;Draw AI Image
Oval 50,50,20,20,1
GrabImage AI,50,50

;Main Loop
Repeat
Cls

If yflag=0 Then ypos=ypos+1
If yflag=1 Then ypos=ypos-1

If ImagesCollide(AI,xpos,ypos,0,background,0,0,0) And yflag=1 Then yflag=0:Goto again
If ImagesCollide(AI,xpos,ypos,0,background,0,0,0) And yflag=0 Then yflag=1

.again
DrawImage background,0,0
DrawImage AI,xpos,ypos

Flip
Until KeyDown(1) 
End


I have been struggling to write this for well over a week but I am still nowhere closer...
Any help would be very much appreciated.

Andy


Andy_A(Posted 2008) [#2]
You already have the endpoints for the contour, now just plot the points that draw another new contour one circle radius on the inside of the existing contour. That is the path you will need to follow.

To plot the "path" contour programatically, you'll need a "line intersect function", a "distance function", and the use of the "Atan2" function. The first two functions can be found in the code archives, the third is an intrinsic function of Blitz+.

For actually moving the circle one pixel at a time you'll need to use Sin() and Cos() functions. An example of moving along a line at 45 degrees (south east) use something like this:

;to move from 100,100 to 300,300

Graphics 640,480,32,2
SetBuffer BackBuffer()

For f# = 100 To 300 Step 1.414
	x% = Cos(45) * f# + 100
	y% = Sin(45) * f# + 100
	toggle% = 1 - toggle
	If toggle = 1 Then Color 255,0,0 Else Color 255,255,255
;Color Rand(128,255),Rand(128,255),Rand(128,255)
	Oval x, y, 20, 20, False
	Flip
Next

WaitMouse()
End


Just make sure the circle is large enough to fully encompass any sprite you might use.

Andy


Normsthename(Posted 2008) [#3]
Thanks Andy
Thats really helpful for something else that I need to do...
But, there is one problem, the actual object I plan to use will be an irregular shape that varies in size and shape from level to level.
That is why I wanted to try and use collision detection instead of Maths.
One problem is that the collision has to work in many different directions according to the terrain.

I think I have worked out a way of plotting the path of the irregular shape using collision detection along the convoluted path in the example.
I have wrote the logic out and it works fine on paper, I just need to try and convert the Logic into actual code.....

Thanks

Andy