Jump to content

Gravity(Once Again)


Dr.Linux
 Share

Recommended Posts

  • 2 weeks later...

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 framework

but 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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...