Hi, good evening
the context is : let’s suppose the character is walking within a bigger 3ds max scene map (loaded using gltf), while the scene could be a plane ground but somewhere there must be a place that has steps that needs to be go up and go down ?
can someone give a suggestion even with a playground link about this ? thanks very much!
Hi moxierichard,
Is your question about how to keep a character a certain distance above a mesh such that it appears the character is walking on the mesh? There are several common approaches including the use of physics engines, but one of the easiest approaches is to use raycasts to detect what meshes are below the character and how far away they are. This code from the Babylon team’s old Kart Racer demo shows an example of this usage.
Hi, Syntheticmagus:
Thanks for you reply.
I mean the situation as a character have to go upstairs and downstairs, how is he able to lift and lower his position.y so that it looks like he is going upstairs or downstairs? does he has to hit a ray in front of his foot to detect in every frame ?
or maybe he does not have to do anything by means of physics imposor? he is a character mesh loading from 3ds max scene, and he is in fact a child bounding to a transform node, but the transform node is dummy and invisible, so the tranform node is not with mass. in such situation, how physicis take effect ? (as I understand , you must have at least some mass quantity, then the physics rule can have effects)
Hi moxierichard,
I think there are a few things to consider here. If you’re trying to use a physics engine (which is possible, but not required), then you may want to attach an imposter to your character to give physical properties. If you do that, the imposter will be a separate bit of logic that has nothing to do with what’s visible or not; it’s purely for physics, not for rendering.
However, especially for characters, you often don’t want to use direct kinematic physics because doing so can have unintended and frustrating effects. For example, characters represented by angular impostors (like rectangular solids) can snag and get stuck on things in the scene, like tiny bumps in the road; likewise, characters represented by rounded imposters (like ellipsoids and capsules) often end up sliding off things and have a notoriously difficult time with inclines like ramps and stairs. For this reason, in complex situations, character locomotion tends to use physics tricks and practices that are almost unique to this problem; and in simple situations, character locomotion is often done without the use of a real physics system at all.
I would again encourage you to take a look at the raycasting code I linked to in my first answer, as I think that does precisely what you’re trying to do. If you want your character to remain a certain distance above the ground and “lift and lower his position.y” to maintain the illusion of being on the floor, raycast downward from the character’s centroid to detect how far away the floor is, then set the character’s position variable so that the character is the desired distance from the floor. This is what the code in the Kart Racer demo does.
Good luck!
Hi, Mr. syntheticmagus
Thanks for you suggestion! As you said,physics deos not necessarily have to do with position translation and it could potentially bring up some unexpected effects and hence stuck to a confusing situation. I 'll try to implement raycasting by ray downcasting and forwardcasting.
By the way, I noticed the Espilit Demo, I am sure you have seen it. The demo is amazing, because it looks like the camera/character knows when to start going upstairs and downstairs, and exactly stop going up or down once the character/camera come another floor. Did the author use the raycasting? In fact, I have downloaded the whole website of babylon, and I have run the Espilit demo locally, but I failed to find out its source code about how to make the camera move upstairs or downstairs so easily. what I found was are a index.html, a demo.js, a loader.js within the build/js folder, those did not tell what hides beneath such a easy implementation. If you know something about how Espilit made it, please let me know.
Hey @moxierichard, have you looked into Cameras, Mesh Collisions and Gravity - Babylon.js Documentation (defining an ellipsoid for your character mesh and using moveWithCollisions
)?
Could you check out the Playground from this Question post: Raycast to check if player is standing on ground? It uses WASD for movement and SPACE for jump and utilizes moveWithCollisions
and raycasts to check if the character is on a floor.
Edit: Added a slide for you
https://www.babylonjs-playground.com/#3EDS3A#16
Also, I don’t remember what was in the Espilit soure code, however it makes sense that raycasts weren’t used, since there was no need to check if the player was on a ground (e.g. for jumping logic). They would only need to use moveWithCollisions
.
Here’s an example using GJK+EPA from: collision-gjk-epa/collision-gjk-epa.js at master · wanadev/collision-gjk-epa · GitHub
And the BVH from: js3d/broad.js at master · d07RiV/js3d · GitHub
https://playground.babylonjs.com/#Z6PR92#7
(WASD-Space). Has a bit of triangle-crawling, though.
Due to EPA, I’m simply using a low-poly capsule created in Max.
I have examples using Minkowski Portal Refinement as well as this gem from Ben Kenwright: http://www.xbdev.net/misc_demos/demos/minkowski_difference_collisions/paper.pdf
(It finds the distance and closest points between disjoint shapes, and depth, points and normal between inter-penetrating shapes).
I prefer EPA, though, as MPR doesn’t necessarily find the best normal, and might transport you huge distances with long, thin triangles/shapes.
And Bens simplified version has other weird issues. (Might very well have been introduced by me, though)