Hi all. Ok, what did I do wrong here? My player keeps falling through the landscape. I've set everything up correctly, at least I think I did, prove me wrong please.
Also, for some reason, I can't jump with my jump code.
Here's the code:
//CONTROLS
//ARROWKEYS AND MOUSE - MOVE
// F1 - WIREFRAME ON. F2 - WIREFRAME OFF
//Setup
set window title "3D Object Loader"
sync on : sync rate 60
center text screen width()/2,screen height()/2,"Loading..." : sync : cls
//Load media
global g_HeightmapImgID : global g_TextureImgID : global g_DetailmapImgID
g_HeightmapImgID=1 : g_TextureImgID=2 : g_DetailmapImgID=3
load image "..\Media\Snowy Mountains\Snowy Mountain 1_Height_Map.bmp",g_HeightmapImgID, 1
load image "..\Media\Snowy Mountains\Snowy Mountain 1_Texture_Map.jpg",g_TextureImgID, 1
load image "..\Media\detail.tga",g_DetailmapImgID
//Create terrain
//Make the terrain
global g_TerrainID
g_TerrainID=BT MakeTerrain()
//Set images
BT SetTerrainHeightmap g_TerrainID,g_HeightmapImgID
BT SetTerrainTexture g_TerrainID,g_TextureImgID
BT SetTerrainDetail g_TerrainID,g_DetailmapImgID
//Set some other values
BT SetTerrainScale g_TerrainID,40.0
BT SetTerrainYScale g_TerrainID,40.0
BT SetTerrainSplit g_TerrainID,16
BT SetTerrainDetailTile g_TerrainID,24.0
//LOD
BT SetTerrainLOD g_TerrainID,3 //3 LOD levels
BT SetTerrainLODDistance g_TerrainID,1,100.0 //LOD Distances start at one and go up to 1 less the LOD level count
BT SetTerrainLODDistance g_TerrainID,2,200.0
//Smoothing
BT SetTerrainSmoothing g_TerrainID,1
//Quad rotation
BT SetTerrainQuadRotation g_TerrainID,1
//Build
global g_TerrainObjectID
g_TerrainObjectID=1
//This command processes the heightmap and creates all the internal structures for the terrain
BT BuildTerrain g_TerrainID,g_TerrainObjectID,1
SC_SetupObject g_TerrainID,2,0
SC_UpdateObject g_TerrainID
//Setup camera
set camera range 10,100000
hide mouse
//Enable auto render
BT EnableAutoRender 1
//Player Stuff
global player = 10000
make object cube player, 10
velocity#=0.04
speed#=0.5
jumppower#=1.3
SC_SetupObject player,2,0
SC_UpdateObject player
`====================================================
global gravity#
gravity# = 15
facing# = 0.0
camrotate# = 0.0
//Main loop
StartTime=timer()
do
//Camera movement
`Elapsedtime#=(timer()-StartTime)/3000.0+Elapsedtime#*0.7
`StartTime=timer()
`cx#=camera angle x()+mousemovey()/4.0
`if cx#>90.0 then cx#=90.0
`if cx#<-90.0 then cx#=-90.0
`rotate camera cx#,camera angle y()+mousemovex()/4.0,0.0
remstart
if spacekey()=1
if upkey() or mouseclick()=1 then move camera 1000.0*Elapsedtime#
if downkey() or mouseclick()=2 then move camera -1000.0*Elapsedtime#
endif
remend
oldx# = x#
oldy# = y#
oldz# = z#
move object player, (upkey()-downkey())*5
facing# = facing# + 1.5 * (rightkey()-leftkey())
yrotate object player, facing#
if downkey()
set camera to follow object position x(player), object position y(player), object position z(player), facing#+180, 75, 50, 60, 1
else
set camera to follow object position x(player), object position y(player), object position z(player), facing#, 75, 50, 60, 1
endif
if spacekey() = 0 then released = 1
if spacekey() = 1 and released = 1 and grav#=0.0
grav#=jumppower#
endif
if spacekey()=1 and released = 1 and grav# <> 0.0
released = 0
endif
remstart
if spacekey() = 0 then powerupreleased = 1
if spacekey() = 1 and powerupreleased = 1 and powerup = 1
powerup=60
grav#=jumppower#*3
endif
if spacekey()=1 and powerupreleased = 1 and powerup = 0
powerupreleased = 0
endif
remend
rem apply gravity
dec grav#,velocity#
inc y#,grav#
rem check for collision with level
rem here I will cast a sphere the size of the player, and check if this sphere is colliding with anything on the level
rem Note that storing the old positions of the player stop the player from slipping through the walls when travelling
rem at high speeds. It will of course work with out them : SC_SphereCast(2,x#,y#,0,x#,y#,0,3.5,0)
rem the radius of the sphere is 7/2=3.5, and there are no objects to exclude.
if SC_SphereSlide(player,oldx#,oldy#,oldz#,x#,y#,z#,object size(player, 1)/2,0)
rem reposition the player so it doesn't fall through any walls
rem Here I am getting the sliding collision points. These are the points where the player would end up if it
rem slid along the face of the collision.
x#=SC_GetCollisionSlideX()
y#=SC_GetCollisionSlideY()
z#=SC_GetCollisionSlideZ()
rem cast a ray to the ground to check if the player is touching the ground. If so, reset gravity
if SC_RayCast(player,x#,y#,z#,x#,y#-4,z#,0)
grav#=0.0
endif
rem cast a ray to the roof to check if the player is touching the roof. If so, reset gravity
if SC_RayCast(player,x#,y#,z#,x#,y#+4,z#,0)
grav#=0-velocity#
endif
endif
gheight#=BT GetGroundHeight(g_TerrainID,object position x(player),object position z(player))
`if camera position y()-40<gheight# then position camera camera position x(),gheight#+40,camera position z()
if object position y(player)<gheight# then position object player, object position x(player), gheight#+40, object position z(player)
rem update player positions
position object player,x#+object size x(1, 1)/2000,y#+10000,z#+object size z(1, 1)/2000
//Wireframe
if keystate(59) then set object wireframe g_TerrainObjectID,1
if keystate(60) then set object wireframe g_TerrainObjectID,0
//FPS
text 10,10,str$(screen fps())
center text screen width()/2, screen height()/2, "Current X:"+str$(object position x(player))
center text screen width()/2, screen height()/2+20, "Current Y:"+str$(object position y(player))
center text screen width()/2, screen height()/2+40, "Current Z:"+str$(object position z(player))
//Position mouse
position mouse screen width()/2,screen height()/2
//Update screen
sync
loop
Thank You all!
CHECK OUT MY WEBSITE AT http://imageposeidon.com/ !