如何让身体忽略重力(AndEngine)? [英] How do I make a body ignore gravity (AndEngine)?

查看:30
本文介绍了如何让身体忽略重力(AndEngine)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在场景中弹跳的精灵(带有身体).它需要不受重力影响,而且还能够与场景中的其他物体发生碰撞.这意味着我不能使用运动体.我试过了:

I have a sprite (with a body) that bounces around the scene. It needs to be unaffected by gravity, but also able to collide with other bodies on the scene. This means I cannot use a kinematic body. I tried:

body = PhysicsFactory.createCircleBody(mPhysicsWorld, this, BodyType.DynamicBody, PhysicsFactory.createFixtureDef(.5f, .5f, .5f));
        MassData md = new MassData();
        md.mass=0;
        body.setMassData(md);
        mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(this, body, true, true));

我的 FixtureDef 具有不同的值,但似乎没有任何效果.我的身体仍然跌落到场景的底部,坐在那里.我看到了一些关于施加相等和相反的力的东西.如果我的重力是地球默认值,这是一个可行的选择,我将如何做?或者有没有更好的方法将这个物体从引力场中移除,并且仍然让它与其他动态物体相互作用?

with different values for my FixtureDef, but nothing seemed to work. My body still falls to the bottom of the scene and sits there. I saw something about applying an equal and opposite force. If my gravitational force is the earth default and this is a viable option, how exactly would I do it? Or is there a better way to remove this body from the gravitational field and still have it interact with other dynamic bodies?

推荐答案

我不确定当前版本的 AndEngine 中包含哪个版本的 Box2D,但您可以尝试 body.setGravityScale(0);

I am not sure what version of Box2D is included in the current version of AndEngine, but you could try body.setGravityScale(0);

如果这不起作用,您必须将其放入 onUpdate:

If that does not work, you'll have to put this in onUpdate:

body.applyForce(new Vector2(0,-SensorManager.GRAVITY_EARTH), new Vector2(body.getWorldCenter()));

这篇关于如何让身体忽略重力(AndEngine)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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