Post by 8XenoTrogdor8 on Dec 10, 2004 1:00:48 GMT -5
Heres a demo on how to load 3D envoirnments, kinda like the walkdemo, but not quite. This code has a collision detection system in it as well, so the camera doesn't fly through walls ;D. Very handy to have.
The controls are the standard W, A, S and D keys plus the mouse to look up/down and left/right. Yes, this is the next version!
I had to paste the code in two halfs since it exceeded 10, 000 characters. Darn... Anyway, here's the first half-
The program will require a txt file called TestMap.Txt to load the tilemaps from. it should look something like this-
I know tilemaps can be frustrating to make, so I created a program to make tilemaps in! currently, its set to only make tilemaps for this demo, but it can be rebuilt to make tilemaps for anything. ;D It's probably like the 9th post under this topic or something. BTW, be careful not to make the camera start inside a wall or something.
Hopefully peeps can learn one form of collision detection from this (albiet a very primitive form! There are many limitations.). Thanks also to Madmockhaven for the basic form of loading the enviornment.
As always, questions/comments would be appreciated!
Happy collisions! Later all y'all!
The controls are the standard W, A, S and D keys plus the mouse to look up/down and left/right. Yes, this is the next version!
I had to paste the code in two halfs since it exceeded 10, 000 characters. Darn... Anyway, here's the first half-
'From the Labs of XenoTrogdor-
'The Easy 3D Demo!
'V 1.5
Struc SCam
Dim X#
Dim Y#
Dim Z#
Dim TestX#
Dim TestZ#
Dim Angle
Dim AngleX
Dim posX
Dim PosY
EndStruc
Dim SCam Cam
Cam.X# = 20 ' Camera starting position. 2 over, 4 down on the tilemap.
Cam.Z# = 40
Dim Opp, Fogmode, Strafe, Speed#, I, Tex(8), FogColor# (3)
Speed# = .5
fogColor# = vec4(0.0, 0.0, 0.0, 0.0)
Tex(0) = LoadTexture ("textures\00008.jpg") ' Green thingy.
Tex(1) = LoadTexture ("textures\00001.jpg") ' Rocks.
Tex(2) = LoadTexture ("textures\00004.jpg") ' Marble.
Tex(3) = LoadTexture ("textures\00005.jpg") ' Moss?
Tex(4) = LoadTexture ("textures\00009.jpg") ' Cracked tiles.
Tex(5) = LoadTexture ("textures\001.jpg") ' Water.
Tex(6) = LoadTexture ("textures\floor01.jpg") ' Tiles.
Tex(7) = LoadTexture ("textures\pavement.jpg")' What could it be?
Tex(8) = LoadTexture ("textures\wall01.jpg") ' Boards.
' Much thanks to MadMockhaven for the file reading and fog code!
dim MapsX, MapsY, File
' Open file to read tilemap from.
File = OpenFileRead ("files\testmap.txt")
' Read the first two lines in the file (map X size and map Y size)
MapsX = Val (ReadLine (File))
MapsY = Val (ReadLine (File))
' Allocate a variable to store the tilemap in.
dim Map (MapsX)(MapsY), Y, X
' Read the tilemap from the text into the variable.
for Y = 0 to MapsY
for X = 0 to MapsX
Map (X)(Y) = Val (ReadText (File, True))
next
next
CloseFile (File)
' Create a fog.
GlEnable (GL_TEXTURE_2D)
GlFogI (GL_FOG_MODE, GL_LINEAR)
GlFogF (GL_FOG_START, -4)
GlFogF (GL_FOG_END, 60)
GlEnable (GL_FOG)
While True
GlClear (GL_DEPTH_BUFFER_BIT or GL_COLOR_BUFFER_BIT)
GlLoadIdentity ()
' Turn.
GlRotatef (-Cam.Angle, 0, 1, 0)
' Look up and down. Multiplying by the sine and cosine determines how
' much the camera will rotate on the X and Z axis.
GlRotatef (Cam.AngleX * Sind (Cam.Angle), 0, 0, -1)
GlRotatef (Cam.AngleX * Cosd (Cam.Angle), 1, 0, 0)
' Move the camera on X, Y and Z axis. So far, the Y axis (up and down)
' is always 0.
GlTranslatef (-Cam.X#, -Cam.Y#, -Cam.Z#)
for y = 0 to MapsY
for x = 0 to MapsX
GlPushMatrix ()
GlTranslatef (10*X, 0, 10*Y)
' Draw a ceiling and a floor, but no walls.
if Map (X)(Y)= -2 then
glBindTexture (GL_TEXTURE_2D, tex (6))
GoSub DrawFloor
GlBindTexture (GL_TEXTURE_2D, Tex (2))
GoSub DrawCeiling
endif
' Draw another ceiling and floor, but no walls.
if Map (X)(Y)= -1 then
GlBindTexture (GL_TEXTURE_2D, Tex (5))
GoSub DrawFloor
GlBindTexture (GL_TEXTURE_2D, Tex (2))
GoSub DrawCeiling
endif
for I = 0 to 8
' Draw another cube with different texture.
if Map (X)(Y) = I then
GlBindTexture (GL_TEXTURE_2D, Tex(I))
gosub DrawCube
endif
next
GlPopMatrix ()
next
next
while SyncTimer (10)
if ScanKeyDown (VK_SHift) then
speed# = 1.5
else
speed# = 0.5
endif
' This is a mouselook type thing.
Cam.Angle = Cam.Angle - Mouse_XD () * 50 ' For the angle.
Cam.AngleX = Cam.AngleX + Mouse_YD () * 50 ' For tipping up and down.
' Limit the camera tip to 80 degrees iether direction.
if Cam.AngleX > 80 then Cam.AngleX = 80 endif
if Cam.AngleX < -80 then Cam.AngleX = -80 endif
' Limit the camera angle to [0, 359).
Cam.Angle = Cam.Angle % 360
if KeyDown ("W") then
Strafe = 0 ' Strafe and Opp are variables used to calculate
Opp = 1 ' the reverse of the last camera movement.
Cam.X# = Cam.X# - sind (Cam.Angle) * speed#
Cam.Z# = Cam.Z# - cosd (Cam.Angle) * speed#
endif
if KeyDown ("S") then
Strafe = 0
Opp = -1
Cam.X# = Cam.X# + sind (Cam.Angle) * speed#
Cam.Z# = Cam.Z# + cosd (Cam.Angle) * speed#
endif : gosub CollisionTest
if KeyDown ("A") then
Strafe = 90
Opp = 1
Cam.X# = Cam.X# + sind (Cam.Angle - 90) * Speed#
Cam.Z# = Cam.Z# + cosd (Cam.Angle - 90) * Speed#
endif
if KeyDown ("D") then
Strafe = -90
Opp = 1
Cam.X# = Cam.X# + sind (Cam.Angle + 90) * Speed#
Cam.Z# = Cam.Z# + cosd (Cam.Angle + 90) * Speed#
endif : gosub CollisionTest
wend
SwapBuffers ()
wend
The program will require a txt file called TestMap.Txt to load the tilemaps from. it should look something like this-
5
5
4 4 4 4 4 4
4 -2 -2 -2 -2 4
4 -2 -2 -2 -2 4
4 -1 -1 -1 -1 4
4 8 -1 -1 -1 4
4 4 4 4 4 4
I know tilemaps can be frustrating to make, so I created a program to make tilemaps in! currently, its set to only make tilemaps for this demo, but it can be rebuilt to make tilemaps for anything. ;D It's probably like the 9th post under this topic or something. BTW, be careful not to make the camera start inside a wall or something.
Hopefully peeps can learn one form of collision detection from this (albiet a very primitive form! There are many limitations.). Thanks also to Madmockhaven for the basic form of loading the enviornment.
As always, questions/comments would be appreciated!
Happy collisions! Later all y'all!