Help with mesh deformation
Blitz3D Forums/Blitz3D Programming/Help with mesh deformation
| ||
After creating a 4x4 mesh, I would need to deform it according to the different heights of the underlying terrain, adapting it, as much as possible, to the terrain's surface. This would be used to create craters on a terrain in realtime, after impacts. I've found no other way of "painting" terrains in "real time" than creating a flexible mesh and adapting it to the underlying terrain surface. The question is: why am I not getting the real height of the terrain under each vertex of the mesh with this function? What's is wrong with it? Thank you very much for your help, and for your time! Here's my piece of code. ;crater(number of tiles x, number of tiles z,positionx,y,z) Function crater(tilex,tilez,cx#,cy#,cz#) ;in this example desired size of mesh is 4x4 tilex=4 : tilez=4 mesh=CreateMesh() surf=CreateSurface(mesh,0) ;positioning each vertex For j#=0 To tilez For i#=0 To tilex AddVertex surf,(i*2)-5,0,(j*2)-5,i/2.0,j/2.0 Next Next ;creating triangles For j#=0 To tilez-1 For i#=0 To tilex-1 AddTriangle surf,i+(j*5),i+((j+1)*5)+1,i+(j*5)+1 AddTriangle surf,i+(j*5),i+((j+1)*5),i+((j+1)*5)+1 Next Next ;positioning mesh PositionEntity mesh,cx#,cy#+.01,cz# ;deformation of created mesh cnt=0 For j#=0 To tilez For i#=0 To tilex xv#=VertexX(surf,cnt) zv#=VertexZ(surf,cnt) yv#=TerrainHeight#(land,xv#,zv#) VertexCoords surf,cnt,xv#,yv#,zv# cnt=cnt+1 Next Next UpdateNormals mesh Return mesh End Function Last edited 2011 |
| ||
Greetings! I have finally found some decent solution to the problem... Still a bit rough, but it works. Here's the code, opinions and advices are welcome!!! Thank you Graphics3D 640,480,32,2 Global player=CreateCube() flotex=LoadTexture("a.jpg") ScaleTexture flotex,128,128 Global camera=CreateCamera(player) PositionEntity camera,0,30,20 PointEntity camera,player Global light=CreateLight(2,player) PositionEntity light,200,500,0 Global earth=LoadTerrain("b.jpg") ScaleEntity earth,1,60,1 PositionEntity earth,-128,0,-128 TerrainShading earth,1 EntityTexture earth,flotex EntityPickMode earth,2 Global enable=1 ; Global surf Global gridx=4 Global gridz=4 Global mesh=crater(gridx,gridz) EntityColor mesh,255,0,0 ; While KeyDown(1)=0 If KeyHit( 17 )=True Then enable=1-enable WireFrame enable If KeyDown(200) MoveEntity player,0,0,-1 EndIf If KeyDown(208) MoveEntity player,0,0,1 EndIf If KeyDown(203) TurnEntity player,0,1,0 EndIf If KeyDown(205) TurnEntity player,0,-1,0 EndIf PositionEntity player,EntityX(player),TerrainY(earth,EntityX(player),EntityY(player),EntityZ(player))+1,EntityZ(player) PositionEntity mesh,EntityX(player)+1,TerrainY(earth,EntityX(player),EntityY(player),EntityZ(player))+.1,EntityZ(player)+1 RotateEntity mesh,0,0,0 update_crater(mesh,surf) UpdateWorld() RenderWorld() Flip Wend End Function crater(tilex,tilez) mesh=CreateMesh() surf=CreateSurface(mesh,0) For j#=0 To tilez For i#=0 To tilex AddVertex surf,(i*2)-5,0,(j*2)-5,i/2.0,j/2.0 Next Next For j#=0 To tilez-1 For i#=0 To tilex-1 AddTriangle surf,i+(j*5),i+((j+1)*5)+1,i+(j*5)+1 AddTriangle surf,i+(j*5),i+((j+1)*5),i+((j+1)*5)+1 Next Next Return mesh End Function Function update_crater(mesh,surf) cnt=0 For j#=0 To gridx For i#=0 To gridz ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; xv#=VertexX(surf,cnt) zv#=VertexZ(surf,cnt) under=LinePick(EntityX(player)+(i*6)-15,100,EntityZ(player)+(j*6)-15,0,-100,0,1) If under<>0 yv#=PickedY#()/6 Else yv#=0 EndIf VertexCoords surf,cnt,xv,yv,zv ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; cnt=cnt+1 Next Next UpdateNormals mesh Return mesh End Function |
| ||
Please feel free to visit youtube for watching this piece of code in action.... Thanks http://youtu.be/lYVCLPhlz5k |
| ||
Hi, Ruben, good work! I like your "RPG maker". Do you have ane web page with info about this project or similar something ? One of my future projects is create a group of Blitz3D programmers mostly Spanish-speaking (to communicate with agility) and create the best game ever made in Blitz3D with the experience of all who are showing great progress in their personal projects . If I continue with this idea I wish I could count with you. Do you like the idea? |