flame icon indicating copy to clipboard operation
flame copied to clipboard

Physics problems

Open rezendegc opened this issue 6 years ago • 5 comments

Hello, i have been messing around with flame with box2d components for some days and and I have been having a lot of problems. On my first project i tried applying physics in many ways and never had the expected results, so i'll leave my project here (it has many things not relevant to the problem, and i can create a simpler project for debug purpose if it turns out to be the engines problem and not mine).

So, the first problem i had was that even without friction the body decelerates when it's on air. I tried applying a vertical force (body.linearVelocity = Vector2(50, 0), for example), but when the player spawned in air, it lost almost all velocity before hit the ground.

The second problem is that applying vertical forces (body.applyForceToCenter(Vector2(0,500))), for example) would cancel the horizontal velocity that the body had. Also appying a zero force (body.applyForceToCenter(Vector2(0,0))) would also slow down the body.

The third and most problematic problem i had was that on slower devices the forces weren't concise. When running on debug mode on my emulator the force would sometimes be stronger (player would make bigger jump), sometimes smaller (player would make a very small jump), kinda randomly. This was also problematic when dealing with huge forces or gravity, for instance, when i applied a force of 10000 or 10000000, for example, the height that the player reached were the same.

Well, hope anyone can help me with these issues and tell me what i have been doing wrong... The repository to the project is here: repo

rezendegc avatar May 01 '20 02:05 rezendegc

Hey, body.linearVelocity = Vector2(myWorld.playerSpeed * (forward ? 1 : -1), body.linearVelocity.y); don't set the linearVelocity directly in the update method if you are using forces and impulses at the same time, it will give you a lot of trouble. You could set it when you create the body, but then after that you should manipulate in with forces and impulses instead. Not sure if that will solve any of your problem though, but try!

I think that your player will jump higher depending on the size of the screen, not depending on the speed of the mobile cpu. Could you try if it works consistently if you remove viewport.size.height here and set it to something static?

body.applyForceToCenter(Vector2(0, viewport.size.height / 8)..scale(10000));

spydon avatar May 01 '20 08:05 spydon

I have just updated the example so the bugs occurs on the branch 'bugs' (and also removed a bunch of non-related stuff).

about updating the linearVelocity on the update it was completely necessary, since the BodyComponent simply loses velocity when falling and when forces are applied on different axis, so this was ugly but necessary to work, i removed it on the repo to show what happens...

it was all static before, but on bigger devices the jump wasn't big enough to jump over obstacles, since the force is the same but obstacles were proportional to viewport height. Removed this too and used static values, jumps still don't have a consistent height.

rezendegc avatar May 01 '20 15:05 rezendegc

So, i discovered the root of all problems: using objects too big. I was creating the body with height/width proportional to screen size, like, 10% of screen size, which would generate a body with width of 100px. So, to things moving i needed a very large force, which was limited by some constants on flame. And when spawning, i would spawn it in middle of screen, so it dropped like 300px or more before hitting ground, which would make the whole thing very buggy. After looking some examples from JapAlekhin i discovered that it all worked well if the game was 9px an then i zoom the canvas. That way, the body would be like 1px wide, so the physics would apply much better...

rezendegc avatar May 07 '20 19:05 rezendegc

I'll close this since you seem to have solved the problem @rezendegc.

spydon avatar May 09 '20 08:05 spydon

that is how box2d works on any platforms. if you look any box2d tutorial out there they will all tell you to use real life dimensions and scaling.

luanpotter avatar May 30 '20 23:05 luanpotter