draw curved road function by Visigoth26th Mar 2007 3:15
|
---|
Summary simple function to create curved road segments by using triangle objects. You can set radius of turn, angle of turn, width of road, and segments per section (resolution) Description by just typing in this: Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com REM Project: Curved Roads Function REM Created: 3/7/2007 8:15:47 PM REM Created by Mike Shihrer REM ***** Main Source File ***** REM REM demonstrates function to draw curved roads. REM at this time, turns must alternate between right and left REM for simplicity sake for me, the z coordinate is actually the height value REM feel free to use this code, a credit to me would be cool though, thanks autocam off rem load your own road texture here, and uncomment out the texture command inside the drawroad function rem load image "E:\Dev\Projects\STRider\media\road.jpg",1 rem load image "E:\Dev\Projects\STRider\media\road1.jpg",2 REM array stores x,y coords for triangles to be drawn dim coordinatearray#(4,36) Global trianglecount rem keeps track of number of triangles Global xcenter as float rem center of radius of turn x position Global ycenter as float rem center of radius of turn y position Global prevangle as float rem the angle of the end of the last curve section Global lastradius as float rem the outside radius of the last curve section Global cameraflag as integer xcenter = 15000 rem pick somewhere way out in space to avoid negative numbers ycenter = 15000 rem pick somewhere way out in space to avoid negative numbers prevangle = 180 rem start angle of first turn trianglecount = 0 rem initialize triangle object numbers here, it increments as you draw more triangles lastradius = 0 rem should start at zero here rem parameters are : drawroad(direction 0 for left, 1 for right,x pos of radius center, y pos of radius center, inside radius, outside radius, segments per curve, angle of turn, last turn ending angle, triangle object number) drawroad(1,xcenter,ycenter,300,400,16,180,prevangle,trianglecount) drawroad(0,xcenter,ycenter,300,400,8,90,prevangle,trianglecount) drawroad(1,xcenter,ycenter,1000,1100,2,5,prevangle,trianglecount) drawroad(0,xcenter,ycenter,200,300,4,90,prevangle,trianglecount) drawroad(1,xcenter,ycenter,300,400,16,180,prevangle,trianglecount) drawroad(0,xcenter,ycenter,300,400,8,90,prevangle,trianglecount) drawroad(1,xcenter,ycenter,1000,1100,2,5,prevangle,trianglecount) drawroad(0,xcenter,ycenter,200,300,4,90,prevangle,trianglecount) position camera xcenter,ycenter,-15 xrotate camera -90 rem comment this out for a top down view of the road Do movecamera() Loop function drawroad(direction as integer,centerx as float, centerY as float, insideradius as float, outsideradius as float, segments as integer, angle as float,lastangle as float,triangles as integer) xpi as float ypi as float xpo as float ypo as float a as float calcangle as float calcangle = angle / segments if direction = 0 rem this is for laft turns rem Calculate radius centerx and centery between turns xcenter = centerx + (insideradius + lastradius) * cos(prevangle) ycenter = centery + (insideradius + lastradius) * sin(prevangle) a = lastangle - 180 rem keep the angle in a clock wise orientation rem plot the x and y positions on the curves for b = 1 to (segments + 1) xpi = xcenter + insideradius * cos(a) ypi = ycenter + insideradius * sin(a) xpo = xcenter + outsideradius * cos(a) ypo = ycenter + outsideradius * sin(a) REM store the segment coordinate positions in an array so we can have the positions for the triangles coordinatearray#(1,b) = xpi coordinatearray#(2,b) = ypi coordinatearray#(3,b) = xpo coordinatearray#(4,b) = ypo a = a + calcangle next b rem update prevangle with ending angle of curve, update lastradius so can calculate next radius centerx and centery prevangle = a - calcangle lastradius = outsideradius endif if direction = 1 rem this is for right turns xcenter = centerx + (insideradius + lastradius) * cos(prevangle) ycenter = centery + (insideradius + lastradius) * sin(prevangle) a = lastangle + 180 for b = 1 to (segments + 1) xpi = xcenter + insideradius * cos(a) ypi = ycenter + insideradius * sin(a) xpo = xcenter + outsideradius * cos(a) ypo = ycenter + outsideradius * sin(a) REM store the segment coordinate positions in an array so we can have the positions for the triangles coordinatearray#(1,b) = xpi coordinatearray#(2,b) = ypi coordinatearray#(3,b) = xpo coordinatearray#(4,b) = ypo a = a - calcangle next b prevangle = a + calcangle lastradius = outsideradius endif rem loop through the coordinatearray and draw the triangles for i = 1 to segments make object triangle triangles + i,coordinatearray#(1,i),coordinatearray#(2,i),0,coordinatearray#(3,i),coordinatearray#(4,i),0,coordinatearray#(3,(i+1)),coordinatearray#(4,(i+1)),0 rem texture object triangles + i,1 next i triangles = triangles + i for ii = 1 to segments make object triangle triangles + ii,coordinatearray#(1,ii),coordinatearray#(2,ii),0,coordinatearray#(3,ii+1),coordinatearray#(4,ii+1),0,coordinatearray#(1,(ii+1)),coordinatearray#(2,(ii+1)),0 rem texture object triangles + ii,2 next ii triangles = triangles + ii trianglecount = triangles + 1 endfunction function movecamera() if upkey() then move camera .50 if downkey() then move camera -.5 if leftkey() then turn camera left .2 if rightkey() then turn camera right .2 endfunction |