Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make a race track ?
#1
Hi, Marcus,

   
I wonder how to make a race track (see the picture above), could you please advice me ?

This is the code that you made some months ago. 
I believe there are some minor changes with the latest s3d library 

I haven't use it (.... just kidding ...)  Big Grin

Code:
' A library that I'm working on, not finished at all, don't use it!
include "s3d.n7"

#dbg
set window "test", 320, 240, false, 2
set redraw off

roadImg = loadimage("road.png")
roadImgW = width(roadImg)
roadImgH = height(roadImg)
decorImg = loadimage("winter_tree.png")
tunnelImg = loadimage("tunnel_winter.png")
bgImg = loadimage("sky_winter.png")

' Set up 3d view.
S3D_SetView(primary, rad(90), 0.1, 20)
' Render polygons as we add them. If direct mode is false, all polygons are batched, sorted by
' distance and rendered when we call S3D_Render. Here we draw from back to front manually.
S3D_SetDirectMode(true)

road = []
for z = 0 to 999  road[z] = [x: sin(z*0.05)*6, y: sin(z*0.1)*3]

playerz = 0.0
playerx = 0
playery = -1
projected = dim(4) ' Don't want to spawn too many tables in a game loop, reused for calculations.
lastTick = clock()
while not keydown(KEY_ESCAPE, true)
    t = clock()
    dt = (t - lastTick)/1000
    lastTick = t

    playerz = playerz + 4*dt

    ' calculate camera y
    if int(playerz) < sizeof(road) - 2
        k = playerz%1
        playery = (1 - k)*road[int(playerz)].y + k*road[int(playerz) + 1].y - 0.5
    else
        playery = road[sizeof(road) - 1].y - 0.5
    endif
    ' move left and right
    if keydown(KEY_LEFT)  playerx = playerx - 4*dt
    if keydown(KEY_RIGHT) playerx = playerx + 4*dt

    set color 255, 255, 255
    draw image bgImg, 0, -80 - playery*5
   
    S3D_Clear()
    S3D_Translate(-playerx, -playery, -playerz)
    S3D_Begin(S3D_QUADS)
    didFill = false

    strips = 0
    fills = 0

    for i = int(playerz) + 20 to int(playerz)
        if i < sizeof(road) - 2
            strips = strips + 1
            ' Do we need to fill with snow color to hide distant objects?
            S3D_Project(projected, 0, road[i + 1].y, i + 1)
            y0 = round(projected[1])
            S3D_Project(projected, 0, road[i].y, i)
            y1 = round(projected[1])
            if y1 > y0
                if not didFill
                    didFill = true
                    fills = fills + 1
                    set color 218, 218, 218
                    draw rect 0, y0, width(primary), height(primary) - y0, true
                    set color 255, 255, 255
                endif
            else
                didFill = false
            endif

            ' Draw road.
            x0 = road[i].x
            x1 = road[i + 1].x
            set color 255, 255, 255
            S3D_Texture(roadImg)
            S3D_Vertex(x0 - 2, road[i].y, i, 0, roadImgH)
            S3D_Vertex(x0 + 2, road[i].y, i, roadImgW, roadImgH)
            S3D_Vertex(x1 + 2, road[i + 1].y, i + 1, roadImgW, 0)
            S3D_Vertex(x1 - 2, road[i + 1].y, i + 1, 0, 0)
           
            ' Draw sprites.
            ' Fade in.
            z = (i + 1) - playerz
            if z > 15
                a = ((20 - z)/5)*255
                set color 255, 255, 255, a
            endif
            if i%20 = 0
                S3D_Texture(tunnelImg)
                S3D_Vertex(road[i].x - 5, road[i].y + 0.2, i, 0, height(tunnelImg))
                S3D_Vertex(road[i].x + 5, road[i].y + 0.2, i, width(tunnelImg), height(tunnelImg))
                S3D_Vertex(road[i].x + 5, road[i].y - 3.5, i, width(tunnelImg), 0)
                S3D_Vertex(road[i].x - 5, road[i].y - 3.5, i, 0, 0)               
            else
                if i%2 = 0
                    S3D_Texture(decorImg)
                    x = x0 - 2.5               
                    S3D_Vertex(x - 0.4, road[i].y, i, 0, height(decorImg))
                    S3D_Vertex(x + 0.4, road[i].y, i, width(decorImg), height(decorImg))
                    S3D_Vertex(x + 0.4, road[i].y - 1, i, width(decorImg), 0)
                    S3D_Vertex(x - 0.4, road[i].y - 1, i, 0, 0)
                else
                    S3D_Texture(decorImg)
                    x = x0 + 2.5               
                    S3D_Vertex(x - 0.4, road[i].y, i, 0, height(decorImg))
                    S3D_Vertex(x + 0.4, road[i].y, i, width(decorImg), height(decorImg))
                    S3D_Vertex(x + 0.4, road[i].y - 1, i, width(decorImg), 0)
                    S3D_Vertex(x - 0.4, road[i].y - 1, i, 0, 0)
                endif
            endif
        endif
    next
    S3D_End()
    'S3D_Render()
    set caret 0, 0
    wln round(1/dt)
    wln strips
    wln fills
    redraw
    wait 1
wend


Attached Files
.zip   racing_s3d.zip (Size: 303.55 KB / Downloads: 4)
Reply


Messages In This Thread
How to make a race track ? - by 1micha.elok - 11-06-2024, 02:14 AM
RE: How to make a race track ? - by johnno56 - 11-06-2024, 04:39 AM
RE: How to make a race track ? - by 1micha.elok - 11-06-2024, 05:07 AM
RE: How to make a race track ? - by Marcus - 11-06-2024, 07:49 AM
RE: How to make a race track ? - by Marcus - 11-06-2024, 04:07 PM
RE: How to make a race track ? - by 1micha.elok - 11-07-2024, 02:46 AM
RE: How to make a race track ? - by Marcus - 11-06-2024, 05:26 PM
RE: How to make a race track ? - by 1micha.elok - 11-07-2024, 10:47 AM
RE: How to make a race track ? - by kevin - 11-10-2024, 04:02 PM

Forum Jump:


Users browsing this thread: 3 Guest(s)