Problem with physics only happening in Android devices (*Solved)

Hi everyone,

I’m making a Halloween game using Cocos2dx v3.17.2 and I have it ready for upload to the stores.

I’m using Chipmunk built in physics created using PhysicsEditor, each object has different restitution value and everything is working like a charm in iOS.

The game is as shown in the picture below. Objects (in yellow) fall from the sky at (0,-300) gravity and you have to move the orange object (pumpkin) to gather the objects. The rest of the items on screen are non dynamic objects that act like some kind of bumpers. The numbers in the picture are the restitution of each object.

HPump

I was porting it to Android and I’m encountering a big problem. The physics sometimes work well like in iOS but sometimes they bounce like 20x the bounce that should be.

As I’m using different restitution values, I wasn’t using a fixed preSolve action like solve.setRestitution(0.1f); to avoid these strange bouncing, but even using this, I still have this problem that make the game unplayable on Android…

Does anybody know why is working different than on iOS? How can I solve it? Please any help will be very appreciate!

maybe @r101 has thoughts?

I use Box2D for physics, so can’t really help with Chipmunk. The only thing I can suggest is trying to update to a newer version of Chimpunk, and also use whatever you can to locate the actual source of the problem, like finding out what values are different between iOS and Android (debug/console output statements etc).

I can ask engineering for thoughts. I only use built in physics. I don’t have a current android device.

Maybe check cpp-tests and see if the issue persists

Thanks for the answers, really appreciate it!

@slackmoehrle I’m using built in physics too, not an external Chipmunk library.

@R101 I wasn’t able to locate the problem. The values of restitution that I’m getting in my onContactPreSolve function when objects collide are the same in both devices (iOS and Android), so I don’t know why is this happening…

The values I’m getting are 0.480000 when the dynamic object (yellow) collides with the green static walls and 0.120000 when the object collides with the pumpkin (orange), and they are always the same values.

I wonder if I will be able to migrate to Box2D just in a few hours, I just need to know how to attach the sprite to the physics bodys and to know which objects are the ones which collide.

Sorry without any sources we cannot know what is different.
Or maybe a movie about the different behavior?
Does the cpptest physic example working without different behavior?

Maybe your steps be different…? Some stuff running behind the game?

This is the normal and intended behavior on iOS:

And this is the video showing the problems I’m having on Android:

Sometimes the candy even rebound out of the screen on Android.

I don’t have any different stuff in the Android version. Is just the same code and same everything.

This is how I’m doing everything related with physics:

In my MyClass.h:

#include "PhysicsShapeCache.h"

private:
    PhysicsWorld *sceneWorld;
    void SetPhysicsWorld( PhysicsWorld *world ) { sceneWorld = world; };
    PhysicsShapeCache *shapeCache;

In my MyClass.cpp:

Scene* MyClass::createScene()
{
    auto scene = Scene::createWithPhysics();
    scene->getPhysicsWorld()->setGravity(Vec2(0, -300));

    auto layer = MyClass::create();
    layer->SetPhysicsWorld( scene->getPhysicsWorld() );

    scene->addChild(layer);
    return scene;
}

bool MyClass::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }

    //THE WAY I CREATE THE PHYSICS OBJECTS
    shapeCache = PhysicsShapeCache::getInstance();
    shapeCache->addShapesWithFile("THalloween/Preload/HPPhysics.plist");

    pumpFace = Sprite::createWithSpriteFrameName("HPBack.png");
    pumpFace->setPosition( pos);
    shapeCache->setBodyOnSprite("HPBack", pumpFace);
    addChild( pumpFace,1 );
}

void MyClass::spawnSprite(const std::string &name, Vec2 pos)
{
    //THE WAY I CREATE CANDY OBJECTS
    // create a sprite with the given image name
    auto sprite = Sprite::createWithSpriteFrameName(name+".png");

    sprite->setName(name);
    sprite->setPosition(pos);
    shapeCache->setBodyOnSprite(name, sprite);

    vecCandy.pushBack(sprite);

    sprite->getPhysicsBody()->setGravityEnable(true);
    addChild( sprite,5);
}

PhysicsShapeCache class is provided by CodeAndWeb here:

I also use:

bool onContactBegin(PhysicsContact &contact);
void onContactPostSolve(PhysicsContact& contact, **const** PhysicsContactPostSolve& solve);

To know which objects are colliding and then do something with the objects.

Ok. Can you enable the debug draw on the physic object?
Maybe the “body/shape is different”?

Seems it is only the position or the body/shape like lollies ?

Sorry for late.

I enabled debug draw but nothing wrong with that. This is happening with all candy even with circle shapes.

I solved it in the onContactSeparate(), limiting the angular velocity and the velocity to a fixed value.

It should work just like on iOS without having to do anything like this. Its working in this minigame but I had to cancel other minigame because of the same problem. I didn’t find a way to solve it and it’s sad cause I spent a lot of time creating that minigame. I will make a post about it.

Anyway, thanks for the help guys!

Can you make an easy example on the cpptest which have the same behavior?

I have rebuild the demo:
PhysicsEditor-Cocos2d-x

And get also an mystery behavior on Windows OS.
Can you build it on android and IOS?
Tell whats happens.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.