schteppe / cannon.js

A lightweight 3D physics engine written in JavaScript.
http://schteppe.github.com/cannon.js
MIT License
4.63k stars 700 forks source link

Heightfield-relative issues (scaling & collisions not detected) #450

Closed loryruta closed 3 years ago

loryruta commented 3 years ago

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:

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:

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 3 years ago

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

OliverXH commented 3 years ago

You can try to use ammo.js

loryruta commented 3 years ago

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!

loryruta commented 3 years ago

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.