Question about mesh movement

Hi, I am starting to learn 3D math and I have a question in regards to using it with Babylon. To make my mesh move, I need to set a velocity and update its velocity. Can I add a new property on the mesh like mesh.velocity = new BABYLON.Vector3(1, 0, 0); or is there a better way of setting the velocity of a mesh?

Hi, @freetoplay , yes, you can add this property, because engine based on javascript so it is dynamically typing: you can add whatever you want and change whatever you want during runtime, but this is a bad habit because of some type reasons, like code support in huge projects.
So, if you want to add new property, you can use it with engine.deltaTime, this will give you time from previous render and multiplying this time will add exactly needed velocity for current frame.
Additionally, you need to use: scene.registerBeforeRender, to update mesh position through velocity each frame render Babylon.js Documentation
Better way to do this, is use BABYLON method mesh.translate, wich takes as argument amount of needed distance to be passed: Babylon.js Documentation

Third way you can do this, through BABYLON Animations, they are build specially to change data of the mesh, just use loopmode as constant and mesh will stop in animation last frame.

This is closest to my hand example:
If you want to do something on animation end, you can use it like:

let someVarName = scene.beginDirectAnimation(...);
 someVarName.onAnimationEnd = function(){
//what you want to do

Thanks @logunov! I have a follow up question on this. If I wanted use a character mesh and have it walk some distance after some key input, which one of the 3 options that you listed above would be best?

@freetoplay, it all depends on what exactly you want and which character you have.
For example, if your character is complex high-polygonal model, that means translate method will move only the pivot of the model, this mean it will move like sliding in world space without any animations.
If this enough, ofcourse, you can use 1st or 2nd method. If we looks deep inside engine, second method is something like first, but integrated in BABYLON Architecture, but, of course, both have some variations. For example, you can use dynamic typing for better code control, but in cost of readeable, code supporting and with chance to screw up data (not because of you, but because of dynamic typing).

Otherwise, you should use animation. Take a look at this perfect example:
This example illustrates, how to animate (not move) game object.
This is good documentation, which explains, how animations works:

If you want to add changing of coordinates to the character, then you don’t need to synchronize changing of position with animation state, you can combine first and second methods with animations, to get needed result.

Here is another example of simple movement, which means there is no animations, but sliding in world space:

And in the last: simple tutorial how to make car movement (step-by-step)


@logunov thanks! This is really helping me understand Babylon better. Some more questions regarding this. For a walking character mesh (low poly), could I combine something like what they have at with the animation demo you showed above?

Yes, sure you can combine it with, that what I meant when said you can add changing of coordinates: any movement in world (same as global) space is changing of coordinates.

1 Like