[Cocos3.0 Tutorial] Predict a trajectory with cocos2d-x and Box2d

@oldanidavide Great tutorial, thank you.

About the “#include <Box2D/Box2D.h>” disappear problem, I recommend you to give “markdown specific-language code” a try, for example:

` ` ` xml
  	<XML CODE />
` ` ` 

Please remove spaces between the Grave accent ` , and change xml to required language, for example: cpp

Testing the include syntax:

#include <Box2D/Box2D.h>

Hope this can help you, good luck…

@catch_up

When you include

#include <Box2D/Box2D.h>

you can add b2ContactListener

@victory144

Thank you so much!! i’ve fixed

Thanks your good Tutorial, but I getting a problem with Win7 - 64

With the same code from Github of yours above ( Just Copy and Past, and added Box2d in Cmakelist)
1/My home PC - Win7 - 64

  • When I built Win32 Platform by cocos console: It’s Error ( 21 error &&^^&$%(@(*%)
  • But when I Built for android : SUCCESSFULL !, hix

2/My copany PC : Win7 -32

  • Built win32: OK
  • Build Android: OK

WHY, is that error of BOX2D and WIN 64???

How to fix it? Anybody Know? Help me!!!

Works great on OS X/ios.

Hi,

Can you show how to enable debug draw for Box2D? I’ve been stuck on this for a while as I’m new to Box2d and Cocos2d-X.

I’ve tried using the tests examples, but with no success.

Nevermind. I’ve used vinova’s B2DebugDraw and now it works. Thanks.

Can you show me how to enable debug draw for Box2D? if I Want to change the shape or combine other shapes I wanted to add debugdrawing to see the outline of my body to know if it matches with my sprite.

Also AISUKAUNTA how you call vinova B2DebugDraw. How you call it? Because as I see it is other call with other b2world. How you call it to your HelloWorld class to enable the drawing?

Just import B2DebugDraw. Other tutorial examples of Box2D debugging is out of date and not compatible with Cocos2d-x 3.0 which is why I had such a hard time to enable it.

All you have to do is follow the readme notes on his github.

Mr Aisukaunta
I just import B2DebugDraw.h but I get nothing in my sprites. Can you explained a little bit about the comments what modifications I have to make to my box2dworld to HelloWorldScene.cpp so as to draw something? It is very confusing

vinova comments are

Just create a new instance of B2DebugDrawLayer, then add it to your CCLayer just as a normal CCNode.

Make sure to give it highest z-order.

addChild(B2DebugDrawLayer::create(mB2World, PTM_RATIO), 9999);

Can you write me the modifications one by one I have to make in the code of HelloWorldScene.cpp?

For example in the top of the code I add #include “B2DebugDrawLayer.h”

Whoops, I never got notification for this.

But yes exactly what the comments say. All I did was, create the b2world object and then add that to my B2DebugDrawLayer object.

So this is exactly what I used:

auto gravity = b2Vec2(0.0f, -10.0f);
world = new b2World(gravity);
world->SetAllowSleeping(true);
world->SetContinuousPhysics(true);

this->setAnchorPoint(CCPointZero);
this->addChild(B2DebugDrawLayer::create(world, PTM_RATIO), 9999);

The bolded parts is the only essential parts. If you’re still having trouble with this, I think game developent might be too high level for you.

I have created a no of sprites and added them to the scene with the same reference and now, how can i remove them

Now, i want to remove all those, even the ones with out any reference. How can i achieve that.

With out using removeChildBytag(); or without using tags.

void Science::onBallMovement(cocos2d::Ref* pSender){

auto sp = (Sprite*)pSender;

v1 = sp->getPosition();


if (v1 == v2) {
    return;
}

if(v1!=v2) {

    if (sp->getTag()==1) {
       s = Sprite::create("Dot.png");
    }
    if (sp->getTag()==2) {
        s = Sprite::create("BlueDot.png");
    }

    s->setPosition(v1);
    s->autorelease();
    this->addChild(s);
}
v2 = v1;
}

void Science::update(float delta){

if (setting) {
    this->onBallMovement(spr);
    this->onBallMovement(spr1);
}
}

By overriding the update method of node class, i am calling the above repeatedly and based on the current position of my sprite which moves randomly, i am creating a tracker of path. But now, i want to remove it based on the user selection, how can i do it?

Doesn’t that simulated throw (simulateTrajectory)'s world->step also changes the position of the original ball too? world->step should normally affect everything in the physics world??