Dropping a bomb with deforming terrain

Blitz3D Forums/Blitz3D Beginners Area/Dropping a bomb with deforming terrain

Blitzplotter(Posted 2016) [#1]
Hello,

[EDIT]

All files for solution available here:


http://www.wishingwellsoftware.co.uk/setup_3bombs_Blitz3D.exe

[end EDIT]

I've been trying to join some code together about dropping bombs and deforming the terrain, with a view to better understanding terrain deformation(s), the following is code from the forums by Yue and RickNasher and David Bird:

I cannot seem to find the thread which was discussing the various methods I've found to possibly create 'craters' in a terrain.

the files that need to be used in conjunction with this code are located within the :

{your drive}\Program Files\Blitz3D\samples\birdie\Terrain Tiling

The bomb doesn't fall through the terrain (which is a mesh... I reckon) It sits 'on it' when the collision is detected:





I suspect the problem I (may) be having here is I am not using a 'proper' terrain in the strict sense of the word? Any help appreciated - can a 'mesh' be deformed - or does it have to be a terrain type of entity ?



Provided in all its glory:



;This is the TERRAIN TILING demo from the Blitz examples folder
; it is eluding me how to destruct the scenery, if indeed it is at all possible...

;Terrain Tiling 2
;David Bird
;dave@...
;use of u/v coords for tileset
;also example of texture coords set

;size of map
Const Gridx=64
Const Gridz=64

bomberx=22
bombery=59
bomberz=20

Graphics3D 640,480
SetBuffer BackBuffer()

lit=CreateLight()
cam=CreateCamera()

CameraRange cam,.1,1000
PositionEntity cam,32,20,50
TurnEntity cam,0,90,0


;Load brush with 16tiles
b1=LoadBrush("16tex.bmp")

Const Texture_Grid=4			;4x4 square texture grid

;Create and store map heights from a hightmap
Dim map#(Gridx,Gridz)
hmap=LoadImage("hmap.bmp")
DrawImage hmap,0,0

For z=0 To Gridz
	For x=0 To Gridx
		GetColor x,z
		map(x,z)=Float(ColorRed())/255
	Next
Next

;make a bomb carrier

Sphere = CreateSphere(25)
PositionEntity Sphere,bomberx,bombery,bomberz
ScaleEntity Sphere,4,4,4

;make a bomb

bomb = CreateSphere(25)
PositionEntity bomb,bomberx,bombery-5,bomberz
ScaleEntity bomb,0.5,1.5,0.5

;bomb vars

bombs=0
Global bombstext=0






;Create a map mesh and scale to size
mesh=Create_Map(Gridx,Gridz,b1,3)
ScaleMesh mesh,1,6,1

;do next level of texturing using second
;set of texture coords

tex1=LoadTexture("mask.bmp")
TextureCoords tex1,1

tex2=LoadTexture("tex0.bmp")
TextureCoords tex2,1
TextureBlend tex2,3

EntityTexture mesh,tex1,0,1		;remember texture index 0 is for tileset
EntityTexture mesh,tex2,0,2

Color 255,255,255

pl=CreatePlane()
tex3=LoadTexture("tex0.bmp"):ScaleTexture tex3,64,64
EntityTexture pl,tex3
EntityOrder pl,1
EntityFX pl,1

EntityType bomb, 1
EntityType mesh, 2

Collisions 1,2,2,2 


MoveEntity Cam,0,0,-40

;WireFrame True
While Not KeyDown(1)
	
	If KeyDown(203) MoveEntity cam,-1,0,0
		If KeyDown(205) MoveEntity cam,1,0,0
			If KeyDown(200) MoveEntity cam,0,0,1
				If KeyDown(208) MoveEntity cam,0,0,-1
					
			
					If KeyDown(16) ;q
				
				TurnEntity cam,1,0,0
				
				Flip
				
			EndIf
			
			If KeyDown(30) ;a
					
				TurnEntity cam,-1,0,0
				
			EndIf
			
			
			If KeyDown(44) 
				
				TurnEntity cam,0,1,0
				
			EndIf
			
			
			If KeyDown(45) 
				
				TurnEntity cam,0,-1,0
				
			EndIf
			
			If KeyDown(48)   ;  B
				
				bombs=1
				
			EndIf
			
			
			If bombs = 1
				
				dropbombs()
				
			Else
				
				stopbombs()
				
			EndIf
	
	If KeyHit(57) Then
		For y=0 To Gridz-1
			For x=0 To Gridx-1
				tl=Rand(15)
				Paint_Tile(x,y,tl,mesh)
			Next
		Next
	End If
			
	UpdateWorld
	RenderWorld
	Text 0,0,"Press <Space> to randomize tiles"
	
	
	If bombstext=1
		Text 20,20,"Dropping a bomb...."
		
		MoveEntity bomb,0,-0.1,0
		
	Else
		Text 20,20,"Press B to drop a bomb."
		
	EndIf
	
	bombstext=0
	
	x = EntityX(bomb)
	y = EntityY(bomb)
	z = EntityZ(bomb)
	TFormPoint (x, y, z,  0, mesh  )
	
	h# = TerrainHeight(mesh,TFormedX(),TFormedY())
	
	If h > 0 Then
		h = h -.1
		If h < .5  Then  h = .5
		
		ModifyTerrain ( mesh, TFormedX()+ Rnd(-2,2), TFormedZ()+Rnd(-2,2),h#,True)	
		
		MakeTerrainCrater(bomb,mesh,radius#=2.5,th#=10,hardness#=0.5)
		
	End If 	
	
	
	Text 40,40," h is: "+h+" Bombs y is: "+y
	
	Flip
Wend

FreeEntity mesh
FreeEntity lit
FreeEntity cam
EndGraphics
End

;
;   Drop Bombs from the sphere
;
Function dropbombs()
	
	bombstext=1
	
End Function

Function stopbombs()
	
	bombstext=0
	
End Function

;
;	Create a flat plane all with tile 1
;
Function Create_Map(tilex,tilez,brush1,tile)
	mesh=CreateMesh()
	surf=CreateSurface(mesh,brush1)
	wid#=Float(1)/Float(Texture_Grid)
	u0#=wid*Float(tile Mod Texture_Grid)
	v0#=wid*Float(tile/Texture_Grid)
	u1#=u0+wid
	v1#=v0
	u2#=u1
	v2#=v0+wid
	u3#=u0
	v3#=v2
	u#=0
	v#=0
	stp#=1.0/Float(tilex)
	For z#=0 To tilez-1
		u=0
		For x#=0 To tilex-1
			h1#=map(x,z)
			h2#=map(x+1,z)
			h3#=map(x+1,z+1)
			h4#=map(x,z+1)
			AddVertex surf,x,h1,z,u0,v0
			VertexTexCoords surf,cnt,u,v,0,1
			
			AddVertex surf,x+1,h2,z,u1,v1
			VertexTexCoords surf,cnt+1,u+stp,v,0,1
			
			AddVertex surf,x+1,h3,z+1,u2,v2
			VertexTexCoords surf,cnt+2,u+stp,v+stp,0,1
			
			AddVertex surf,x,h4,z+1,u3,v3
			VertexTexCoords surf,cnt+3,u,v+stp,0,1
			
			AddTriangle surf,cnt,cnt+2,cnt+1
			AddTriangle surf,cnt,cnt+3,cnt+2
			cnt=cnt+4
			u=u+stp
		Next
		v=v+stp
	Next
	UpdateNormals mesh
	Return mesh
End Function

;paint one tile with new tile texture
Function Paint_Tile(tx,tz,texture_index,mesh)
	;firstly find texture coords
	wid#=Float(1)/Float(Texture_Grid)
	u0#=(wid*Float(texture_index Mod Texture_Grid))
	v0#=(wid*Float(texture_index/Texture_Grid))
	u1#=u0+wid
	v1#=v0
	u2#=u1
	v2#=v0+wid
	u3#=u0
	v3#=v2
	;retrieve the surface from the mesh
	surf=GetSurface(mesh,CountSurfaces(mesh))
	;find triangle at grid position tx,tz
	cnt=((tz*Gridz)+tx)*2
	;Now reset its texture coords
	index=TriangleVertex(surf,cnt,0)
	VertexTexCoords surf,index,u0,v1
	VertexTexCoords surf,index+1,u1,v1
	VertexTexCoords surf,index+2,u2,v2
	VertexTexCoords surf,index+3,u3,v3
End Function




Function MakeTerrainCrater(entity,Terrain,radius#=.5,th#=10,hardness#=0.5)
	
; ex#,ez#,x#,z#,terrainscale ,radius# , hardness#,d#,h#,scale#
	
; A righthandy function that easily makes a nice round crater. 
; Assumes terrain sections are 1x1 Blitz units (add Step to change). 
; Also fun to tinker with, and easy to add particles, crater ring, etc.
;
; entity=entity crater is created around
; terrain=terrain in which the crater is formed
; radius=crater's radius (default is 1)
; th=maximum height of any terrain section (default is 100)
; hardness=hardness of the ground, 1 is completely soft, 0 is completely solid (default is 1) 
	
	
	ex#=EntityX#(entity)/terrainscale ; added /terrainscale to make work
	ey#=EntityY#(entity)
	ez#=EntityZ#(entity)/terrainscale
	
	;=============================
	; generate the crater
	;=============================
	For x#=-radius# To radius#
		For z#=-radius To radius#
			If Sqr(x#^2+z#^2)<=radius# And hardness#<>0
				d#=Sqr(x#*x#+z#*z#)	
				h#=TerrainHeight#(Terrain,ex+x#,ez+z#)
				scale#=th#/hardness#
				h#=h#+d#/scale#-radius#/scale#
				If h#<0 Then h#=0
				ModifyTerrain(Terrain,ex+x#,ez+z#,h#,True)
				
				;ScorchTerrain(ex+x#,ez+z#) ;generate the black scorch 
				;ScorchTerrain(EntityX(marker2)/terrainscale,EntityZ(marker2)/terrainscale)	
			EndIf
		Next
	Next
	
End Function





RemiD(Posted 2016) [#2]
You have an example on how to deform a terrain in the castle demo Blitz3D\samples\mak\castle


Blitzplotter(Posted 2016) [#3]
@RemiD, thanks I'll refer to that. I wasn't sure if I could treat a 'mesh' as a 'terrain' and whether the 2 were interchangeable.


RemiD(Posted 2016) [#4]
For what you want to do a terrain is better, because you can modify one (or several) height(s) at X,Z, and the ROAM algorithm will manages the recreation of the terrain depending on where the camera is and depending on the maximum LOD (level of detail) that you have defined (more details around the camera, less details far away)

Take a look at what effect you can achieve with 2 terrains : http://www.blitzbasic.com/gallery/view_pic.php?id=2478&gallery=hr&page=1

I have never seen a game where you can digg in the ground like i demonstrated in this demo ;) (but the ambiance was very dark (i was listening a bit too much at Marilyn Manson's music at the time :P))


Blitzplotter(Posted 2016) [#5]
Thankyou, I was beginning to suspect I had made an error trying to deform a simple mesh with a function that I suspect was designed to be used with Terrains.

The code here leads me to suspect 'LoadTerrain' may do some trick stuff when loading the heightmap to allow it be be deformed afterwards.

http://jnoodle.com/Blitz3D/lesson10.html


Rick Nasher(Posted 2016) [#6]
@RemiD
It's looking pretty cool. But I don't quite get why you have to use 2 terrains for this and how you would apply that?

I've done one in which I could throw grenades and then it would create a relatively nice crater in the terrain(also possible to dig in that fashion), but as I like my terrains pretty big & high they could come across a bit less round. I had to make a compromise in details.


RemiD(Posted 2016) [#7]
@Rick>>in order to be able to deform a terrain precisely you need it to scale it on the y axis not too much, for example i used a scale of 25.5 on the y axis. Why ? Because you can have a maximum of 255 heights in a heightmap (between 0 and 255), so if the scale on the y axis is 25.5, this means that you can have a height precision of 0.1, so you can digg in the ground by 0.1 step, so it looks convincing.

For this demo, i used 2 terrains because one represents the grass and the other represents the dirt... Only when you digg you can see the dirt, but you can probably achieve a similar effect using a color texture...


Blitzplotter(Posted 2016) [#8]
@RemiD, yeah the ambience is pretty dark, used to great effect.

For this demo, i used 2 terrains because one represents the grass and the other represents the dirt... Only when you digg you can see the dirt, but you can probably achieve a similar effect using a color texture...
ingenius !

I've had a look at the castle demo, and it whilst it puts hole 'sprites' on the landscape it doesn't seem to deform the terrain whcih is what I was hoping for. The Castle demo is a good example of what can be achieved with Blitz3D nevertheless.


Blitzplotter(Posted 2016) [#9]
Making progress, although my terrain currently 'grows' towards the bomber before its released:



More investigation required, pleasant progress though, thanks for the help.


Rick Nasher(Posted 2016) [#10]
@Blitzplotter
If dropping the bomb then when bomb entity collided with the terrain then you stop it moving, store it's x,y,z, hide the bomb entity(and/or let it explode using particles or sprite animation with a nice boom sound) and run the modify terrain function on that spot to create a nice crater.

@RemiD
Ah, yeah indeed in that sense it's a great idea. I was using a scorch texture for it, but this might be better looking combined with that.

-Does having multiple terrains impact fps a lot?


Blitzplotter(Posted 2016) [#11]
@Rick, thanks for the fb, that was an approach I was considering prior to the green terrain code, within my first attempt (the rocky image at the top).

The modify terrain function seems to make the terrain grow upwards at the moment before the bomb is even released, (hence the green spike in the image) I suspect I need to play around with the collision vars as the bomb falls straight through the terrain on the green snapshot.

I'm just pleased the terrain is deforming at all ;)


Rick Nasher(Posted 2016) [#12]
Perhaps this example helps in understanding the whole modify terrain thingy:

ModifyTerrain.bb


Also some bits of code I pulled out of my own game(haven't got time to rework it for it all to be working outside of it, so hope is clear and usefull to you:




RemiD(Posted 2016) [#13]

it doesn't seem to deform the terrain whcih is what I was hoping for.


yes it does, search for "modifyterrain"


RemiD(Posted 2016) [#14]
What can be confusing is which value to put in the height parameter of ModifyTerrain()
You can know the y# at x,z by using TerrainY at x,z (or by picking it from top to down at x,z)
You also know the scale on the y axis that you have defined when creating your terrain (i suggest to use 25.5 so that you can have a precision of 0.1unit when you digg in it)

So for a terrain with a max height of 25.5units, the height 0 corresponds to 0y and the height 1.0 corresponds to 25.5y
if you know that the y at x,z is 3.0y, and you want to digg a hole of 0.5 unit, how do you calculate the height (for modifyterrain())
3.0/25.5 = 0.117647 (the current height)
0.5/25.5 = 0.019607 (the height of the hole you want to digg)
0.117647 - 0.019607 = 0.09804 or (3.0-0.5)/25.5 = 0.09804 (the new height after having digged the hole)

That's what i remember, but test it just to be sure...


Another reason to choose a scale of 25.5 on the y axis is that then each shade of grey of each pixel on your heightmap represents a height with a 0.1 precision.


Blitzplotter(Posted 2016) [#15]
@Rick and RemiD, thanks for the feedback, I'll try it as soon as my son vacates (my...) PC. I really appreciate it.

[edit] Am considering an a, b and c-bomb approach to learning my terrain deformations.




Blitzplotter(Posted 2016) [#16]
@RemiD and [EDIT] Rick Nasher, thank you for your assistance, its been a (minor) journey, enjoyable and really quite gratifying nonetheless ;)

I even dug out my printed B3D manual, which although has seen better days, still also helps.





the code (not too pretty - but it works):




RustyKristi(Posted 2016) [#17]
http://www.blitzbasic.com/codearcs/codearcs.php?code=3284
Generated with help from RemiD and Rusty


Hey BP, Looks interesting, though I should correct you to credit Rick Nasher and not me, but thanks for thinking of me though and I hope you're not confused. ;-)


Blitzplotter(Posted 2016) [#18]
Ooops, thanks Rusty for pointing out my typo - all the RRrrrr s ;)

Sorry Rick, thanks again for your help. I'm recovering from right knee ACL surgery and am using B3D to help see me through ;)

Next, dig out some of my old JV-ODE physics lib stuff and integrate some outlandish explosions with smoke and some sonic booms.....


RustyKristi(Posted 2016) [#19]
haha np. oh hey, feel better soon. :-)


RemiD(Posted 2016) [#20]
@Blitzplotter>>curious to test your code, but please provide the necessary files...


Blitzplotter(Posted 2016) [#21]
Will wrap the necessary files later, thx for your help.


Blitzplotter(Posted 2016) [#22]
@RemiD, here you go:

http://www.wishingwellsoftware.co.uk/setup_3bombs_Blitz3D.exe

All the files required to run the code ;)

I'd to use Innossetup as I don't have Winzip installed or 7zip for that matter, enjoy.


Yue(Posted 2016) [#23]


Something tells me that is the 1106 version of Blitz3D.


Blitzplotter(Posted 2016) [#24]
The code is available to compile yourself within the installed prog directory, sorry if I'm running and old B3d, I believe the one I'm using is compatible with fastlibs, if not W10, I'm on W7 and works fine for moi.


Yue(Posted 2016) [#25]
Hello, I commented that Nvidia released a new update drivers for my graphics card . 341.96 WHQL, and apparently the closure problem in Blitz3D 1,106 apliacación I was solved , I will make more evidence.


Blitzplotter(Posted 2016) [#26]
Oh, alright, the code is only one file plus a heightmap and 3 textures. I'd imagine it should compile ok on your own machine as that should have the requisite resources to draw in.

Just fired up my sons creaky laptop (it blue screens now and then), managed to download the exe and run it no problems. I appreciate that does not help you Yue, however, the code to compile it yourself is installed in something like the following directory:

C:\Program Files (x86)\3bombs

Rgds,

BP


RemiD(Posted 2016) [#27]
Seems to work well here.
However i am a bit disappointed because you have not even used the math formula to calculate a precise height that i explained in post#14... So don't credit me ;)


Blitzplotter(Posted 2016) [#28]
However i am a bit disappointed because you have not even used the math formula


On my TO DO list ;) I was struggling with getting the TerrainDetail variable to compile for a bit - then I confess I was trial and error with correlating the Terrains X znd Z co-ords with where the craters should be.


RemiD(Posted 2016) [#29]
TerrainDetail is to define the number of triangles that the terrain will be made of each frame. So you want to have a high enough value but not too high either...
for a 100x100z terrain (not scaled on x or z), the max amount is 100x100x2=20 000tris
so i usually use 2 000tris (but this is arbitrary, do some tests and see how it looks and how much time it takes to build it)


Blitzplotter(Posted 2016) [#30]
Thanks for the additional info RemiD, I was wondering why it was failing to compile above a certain number of tri's, however my terrain is rather big.

My array of discs - only dropped the one missile so far - but the logic is good ;)