Trying to create some buildings.

Blitz3D Forums/Blitz3D Beginners Area/Trying to create some buildings.

SheepOnMintSauce(Posted 2011) [#1]
Hello folks,

I'm trying to get in my head how to make a simple building out of some cubes. Here's my code so far..



Sorry if it's a bit messy. I know I could probably make every box seperately, but then it kind of defies the point of using loops. :)

Can anyone help?


Midimaster(Posted 2011) [#2]
I found a bug in your code:

1. do not use i% for counting a FOR/NEXT-loop and the same time for creating/refering to a object:
wrong:
For i = 1 To 5
	i = CreateCube()
	EntityTexture i,buildingtex
	ScaleEntity i,basewidth-i,baseheight-1,basedepth-i	
	PositionEntity i,0,baseheight*i-i,0
Next


better:
For i = 1 To 5
	Cube = CreateCube()
	EntityTexture Cube,buildingtex
	ScaleEntity Cube,basewidth-i,baseheight-1,basedepth-i	
	PositionEntity Cube,0,baseheight*i-i,0
Next




here is a code, which simulates a city of boxes:

Graphics3D 800,600
Tram=CreateCube()
MoveEntity tram, 0,1,0


Camera=CreateCamera(Tram)
CameraFogMode camera,1
CameraFogColor Camera,255,255,255
CameraFogRange Camera,1,300


;Zufalls_Landschaft:
SeedRnd 12345

Boden=CreatePlane()
EntityColor boden,115,115,115
Himmel=CreateSphere(32)
FlipMesh himmel
ScaleEntity Himmel,800,99,800
EntityColor himmel,0,0,255
MoveEntity himmel,0,0,00
For i=0 To 99
	b=CreateCube()
	ScaleEntity b, Rand(4),Rand(8),Rand(4)
	EntityColor b,Rand(255),Rand(255),Rand(255)
	MoveEntity b,100-Rand(200),0,100-Rand(200)
Next
Licht=CreateLight()
TurnEntity licht, 0,90,0
MoveEntity licht,-30,30,30

Repeat
	MoveEntity Tram,0,0,.05
	RenderWorld()
	Text 10,60, "Position-X:" + Int(EntityX(Tram))
	Text 10,80, "Position-Y:" + Int(EntityZ(Tram))
	Text 10,550, "Press A and S for turning, P for break"
	Flip 0

	
	If KeyDown(30) Then
		TurnEntity Tram,0,.3,0 
	ElseIf KeyDown(31) Then 
		TurnEntity tram, 0,-.3,0
	ElseIf KeyHit(25) Then 

		If Stopp=0 Then
			FlushKeys()
			Stopp=1
			WaitKey()
		Else
			Stopp=0
		EndIf
	EndIf 
Until KeyHit(1)


Use this surrounding for testing your building. It is more comfortable then your "nervous" plot-textures.


and here are some changes in your code:

Graphics3D 800,600,32,2
SetBuffer BackBuffer()
camtarget = CreatePivot()
cam=CreateCamera(camtarget)
PositionEntity cam,0,7,-18
CameraViewport cam,0,0,GraphicsWidth(),GraphicsHeight()
ground = CreatePlane(16)
basewidth = Rnd(10,20)
basedepth = Rnd(10,20)
baseheight = Rnd(8,20)
EntityColor ground,0,199,0

Licht=CreateLight()
TurnEntity licht, 15,10,15
MoveEntity licht,-10,30,0
Himmel=CreateSphere(32)
FlipMesh himmel
ScaleEntity Himmel,800,99,800
EntityColor himmel,0,0,255
MoveEntity himmel,0,0,00

SetBuffer BackBuffer()
	cube1 = CreateCube()									; I want it to create some boxes..
	PositionEntity cube1,-3,2,0					; and position this new box above the previous box..

For i = 1 To 5											; This for loop is my problem.
	cube = CreateCube()									; I want it to create some boxes..
	;EntityTexture i,buildingtex							; texture them..
	;ScaleEntity i,basewidth-i,baseheight-1,basedepth-i	; scale it a bit smaller than the previous box..
	EntityColor cube, 155,155,0
	ScaleEntity Cube, 3-(i*0.4),1,3-(i*0.3)
	PositionEntity cube,0,i*2,0					; and position this new box above the previous box..
Next													; so it's a bit smaller. Making a stack of boxes, where..
														; every new one is a bit smaller than the last.

While Not KeyDown(1)
	SetBuffer BackBuffer()
	;WireFrame True
	Select MouseZSpeed () 
		Case -1
			MoveEntity cam,0,1,-1 
		Case 1 
			MoveEntity cam,0,-1,1
		End Select 
	
TurnEntity camtarget,0,.5,0
PointEntity cam,camtarget

UpdateWorld()
RenderWorld()

Flip 0
Wend

Last edited 2011

Last edited 2011


SheepOnMintSauce(Posted 2011) [#3]
I found a solution this morning to it. I've got it to make a building now with a random width,depth,and number of floors, but the height of building is now working properly now.

I wanted every floor to be half as high as the previous floor, but I've tried a few different things, but I still can get it to look right.

Graphics3D 800,600,32,2
SetBuffer BackBuffer()
SeedRnd MilliSecs()
camtarget = CreatePivot()
cam=CreateCamera(camtarget)
PositionEntity cam,0,40,-20
CameraViewport cam,0,0,GraphicsWidth(),GraphicsHeight()
ground = CreatePlane(16)
groundtexture = CreateTexture(256,256)
SetBuffer TextureBuffer(groundtexture)
For x=0 To 255
	For y=0 To 255
		Color (x Xor y),(x Xor y),(x Xor y)
		Plot x,y
	Next
Next
SetBuffer BackBuffer()
EntityTexture ground,groundtexture						
basewidth = Rand(10,20)
basedepth = Rand(10,20)
baseheight = Rand(10,50)
buildingtex = CreateTexture(256,256)
SetBuffer TextureBuffer(buildingtex)
For x=0 To 255
	For y=0 To 255
		Color y,y,y
		Plot x,y 
	Next
Next

SetBuffer BackBuffer()

numtiers = Rand(1,10)
Dim tiers(numtiers)
For i = 0 To numtiers												
	tiers(i) = CreateCube()											
	EntityTexture tiers(i),buildingtex								
	ScaleEntity tiers(i),basewidth-i,baseheight,basedepth-i
	PositionEntity tiers(i),0,baseheight*i,0
	targetheight=targetheight+baseheight						
Next																

PositionEntity camtarget,0,targetheight/2,0													
While Not KeyDown(1)
	SetBuffer BackBuffer()
	;WireFrame True
	Select MouseZSpeed () 
		Case -1
			MoveEntity cam,0,1,-2 
		Case 1 
			MoveEntity cam,0,-1,2
		End Select 
	If EntityY(cam) <= 1 Then 
		PositionEntity cam,EntityX(cam),1,EntityZ(cam)
	EndIf
	If EntityZ(cam) <= 20 Then
		PositionEntity cam,EntityX(cam),EntityY(cam),20
	EndIf
	
TurnEntity camtarget,0,0.1,0
PointEntity cam,camtarget

UpdateWorld()
RenderWorld()

Flip
Wend


The problem is again here..

numtiers = Rand(1,10)
Dim tiers(numtiers)
For i = 0 To numtiers												
	tiers(i) = CreateCube()											
	EntityTexture tiers(i),buildingtex								
	ScaleEntity tiers(i),basewidth-i,baseheight,basedepth-i
	PositionEntity tiers(i),0,baseheight*i,0
	targetheight=targetheight+baseheight						
Next	


Last edited 2011


Midimaster(Posted 2011) [#4]
So you have to divide BaseHeight%
And you have to position the next Cube at the "last top position" TargetHeight%.
But you have to place the cubes always BaseHeight%/2 to high, because the positions relates always to the center of the building. (Without this, the first Cube would be half under the earth....)




SheepOnMintSauce(Posted 2011) [#5]
Ah great! I'll have a fiddle about with that, cheers! :)

It makes sense, I think I spent too much time just staring at the problem without taking a step back to look at it.