Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
R3d.HeightFieldShape — Gideros Forum

R3d.HeightFieldShape

MoKaLuxMoKaLux Member
edited March 2021 in General questions
I need some help please :)
In reactphysics3D I would like to use the HeightField shape. It is working great for the collision shape (r3d debugger view) but my problem is how do I build its mesh?
I have tried many things but I don't know how r3d builds its collision shape so I can build the mesh on top of it.
https://wiki.gideros.rocks/index.php/R3d.HeightFieldShape.new
This is the code for the HeightField shape (for collision):
	local nbc, nbr = 8, 8 -- number of columns, number of rows
	local minh, maxh = 0, 2 -- min height, max height
	local ht = { -- heights table example
		0,0,0,0,0,0,0,0,
		0,1,1,1.5,0,0,0,0,
		0,1,1,1.5,1,0.5,0,0,
		0,0,1.5,2,2,2,0,0,
		0,0,0,2,2,2,0,0,
		0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,
	}
	local mesh = HeightField3Db.new(nbc, nbr, ht) -- problem building the corresponding mesh
	local view = Viewport.new()
	view:setContent(mesh)
	view.body = xworld:createBody(view:getMatrix())
	-- the collision shape
	local shape = r3d.HeightFieldShape.new(nbc, nbr, minh, maxh, ht) -- collision is ok but I need to build its mesh!
	shape:setScale(5,5,5)
And my attempt at building the HeightField mesh:
local HeightField3Db=Core.class(Mesh3Db)
 
function HeightField3Db:init(nbc, nbr, ht) -- num of cols, num of rows, heights table
	local va,ia,na={},{},{}
	local i, j = 1, 1
	for y = 1, nbr do -- XXX I am lost for the index array :-(
		for x = 1, nbc do 
			local a = y*nbc+x
			ia[i+0]= j
			ia[i+1]= j+1
			ia[i+2]= a
			ia[i+3]= j
			ia[i+4]= a
			ia[i+5]= a-1
			j += 1
		end
	end
--	na={ 0,0,-1, 0,0,-1, 0,0,-1, 0,0,-1, 0,0,1, 0,0,1, 0,0,1, 0,0,1, }
 
	i, j = 1, 1
	for y = 1, nbr do -- the vertex array is fine :-)
		for x = 1, nbc do
			va[i+0]=x
			va[i+1]=ht[j]
			va[i+2]=y
			i+=3
			j+=1
		end
	end
 
--	self:setGenericArray(3,Shader.DFLOAT,3,24,Box3Db.na)
	self:setVertexArray(va) -- good
	self:setIndexArray(ia) -- bad
	self._va=va self._ia=ia
end
Could somebody help please?
my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
Tagged:

Comments

  • here is a pic of the height field shape from the code above:

    And I join a sample project.
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • MoKaLuxMoKaLux Member
    edited March 2021
    I cannot post the file :(
    error: "Cannot use object of type stdClass as array"

    I posted the project on GH: https://github.com/mokalux/gideros-3D-FP-forum
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • MoKaLuxMoKaLux Member
    edited March 2021
    duplicate
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • MoKaLuxMoKaLux Member
    edited March 2021
    duplicate
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • hgy29hgy29 Maintainer
    Accepted Answer
    MoKaLux said:

    	local i, j = 1, 1
    	for y = 1, nbr do -- XXX I am lost for the index array :-(
    		for x = 1, nbc do 
    			local a = y*nbc+x
    			ia[i+0]= j
    			ia[i+1]= j+1
    			ia[i+2]= a
    			ia[i+3]= j
    			ia[i+4]= a
    			ia[i+5]= a-1
    			j += 1
    		end
    	end
    Could somebody help please?
    Looks like you are not incrementing your 'i' variable in the loop, shouldn't you add a 'i+=6' in it ?

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • superb, thank you hgy29.

    Almost there :smile:
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • MoKaLuxMoKaLux Member
    edited March 2021
    one more question please :)
    Do you think we can calculate the normals for the texture on this shape?
    EDIT: this should be working
    	-- normals
    	i, v = 1, 1
    	for y = 1, nbr do
    		for x = 1, nbc do
    			na[i+0]=0
    			na[i+1]=0
    			na[i+2]=-1
    			i+=3
    			v+=1
    		end
    	end
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • ok I have pushed gideros and r3d a little bit further :)

    we can walk on terrains.

    I am stuck here because I need to retrieve the normals of the terrain and it's complicated. I have read that r3d automatically calculates normal of the terrain but I don't know how it does it (it's maybe in the GH source somewhere).

    That would be nice to have a function in gideros that returns this information so we can use it when building the mesh ;) and for texturing as well.

    I will tidy up the code and put the new terrain on GH.

    PS: I also have R3d.ConcaveMeshShape working (but still a mess with the normals too).

    Will try to work on it later...
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
Sign In or Register to comment.