Implementing gravity in simple 2d game

最后都变了- 提交于 2019-12-30 11:34:33

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/23150603/implementing-gravity-in-simple-2d-game

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!