I’m using Cannon.js to simulate a rocket launch by applying a varying force upwards to a cone mesh.
I’ve got graphs printed of position, velocity and thrust but the position and velocity ones change depending on the device.
Here’s the live page : http://adam.teaches.engineering/Kerbciniak-Space-Program/
On my phone, the max height goes up to about 20 m. On my computer, it goes to about 70-80.
I’ve got an array of force values over time which you can see printed in the top graph. The force was originally measured at an interval of 1000 samples per second. I think this is where something is wrong because in each render loop, the index goes up by one which as far as the force array is concerned, has just happened in 1/1000 th of a second. But the renderloop is actually occurring much slower than that and so I’m using an index value that is based on the time between render loops. I’m not sure how to tackle this?
Here’s a piece of code from the render loop:
let count = 0;
const startTime = Date.now();
engine.runRenderLoop(function () {
count += 1;
const timeDiff = Date.now() - startTime;
const i = Math.round(timeDiff)
var forceDirection = new BABYLON.Vector3(0, 1, 0);
var forceMagnitude = slicedRocketData[i] < 1 ? 0 : slicedRocketData[i] ;
var contactLocalRefPoint = BABYLON.Vector3.Zero();
particleSystem.emitRate = 1000 * forceMagnitude;
var dragDirection = new BABYLON.Vector3(0, -1, 0);
var dragMagnitude = CD * rho * (1/2) * cone.physicsImposter.getLinearVelocity().y ** 2;
cone.physicsImposter.applyForce(forceDirection.scale(forceMagnitude), cone.getAbsolutePosition().add(contactLocalRefPoint));
cone.physicsImposter.applyForce(dragDirection.scale(dragMagnitude), cone.getAbsolutePosition().add(contactLocalRefPoint));
//console.log(`index = ${i} Force = ${forceMagnitude} velocity = ${cone.physicsImposter.getLinearVelocity().y} position = ${cone.getAbsolutePosition().y}`);
scene.render();