schteppe/cannon.js

Heightfield-relative issues (scaling & collisions not detected)

Closed this issue · 4 comments

Hi,
I need some suggestions about how to transfer my terrain to cannon.

Basically I have a pretty large terrain of more than 1000x1000 in world units, that I draw using 100x100 vertices (so I have 1 vertex every 10 world units ^2) and its data is obtained through what I call a "height function", basically a h(x, z) that returns the height at the given point, I don't have any 1000^2 array of heights.

I need to transfer this terrain to cannon, but the API doesn't permit me to do that as I would:

  • I don't want to generate a 1000^2 array of heights in order to create the Heightfield. And that seems the only way I have to generate the terrain loyally to what I have on screen.
  • Every entry in the data array given to the Heightfield, is one step in world units, so I have no chance to give, like, a 100^2 data array and make it extend to 1000^2, as my real terrain is. I didn't find any way to scale the Heightfield (or generally, any shape), is it possible?

Not to ask anything else, basically the Heightfield's constructor I'd like is like that:

constructor(width: number, depth: number, height_fn: (x: number, z: number) => number, precision: [number, number] = [1, 1])

Where:

  • width is the width of the Heightfield in world units.
  • depth is the depth (or height) of the Heightfield in world units.
  • height_fn is the height function described above.
  • precision is the step that should be took for creating the ConvexTrianglePillar (I've skimmed the ConvexHeightfield collision resolver). Currently is 1 world unit but in my case I'd need to have larger distances.

I've seen the project isn't updated since 5 years, so I'll potentially try to implement these features on a forked repository.
Hope I've been clear!

arpu commented

a compatible rewrite is in https://github.com/pmndrs/cannon-es from @codynova

You can try to use ammo.js

I've found out there was an option called elementSize that permits me to control the size of the single sample on the XY direction.

However I've encountered an issue that I'm currently investigating (actually it's the whole day I'm investigating it): for some reason the Heightfield collisions aren't detected. I'm using CannonDebugRenderer and all shapes seem to correctly intersect. The weirdest thing is that I transcribed this example to Typescript and it doesn't work (some shapes pass through the heightfield). If I run the same on Javascript directly from its repository works. Maybe I'm using a wrong version of the cannon dependency? That's weird since I'm mirroring it directly from github "cannon": "github:schteppe/cannon.js".
I've also tried using the fork that @arpu suggested me, but it still gives me the same problem!

Finally obviously that was my fault, I was changing for some reason the Heightfield type to a number that didn't match any Shape.types.