How to determine contact ended?

How to determine contact ended?
0

I use onContactBegin event listener, when my sprite begin to contact I use my lambda function to set isContactingPtr to true.

But I need to set my isContactingPtr flag to false when my sprite is not contacting.

How can I do this?

Are you using collision bit masks?

https://docs.cocos2d-x.org/cocos2d-x/v4/en/physics/collisions.html

I solved this with onContactSeperate but another issue encountered.
contact callback works as expected but when I drive sprite to slope onContactSeperate called even it is still on ground and touching it. Looks like problem related to chipmunk physics

Perhaps showing some code might help. Physics stuff is hard to envision just thinking about it.

	auto contactListener2 = EventListenerPhysicsContact::create();
	contactListener2->onContactBegin = [&](PhysicsContact &contact)
	{
		CCLOG("contacted");
		*contactingPtr = true;
		return true;
	};
	
	contactListener2->onContactSeparate = [&](PhysicsContact &contact)
	{
		CCLOG("seperated");
		*contactingPtr = false;
		return true;
	};

	this->_eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener2, sprite3);

above code is in init()

All works nice except when I go to slope it calls Separate (sometimes seperate called on flat surface)

I think I need to switch to box2d from chipmunk , looks like problem related lack of continious collition in chipmunk

How can i switch to box2d?

https://chipmunk-physics.net/forum/viewtopic.php?f=1&t=7543

My all efforts to switch box2d failed, Every building log shows still libchipmunk included project somehow.

second thing is friction and restituion doesnt work, I change material numbers both contaction bodies nothing changes. Most people use physics engine for collision but chipmunk fails at collison.

You can use the latest Box2D source from https://github.com/erincatto/Box2D/, and add it to your project relatively easily.

For example, if your project is in c:\MyProject\, you can do something like this:

Create a folder named “external” inside “MyProject”, then copy the contents of \Box2D\src to C:\MyProject\external\Box2D\.

It would look like this:

C:\MyProject\Classes
C:\MyProject\cocos
C:\MyProject\external\Box2D
C:\MyProject\proj.android
... etc.

Now in your root CMakeLists.txt, for instance, C:\MyProject\CMakeLists.txt, you would add this:

set(BUILD_EXT_BOX2D OFF CACHE BOOL "Build with internal Box2D support" FORCE)

It needs to go before the include(CocosBuildSet) line.

Then add this section to build and link it after the target_link_libraries(${APP_NAME} cocos2d) line:

# add box2d
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/external/Box2D ${PROJECT_BINARY_DIR}/Box2D)
get_target_property(box2d_INCLUDE_DIRS Box2D INTERFACE_INCLUDE_DIRECTORIES)
target_include_directories(cocos2d PRIVATE ${box2d_INCLUDE_DIRS}) #cocos2d lib needs the path too
target_link_libraries(${APP_NAME} Box2D)
target_include_directories(${APP_NAME}
	PRIVATE ${box2d_INCLUDE_DIRS}
)

That should work. The Box2D folder I use looks like this:
image

The folder tree should be like this:
image

@R101 I use 3.17 and there is already Box2D in external folder but folder structure is different than yours.

3.17 includes chipmunk as default in Additional Include Directories: I changed with $(EngineRoot)external\Box2D\include

I also enabled box2d by adding to Preprocessor Definitions CC_ENABLE_BOX2D_INTEGRATION=1 flag

and after this two step rebuild project.
builds outputs this error (error comes after adding this flag : CC_ENABLE_BOX2D_INTEGRATION=1):

#error: "Cannot recognize the target platform; are you targeting an unsupported platform?" (compiling source file ..\Classes\AppDelegate.cpp)

c:\users\abc\documents\mygame\cocos2d\cocos\platform\ccplatformconfig.h 144

Is there any way to switch Box2D

I am well aware of that, and while I don’t recall the exact reason for not using it, having Box2D as part of my game project allows me to use the latest version from the Box2D github repo. The details in my previous post enable you to do just that. Also, I wouldn’t have posted it unless it works, so whether you choose to go down that path or not is entirely up to you.

CC_ENABLE_BOX2D_INTEGRATION is only required if you want to use PhysicsSprite, but I’m not sure that it actually works as intended. The problem is that the PhysicsSprite class internally uses the cpBody type, which is from Chipmunk, even if Chipmunk disabled (CC_ENABLE_CHIPMUNK_INTEGRATION = 0). So, if you disable Chipmunk and enable Box2D, you suddenly run into compilation issues, complaining about missing the cpBody implementation. Perhaps someone forgot to add ‘#ifdef’ pre-processor checks for certain sections of code in that class.

Anyhow, you do not need to use that PhysicsSprite class at all. If see a use for it, then you can copy it’s entire contents to your own custom class, strip out the Chipmunk related code, and use that instead.

I will try your solution with latest Box2D source and post here details

In Box2D source there is CMakeLists.txt in folder root,
but your Box2D folder has additional files: UseBox2D.cmake and Box2Dconfig.cmake.in

second thing I placed new Box2D folder to cocos2d folder so cocos folder inside cocos2d and changed CMakeList.txt direction path regarding this.

And Box2D src folder doesnt contain include folder, but located in upper directory .
I moved include folder from upper directory and changed Box2D CMakeList.txt include folder paths

finally I build and got this (Visual Studio 17 ):

Error LNK1181 cannot open input file 'libbox2d.lib' (project: libcocos2d) C:\Users\abc\Documents\mygame\cocos2d\cocos\2d\LINK

,

Ignore those files, they aren’t important.

I’ve attached my version which include my custom CMakeLists.txt etc.
Box2D.zip (198.9 KB)

Do not put it in the cocos\external\ path. You shouldn’t have to modify anything in the Cocos2d-x code in order to use this.

Thank you I placed your Box2D folder into external folder that located on project root
Box2D property appeared on solution panel now it builds without error.

How to know current physics engine is set to box2d, I guess its still not switched.

So collition problem not fixed again, my sprite is standing at X,Y point and onContactBegin and onContactSeparate called :

contact called XY: 1213.585693, 331.709106
Separate called XY: 1213.585693, 331.709106

To check if Box2D is actually getting called, then put breakpoints or CCLOG statements in the code related to Box2D; you’ll find out pretty quickly.

You can also use Box2D directly (this is what I do in my own project), instead of going through the cocos2d-x built-in physics code. Judging by the source code, the cocos2d-x physics implementation may be reliant on Chipmunk, so if it’s not suitable for your needs, just use Box2D directly.

I didnt see any examples for Box2D usage with cocos2d-x is there any shared parts or completely different?

so do I need to start Box2D physics in Init() like this?

b2Vec2 gravity = b2Vec2(0.0f, -10.0f);
world = new b2World(gravity); 

I need to dig some tuturoials for binding box2d body to cocos2d sprite

This should help: http://www.lavaxp.net/integrating-box2d-with-cocos2d-x/

If you’re using Cocos2d-x v4, then you can’t use the GLESDebugDraw class in the above link for debug drawing of the physics objects, but use this implementation instead (which uses cocos2d::DrawNode): https://gist.github.com/rh101/5c1877500372281f69593ca1e1270956

@R101 thank you very much, I will read this tutorial, looks like tutorial quite enough detailed .