Dr.Linux Posted August 9, 2014 Share Posted August 9, 2014 A while back i put a topic asking on how to make gravity in a platformer game. Now I want to know how to do it without a library, engine or framework. Thanks in Advance! Quote Link to comment Share on other sites More sharing options...
harrywatson Posted August 22, 2014 Share Posted August 22, 2014 If you are using javascript and canvas ?var canvas = document.getElementById('canvas'),context = canvas.getContext('2d'),Gravity = 9.81,height = 10 ; //ten will be the height of your game Character.y = 0 + Gravity / height;Hmm just off the top of my head this might work. I haven't tried it. You could have a look where I simplified it from here.... http://corehtml5canvas.com/code-live/ch07/example-7.1/example.html Also I believe it could be very easy to apply gravity to the whole scene and create exceptions, this is in a frameworkbut the code is not reliant on that framework.....var i = 0; var speed1 = 0.5, speed2 = 0.3; z1.y += speed1; //z1 and z2 are zombies subject to gravity z2.y += speed2;Well that's my own attempt at gravity which works well, hope you can get something from this Harry Quote Link to comment Share on other sites More sharing options...
alex_h Posted August 22, 2014 Share Posted August 22, 2014 At it's heart the issue is really as simple as having an update loop on the game object to which gravity should be applied, that... well, applies gravity :var gravity = <some positive value>;//var speed = <whatever>;//horizontal movement stepthis.update = function(deltaTime){ //******************************************** //VERTICAL velocity //******************************************** this.velocity.y += gravity; //******************************************** //HORIZONTAL VELOCITY //******************************************** this.velocity.x = speed; //******************************************** //update position //******************************************** this.position.y += this.velocity.y * deltaTime; this.position.x += this.velocity.x * deltaTime; //************************************}After doing this each frame you then collision check against any nearby platforms and correct if an overlap was detected. Then you apply the x and y from the position point to your display objects transform properties. Make the player jump by setting the velocity.y to a negative number, something like gravity times minus 10, depending on how high you want them to jump. This is roughly equivalent to applying an impulse to a body in a physics engine. harrywatson 1 Quote Link to comment Share on other sites More sharing options...
Dr.Linux Posted August 23, 2014 Author Share Posted August 23, 2014 Thanks! It was a lot easier than I thought. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.