Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a terrain request #18

Open
x1911 opened this issue Sep 16, 2023 · 3 comments
Open

a terrain request #18

x1911 opened this issue Sep 16, 2023 · 3 comments

Comments

@x1911
Copy link

x1911 commented Sep 16, 2023

First of all, thats a really really great project.

I'd like to ask if you could provide the code for Havok terrain?

I would be incredibly grateful.

@lo-th
Copy link
Owner

lo-th commented Sep 16, 2023

Hi thanks
for havok terrain work like other body you just need this shape

const shape = havok.HP_Shape_CreateHeightField( divid[0], divid[1], scale, heights.byteOffset )[1]
const body = havok.HP_Body_Create()[1];
havok.HP_Body_SetShape( body, shape  )

divid is number of square ex [ 128,128 ]
scale is size of terrain ex [ 200, 3, 200]
heights is array of all point height you need create _malloc array ( ar is your array of y position )

const nFloats = ar.length
const bytesPerFloat = 4
const nBytes = nFloats * bytesPerFloat
const bufferBegin = havok._malloc(nBytes)
heights = new Float32Array( havok.HEAPU8.buffer, bufferBegin, nFloats )
let i = ar.length, n = 0
while(i--){  this.heights[i] = ar[n++];   }

@x1911
Copy link
Author

x1911 commented Sep 18, 2023

Thank you very much.

It seems that the movement of your terrain is experiencing significant stuttering or lag.

This is my code

        const data = [
            [0.24, 0.5, 0.25, 0.25, 0.25, 0.15, 0.1, 0],
            [0.24, 0.5, 0.25, 0.25, 0.25, 0.15, 0.1, 0],
            [0.24, 0.5, 0.25, 0.25, 0.25, 0.15, 0.1, 0],
        ]

            const scale = new THREE.Vector3(5, 8, 3)
            const numXSamples = data[0].length
            const numZSamples = data.length

            const nFloats = data.length;
            const bytesPerFloat = 4;
            const nBytes = nFloats * bytesPerFloat;
            const bufferBegin = havok._malloc(nBytes);

            const heights = new Float32Array(havok.HEAPU8.buffer, bufferBegin, nFloats)
            let i = arr.length, n = 0
            while (i--) { heights[i] = arr[n++]; }

            const ss = [scale.x, scale.y, scale.z]
            const shape = havok.HP_Shape_CreateHeightField(numXSamples, numZSamples, ss, bufferBegin)[1]
            

        const { position, quaternion: q } = mesh

        const boxBody = HK.HP_Body_Create()[1];
        HK.HP_Body_SetShape(
            boxBody,
            shape
        );
        HK.HP_Body_SetQTransform(
            boxBody, [
            [position.x, position.y, position.z],
            [q.x, q.y, q.z, q.w]
        ]
        )
        HK.HP_World_AddBody(world, boxBody, false);

@lo-th
Copy link
Owner

lo-th commented Sep 22, 2023

about lag yes i have to experiment
height data should be update not recreate on every frame.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants