在简单的2D游戏中实现引力 [英] Implementing gravity in simple 2d game

查看:221
本文介绍了在简单的2D游戏中实现引力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个简单的游戏,比如马里奥,一个角色固定在一个位置,地面向左移动,给人一种角色运动的幻觉。地面由左上角和右下角坐标定义的矩形块组成:

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
};

跳跃只是在向左移动曲面时改变角色的y坐标,所以跳跃看起来像是反转 V letter。

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

代码的其余部分 - 动画,移动的精灵 - 与 http://zetcode.com/tutorials/javagamestutorial/movingsprites/

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

我想跳得更真实,所以我的问题是 - 我怎样才能在这里实施引力?我必须使用dx的地面,而角色的dy才能做到,那可以吗?

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...?

怎么做?

推荐答案

让我们看看:

g = 9.8 meters/second

获取角色的高度(以像素为单位)并将其与平均身高相匹配,比如说1.7米高。

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

然后使用引力公式:

y = y0 - g * t^2 / 2

其中 g 以像素/秒为单位。

where g is in pixels/second.

这篇关于在简单的2D游戏中实现引力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆