+ 1

Jumping Mechanic

I've been working on a platforming engine in JavaScript. Collisions and Physics are all calculated from scratch and lately I've been having issue with jumping. Normally, this would be fine for me, but this is the first time I've used my own form of collision detection and physics, (which engines such as unity will make simple for you) and this changes things up. So, I would like to know how you guys would implement a nice jumping mechanic (that also includes a groundcheck if you can!) The only variables you would need to know are y (player's y coordinate), gravity (force of gravity), and a spacebar variable that tracks whether or not the spacebar is being pressed. My overall physics function is being called once per frame with the engine test map running at 60 FPS. TL;DR I would like to see examples of jump code for a 2D platformer that uses standard gravity and y variables that triggers on space press. edit: I don't need physical code if you aren't too familiar with js. Describing in detail the recommended process will work fine as well!

17th Jan 2017, 4:26 PM
Lux
Lux - avatar
3 Antworten
+ 4
I dont know much about physicjs, but i think i can help you. I tried to make it look like a real JS script :P var canJump = ... ; /* each tile could apply a 'canJump' property on the player when they walk over it, like ground:True, air:False, rock:True, water:False, etc.*/ ('Spacebar').onPress(jump()); var gravity = setInterval(gravDown,500); /*the higher the interval, the slower player will fall*/ function gravDonw(){ if(tileUnder.state != 'solid'){ player.y -= 1; } } function moveUp(){ while(y<maxJumpHeight && player.topCollide == 'False'){ player.y += 1; } } function jump(){ if(canJump == 'True' /*or 1*/){ clearInterval(gravity); moveUp(); gravity = setInterval(gravDown,500); } } Im not sure if it was helpful (or at least was related to you question, cuz i got.. excited)
18th Jan 2017, 3:25 PM
IgorSM
IgorSM - avatar
+ 4
Hey, thanks for the answer! After rearranging bits to function with what I currently have and compressing it to work with only one function, I finally got smooth jumping working properly. (Along with groundchecking!) This is a big checkpoint for me, so I'm relieved...to an extent...as I now realize I can jump straight through the bottom of blocks...time to double-check the work on my collision code xD Thanks for the help!
18th Jan 2017, 4:19 PM
Lux
Lux - avatar
+ 2
Also, for smoother jumping mechanics, you could use some quadractic function concepts (having 'a' as a negative value, to make the curve point up/ the main-axis being time and cross-axis being height)
18th Jan 2017, 3:30 PM
IgorSM
IgorSM - avatar