I’m trying to understand how to create a rocket that would consistently fly using physics forces. From my understanding forces would be used for quick one-off events like an explosion but velocity would be used for more constant movements in a specific direction.
When I use SetLinearVelocity(new Vector3(0,50,0) the rocket I’m using shoots up but then comes back down due to gravity. Does linear velocity move the rocket to (0,50,0) then stop, or is there something I’m missing here? How would I go about creating a constant velocity if I wanted to have the rocket continually flying upward?
The glib answer is that it’s easy: just keep pushing the rocket upward!
The actual answer isn’t that far removed from the glib, however - just like a real rocket has to maintain thrust or else it will not go to space today, so does your simulated one. This recent post discusses a similar related question.
Setting linear velocity is a one-off effect, after that the forces in the simulation will affect the frame by frame instantaneous velocity. You can set the velocity directly each frame if you want a constant velocity, or you can apply a constant force each frame to get constant acceleration. Your choice for your needs!
Thanks, that’s what I figured. So in that case what would be the main difference between updating the velocity per frame versus the force? I know technically they do different things, but in this sense couldn’t they be used to apply propulsion to the rocket?
Ok so I updated my rocket method using scene.beforeRender. If there’s a better way to approach this I’d like to know. I consider the “beforeRender” similar to the Update function in Unity.
It looks like you want a fixed speed and rotation. I don’t know if it’s a better way, but i have a different way that is more event based, so maybe every time a button is clicked or key pressed if you wanted to propel your rocket up and then it’s not frame based. If you applyImpulse each frame the rocket would suddenly be going super fast, so you could only apply if speed is below a max velocity perhaps. Anyway, I don’t know if you are trying to do that.
Applying a force per-frame affects the linear/angular velocities according to the mass of the body, the direction of force, and the distribution of matter (ie the shape of the physics impostor). The instantaneous linear velocity is computed based on the forces affecting the object, whereas setting the velocity bypasses those messy Newtonian laws of motion and just specifies the end result directly.
Thanks. For this specific example, the rocket would essentially start off moving. I could see the impulse mechanic being useful for a “boost” type feature.
Thanks. Makes more sense now that the velocity and force methods would have quite a vast difference in the values needed to move an object. So velocity would be used to move an object around and still be affected by physics, while the force takes into account the weight/shape of the object as well.
If you really just wanted to move at a fixed speed and direction/rotation - it can be done without a physics engine at all. You only need a velocity and direction and then you adjust the position with animations or instead of by frame by time between the last frame: Engine | Babylon.js Documentation (babylonjs.com)