sorry if it doesn't come out well, the code required a house.x object, you can download the entire program from the link on the top of this page (my first post!) if you find any difficulties in understanding the code just give me a shout and i'll be glad to xplain ^_^
Rem * Title : First Person Camera
Rem * Author : DBS-LB
Rem * Date : 1st Sept 99
remstart -----------------------------------------------------------------
athr: hexgear
code: pathfinding
note: this just shows a very easy method of pathfinding, can be applie to
many types of games, e.g. commandos 2, if your man is in the sitting
room and you click the mouse for him to move to the bathroom, the
algorithm gets him to the bathroom then he makes his way to the
clicked point withough passing through any walls, hope it helps.
house plan showing key rooms/points
9 - bathroom.
4 - sitting room.
7 - bedroom.
2 - kitchen
5 - corridor
1,3,6,8 - connection points
__________________________________
| | |
| | |
| 9 | 4 |
| | |
|_____ ____|________ __________|
| |
| 8 6 5 3 1 |
|_________ _____________ ______|
| | |
| | |
| 7 | 2 |
| | |
|______________|_________________|
remend -------------------------------------------------------------------
rem settings 1
set display mode 1024,768,16
hide mouse
sync on
sync rate 50
autocam off
set camera range 1,200
rem images
load image "floor.bmp",1
rem the matrix
make matrix 1,100,60,10,6
prepare matrix texture 1,1,1,1
update matrix 1
delete image 1
rem objects
load object "house.3ds",1
make object sphere 2,2
color object 2,rgb(255,0,0)
rem settings 2
color backdrop rgb(0,0,0)
rem path finding variables
dim coord(9,2)
dim paths(5,4)
`bathroom
paths(1,1) = 985
paths(1,2) = 9867
paths(1,3) = 9812
paths(1,4) = 9834
coord(9,1) = 15.1
coord(9,2) = 48.2
`corridor
paths(2,1) = 589
paths(2,2) = 567
paths(2,3) = 512
paths(2,4) = 534
coord(1,1) = 74.7
coord(1,2) = 29.8
coord(3,1) = 64.9
coord(3,2) = 30.3
coord(5,1) = 51.9
coord(5,2) = 30.5
coord(6,1) = 24.9
coord(6,2) = 31.0
coord(8,1) = 15.1
coord(8,2) = 29.3
`bedroom
paths(3,1) = 7689
paths(3,2) = 765
paths(3,3) = 7612
paths(3,4) = 7634
coord(7,1) = 24.9
coord(7,2) = 10.8
`sittingroom
paths(4,1) = 4389
paths(4,2) = 435
paths(4,3) = 4312
paths(4,4) = 4367
coord(4,1) = 64.9
coord(4,2) = 48.2
`kitchen
paths(5,1) = 2189
paths(5,2) = 215
paths(5,3) = 2167
paths(5,4) = 2134
coord(2,1) = 74.7
coord(2,2) = 10.8
srce_pos = 2
dest_pos = 0
path = 0
arrived = 1
`initial positions
position object 2,coord(1,1),4,coord(1,2)
position camera 50,50,-10
point camera 50,0,30
rem --------------------------------------------------------------------- rem
do
rem display directions
set cursor 25,25:print "1 - goto the bathroom."
set cursor 25,50:print "2 - goto the bedroom."
set cursor 25,75:print "3 - goto the kitchen."
set cursor 25,100:print "4 - goto the sitting room."
set cursor 25,125:print "5 - goto the corridor."
rem display room names
set cursor 200,225:print "- bathroom -"
set cursor 600,225:print "- sitting room -"
set cursor 400,350:print "- corridor -"
set cursor 175,550:print "- bedroom -"
set cursor 700,550:print "- kitchen -"
rem changing location
if arrived = 1
if keystate(2) = 1 then dest_pos = 9:if srce_pos <> dest_pos:gosub find_path:arrived = 0:endif
if keystate(3) = 1 then dest_pos = 7:if srce_pos <> dest_pos:gosub find_path:arrived = 0:endif
if keystate(4) = 1 then dest_pos = 2:if srce_pos <> dest_pos:gosub find_path:arrived = 0:endif
if keystate(5) = 1 then dest_pos = 4:if srce_pos <> dest_pos:gosub find_path:arrived = 0:endif
if keystate(6) = 1 then dest_pos = 5:if srce_pos <> dest_pos:gosub find_path:arrived = 0:endif
endif
rem control object
gosub object_ctrl
sync
loop
rem --------------------------------------------------------------------- rem
object_ctrl:
if arrived = 0
`log object coordinates
x# = object position x(2)
z# = object position z(2)
`obtain house position
ctr1 = val(mid$(str$(path),ctr2))
if sqrt((x#-coord(ctr1,1))*(x#-coord(ctr1,1)))+((z#-coord(ctr1,2))*(z#-coord(ctr1,2))) <= 1.5
`get next house position
ctr2 = ctr2 + 1
ctr1 = val(mid$(str$(path),ctr2))
if ctr1 = 0
arrived = 1
srce_pos = dest_pos
dest_pos = 0
path = 0
endif
endif
`move object to next house position
point object 2,coord(ctr1,1),1,coord(ctr1,2)
move object 2,1
else
ctr2 = 2
endif
return
rem --------------------------------------------------------------------- rem
find_path:
for x = 1 to 5
for y = 1 to 4
if len(str$(paths(x,y))) = 3
`3 point route
if val(mid$(str$(paths(x,y)),1)) = srce_pos & val(mid$(str$(paths(x,y)),3)) = dest_pos then path = paths(x,y)
else
`4 point route
if val(mid$(str$(paths(x,y)),1)) = srce_pos & val(mid$(str$(paths(x,y)),4)) = dest_pos then path = paths(x,y)
endif
next y
next x
return
rem --------------------------------------------------------------------- rem
everyday of life is a new chapter that has already been fortold but is up to the soul to capture.