liabru / matter-js

a 2D rigid body physics engine for the web ▲● ■
MIT License
16.81k stars 1.96k forks source link

ApplyForce with same parameters and to the same body doesn't have the same effect #328

Closed kozlown closed 7 years ago

kozlown commented 7 years ago

Hello Everyone,

First of all, thx a lot if you can help me :P

I'm creating a 2D Multiplayer Platformer , making jump my player, and the jumps doesn't have the same effect every times, I think that it's a problem of delta in Engine.update but it seems to be correct, tell me if I'm wrong :

    /**
     * @method start
     * @description Start the game
     */
    start() {
        // launch the main loop and save it, so it will be stopped when the game stop
        this.mainLoop = setInterval(this.step.bind(this), 1000 / 60);
        this.delta = 1000 / 60
        this.lastStepTimestamp = new Date().getTime()
        console.log(`Game started !`)

    }

    /**
     * @method step
     * @description Go to the next state of the game
     */
    step(){
        _.each( this.getPhysicalElementsOfType( "Player" ) ,(player)=>{
            player.move()

        })
        this.lastDelta = this.delta
        this.delta = new Date().getTime() - this.lastStepTimestamp

        Engine.update(this.engine, this.delta , this.delta/this.lastDelta); // Update the Engine

        // Send informations to the players
        _.each( this.getPhysicalElementsOfType( "Player" ) ,(player)=>{
            player.socket.emit("gameUpdate", this.getGameUpdateInfos(player))
        })

        this.lastStepTimestamp = new Date().getTime()
    }

ps : the repository of my project : https://github.com/kozlown/Platformer and if you want to see what it looks like : https://www.youtube.com/playlist?list=PL-UR3u1UOEVia-JyJGBsLQ0fPgR9zhN8N OR try the last version : http://213.246.52.56:8080/

liabru commented 7 years ago

Try changing to this to check if it is a timestep issue:

Engine.update(this.engine, 1000 / 60, 1);
kozlown commented 7 years ago

This is what I had before, I changed for the code above because I had issues, my delta was sometimes very different from 1000 / 60.

Here is the delta displayed with the code bellow (after 2-3 min running) :

 /**
     * @method step
     * @description Go to the next state of the game
     */
    step(){
        _.each( this.getPhysicalElementsOfType( "Player" ) ,(player)=>{
            player.move()

        })
        this.lastDelta = this.delta
        this.delta = new Date().getTime() - this.lastStepTimestamp
        if(this.delta > 20)
        console.log(this.delta)

        Engine.update(this.engine, 1000 / 60 , 1); // Update the Engine

        // Send informations to the players
        _.each( this.getPhysicalElementsOfType( "Player" ) ,(player)=>{
            player.socket.emit("gameUpdate", this.getGameUpdateInfos(player))
        })
        this.lastStepTimestamp = new Date().getTime()
    }

image

As you can see it's not fixed even if I ask my setInterval to execute it every 1000 / 60 msec, so I need the correction imo, but I have bugs anyway, do you have an idea about why this happends ?

ps: because it's a multiplayer platformer, I can't admit random simulation, it would totally destroy the game, and even more if it is competitive :/

kozlown commented 7 years ago

Edit: I found this issue #303 which looks to be the same as mine ^^

For example, here is the body.position.y for each step, describing the same body.applyForce executed twice from the ground (every body have restitution = 0).

1475.0500009720008 1475.0500008670008 1475.0500009720008 1475.0500009720008 1475.0500009720008 1475.0500008670008 1475.0500009720008 1475.0500008670008 1475.050000867001 1433.5833342003343 1400.966000867001 1369.128800867001 1339.855460867001 1313.017787867001 1289.8054312586678 1267.428531376168 1248.2184795371472 1229.867427393191 1213.4059278564328 1199.5033046609524 1186.490901329028 1175.1011181636998 1165.7489516014748 1157.313772294766 1150.2723519533927 1144.555002629088 1140.0955207709985 1136.7066514633032 1134.5702256209925 1133.6194423629129 1133.6881982677373 1134.6168875934547 1136.685937142231 1139.5200817361297 1142.9299392467665 1147.940956464537 1152.8543528678867 1158.389079451069 1166.2072438825091 1174.010142358104 1182.0047509861395 1190.571629182773 1199.1250226758636 1229.7492266231206 1241.4084488169592 1253.4567099011058 1265.874557931045 1277.883127135685 1290.158267880093 1303.4776153347623 1315.493064296483 1329.306575374322 1343.401410898269 1357.7635046460186 1372.3794937063808 1387.2366833137248 1404.119272343699 1429.8980387324887 1447.9875753320405 1463.4618761361785 1475.0500049492023 1475.0500009720051 1475.050001083001 1475.0500012000011 1475.0500008670012 1475.0500008670008 1475.0500008670008 1475.050001200001 1475.050000972001 1475.0500009720008 1475.050001083001 1475.0500012000011 1475.0500010830012 1475.050000867001 1475.0500008670008 1475.0500009720008 1475.050001083001 1475.050000972001 1475.050000867001 1475.0500007680007 1475.0500012000007 1475.050000867001 1475.0500008670008 1475.0500009720008 1475.0500009720008 1475.0500008670008 1475.050000867001 1475.0500009720008 1475.0500008670008 1441.4620008670008 1410.5254008670008 1382.1076308670008 1357.4775761170008 1336.2234095170008 1313.3290528213759 1294.7358496300947 1277.9393065983775 1262.015960372356 1246.4079838004443 1234.6715427186257 1224.388923690898 1215.0178186688897 1205.9545106028304 1199.5463883204677 1194.6638786060146 1190.7246482461824 1187.9543794043418 1186.3358326379127 1185.6652132098052 1185.8951247531031 1187.098388717244 1191.4630510623597 1194.7607164717947 1199.0497799130499 1204.4337574194196 1210.2513371751525 1216.3379990115461 1222.98732775612 1229.236029792013 1238.3510523708105 1247.1163966756824 1255.8478583714425 1265.6026816065894 1275.2219257870129 1285.2272077584153 1295.5992256312475 1307.00425537392 1318.8110336294587 1330.9994729722207 1343.5504903478445 1357.9987864665586 1371.324079648059 1385.7693875328414 1398.9148992349064 1412.2701353518682 1425.8246096629819 1439.5683602585398 1453.4919233243197 1469.358471167692 1475.0500086620368 1475.0500008670087 1475.0500009720008 1475.0500009720008 1475.0500008670008 1475.050000867001 1475.0500009720008 1475.0500008670008

1133.6194423629129 is the maximum point of the first jump 1185.6652132098052 is the maximum point of the second jump

I'm a bit lost right now and I don't know how to fix that ...

kozlown commented 7 years ago

Okay so here is how I solved my problem, the problem wasn't coming from the library, the problem was coming from the setInterval primitive, I had already read that calling setInterval was a bad idea, but I realised that nobody should use it, the step 15 can be called after the step 16 because of async.. It can generate dirty things with a physics engine like matter-js.

Well, so now I'm using reissue which is using setTimeout and because it uses recursivity, next step isn't called while the one which is just before isn't finished :)

Hope it can help some people in the same situation as mine, maybe @liabru you should put it in the documentation so people like me (well, maybe i'm the only one xD) don't waste their time on this obvious mistake.