Problem with Physics and Last version

Hi,

Im making a new game with latest version and i have some problems with physics.

I make a boundary and a floor physics objects to prevent player go out of screen, but it is going down every time, like ignoring all physics things.

Here is a video:

Here is the player with “setDynamic(false)”:

My scene code:

Scene* GameScene::createScene()
{
    auto scene = Scene::createWithPhysics();
    scene->getPhysicsWorld()->setGravity(Vec2(0.0f, -1.0f));
    
    auto gameLayer = GameScene::create();
    scene->addChild(gameLayer);
    gameLayer->toggleDebug();
    
    return scene;
}

My player creation code:

auto playerPhysicsBody = PhysicsBody::createBox(Size(100.0f, 100.0f), PhysicsMaterial(10, 1.0f, 0.5f), Vec2(0.0f, 0.0f));
playerPhysicsBody->setRotationEnable(false);
playerPhysicsBody->setContactTestBitmask(0xFFFFFFFF);
character->setPhysicsBody(playerPhysicsBody);
character->getSprite()->addComponent(character->getPhysicsBody());

My init code:

bool GameScene::init()
{
    if (!Layer::init())
    {
        return false;
    }
    
    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    
    createBackgroundWithAnimation();
    createPlayer();
    
    auto label = Label::createWithTTF("Points", "fonts/Marker Felt.ttf", 20);
    label->setPosition(Vec2(visibleSize.width - label->getContentSize().width - 10, origin.y + visibleSize.height - label->getContentSize().height));
    label->setAlignment(TextHAlignment::RIGHT);
    label->setAnchorPoint(Vec2(0.0f, 1.0f));
    this->addChild(label);
    
    auto edgeBody = PhysicsBody::createEdgeBox(visibleSize, PHYSICSBODY_MATERIAL_DEFAULT, 3);
    edgeBody->setContactTestBitmask(0xFFFFFFFF);
    auto edgeNode = Node::create();
    edgeNode->setPosition(Point(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
    edgeNode->setPhysicsBody(edgeBody);
    this->addChild(edgeNode);
    
    auto bottomBody = PhysicsBody::createBox(Size(visibleSize.width, 20));
    bottomBody->setDynamic(false);
    bottomBody->setContactTestBitmask(0xFFFFFFFF);
    auto bottomNode = Node::create();
    bottomNode->setPosition(Point(visibleSize.width/2, 50));
    bottomNode->setPhysicsBody(bottomBody);
    this->addChild(bottomNode);
    
    this->scheduleUpdate();
    
    return true;
}

And my update code:

void GameScene::update(float delta)
{
    Layer::update(delta);
    
    parallaxNode->setPosition(parallaxNode->getPosition() + Vec2(-2.0f, 0.0f));
    parallaxNode->updatePosition(Vec2(-2.0f, 0.0f));
}

Can anyone help me?

Please…any help?

I don’t use the inbuilt physics, - but it looked like it was moving very fast from the video - so it could be just travelling too fast through the floor.

Yes. This is the problem. Im setting the gravity to 0.1 to check, but with any value the problem happen.

I dont know what to do. I already debug the library code and appear be fine.

Is another thing that i cant see.

Can anyone help me?

Things I would try:
set the gravity to zero - if it still moves as fast then it must be being given some initial velocity -
set the mass to zero - same reason.
set gravity to (1,0) - does it move to the right ? If not, then again it points to something giving it an initial velocity.

Sorry can’t be more help

Hi,

Thanks, but the screenshot is exactly it, the gravity = Zero. Without gravity the player is stopped, like on screenshot. But any physics is making it be crazy.

Thanks.

are you running any MoveTo or MoveBy action on your player sprite? If yes then disable it,

My latest project work with physic and have same error like you. But i solved it like this =

void FlappyBirdCloneGameView::createPlayer() {
    
    CCLOG("Player Created");
    
    bird = Sprite::create("bird/Bird1.png");
    
    bird->setPosition(Vec2(VW * 0.4 + (50*contentScaleX), VH * 0.4 + (50*contentScaleY)));
    
    bird->setScale(contentScaleX + FIX_VALUE_SCALE, contentScaleY + FIX_VALUE_SCALE);
    
    auto birdBody = PhysicsBody::createCircle(bird->getContentSize().width/2, PhysicsMaterial(DENSITY,1,0));
    
    animateBird();
    
    birdBody->setPositionOffset(Vec2(0, 55));
    
    bird->setPhysicsBody(birdBody);
    
    this->addChild(bird, 300);
    
    bird->getPhysicsBody()->setDynamic(false);
    
}

void FlappyBirdCloneGameView::animateBird() {
    Vector<SpriteFrame*> animFrames(4);
    char str[100] = {0};
    for(int i = 1; i < 4; i++)
    {
        sprintf(str, "bird/Bird%d.png",i);
        auto frame = SpriteFrame::create(str,Rect(0, 0, 48, 48)); //we assume that the sprites' dimentions are 40*40 rectangles.
        frame->setAnchorPoint(Vec2(0, 0));
        animFrames.pushBack(frame);
    }
    
    auto animation = Animation::createWithSpriteFrames(animFrames, 0.1f);
    animation->setLoops(-1);
    auto animate = Animate::create(animation);
    bird->runAction(animate);

}


Scene* FlappyBirdCloneGameView::createScene()
{
    
    auto scene = Scene::createWithPhysics();
    scene->getPhysicsWorld()->setGravity(Vec2(0, GRAVITY));
    scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
    
    auto layer = FlappyBirdCloneGameView::create();
    layer->setPhysicWorld(scene->getPhysicsWorld());
    
    scene->addChild(layer);
    
    return scene;
}

Hi @Lazy_Gamer,

Im not moving the player, i dont reach this part of game, im only creating it and putting on screen, nothing more.

@adesuluh,

I already try it:
gameLayer->setPhysicWorld(scene->getPhysicsWorld());

But i dont know why, the method “setPhysicWorld” not exist or was removed from cocos2d-x.

:frowning:

I put the game source code on my github:

It is easy now to detect the problem.

This will be a sample game to be used as a reference for other games and after finish my goals with it, i will put a free version on appstore and playstore, and can be a use case open source.

Can anyone help me?

Hi,

With my sample project can anyone help understand what is wrong?

Thanks.

Hi,

I solve the problem with strange behavior of player sprite. I cant create sprite without image. I change the project on github with the correct code and it is working, but i get other problem. Player is moving automatic o.o

Can anyone help me?

Hi,

I solved the problem. I solved adjusting the pivot on TexturePacker. But im stop on another problem. When i click on joystick button, i get crash, i think because a problem with retain, i dont know. Can anyone help me?

Thanks.

Hi,

The project:

And the video:

Thanks for any help.

What is the problem you have now?

Edit: I see. Can I clone the repo?

Hi,

My problem is what showd on video. After click on button A, the app crash when the animation will be changed.

Thanks.

And you can clone the repo, yes.

Can you help me? I dont know if i need put retain/release or something like this.

:frowning:

Ok. Thanks. I’ll remind myself tomorrow. Or email me to remind me. Slackmoehrle@cocos2d-x.org

Can anyone help me please?