简体   繁体   English

在2D游戏中跳跃并站立在物体上

[英]Jumping and standing on objects in 2D game

Im making small 2D game and i would like how should, theoretically work jumping and standing on objects. 我正在制作小型2D游戏,我想在理论上应该如何跳跃并站在物体上。
For jumping should there be some kind of gravity?Should i use collision detection? 对于跳跃,应该有某种重力吗?我应该使用碰撞检测吗?
Good example of what i want to undestand is jumping in Mario. 我想理解的一个很好的例子是在马里奥跳。

I think for jumping it would be best to create some simple model which use "game time". 我认为对于跳跃,最好创建一个使用“游戏时间”的简单模型。 I would personally create some physical model based on gravity, but you can go for whatever you want. 我会根据重力亲自创建一些物理模型,但是您可以随心所欲。

If you want your character to "stand" on object, you will have to create some collision detection. 如果希望角色“站立”在对象上,则必须创建一些碰撞检测。 Good start is to approximate object by one or more circles (or lines) compute collision of them and in case the approximation collides determine the final state by some more precise method. 一个好的开始是通过一个或多个圆(或线)来近似对象,以计算它们的碰撞,并在近似碰撞的情况下,通过某种更精确的方法确定最终状态。

In terms of should there be gravity and collision detection - personally for such a thing I'd say yes and yes! 关于是否应该有重力和碰撞检测-就我个人而言,我会说“是”和“是”! It doesn't have to be complicated but once you have those things in place the pseudocode for the "main loop" becomes relatively simple: 不必太复杂,但是一旦有了这些内容,“主循环”的伪代码就会变得相对简单:

if not colliding with object underneath, apply gravity
if user presses jump key and not colliding with surface //i.e. if we're in the air already, don't jump again
    apply upward velocity

That is of course oversimplified and there are other corner cases to deal with (like making sure when you're coming down after jumping, you don't end up embedded or potentially going through the floor. 当然,这过于简单了,还有其他一些极端的情况要处理(例如,确保跳跃后跌倒时,您不会最终陷入困境或可能无法通过地板。

You might want to take a look at Greenfoot which handles a lot of things like all the Java2d stuff, collision detection etc. for you. 您可能想看看Greenfoot ,它可以为您处理很多事情,例如所有Java2d东西,冲突检测等。 Even if you don't stick with it it'd be a good platform for building a prototype or playing around with the ideas talked about. 即使您不坚持使用它,它也将是构建原型或处理所讨论想法的好平台。

Standing on objects implies collision detection, you can approximate the characters and the environment objects with primitive shapes (rectangles, circles etc.). 站在对象上意味着碰撞检测,您可以使用原始形状(矩形,圆形等)来近似字符和环境对象。 To check if to shapes are colliding, check axis one by one (X and Y axis in your case). 要检查形状是否碰撞,请一一检查轴(在您的情况下为X和Y轴)。 Here is an explanation of the "separating axis theorem" (Section 1). 是“分离轴定理”的说明(第1节)。

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

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