【问题标题】:how to know if collision happened at the right side or bottom side of the body如何知道碰撞发生在车身右侧还是底部
【发布时间】:2014-05-07 08:41:09
【问题描述】:

我正在创建一个andengine 游戏(参考本教程http://www.matim-dev.com/full-game-tutorial---part-1.html),我的主要角色在其中行走并被顶部落下的砖块击中。如果他被落下的砖块击中,他就会死。但是一旦砖块在地上,主角可以触摸它并跳上它。现在我知道如何判断两个物体是否相互接触。但是如何找到触点是在字符的上侧还是在身体的左侧或右侧。

我的联系方式如下

private ContactListener contactListener()
{
    ContactListener contactListener = new ContactListener()
    {
        public void beginContact(Contact contact)
        {
            final Fixture x1 = contact.getFixtureA();
            final Fixture x2 = contact.getFixtureB();


            Vector2[] contactPoints = contact.getWorldManifold().getPoints();

            System.out.println("the points are as ::"+contactPoints);


            if (x1.getBody().getUserData() != null && x2.getBody().getUserData() != null)
            {
                if (x2.getBody().getUserData().equals("player"))
                {
                    player.increaseFootContacts();
                }

                if (x1.getBody().getUserData().equals("platform2") && x2.getBody().getUserData().equals("player"))
                {
                    engine.registerUpdateHandler(new TimerHandler(0.2f, new ITimerCallback()
                    {                                   
                        public void onTimePassed(final TimerHandler pTimerHandler)
                        {
                            pTimerHandler.reset();
                            engine.unregisterUpdateHandler(pTimerHandler);
                            x1.getBody().setType(BodyType.DynamicBody);
                        }
                    }));
                }

                if (x1.getBody().getUserData().equals("platform3") && x2.getBody().getUserData().equals("player"))
                {
                    x1.getBody().setType(BodyType.DynamicBody);
                }
            }
        }

        public void endContact(Contact contact)
        {
            final Fixture x1 = contact.getFixtureA();
            final Fixture x2 = contact.getFixtureB();



            if (x1.getBody().getUserData() != null && x2.getBody().getUserData() != null)
            {
                if (x2.getBody().getUserData().equals("player"))
                {
                    player.decreaseFootContacts();
                }
            }
        }

        public void preSolve(Contact contact, Manifold oldManifold)
        {

        }

        public void postSolve(Contact contact, ContactImpulse impulse)
        {

        }
    };
    return contactListener;
}

请给我一些建议。

【问题讨论】:

    标签: android andengine collision-detection collision game-physics


    【解决方案1】:

    这是否对您有足够的帮助,或者您需要有关身体接触方法的更多信息?

    public boolean areBodiesContacted(Body pBody1, Body pBody2,
    Contact pContact)
    {
    if(pContact.getFixtureA().getBody().equals(pBody1) ||
           pContact.getFixtureB().getBody().equals(pBody1))
       if(pContact.getFixtureA().getBody().equals(pBody2) ||
               pContact.getFixtureB().getBody().equals(pBody2))
           return true;
    return false;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-29
      • 2015-12-02
      • 2021-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多