Thanks puzzler ive spent ages going through threads in the past
to find code that's buried deep in some other code in one of the pages
Object Mesh Building
Changed to the following this way it don't ever error and a few other things like crater creation etc
// Project: heights
// Created: 2019-01-01
// show all errors
SetErrorMode(2)
#constant screenwidth=1024
#constant screenheight=768
#constant fullscreen=0
#constant screenrate=0
// set window properties
SetWindowTitle( "heights" )
SetWindowSize( screenwidth, screenheight, fullscreen )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( screenwidth, screenheight ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( screenrate, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
Create3DPhysicsWorld()
#constant sizex=21 // change this to extend the map
#constant sizez=21 // change this to exeend the map
global angx#, angy#,startx#,starty#
startx#=screenwidth/2
starty#=screenheight/2
size=5
type _map
id
x#
height#
z#
colorred
colorgreen
colorblue
texture
endtype
global map as _map[sizex,sizez]
global vertexnumber
//textimages as integer[10]
//for loops=0 to 10
// textimages[loops]= createtexture(64,64,MakeColor(0,255,0),255)
//next
textimages as integer[10]
textimages[0]= createtexture(64,64,MakeColor(0,255,0),255)
textimages[1]= createtexture(64,64,MakeColor(255,255,255),255)
land = CreateObjectBox(.01,.01,.01)
colorgreen=150
for x=1 to sizex
for z=1 to sizez
map[x,z].id = CreateObjectBox(9,9,9)
map[x,z].x# = x * GetObjectSizeMaxX(map[x,z].id)*2 // Remove the * 2 to join the cubes together
map[x,z].height#=random(0,0)
map[x,z].z# = z * GetObjectSizeMaxZ(map[x,z].id)*2 // Remove the * 2 to join the cubes together
//map[x,z].colorgreen = random(150,200)
//SetObjectImage(map[x,z].id,textimages[0],0)
if colorgreen=150
colorgreen=200
else
colorgreen=150
endif
map[x,z].colorgreen =colorGreen
SetObjectImage(map[x,z].id,textimages[0],0)
Create3DPhysicsStaticBody(map[x,z].id)
SetObjectColor(map[x,z].id,0,map[x,z].colorgreen,0,255)
SetObjectPosition(map[x,z].id,map[x,z].x#,0,map[x,z].z#)
FixObjectToObject(map[x,z].id,land)
next
next
RotateObjectLocalY(land,-30)
prepareheights()
camerax#=20
cameray#=30
cameraz#=0
// frameImg=LoadImage("\media\frame2.png")
//FrameSpr=createSprite(frameImg)
collisioner=CreateObjectBox(10,10,10)
do
// SetSpriteDepth(frameSpr,100) //set the frame to behind all tiles to allow clicking to work
unit_x#=Get3DVectorXFromScreen(getpointerx(),getpointery())
unit_y#=Get3DVectorYFromScreen(getpointerx(),getpointery())
unit_z#=Get3DVectorZFromScreen(getpointerx(),getpointery())
// calculate the start of the ray cast, which is the unit vector + the camera position
start_x# = unit_x# + camerax#
start_y# = unit_y# + cameray#
start_z# = unit_z# - cameraz#
// calculate the end of the vector, which is the unit vector multiplied by the length of the ray cast and then add the camera position to it
end_x# = 800*unit_x# + camerax#
end_y# = 800*unit_y# + cameray#
end_z# = 800*unit_z# - cameraz#
// determine which object has been hit
object_hit = ObjectRayCast(0,start_x#,start_y#,start_z#,end_x#,end_y#,end_z#)
// if an object has been hit then turn it red
if object_hit <> 0
x=getobjectx(object_hit)/9
z=getobjectz(object_hit)/9
else
endif
if GetRawKeyState(13)
RotateObjectLocalY(land,.05)
endif
if GetRawMouseLeftState()
flag=1
if x<2 or x>sizex-1 then flag=0
if z<2 or z>sizez-1 then flag=0
if flag=1
inc map[x,z].height#,.01
SetObjectPosition(map[x,z].id,map[x,z].x#,map[x,z].height#,map[x,z].z#)
changeverts(x,z,.01)
Delete3DPhysicsBody(map[x,z].id)
Create3DPhysicsStaticBody(map[x,z].id)
endif
endif
if GetRawMouseRightState()
flag=1
if x<2 or x>sizex-1 then flag=0
if z<2 or z>sizez-1 then flag=0
if flag=1 and map[x,z].height#>0
dec map[x,z].height#,.01
SetObjectPosition(map[x,z].id,map[x,z].x#,map[x,z].height#,map[x,z].z#)
changeverts(x,z,-.01)
Delete3DPhysicsBody(map[x,z].id)
Create3DPhysicsStaticBody(map[x,z].id)
endif
endif
if GetRawMouseMiddlePressed()
flag=1
if x<2 or x>sizex-1 then flag=0
if z<2 or z>sizez-1 then flag=0
if flag=1
//if map[x,z].height#>0
dec map[x,z].height#,10
SetObjectPosition(map[x,z].id,map[x,z].x#,map[x,z].height#,map[x,z].z#)
changeverts(x,z,-10.01)
Delete3DPhysicsBody(map[x,z].id)
Create3DPhysicsStaticBody(map[x,z].id)
//endif
//SetObjectColor(map[x,z].id,GetObjectColorRed(map[x,z].id)-50,GetObjectColorGreen(map[x,z].id)-50,GetObjectColorBlue(map[x,z].id)-50,255)
//SetObjectColor(map[x+1,z+1].id,GetObjectColorRed(map[x+1,z+1].id)-50,GetObjectColorGreen(map[x+1,z+1].id)-50,GetObjectColorBlue(map[x+1,z+1].id)-50,255)
//SetObjectColor(map[x-1,z+1].id,GetObjectColorRed(map[x-1,z+1].id)-50,GetObjectColorGreen(map[x-1,z+1].id)-50,GetObjectColorBlue(map[x-1,z+1].id)-50,255)
//SetObjectColor(map[x-1,z-1].id,GetObjectColorRed(map[x-1,z-1].id)-50,GetObjectColorGreen(map[x-1,z-1].id)-50,GetObjectColorBlue(map[x-1,z-1].id)-50,255)
//SetObjectColor(map[x+1,z].id,GetObjectColorRed(map[x+1,z].id)-50,GetObjectColorGreen(map[x+1,z].id)-50,GetObjectColorBlue(map[x+1,z].id)-50,255)
//SetObjectColor(map[x-1,z].id,GetObjectColorRed(map[x-1,z].id)-50,GetObjectColorGreen(map[x-1,z].id)-50,GetObjectColorBlue(map[x-1,z].id)-50,255)
//SetObjectColor(map[x,z+1].id,GetObjectColorRed(map[x,z+1].id)-50,GetObjectColorGreen(map[x,z+1].id)-50,GetObjectColorBlue(map[x,z+1].id)-50,255)
//SetObjectColor(map[x,z-1].id,GetObjectColorRed(map[x,z-1].id)-50,GetObjectColorGreen(map[x,z-1].id)-50,GetObjectColorBlue(map[x,z-1].id)-50,255)
SetObjectColor(map[x,z].id,100,100,100,255):SetObjectImage(map[x,z].id,textImages[1],0)
SetObjectColor(map[x+1,z+1].id,100,100,100,255):SetObjectImage(map[x+1,z+1].id,textImages[1],0)
SetObjectColor(map[x-1,z+1].id,100,100,100,255):SetObjectImage(map[x-1,z+1].id,textImages[1],0)
SetObjectColor(map[x-1,z-1].id,100,100,100,255):SetObjectImage(map[x-1,z-1].id,textImages[1],0)
SetObjectColor(map[x+1,z].id,100,100,100,255):SetObjectImage(map[x+1,z].id,textImages[1],0)
SetObjectColor(map[x-1,z].id,100,100,100,255):SetObjectImage(map[x-1,z].id,textImages[1],0)
SetObjectColor(map[x,z+1].id,100,100,100,255):SetObjectImage(map[x,z+1].id,textImages[1],0)
SetObjectColor(map[x,z-1].id,100,100,100,255):SetObjectImage(map[x,z-1].id,textImages[1],0)
endif
endif
if GetRawKeyState(68) then inc camerax#,.1
if GetRawKeyState(65) then dec camerax#,.1
if GetRawKeyState(83) then inc cameray#,.1
if GetRawKeyState(87) then dec cameray#,.1
movecamera()
SetCameraPosition(1,camerax#,cameray#,cameraz#)
Print( ScreenFPS() )
Sync()
loop
function movecamera()
if GetRawKeyState(32)
fDiffX# = (GetPointerX() - startx#)/4.0
fDiffY# = (GetPointerY() - starty#)/4.0
newX# = angx# + fDiffY#
if ( newX# > 89 ) then newX# = 89
if ( newX# < -89 ) then newX# = -89
SetCameraRotation(1, newX#, angy# + fDiffX#, 0 )
endif
endfunction
function prepareheights()
for x=2 to sizex-1
for z=2 to sizez-1
changeverts(x,z,map[x,z].height#)
next
next
endfunction
function changeverts(x,z,slope#)
CreateMemblockFromObjectMesh(2,map[x+1,z-1].id,1) // bottom right corner
CreateMemblockFromObjectMesh(4,map[x+1,z+1].id,1) // top right corner
CreateMemblockFromObjectMesh(5,map[x-1,z+1].id,1) // top left corner
CreateMemblockFromObjectMesh(6,map[x-1,z-1].id,1) // bottom left corner
CreateMemblockFromObjectMesh(3,map[x+1,z].id,1) // right edges
CreateMemblockFromObjectMesh(7,map[x-1,z].id,1) // left cube edges
CreateMemblockFromObjectMesh(8,map[x,z-1].id,1) // bottom cube edges
CreateMemblockFromObjectMesh(9,map[x,z+1].id,1) // top cube edges
for i=0 to 23
// bottom rightcorner
if i=8 then SetMeshMemblockVertexPosition(2,i,GetMeshMemblockVertexX(2,i),GetMeshMemblockVertexY(2,i)+slope#,GetMeshMemblockVertexZ(2,i))
if i=16 then SetMeshMemblockVertexPosition(2,i,GetMeshMemblockVertexX(2,i),GetMeshMemblockVertexY(2,i)+slope#,GetMeshMemblockVertexZ(2,i))
if i=14 then SetMeshMemblockVertexPosition(2,i,GetMeshMemblockVertexX(2,i),GetMeshMemblockVertexY(2,i)+slope#,GetMeshMemblockVertexZ(2,i))
// top rightcorner
if i=9 then SetMeshMemblockVertexPosition(4,i,GetMeshMemblockVertexX(4,i),GetMeshMemblockVertexY(4,i)+slope#,GetMeshMemblockVertexZ(4,i))
if i=0 then SetMeshMemblockVertexPosition(4,i,GetMeshMemblockVertexX(4,i),GetMeshMemblockVertexY(4,i)+slope#,GetMeshMemblockVertexZ(4,i))
if i=18 then SetMeshMemblockVertexPosition(4,i,GetMeshMemblockVertexX(4,i),GetMeshMemblockVertexY(4,i)+slope#,GetMeshMemblockVertexZ(4,i))
// top leftcorner
if i=11 then SetMeshMemblockVertexPosition(5,i,GetMeshMemblockVertexX(5,i),GetMeshMemblockVertexY(5,i)+slope#,GetMeshMemblockVertexZ(5,i))
if i=2 then SetMeshMemblockVertexPosition(5,i,GetMeshMemblockVertexX(5,i),GetMeshMemblockVertexY(5,i)+slope#,GetMeshMemblockVertexZ(5,i))
if i=4 then SetMeshMemblockVertexPosition(5,i,GetMeshMemblockVertexX(5,i),GetMeshMemblockVertexY(5,i)+slope#,GetMeshMemblockVertexZ(5,i))
// tbottom leftcorner
if i=10 then SetMeshMemblockVertexPosition(6,i,GetMeshMemblockVertexX(6,i),GetMeshMemblockVertexY(6,i)+slope#,GetMeshMemblockVertexZ(6,i))
if i=6 then SetMeshMemblockVertexPosition(6,i,GetMeshMemblockVertexX(6,i),GetMeshMemblockVertexY(6,i)+slope#,GetMeshMemblockVertexZ(6,i))
if i=12 then SetMeshMemblockVertexPosition(6,i,GetMeshMemblockVertexX(6,i),GetMeshMemblockVertexY(6,i)+slope#,GetMeshMemblockVertexZ(6,i))
// right cube
if i=8 then SetMeshMemblockVertexPosition(3,i,GetMeshMemblockVertexX(3,i),GetMeshMemblockVertexY(3,i)+slope#,GetMeshMemblockVertexZ(3,i))
if i=9 then SetMeshMemblockVertexPosition(3,i,GetMeshMemblockVertexX(3,i),GetMeshMemblockVertexY(3,i)+slope#,GetMeshMemblockVertexZ(3,i))
if i=16 then SetMeshMemblockVertexPosition(3,i,GetMeshMemblockVertexX(3,i),GetMeshMemblockVertexY(3,i)+slope#,GetMeshMemblockVertexZ(3,i))
if i=18 then SetMeshMemblockVertexPosition(3,i,GetMeshMemblockVertexX(3,i),GetMeshMemblockVertexY(3,i)+slope#,GetMeshMemblockVertexZ(3,i))
if i=0 then SetMeshMemblockVertexPosition(3,i,GetMeshMemblockVertexX(3,i),GetMeshMemblockVertexY(3,i)+slope#,GetMeshMemblockVertexZ(3,i))
if i=14 then SetMeshMemblockVertexPosition(3,i,GetMeshMemblockVertexX(3,i),GetMeshMemblockVertexY(3,i)+slope#,GetMeshMemblockVertexZ(3,i))
// left cube
if i=10 then SetMeshMemblockVertexPosition(7,i,GetMeshMemblockVertexX(7,i),GetMeshMemblockVertexY(7,i)+slope#,GetMeshMemblockVertexZ(7,i))
if i=11 then SetMeshMemblockVertexPosition(7,i,GetMeshMemblockVertexX(7,i),GetMeshMemblockVertexY(7,i)+slope#,GetMeshMemblockVertexZ(7,i))
if i=4 then SetMeshMemblockVertexPosition(7,i,GetMeshMemblockVertexX(7,i),GetMeshMemblockVertexY(7,i)+slope#,GetMeshMemblockVertexZ(7,i))
if i=6 then SetMeshMemblockVertexPosition(7,i,GetMeshMemblockVertexX(7,i),GetMeshMemblockVertexY(7,i)+slope#,GetMeshMemblockVertexZ(7,i))
if i=2 then SetMeshMemblockVertexPosition(7,i,GetMeshMemblockVertexX(7,i),GetMeshMemblockVertexY(7,i)+slope#,GetMeshMemblockVertexZ(7,i))
if i=12 then SetMeshMemblockVertexPosition(7,i,GetMeshMemblockVertexX(7,i),GetMeshMemblockVertexY(7,i)+slope#,GetMeshMemblockVertexZ(7,i))
// bottom cube
if i=8 then SetMeshMemblockVertexPosition(8,i,GetMeshMemblockVertexX(8,i),GetMeshMemblockVertexY(8,i)+slope#,GetMeshMemblockVertexZ(8,i))
if i=10 then SetMeshMemblockVertexPosition(8,i,GetMeshMemblockVertexX(8,i),GetMeshMemblockVertexY(8,i)+slope#,GetMeshMemblockVertexZ(8,i))
if i=6 then SetMeshMemblockVertexPosition(8,i,GetMeshMemblockVertexX(8,i),GetMeshMemblockVertexY(8,i)+slope#,GetMeshMemblockVertexZ(8,i))
if i=12 then SetMeshMemblockVertexPosition(8,i,GetMeshMemblockVertexX(8,i),GetMeshMemblockVertexY(8,i)+slope#,GetMeshMemblockVertexZ(8,i))
if i=14 then SetMeshMemblockVertexPosition(8,i,GetMeshMemblockVertexX(8,i),GetMeshMemblockVertexY(8,i)+slope#,GetMeshMemblockVertexZ(8,i))
if i=16 then SetMeshMemblockVertexPosition(8,i,GetMeshMemblockVertexX(8,i),GetMeshMemblockVertexY(8,i)+slope#,GetMeshMemblockVertexZ(8,i))
// top cube
if i=9 then SetMeshMemblockVertexPosition(9,i,GetMeshMemblockVertexX(9,i),GetMeshMemblockVertexY(9,i)+slope#,GetMeshMemblockVertexZ(9,i))
if i=11 then SetMeshMemblockVertexPosition(9,i,GetMeshMemblockVertexX(9,i),GetMeshMemblockVertexY(9,i)+slope#,GetMeshMemblockVertexZ(9,i))
if i=0 then SetMeshMemblockVertexPosition(9,i,GetMeshMemblockVertexX(9,i),GetMeshMemblockVertexY(9,i)+slope#,GetMeshMemblockVertexZ(9,i))
if i=2 then SetMeshMemblockVertexPosition(9,i,GetMeshMemblockVertexX(9,i),GetMeshMemblockVertexY(9,i)+slope#,GetMeshMemblockVertexZ(9,i))
if i=4 then SetMeshMemblockVertexPosition(9,i,GetMeshMemblockVertexX(9,i),GetMeshMemblockVertexY(9,i)+slope#,GetMeshMemblockVertexZ(9,i))
if i=18 then SetMeshMemblockVertexPosition(9,i,GetMeshMemblockVertexX(9,i),GetMeshMemblockVertexY(9,i)+slope#,GetMeshMemblockVertexZ(9,i))
next
SetObjectMeshFromMemblock(map[x+1,z-1].id,1,2)
SetObjectMeshFromMemblock(map[x+1,z].id,1,3)
SetObjectMeshFromMemblock(map[x+1,z+1].id,1,4)
SetObjectMeshFromMemblock(map[x-1,z+1].id,1,5)
SetObjectMeshFromMemblock(map[x-1,z-1].id,1,6)
SetObjectMeshFromMemblock(map[x-1,z].id,1,7)
SetObjectMeshFromMemblock(map[x,z-1].id,1,8)
SetObjectMeshFromMemblock(map[x,z+1].id,1,9)
DeleteMemblock(2)
DeleteMemblock(3)
DeleteMemblock(4)
DeleteMemblock(5)
DeleteMemblock(6)
DeleteMemblock(7)
DeleteMemblock(8)
DeleteMemblock(9)
endfunction
// Function to create a texture
//
// Inputs - Sizex - size of the texture to create - width
// Sizey - size of the texture to create - height
// Color - is the main color of the image
// Denisity - is a the depth of the texture - the lower the value, the more detail. higher value = no detail
//
// Returns the image for the resulting texture
//
// EG. CreateTexture ( 100, 100, makecolor(0,0,255), 100)
// This could create a DEEP water effect texture?
function createtexture(sizex# as float, sizey# as float, color, density as integer)
swap()
drawbox(0,0,sizex#, sizey#, color, color,color,color, 1)
render()
img = getimage(0,0,sizex#, sizey#)
memblockid = CreateMemblockFromImage (img)
imgwidth = GetMemblockInt(memblockid, 0)
imgheight = GetMemblockInt(memblockid, 4)
size=GetMemblockSize(memblockid)
for offset=12 to size-4 step 4
r=GetMemblockByte(memblockid, offset)
g=GetMemblockByte(memblockid, offset+1)
b=GetMemblockByte(memblockid, offset+2)
a=GetMemblockByte(memblockid, offset+3)
strength=random(1,density)
SetMemblockByte (memblockid, offset, r-strength)
SetMemblockByte (memblockid, offset+1, g-strength)
SetMemblockByte (memblockid, offset+2, b-strength )
SetMemblockByte (memblockid, offset+3, a-strength)
next
deleteimage (img)
img = CreateImageFromMemblock(memblockid)
DeleteMemblock(memblockid)
endfunction img
fubar