Page 1 of 1

Terrain collisions after modifying terrain

Posted: Sun Jul 13, 2014 9:05 pm
by Realizimo
Hello,

I'm having problems with Ogre and physics when updating the terrain.

The terrain is created using CreateTerrain and AddTerrainTexture. The objects collide perfect with the loaded terrain.

The terrain is then modified using SetTerrainTileHeightAtPoint. After that UpdateTerrain is called. The graphics is updated correct, but objects don't collide with the new terrain. I have also tried to call TerrainPhysicsBody, BuildTerrain and EnableWorldCollisions after the terrain is modified, but it doesn't improve anything.

version 5.30 beta 7 (win7 x64)
In the attached example the terrain can be modified using the space key. The camera is looking from the ground (terrain) looking at the object (the ball). When the terrain is modified the camera angle changes as it should but the object is not affected by the new terrain.

Code: Select all

; ------------------------------------------------------------
;
;   PureBasic - Terrain : Terrain Height change
;
;    (c) 2012 - Fantaisie Software
;
; ------------------------------------------------------------
IncludeFile "Screen3DRequester.pb"
#NbSommet = 36
Declare DoTerrainModify(tx, ty, wx.f, wy.f, wz.f, mBrushSizeTerrainSpace.f, height.f)
If InitEngine3D()=0 : End : EndIf
InitSprite()
InitKeyboard()
If Screen3DRequester()=0  : End : EndIf
Add3DArchive("Data/Textures/", #PB_3DArchive_FileSystem)
Add3DArchive("Data/Textures/nvidia", #PB_3DArchive_FileSystem)
Add3DArchive("Data/GUI", #PB_3DArchive_FileSystem) 
Add3DArchive("Data/Packs/desert.zip", #PB_3DArchive_Zip)
Parse3DScripts()
;- Light
light = CreateLight(#PB_Any ,RGB(185, 185, 185), 4000, 1200, 1000, #PB_Light_Directional)
SetLightColor(light, #PB_Light_SpecularColor, RGB(255*0.4, 255*0.4,255*0.4)) 
LightDirection(light ,0.55, -0.3, -0.75) 
AmbientColor(RGB(5, 5,5))
;- Camera
CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0,  0, 500, -50, #PB_Absolute)
CameraRange(0, 1, 0)
;- ball; new
CreateSphere(0, 3)    ; new
CreateEntity(0,MeshID(0),#PB_Material_None,0,500,0); new
EntityPhysicBody(0,#PB_Entity_SphereBody)  ; new
; terrain definition
SetupTerrains(LightID(Light), 3000, #PB_Terrain_NormalMapping)
; initialize terrain 
CreateTerrain(0, 513, 12000, 600, 3, "TerrainHeight", "dat")
; set all texture will be use when terrrain will be constructed 
AddTerrainTexture(0,  0, 100, "dirt_grayrocky_diffusespecular.jpg",  "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, "terrain513.png", ty % 2, tx % 2)     
BuildTerrain(0)  
TerrainPhysicBody(0, 2, 1)  ; new
UpdateTerrain(0)
; SkyBox
SkyBox("desert07.jpg")

Repeat
  ExamineKeyboard() 
  MoveCamera(0, EntityX(0), TerrainHeight(0, CameraX(0), CameraZ(0)) + 10, EntityZ(0)-40, #PB_Absolute)
  CameraLookAt(0, EntityX(0), EntityY(0),EntityZ(0))
  If KeyboardPushed(#PB_Key_Space)
    DoTerrainModify(0, 0, EntityX(0),EntityY(0),EntityZ(0), 0.004, 30) 
  EndIf  
  RenderWorld()    
  FlipBuffers()       
Until KeyboardPushed(#PB_Key_Escape)   
End 

Procedure DoTerrainModify(tx, ty, wx.f, wy.f, wz.f, mBrushSizeTerrainSpace.f, newheight.f)
  terrainSize.f = TerrainTileSize(0, tx, ty) - 1
  Pointx.f = TerrainTilePointX(0, tx, ty, wx, wy, wz) 
  Pointy.f = TerrainTilePointY(0, tx, ty, wx, wy, wz)   
  startx = (Pointx - mBrushSizeTerrainSpace) * terrainSize
  starty = (Pointy - mBrushSizeTerrainSpace) * terrainSize
  endx   = (Pointx + mBrushSizeTerrainSpace) * terrainSize
  endy   = (Pointy + mBrushSizeTerrainSpace) * terrainSize  
  For y = starty To endy
    For x = startx To endx  
      SetTerrainTileHeightAtPoint(0, tx, ty, x, y, newheight)      
    Next x
  Next y
  UpdateTerrain(0)
EndProcedure

Re: Terrain collisions after modifying terrain

Posted: Mon Jul 14, 2014 8:48 am
by Bananenfreak
Terrainphysicsbody isn´t updated after modifying an Terraintile.


EDIT: I modified your code a bit. Now camera is looking from above.
Last time I used Worlddebug() Terrain also got physicsbodylines, but now it gets None Oo
I can´t figure out why...

Code: Select all

; ------------------------------------------------------------
;
;   PureBasic - Terrain : Terrain Height change
;
;    (c) 2012 - Fantaisie Software
;
; ------------------------------------------------------------
IncludeFile #PB_Compiler_Home + "Examples\3D\Screen3DRequester.pb"
#NbSommet = 36
Declare DoTerrainModify(tx, ty, wx.f, wy.f, wz.f, mBrushSizeTerrainSpace.f, height.f)
If InitEngine3D()=0 : End : EndIf
InitSprite()
InitKeyboard()
If Screen3DRequester()=0  : End : EndIf
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/GUI", #PB_3DArchive_FileSystem) 
Add3DArchive(#PB_Compiler_Home + "Examples\3D\Data/Packs/desert.zip", #PB_3DArchive_Zip)
Parse3DScripts()
;- Light
light = CreateLight(#PB_Any ,RGB(185, 185, 185), 4000, 1200, 1000, #PB_Light_Directional)
SetLightColor(light, #PB_Light_SpecularColor, RGB(255*0.4, 255*0.4,255*0.4)) 
LightDirection(light ,0.55, -0.3, -0.75) 
AmbientColor(RGB(5, 5,5))
;- Camera
CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0,  0, 500, -50, #PB_Absolute)
CameraRange(0, 1, 0)
CameraLookAt(0, 0, 0, 0)
WorldDebug(#PB_World_DebugBody)
;- ball; new
CreateSphere(0, 3)    ; new
CreateEntity(0,MeshID(0),#PB_Material_None,0,500,0); new
EntityPhysicBody(0,#PB_Entity_SphereBody)  ; new
; terrain definition
SetupTerrains(LightID(Light), 3000, #PB_Terrain_NormalMapping)
; initialize terrain 
CreateTerrain(0, 513, 12000, 600, 3, "TerrainHeight", "dat")
; set all texture will be use when terrrain will be constructed 
AddTerrainTexture(0,  0, 100, "dirt_grayrocky_diffusespecular.jpg",  "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, "terrain513.png", ty % 2, tx % 2)     
BuildTerrain(0)  
TerrainPhysicBody(0, 2, 1)  ; new
UpdateTerrain(0)
; SkyBox
SkyBox("desert07.jpg")

Repeat
  ExamineKeyboard() 
  ;MoveCamera(0, EntityX(0), TerrainHeight(0, CameraX(0), CameraZ(0)) + 10, EntityZ(0)-40, #PB_Absolute)
  ;CameraLookAt(0, EntityX(0), EntityY(0),EntityZ(0))
  If KeyboardPushed(#PB_Key_Space)
    DoTerrainModify(0, 0, EntityX(0),EntityY(0),EntityZ(0), 0.004, 30) 
  EndIf  
  RenderWorld()    
  FlipBuffers()       
Until KeyboardPushed(#PB_Key_Escape)   
End 

Procedure DoTerrainModify(tx, ty, wx.f, wy.f, wz.f, mBrushSizeTerrainSpace.f, newheight.f)
  terrainSize.f = TerrainTileSize(0, tx, ty) - 1
  Pointx.f = TerrainTilePointX(0, tx, ty, wx, wy, wz) 
  Pointy.f = TerrainTilePointY(0, tx, ty, wx, wy, wz)   
  startx = (Pointx - mBrushSizeTerrainSpace) * terrainSize
  starty = (Pointy - mBrushSizeTerrainSpace) * terrainSize
  endx   = (Pointx + mBrushSizeTerrainSpace) * terrainSize
  endy   = (Pointy + mBrushSizeTerrainSpace) * terrainSize  
  For y = starty To endy
    For x = startx To endx  
      SetTerrainTileHeightAtPoint(0, tx, ty, x, y, newheight)      
    Next x
  Next y
  UpdateTerrain(0)
EndProcedure

Re: Terrain collisions after modifying terrain

Posted: Mon Jul 14, 2014 12:59 pm
by Realizimo
Ok thanks anyway Bananenfreak.

Re: Terrain collisions after modifying terrain

Posted: Mon Jul 14, 2014 1:22 pm
by Comtois
It's a bug, it should be possible to update terrain's body. Should be fixed for next release.

Re: Terrain collisions after modifying terrain

Posted: Wed Jul 16, 2014 8:41 am
by Bananenfreak
@Alexi:
You said this many times and I answered with this: It´s a small team and not only 3D-side gets attention. Yes, I want much things, too, but they will do their best.
To whine is contraproductive.

Re: Terrain collisions after modifying terrain

Posted: Tue Jul 22, 2014 8:59 am
by Realizimo
Thanks Comtois
I am very glad when it's done.
You and fred do a great job.

Re: Terrain collisions after modifying terrain

Posted: Wed Jul 23, 2014 5:20 pm
by Comtois
It's done, just call TerrainPhysicBody(0, 2, 1) after UpdateTerrain(0) in DoTerrainModify().
It's done also for entity, call EntityPhysicBody() to change a body (for example after scaling an entity).

Re: Terrain collisions after modifying terrain

Posted: Wed Jul 23, 2014 5:54 pm
by Realizimo
Awesome, now I can continue with my project (a cargame)

Re: Terrain collisions after modifying terrain

Posted: Thu Jul 24, 2014 7:02 am
by Bananenfreak
Cool, thank you PB-Team :)

Re: Terrain collisions after modifying terrain

Posted: Mon Aug 04, 2014 8:55 am
by Kelebrindae
@Alexi:
You ask much, but give very little.
The statement seems harsh, so please let me explain. I'm sure you're a very competent coder, Alexi, and your posts in the "announcements" section sure are compelling. But I've never seen you post a single line of your own code here.
People have done a lot to help you or answer your questions (Samuel did a lot to help you with shaders, Comtois never fails to answer your questions about meshes, etc..), and yet, though your terrain system seems very advanced and would greatly benefit the community, you don't want to share your knowledge. Oh, so you " brought the library to the limits and above"? Then share the code, show us mere mortals how it's done and help Fred and the Team to get more of the well-deserved public attention they need to make PB bigger.

Believe me, we won't steal your work; We won't run away with it and claim the credits. It'll only encourage us to continue to help you.