this is to study the terrain, and particularly the size parameters of the function :
CreateTerrain(#Terrain, Size, WorldSize, Scale, NbLayers, Filename$, Extension$)
my descriptions depends on practical results, so whether it is correct or wrong i describe the finding only. i have provided several almost the same code with a slight change so for your convenience and easier to test. i hope i haven't made big mistakes.
we will make a terrain with one tile only, the texture is Geebee2.bmp and the terrain definition is terrain.png
1- worldSize: if it is 100 then it is one Tile such as:
CreateTerrain(0, 129, 100, 40, 3, "TerrainHeight", "dat")
you will see one Bee only on the terrain
CreateTerrain(0, 129, 200, 40, 3, "TerrainHeight", "dat")
you will see 4 tiles
CreateTerrain(0, 129, 300, 40, 3, "TerrainHeight", "dat")
you will see 9 tiles
Edit: look correction below http://purebasic.fr/english/viewtopic.p ... 85#p454135
=================================
2- Size: the lowest value is 64+1, then 128+1, 512+1, ...etc
it defines the highest coordinates such as x=0 to 64, y=0 to 64
CreateTerrain(0, 129, 100, 40, 3, "TerrainHeight", "dat")
here the x=0 to 128, y=0 to 128
and this apply whatever the worldSize is, ie if the terrain made fron one tile or from 9 or more , the x,y are going from 0 to 128 in the whole terrain.
this is usefull when we want to do precise shapes on the terrain with SetTerrainTileHeightAtPoint(...)
======================================================
3- Scale: is the vertical amplification of mountains and valleys, set it to 1 and you will have a flat terrain. change it to more and you will get more real terrain (depends on terrain.png picture)
Code: Select all
Enumeration
#TEXTURE = 200
#MATERIAL
#ENTITY
#CAMERA
#PARTICLE
#ground
#remoteLand
#Catapult
#arm
#stone
#obstacle
EndEnumeration
#CameraSpeed = 1
Global power = 1
Define.f KeyX, KeyY, MouseX, MouseY
InitEngine3D()
Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures/" , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures/nvidia" , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts" , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/GUI" , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/desert.zip", #PB_3DArchive_Zip)
Parse3DScripts()
ExamineDesktops()
DesktopW = DesktopWidth(0)
DesktopH = DesktopHeight(0)
InitSprite()
InitKeyboard()
InitMouse()
OpenWindow(0, 0, 0, DesktopW, DesktopH, "use mouse and keys to move camera")
OpenWindowedScreen(WindowID(0), 0, 0, DesktopW, DesktopH, 0, 0, 0)
GetScriptMaterial(2, "Color/Red")
CreateMaterial(6, LoadTexture(2, "soil_wall.jpg"))
CreateMaterial(3, LoadTexture(3, "terrain_detail.jpg"))
CreateCamera(#Camera, 0, 0, 100, 100)
MoveCamera(#Camera,0,100,140, #PB_Absolute)
CameraLookAt(#Camera,0,0,0)
CreateLight(0, $FFFFFF, 1500, 800, 500)
AmbientColor(RGB(0,200,0))
SkyBox("desert07.jpg")
CreateCube(1, 1)
SetupTerrains(LightID(0), 3000, #PB_Terrain_NormalMapping)
; initialize terrain
;Result = CreateTerrain(#Terrain, Size, WorldSize, Scale, NbLayers, Filename$, Extension$)
CreateTerrain(0, 129, 100, 40, 3, "TerrainHeight", "dat")
; set all texture will be use when terrrain will be constructed
AddTerrainTexture(0, 0, 100, "Geebee2.bmp", "dirt_grayrocky_normalheight.jpg")
AddTerrainTexture(0, 1, 30, "grass_green-01_diffusespecular.jpg", "grass_green-01_normalheight.jpg")
AddTerrainTexture(0, 2, 200, "growth_weirdfungus-03_diffusespecular.jpg", "growth_weirdfungus-03_normalheight.jpg")
;- define terrains
DefineTerrainTile(0, 0, 0, "terrain.png", 0, 0)
BuildTerrain(0)
CreateTerrainBody(0, 0.5, 1)
UpdateTerrain(0)
; SkyBox
SkyBox("desert07.jpg")
EnableWorldPhysics(1)
EnableWorldCollisions(1)
Result = TerrainTileSize(0, 0, 0)
;SetTerrainTileHeightAtPoint(#Terrain, TileX, TileY, x, y, Height)
;SetTerrainTileHeightAtPoint(0, 0, 0, 4, 4, 50)
SetTerrainTileHeightAtPoint(0, 0, 0, 64, 64, 100)
CreateTerrainBody(0, 0.5, 1) ; new
UpdateTerrain(0)
CreateSphere(2,1.5)
CreateEntity(2,MeshID(2), #PB_Material_None ,-10,70,10)
CreateEntityBody(2, #PB_Entity_SphereBody,1, 0.5, 1)
EnableWorldPhysics(1)
EnableWorldCollisions(1)
;Debug Result
Repeat
Repeat
event = WindowEvent()
Until event = 0
If ExamineMouse()
MouseX = -MouseDeltaX()/20
MouseY = -MouseDeltaY()/20
EndIf
If ExamineKeyboard()
If KeyboardPushed(#PB_Key_Left)
KeyX = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Right)
KeyX = #CameraSpeed
Else
KeyX = 0
EndIf
If KeyboardPushed(#PB_Key_Up)
KeyY = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Down)
KeyY = #CameraSpeed
Else
KeyY = 0
EndIf
EndIf
RotateCamera(#Camera, MouseY, MouseX, 0, #PB_Relative)
MoveCamera(#Camera, KeyX, 0, KeyY)
RenderWorld()
FlipBuffers()
Until KeyboardReleased(#PB_Key_Escape)
=====================================================
now to position an entity such as a sphere, its coordinates depends on the WorldSize, but here the (0,0) position is in the center of the terrain.
example:
CreateTerrain(0, 129, 200, 40, 3, "TerrainHeight", "dat")
CreateSphere(2,1.5)
CreateEntity(2,MeshID(2), #PB_Material_None ,-100,70,100)
will position the sphere at altitude 70, on the (-100,100) ie at (0,0) with regarding to the
x=0,y=0 in line
SetTerrainTileHeightAtPoint(0, 0, 0, x, y, 50)
to let you visualize this easily look this example and watch the sphere, and the spike on the terrain made with SetTerrainTileHeightAtPoint(0, 0, 0, 0, 0, 50)
it seems TileX, TileY should always be 0
Code: Select all
Enumeration
#TEXTURE = 200
#MATERIAL
#ENTITY
#CAMERA
#PARTICLE
#ground
#remoteLand
#Catapult
#arm
#stone
#obstacle
EndEnumeration
#CameraSpeed = 5
Global power = 1
Define.f KeyX, KeyY, MouseX, MouseY
InitEngine3D()
Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures/" , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures/nvidia" , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts" , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/GUI" , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/desert.zip", #PB_3DArchive_Zip)
Parse3DScripts()
ExamineDesktops()
DesktopW = DesktopWidth(0)
DesktopH = DesktopHeight(0)
InitSprite()
InitKeyboard()
InitMouse()
OpenWindow(0, 0, 0, DesktopW, DesktopH, "use mouse and keys to move camera")
OpenWindowedScreen(WindowID(0), 0, 0, DesktopW, DesktopH, 0, 0, 0)
GetScriptMaterial(2, "Color/Red")
CreateMaterial(6, LoadTexture(2, "soil_wall.jpg"))
CreateMaterial(3, LoadTexture(3, "terrain_detail.jpg"))
CreateCamera(#Camera, 0, 0, 100, 100)
MoveCamera(#Camera,0,100,300, #PB_Absolute)
CameraLookAt(#Camera,0,0,0)
CreateLight(0, $FFFFFF, 1500, 800, 500)
AmbientColor(RGB(0,200,0))
SkyBox("desert07.jpg")
CreateCube(1, 1)
SetupTerrains(LightID(0), 3000, #PB_Terrain_NormalMapping)
; initialize terrain
;Result = CreateTerrain(#Terrain, Size, WorldSize, Scale, NbLayers, Filename$, Extension$)
CreateTerrain(0, 129, 200, 40, 3, "TerrainHeight", "dat")
; set all texture will be use when terrrain will be constructed
AddTerrainTexture(0, 0, 100, "Geebee2.bmp", "dirt_grayrocky_normalheight.jpg")
AddTerrainTexture(0, 1, 30, "grass_green-01_diffusespecular.jpg", "grass_green-01_normalheight.jpg")
AddTerrainTexture(0, 2, 200, "growth_weirdfungus-03_diffusespecular.jpg", "growth_weirdfungus-03_normalheight.jpg")
;- define terrains
DefineTerrainTile(0, 0, 0, "terrain.png", 0, 0)
BuildTerrain(0)
CreateTerrainBody(0, 0.5, 1) ; new
UpdateTerrain(0)
; SkyBox
SkyBox("desert07.jpg")
EnableWorldPhysics(1)
EnableWorldCollisions(1)
Result = TerrainTileSize(0, 0, 0)
;SetTerrainTileHeightAtPoint(#Terrain, TileX, TileY, x, y, Height)
SetTerrainTileHeightAtPoint(0, 0, 0, 0, 0, 50)
SetTerrainTileHeightAtPoint(0, 0, 0, 64, 64, 100)
CreateTerrainBody(0, 0.5, 1) ; new
UpdateTerrain(0)
CreateSphere(2,2.5)
;CreateEntity(2,MeshID(2), #PB_Material_None ,-10,70,10)
;CreateEntity(2,MeshID(2), #PB_Material_None ,-50,70,50)
CreateEntity(2,MeshID(2), #PB_Material_None ,20,70,0)
;CreateEntity(2,MeshID(2), #PB_Material_None ,0,70,0)
CreateEntityBody(2, #PB_Entity_SphereBody,1, 0.5, 1)
EnableWorldPhysics(1)
EnableWorldCollisions(1)
;Debug Result
Repeat
Repeat
event = WindowEvent()
Until event = 0
If ExamineMouse()
MouseX = -MouseDeltaX()/20
MouseY = -MouseDeltaY()/20
EndIf
If ExamineKeyboard()
If KeyboardPushed(#PB_Key_Left)
KeyX = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Right)
KeyX = #CameraSpeed
Else
KeyX = 0
EndIf
If KeyboardPushed(#PB_Key_Up)
KeyY = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Down)
KeyY = #CameraSpeed
Else
KeyY = 0
EndIf
EndIf
RotateCamera(#Camera, MouseY, MouseX, 0, #PB_Relative)
MoveCamera(#Camera, KeyX, 0, KeyY)
RenderWorld()
FlipBuffers()
Until KeyboardReleased(#PB_Key_Escape)
now how to make holes :
look DrawHole(xcenter, ycenter, HoleRadius) such as DrawHole(64, 64, 10)
we choose only the points wich are inside certain radius according to a formula
radiusSquared = Pow(x-xcenter,2) + Pow(y-ycenter,2)
Code: Select all
Declare DrawHole(xcenter, ycenter, HoleRadius)
Enumeration
#TEXTURE = 200
#MATERIAL
#ENTITY
#CAMERA
#PARTICLE
#ground
#remoteLand
#Catapult
#arm
#stone
#obstacle
EndEnumeration
#CameraSpeed = 4
Global power = 1
Define.f KeyX, KeyY, MouseX, MouseY
InitEngine3D()
Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures/" , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures/nvidia" , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts" , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/GUI" , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/desert.zip", #PB_3DArchive_Zip)
Parse3DScripts()
ExamineDesktops()
DesktopW = DesktopWidth(0)
DesktopH = DesktopHeight(0)
InitSprite()
InitKeyboard()
InitMouse()
OpenWindow(0, 0, 0, DesktopW, DesktopH, "use mouse and keys to move camera")
OpenWindowedScreen(WindowID(0), 0, 0, DesktopW, DesktopH, 0, 0, 0)
GetScriptMaterial(2, "Color/Red")
CreateMaterial(6, LoadTexture(2, "soil_wall.jpg"))
CreateMaterial(3, LoadTexture(3, "terrain_detail.jpg"))
CreateCamera(#Camera, 0, 0, 100, 100)
MoveCamera(#Camera,0,100,300, #PB_Absolute)
CameraLookAt(#Camera,0,0,0)
CreateLight(0, $FFFFFF, 1500, 800, 500)
AmbientColor(RGB(0,200,0))
SkyBox("desert07.jpg")
CreateCube(1, 1)
SetupTerrains(LightID(0), 3000, #PB_Terrain_NormalMapping)
; initialize terrain
;Result = CreateTerrain(#Terrain, Size, WorldSize, Scale, NbLayers, Filename$, Extension$)
CreateTerrain(0, 129, 200, 1, 3, "TerrainHeight", "dat")
; set all texture will be use when terrrain will be constructed
AddTerrainTexture(0, 0, 100, "Geebee2.bmp", "dirt_grayrocky_normalheight.jpg")
AddTerrainTexture(0, 1, 30, "grass_green-01_diffusespecular.jpg", "grass_green-01_normalheight.jpg")
AddTerrainTexture(0, 2, 200, "growth_weirdfungus-03_diffusespecular.jpg", "growth_weirdfungus-03_normalheight.jpg")
;- define terrains
DefineTerrainTile(0, 0, 0, "terrain.png", 0, 0)
BuildTerrain(0)
CreateTerrainBody(0, 0.5, 1) ; new
UpdateTerrain(0)
; SkyBox
SkyBox("desert07.jpg")
EnableWorldPhysics(1)
EnableWorldCollisions(1)
Result = TerrainTileSize(0, 0, 0)
;SetTerrainTileHeightAtPoint(#Terrain, TileX, TileY, x, y, Height)
SetTerrainTileHeightAtPoint(0, 0, 0, 0, 0, 50)
;SetTerrainTileHeightAtPoint(0, 0, 0, 64, 64, 100)
CreateTerrainBody(0, 0.5, 1) ; new
UpdateTerrain(0)
CreateSphere(2,3)
;CreateEntity(2,MeshID(2), #PB_Material_None ,-10,70,10)
;CreateEntity(2,MeshID(2), #PB_Material_None ,-50,70,50)
CreateEntity(2,MeshID(2), #PB_Material_None ,0,100,0)
;CreateEntity(2,MeshID(2), #PB_Material_None ,0,70,0)
CreateEntityBody(2, #PB_Entity_SphereBody,1, 0.5, 1)
EnableWorldPhysics(1)
EnableWorldCollisions(1)
DrawHole(64, 64, 10)
;Debug Result
Repeat
Repeat
event = WindowEvent()
Until event = 0
If ExamineMouse()
MouseX = -MouseDeltaX()/20
MouseY = -MouseDeltaY()/20
EndIf
If ExamineKeyboard()
If KeyboardPushed(#PB_Key_Left)
KeyX = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Right)
KeyX = #CameraSpeed
Else
KeyX = 0
EndIf
If KeyboardPushed(#PB_Key_Up)
KeyY = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Down)
KeyY = #CameraSpeed
Else
KeyY = 0
EndIf
EndIf
RotateCamera(#Camera, MouseY, MouseX, 0, #PB_Relative)
MoveCamera(#Camera, KeyX, 0, KeyY)
RenderWorld()
FlipBuffers()
Until KeyboardReleased(#PB_Key_Escape)
Procedure DrawHole(xcenter, ycenter, HoleRadius)
xcenter = xcenter: ycenter = ycenter
HoleRadius = HoleRadius
For y = ycenter-HoleRadius To ycenter+HoleRadius
For x = xcenter-HoleRadius To xcenter+HoleRadius
radiusSquared = Pow(x-xcenter,2) + Pow(y-ycenter,2)
If radiusSquared <= HoleRadius * HoleRadius
SetTerrainTileHeightAtPoint(0, 0, 0, x, y, -50)
CreateTerrainBody(0, 0.5, 1)
UpdateTerrain(0)
EndIf
Next
Next
EndProcedure

=============================================================
in a very big terrain it will be speedier to deform parts of the terrain because we need less points to deform a certain circle size, since the size (ie x,y of the whole terrain will not change)
Code: Select all
Declare DrawHole(xcenter, ycenter, HoleRadius)
Enumeration
#TEXTURE = 200
#MATERIAL
#ENTITY
#CAMERA
#PARTICLE
#ground
#remoteLand
#Catapult
#arm
#stone
#obstacle
EndEnumeration
#CameraSpeed = 16
Global power = 1
Define.f KeyX, KeyY, MouseX, MouseY
InitEngine3D()
Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures/" , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures/nvidia" , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts" , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/GUI" , #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/desert.zip", #PB_3DArchive_Zip)
Parse3DScripts()
ExamineDesktops()
DesktopW = DesktopWidth(0)
DesktopH = DesktopHeight(0)
InitSprite()
InitKeyboard()
InitMouse()
OpenWindow(0, 0, 0, DesktopW, DesktopH, "use mouse and keys to move camera")
OpenWindowedScreen(WindowID(0), 0, 0, DesktopW, DesktopH, 0, 0, 0)
GetScriptMaterial(2, "Color/Red")
CreateMaterial(6, LoadTexture(2, "soil_wall.jpg"))
CreateMaterial(3, LoadTexture(3, "terrain_detail.jpg"))
CreateCamera(#Camera, 0, 0, 100, 100)
MoveCamera(#Camera,0,3000,2000, #PB_Absolute)
CameraLookAt(#Camera,0,0,0)
CreateLight(0, $FFFFFF, 100, 1000, 1000)
AmbientColor(RGB(0,200,0))
SkyBox("desert07.jpg")
CreateCube(1, 1)
SetupTerrains(LightID(0), 3000, #PB_Terrain_NormalMapping)
; initialize terrain
;Result = CreateTerrain(#Terrain, Size, WorldSize, Scale, NbLayers, Filename$, Extension$)
;CreateTerrain(0, 129, 200, 1, 3, "TerrainHeight", "dat")
CreateTerrain(0, 129, 12000, 600, 3, "TerrainHeight", "dat")
; set all texture will be use when terrrain will be constructed
AddTerrainTexture(0, 0, 100, "Geebee2.bmp", "dirt_grayrocky_normalheight.jpg")
AddTerrainTexture(0, 1, 30, "grass_green-01_diffusespecular.jpg", "grass_green-01_normalheight.jpg")
AddTerrainTexture(0, 2, 200, "growth_weirdfungus-03_diffusespecular.jpg", "growth_weirdfungus-03_normalheight.jpg")
;- define terrains
DefineTerrainTile(0, 0, 0, "terrain.png", 0, 0)
BuildTerrain(0)
CreateTerrainBody(0, 0.5, 1) ; new
UpdateTerrain(0)
; SkyBox
SkyBox("desert07.jpg")
EnableWorldPhysics(1)
EnableWorldCollisions(1)
Result = TerrainTileSize(0, 0, 0)
;SetTerrainTileHeightAtPoint(#Terrain, TileX, TileY, x, y, Height)
SetTerrainTileHeightAtPoint(0, 0, 0, 0, 0, 50)
;SetTerrainTileHeightAtPoint(0, 0, 0, 64, 64, 100)
CreateTerrainBody(0, 0.5, 1) ; new
UpdateTerrain(0)
CreateSphere(2,1.5)
;CreateEntity(2,MeshID(2), #PB_Material_None ,-10,70,10)
;CreateEntity(2,MeshID(2), #PB_Material_None ,-50,70,50)
CreateEntity(2,MeshID(2), #PB_Material_None ,-100,70,100)
;CreateEntity(2,MeshID(2), #PB_Material_None ,0,70,0)
CreateEntityBody(2, #PB_Entity_SphereBody,1, 0.5, 1)
EnableWorldPhysics(1)
EnableWorldCollisions(1)
DrawHole(64, 64, 10)
;Debug Result
Repeat
Repeat
event = WindowEvent()
Until event = 0
If ExamineMouse()
MouseX = -MouseDeltaX()/20
MouseY = -MouseDeltaY()/20
EndIf
If ExamineKeyboard()
If KeyboardPushed(#PB_Key_Left)
KeyX = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Right)
KeyX = #CameraSpeed
Else
KeyX = 0
EndIf
If KeyboardPushed(#PB_Key_Up)
KeyY = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Down)
KeyY = #CameraSpeed
Else
KeyY = 0
EndIf
EndIf
RotateCamera(#Camera, MouseY, MouseX, 0, #PB_Relative)
MoveCamera(#Camera, KeyX, 0, KeyY)
RenderWorld()
FlipBuffers()
Until KeyboardReleased(#PB_Key_Escape)
Procedure DrawHole(xcenter, ycenter, HoleRadius)
xcenter = xcenter: ycenter = ycenter
HoleRadius = HoleRadius
For y = ycenter-HoleRadius To ycenter+HoleRadius
For x = xcenter-HoleRadius To xcenter+HoleRadius
radiusSquared = Pow(x-xcenter,2) + Pow(y-ycenter,2)
If radiusSquared <= HoleRadius * HoleRadius
SetTerrainTileHeightAtPoint(0, 0, 0, x, y, -50)
CreateTerrainBody(0, 0.5, 1)
UpdateTerrain(0)
EndIf
Next
Next
EndProcedure