简体   繁体   English

为什么Android和iOS的碰撞行为会有所不同?

[英]Why do collisions behave differently in Android than in iOS?

I am using Box2D to simulate the physics world independently of my sprites, and I update the position of my sprites based on the position of b2body. 我正在使用Box2D独立于我的精灵来模拟物理世界,并根据b2body的位置更新了精灵的位置。

I have a player that consists of two fixtures. 我有一个包含两个装置的播放器。 One fixture is not a sensor and can make contact with the ground. 一个固定装置不是传感器,可以与地面接触。 The other fixture is a sensor and is used to check for contacts with things like powerups eatables etc. 另一个固定装置是传感器,用于检查是否与上电的可食用物品等接触。

. I have some bodies (eatables, powers) in which the fixture consists of a single sensor that can make contact with the player sensor. 我有一些身体(可食用的东西,电源),其中的固定装置包括一个可以与播放器传感器接触的传感器。

In iOS everything works fine - when the player collides with the powerup / eatable, he continues running as if nothing is wrong. 在iOS中,一切正常-当播放器与通电/可食设备发生碰撞时,他会继续运行,好像没有任何问题。 But in Android he bounces off the powerup. 但是在Android中,他会自动启动。

What could be the problem ? 可能是什么问题呢 ? The plist file generated by physics editor is the same for both. 物理编辑器生成的plist文件对于两者是相同的。 Why should the simulation be different ? 为什么模拟应该不同? Box2d is part of cocos2d, so the library is the same and should be the same for both. Box2d是cocos2d的一部分,因此两者的库是相同的,并且应该相同。

I am using cocos2d-x 3.7. 我正在使用cocos2d-x 3.7。

EDIT: I use the following: 编辑:我使用以下内容:

ContactListener.h class: ContactListener.h类:

class ContactListener : public b2ContactListener {

public:


    void BeginContact(b2Contact* contact) {
        void* bodyUserDataA = contact->GetFixtureA()->GetBody()->GetUserData();
        if (bodyUserDataA) {

        }
    }

    void EndContact(b2Contact* contact) {

    }
};

my ContactListener.cpp is empty. 我的ContactListener.cpp为空。

Xcode automatically handles building everything properly and things "just work". Xcode会自动处理正确构建所有事物的过程,并且一切“正常”。 On Android, I had to specify my cpp files in Application.mk, so I wonder whether this could cause some different behaviour, because I am not specifying the .h files anywhere. 在Android上,我必须在Application.mk中指定我的cpp文件,所以我想知道这是否会导致某些不同的行为,因为我没有在任何地方指定.h文件。

This is due to a bug in GB2ShapeCache.cpp provided by the PhysicsEditor file. 这是由于PhysicsEditor文件提供的GB2ShapeCache.cpp中的错误所致。 While the boolean check for isSensor works in iOS / OS X , in Android it doesn't work as expected and sets it to false instead of true. 虽然isSensor的布尔检查在iOS / OS X中有效,但在Android中却无法按预期工作,并将其设置为false而不是true。

The fix: 解决方法:

#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
            basicData.isSensor = (bool)static_cast<CCString *>(fixtureData->objectForKey("isSensor"))->intValue();
#else
            CCString *foo = static_cast<CCString *>(fixtureData->objectForKey("isSensor"));
            const char *s = foo->getCString();
            if (s==NULL) {
                basicData.isSensor = false;
            }else{
                std::string str(s);
                if (str=="true") {
                    basicData.isSensor = true;
                }else{
                    basicData.isSensor = false;
                }
            }
#endif

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

相关问题 Android手机上的Chrome行为与台式机Chrome模拟器上的行为不同吗? - Does Chrome on an android phone behave differently than on the desktop Chrome emulator? 为什么Android按钮的呈现方式与ImageButtons有所不同? - Why do Android Buttons render differently than ImageButtons? 应用程序在Android和iOS上使用phonegap呈现的方式不同 - app renders differently on android than iOS using phonegap Android ListView在同类设备上的行为不同 - Android listview behave differently on same kind of device Android canvas:drawTextOnPath()在不同设备上的行为不同 - Android canvas:drawTextOnPath() behave differently on different devices Android 尽管代码相同,但应用程序的行为不同 - Android Apps behave differently despite identical code Android ART 和 HotSpot 在非易失性变量可见性方面的行为是否不同? - Do Android ART and HotSpot behave differently in non-volatile variable visibility? 为什么Android WebView加载网页的方式与默认浏览器不同? - Why is Android WebView loading webpage differently than the default browser? 为什么proguard -keepclassmembers基于类说明符的行为不同? - Why does proguard -keepclassmembers behave differently based on class-specifier? 为什么按钮对象的行为取决于声明的位置 - Why does the button object behave differently depending on where it is declared
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM