Cocos2D重力? [英] Cocos2D Gravity?

查看:325
本文介绍了Cocos2D重力?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的想在我的游戏中尝试重力。我知道大家都说使用Box2D,但在我的情况下我不能。我需要使用Cocos2D的重力。



我知道Cocos2D没有内置的重力API,所以我需要手动做一些事情。



任何人都可以告诉我他们做了什么,或者可以解释一下 step by步骤,了解如何应用非常量引力(下降时稍强的引力)。



帮助很多正面临同一问题的人。



谢谢!

解决方案

重力只不过是每个物理步骤对身体施加的恒定速度。请查看此示例性更新方法:

   - (void)update:(ccTime)delta 
{
//对你的应用使用感觉很好的重力速度
const CGPoint gravity = CGPointMake(0,-0.2);

//应用向下重力速度后更新sprite位置
CGPoint pos = sprite.position;
pos.y + = gravity.y;
sprite.position = pos;
}

每一帧精灵y的位置都会减少。这是简单的方法。为了更逼真的效果,您需要为每个移动物体建立一个速度矢量,并对速度应用重力(这也是一个CGPoint)。

   - (void)update:(ccTime)delta 
{
//使用对你的应用程序感觉很好的重力速度
const CGPoint gravity = CGPointMake ,-0.2);

//通过重力影响更新速度
velocity.y + = gravity.y;

//用速度更新sprite位置
CGPoint pos = sprite.position;
pos.y + = velocity.y;
sprite.position = pos;
}

这样做的效果是速度随着时间的推移在向下的y方向上增加。这将使对象加速越来越快向下,它是下降越长。



然而,通过修改速度,你仍然可以改变对象的一般方向。例如,为了使字符跳跃,你可以设置 velocity.y = 2.0 ,它会向上移动,并由于随着时间的推移重力的作用再次下降。 p>

这仍然是一个简化的方法,但在不使用真实物理引擎的游戏中非常常见。


I am really looking to try to have gravity in my game. I know everyone says use Box2D, but in my case I can't. I need to use Cocos2D for the gravity.

I know Cocos2D does not have any gravity API built in so I need to do something manually. The thing is there is like no tutorials or examples anywhere on the web that show this.

Can anyone show me what they have done or can some explain step by step on how to apply a non-constant gravity (One that gets slightly stronger while falling).

I think this will help a lot of people that are facing the same issue that I am having!

Thanks!

解决方案

Gravity is nothing but a constant velocity applied to the body for every physics step. Have a look at this exemplary update method:

-(void) update:(ccTime)delta
{
   // use a gravity velocity that "feels good" for your app
   const CGPoint gravity = CGPointMake(0, -0.2);

   // update sprite position after applying downward gravity velocity
   CGPoint pos = sprite.position;
   pos.y += gravity.y;
   sprite.position = pos;
}

Every frame the sprite y position will be decreased. That's the simple approach. For a more realistic effect you will want to have a velocity vector for each moving object, and apply gravity to the velocity (which is also a CGPoint).

-(void) update:(ccTime)delta
{
   // use a gravity velocity that "feels good" for your app
   const CGPoint gravity = CGPointMake(0, -0.2);

   // update velocity with gravitational influence
   velocity.y += gravity.y;

   // update sprite position with velocity
   CGPoint pos = sprite.position;
   pos.y += velocity.y;
   sprite.position = pos;
}

This has the effect that velocity, over time, increases in the downward y direction. This will have the object accelerate faster and faster downwards, the longer it is "falling".

Yet by modifying velocity you can still change the general direction of the object. For instance to make the character jump you could set velocity.y = 2.0 and it would move upwards and come back down again due to the effect of gravity applied over time.

This is still a simplified approach but very common in games that don't use a "real" physics engine.

这篇关于Cocos2D重力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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