Sign in to follow this  
Sme

How to create solid ground with heightmap

Recommended Posts

I'm new to BJS, so I apologize if this is such a basic question. I have created a ground with a heightmap, and I am simply wondering how can I make sure all entities remain on the ground? Note that there is no physics in my game, so no jumping, etc. In other words, entities will always be 'stuck' to the ground. Right now when creating entities, I am using Vector3 and setting their Y value, but since I am using a heightmap this Y value obviously needs to be dynamic, depending on its current location on the ground.

I was looking here at this link https://doc.babylonjs.com/babylon101/cameras,_mesh_collisions_and_gravity , it seems like I can achieve this by setting the scene's gravity, and setting ground.checkCollisions = true. Considering my use case here is very basic (no physics, entites stuck to ground, etc.), is this still the proper way to handle this scenario?

Thanks

Share this post


Link to post
Share on other sites

Hiya SME, and welcome.  You have asked a very good question.

Although scene.gravity IS used on physics engines IF no physics engine gravity is also set, scene.gravity is only used for a very specific built-into-BJS collision system.  It is designed for first-person shooters, where a freeCamera is the player (no player mesh).  We can talk about it more, later.  Generally-speaking, scene.gravity is only used on freeCameras that have .applyGravity = true.  It is not used on mesh.

Somebody invented a mesh.moveWithCollisions() that CAN be used with that FPS system.... for simulating gravity on a mesh.  I have never tried it, and I don't have a playground demo handy.

But I DO know of a much easier way.

http://jerome.bousquie.fr/BJS/demos/getHeightAtCoordinates.html

GetHeightAtCoordinates() is a VERY HANDY feature.  Here is a playground...

https://www.babylonjs-playground.com/#BLAJPA#4

You can see the getHeightAtCoordinates() in line 37 of the renderLoop.

This is ONE way of doing it.  I hope this helps.  Let us know what you think, and stay tuned for more possible comments/ideas.

Share this post


Link to post
Share on other sites

@Wingnut you beat me to it. Was in the middle of producing a solution when I had to go out. Good job you did.  🙂 My solution would have involved forward probes, rays and a lot of unnecessary calculations.

 

 

Share this post


Link to post
Share on other sites

:) Sorry John.  I keep forgetting the name of getHeightAtCoordinates()... so it took me a LONG time to find it again.

Don't throw away your solution, yet.  SME might need something different.  We'll listen to what he/she thinks.

Side note:  Recreational weed recently became legal... here in Michigan.  So, it's likely that I will forget MANY MANY things, now.  :D

Share this post


Link to post
Share on other sites
11 minutes ago, Sme said:

and adding that to it's z-index to compensate

Actually, you add it to the box.position.y  :)

Notice line 39.  We do some boundingBox measuring...

box.getBoundingInfo().boundingBox.extendSize.y

SO... everytime you position a new box, you could do...

box.position = new BABYLON.Vector3(someX, box.getBoundingInfo().boundingBox.extendSize.y, someZ);

You could also wait for scene-ready, and loop thru all the boxes (scene.mesh)... and set each box.position.y that way, too.

It might be handy for ALL mesh to have a mesh.groundIt() function, eh?  Or maybe mesh.placeAtop(ground);  :)

Maybe you could code this for yourself, and maybe become famous... when it is accepted for BJS-wide core-use.

You could get your "I contributed to BJS Core" merit badge!  :)   It comes with a new car!  (not)  :)

Share this post


Link to post
Share on other sites
4 minutes ago, Wingnut said:

Actually, you add it to the box.position.y  :)

RIght, and actually the issue was with my code, and not with the call to getHeightAtCoordinates. Nonetheless thanks for the quick response

 

Edit: Sorry, I can't get the PlayGround to load right now, so I can't check out the source code (I'm in China so my internet isn't the quickest at times)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.