简体   繁体   中英

Implementing gravity in simple 2d game

I'm making a simple game, something like mario, with a character fixed in one position, and the ground moving left, giving the illusion of character movement. The ground is made of rectangular blocks defined by top-left and bottom-right coordinates:

private int surfaceMatrix[][] = {
   {0, 100, 300, 0} // block having 100 height and 300 width
};

Jumping is just changing character's y coordinate while moving the surface left, so the jump looks like reversed V letter.

The rest of the code - animation, moving sprites - is very similar to http://zetcode.com/tutorials/javagamestutorial/movingsprites/


I want jumping to be more real, so my question is - how can I implement gravity here? I'd have to use dx of the ground, and dy of the character to do it, is that ok?

What would be the velocity here? Pixels per second...?

How to do that?

Let's see:

g = 9.8 meters/second

Take height of your character in pixels and match it to an average man height, say 1.7 meters tall.

Then use gravitation formula:

y = y0 - g * t^2 / 2

where g is in pixels/second.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM