简体   繁体   English

在简单的2D游戏中实现重力

[英]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. 跳跃只是在向左移动表面的同时更改角色的y坐标,因此跳跃看起来像是反向的V字母。

The rest of the code - animation, moving sprites - is very similar to http://zetcode.com/tutorials/javagamestutorial/movingsprites/ 其余代码-动画,移动精灵-与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? 我必须使用地面的dx角色的dy来做到这一点,好吗?

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. 以字符为单位的人物高度,并使其与人的平均身高相匹配,例如,身高1.7米。

Then use gravitation formula: 然后使用引力公式:

y = y0 - g * t^2 / 2

where g is in pixels/second. 其中g以像素/秒为单位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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