Ground Collision Issues

The documentation is rather sketchy here. I’ve got a 100x100 heightmap that is generated by my “terrain” module, which is passed into a GeoMipTerrain, transformed into a terrain, textured, and rendered.

terrain=maketerrain("fractal")
terrain.generate()
environ=terrain.getRoot()
grasstex = loader.loadTexture("grasstex.png")
grasstex.setWrapU(Texture.WMMirror)
grasstex.setWrapV(Texture.WMMirror)
grasstex.setAnisotropicDegree(32)
environ.setTexScale(TextureStage.getDefault(), 150, 150)
environ.setTexture(grasstex)
environ.reparentTo(render)
environ.setScale(20000,20000,20000)
environ.flattenLight()
environ.setPos(0,0,-3000)

Some of this is placeholder, such as the single texture. Anyway, my character has no collisions, and walks through the hills and whatnot generated by the terrain generator. I wanted to use the getElevation method to set the actor’s Z to the terrain’s Z at his current position, but am having trouble getting that worked out.

I read various forum posts and documentation and came up with this so far:

		elevation=terrain.getElevation(base.camera.getX(),base.camera.getY())*environ.get_sz()
		self.actor.setZ(elevation)

This particular code complains due to the lack of a get_sz() method, but in the documentation it is recommended to do almost this exact code to get the elevation… Confusing.

I realize that the X/Y of the camera will not be in pixel value, and will thusly break my code’s efficacy. How can I translate the character/camera’s current position into a pixel value on the heightmap?

Ideas? Suggestions?

This game is going to have very high-end options, and many other collisions by the time it is completed. What sort of system would you recommend for the absolute most realistic collision between models? Is there a literal volumetric model collision detection I could use, taking the entire shape of the model into account?

get_sz is the c++ function name, python function names are in camelcase, so use getSz() instead. This is more or less the same as getScale().getZ() though.

Actually, you don’t need to do a getSz if you use panda’s relative setPos methods (see bottom of this post for example)

The x and y positions are more or less translated 1:1 by the GeoMipTerrain, so you can just grab a pos relative to the terrain like this:

newPos = yourModel.getPos(environ)
# Now, you can use it to grab the elevation of the terrain:
elev = terrain.getElevation(newPos.getX(), newPos.getY())
# We can now use it to put yourModel on the terrain:
yourModel.setZ(environ, elev)

(The getSz is only needed when you don’t want to use relative setPos calls for some reason.)

Ty, testing it out in a min.

Dunno wut I’d do without u and drwr :slight_smile:

Not working…

I think it’s because the model is scaled up some 20 thousand times for the terrain, as when I move forward or back, the character moves up and down pretty quick.

I have tried removing the relative environ from the last line, adding various scale references to the elevation line.

Here’s what I have this second that doesn’t crash but doesn’t work:

	def stabalize(self,task):
		curpos=self.actor.getPos(environ)
		elevation=terrain.getElevation(curpos.getX(),curpos.getY())*environ.getScale().getZ()
		self.actor.setZ(environ, elevation)
		return Task.cont

As you can see, it is a recurrant task, that is added in the character class’s init.

What about this then:

   def stabalize(self,task):
      curpos=self.actor.getPos(environ)
      self.actor.setZ(environ, terrain.getElevation(curpos.getX(),curpos.getY()))
      return Task.cont 

Lol that’s the same code with one less variable assignment >.>

No, it does the same thing.

It’s not the same, I’m not multiplying by the scale anymore.
You should be able to see a change. What is the exact thing happening when using this code?

When I hit W the character dips down into the ground, going under the terrain without hardly moving forward, and S is vice versa. It’s like I’m on a steep hill.

The character may not start ON the terrain, btw, it may be a few pixels off of it. Could that cause an issue this gratuitous? tries moving the character at start

With:

		#Starting Position
		self.actor.setPos(environ,-1000,-1000,0)

or a bit different but equally puzzling results at (environ,10,10,0),

the character walks up to an arbitrary height, then back down (and down to infinity), if W is held, or S if on the other side of imaginary hill.

BUMP

Oh, sorry.

I tried a few test scripts here but can’t reproduce it – could you send me some test code?