Problem About PhysicsBody

Problem About PhysicsBody

Hi everyone,

I have a problem about PhysicsBody. I added PhysicsBody to my character and ground. When I run the game, the two PhysicsBody are intertwined like in the picture. I added the codes below. What did i missed?

My Character’s Code:

    charFinal = Sprite::create("CharacterFinal-Idle.png");
	charFinal->setAnchorPoint(Vec2(0.0, 0.0));
	charFinal->setPosition(Point(100, 100));
	auto charBody = PhysicsBody::createBox(charFinal->getContentSize(), PhysicsMaterial(0, 1, 0));
	layer->addChild(charFinal, 3);

Ground’s code:

    bossBlockSprite = Sprite::create("Materials/BossBlock.png");
	bossBlockSprite->setAnchorPoint(Vec2(0.0, 0.0));
	bossBlockSprite->setPosition(Point(0, 0));
	auto bossBlockBody = PhysicsBody::createBox(bossBlockSprite->getContentSize(), PhysicsMaterial(0, 1, 0));
	layer->addChild(bossBlockSprite, 10);

What is the height of the ground? Maybe you are positioning the character to a point that is inside the height of the ground?

Height of the ground is 35px, character 65px higher from the ground.

one thing is your z-order is different, but I don’t think that is having an issue on this.

Another thing would be to base the position of the character on the position of the ground y position.

  1. Nothing serious, but could you please use code tags next time? It makes questions more readable, It can be achieved like so:
    If code if one line, place a ‘`’ at each end of the code like so:

  1. Are you running an action to the Sprite or ground? Actions and Physics don’t combine too well at times. Mainly when working with 2 or more bodies, hence this previous issue someone had:
    About Cocos2dx C++ physicsbody
    Physics chipmunk? (the video is no longer available, but I similarly to your image, his block sank into another body)

I have a game with hundreds to thousands of physics bodies that are controlled by sequences and spawns. I don’t have any issues.

I’m not going to disagree, but i’m merely responding taking into account what @smitpatel88 says here Physics chipmunk?

Yup, he makes valid points. Force and velocity are important.

I found solution. I wrote simple jumping code for my character. Apparently my jumping code was forcing my character to drop it down even though it hit PhysicsBody.

Yes, cocos actions were not made to consider physics bodies. The sprite sank straight into the ground because the action completely ignores the ground. When you want to make a physics sprite perform an action, such as a jump, many of use would advise, rather than using an action such as MoveTo or JumpTo,