Sme Posted November 8, 2018 Share Posted November 8, 2018 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 Quote Link to comment Share on other sites More sharing options...
Wingnut Posted November 8, 2018 Share Posted November 8, 2018 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. NasimiAsl and JohnK 2 Quote Link to comment Share on other sites More sharing options...
JohnK Posted November 8, 2018 Share Posted November 8, 2018 @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. Quote Link to comment Share on other sites More sharing options...
Wingnut Posted November 8, 2018 Share Posted November 8, 2018 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. JohnK 1 Quote Link to comment Share on other sites More sharing options...
Sme Posted November 8, 2018 Author Share Posted November 8, 2018 Awesome, it works great, thanks! Quote Link to comment Share on other sites More sharing options...
Wingnut Posted November 8, 2018 Share Posted November 8, 2018 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) :) Quote Link to comment Share on other sites More sharing options...
Sme Posted November 8, 2018 Author Share Posted November 8, 2018 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) GameMonetize and Wingnut 2 Quote Link to comment Share on other sites More sharing options...
Wingnut Posted November 8, 2018 Share Posted November 8, 2018 *nod*. Good to have you with us, my friend. I hope we get smooth data pipes between/across all of our countries... in the near future. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.