0

Hi i am trying to move a spite from the top of the screen to the bottom. I want to achieve a gravity like effect. To pre-empt people suggesting using a game engine, A: this js is running on a node.js server and not a client (you may suggest a game engine for node) and B: this is the only place i need to use a gravity effect so i feel that surely it is simpler just to make a loop with some kind of acceleration calculations inside?

In this example i don't need to do anything except the calculations.

var theMeteor = {
"x":500, //the start x position
"y":1000, //the start y position
"v":1 // the velocity
};

function MeteorFall(dt){

theMeteor.y += (theMeteor.v * dt) * -1; // move the meteor down
theMeteor.v++;// increase the velocity

// keep looping until its at the bottom
if(theMeteor.y <= 0){
// its at the bottom so clear the loop
clearInterval(theDeamon);
}

}

var dt = 0.025; //set this to whatever the loop refreshes at (0.025 = 25)
var theDeamon = setInterval(MeteorFall, 25, dt);

This works but it's not very good at all, is there any one who can show me how to do this correctly please?

  • 2
    What part is "*not very good*," what's the problem you're having with this current implementation? – David Thomas Jun 27 '18 at 10:59
  • How can i slow the acceleration down a little or like throttle it so it looks more realistic. It starts very slow and very quickly its going super fast. I want it to simulate gravity as best as possibe – David Coleman Jun 27 '18 at 11:04
  • Change relation between velocity and acceleration level. – MBo Jun 27 '18 at 11:16
  • I changed this; theMeteor.v++; to this - theMeteor.v += 1.5 - it seems to be sufficient. – David Coleman Jun 27 '18 at 11:44
  • "not very good at all" - I think your understanding of the physics is the problem. Your equations aren't correct. – duffymo Jun 27 '18 at 11:56
  • 1
    @DavidColeman why not `theMeteor.v+=9.81*dt;` where 9.81 is the gravity acceleration on surface of Earth in `[m/s^2]` if you use different then SI units convert the value to your units ... see these: [How to calculate motion in openGL](https://stackoverflow.com/a/46110339/2521214) and [Is it possible to make realistic n-body solar system simulation](https://stackoverflow.com/a/28020934/2521214) – Spektre Jun 28 '18 at 07:18

0 Answers0