

- #GIDEROS BOX2D KEEP BODY UPRIGHT HOW TO#
- #GIDEROS BOX2D KEEP BODY UPRIGHT MANUALS#
- #GIDEROS BOX2D KEEP BODY UPRIGHT CODE#
create a ground body and attach an edge shape this table holds the dynamic bodies and their sprites

Local sad = Bitmap.new(Texture.new("sad-box.png", true)) Local happy = Bitmap.new(Texture.new("happy-box.png", true)) this function creates a box sprite with 2 happy and sad children What is difference between edgeshape.new() and polygonshape.new()? require "box2d" I've created a small complete-ish example here on coliru of this workflow.In below mentioned code, what exactly is the difference between sprite, body and ground? Why are there no parameters passes to createBody while declaring ground? This is a trivial example mFixtureUserData could also be an std::map> to allow faster search.īut you get the gist of it, the idea is to keep track of your user data in some way an make sure you clean it when you destroy the associated Box2d objects. Std::swap( mFixtureUserData, mFixtureUserData ) īody->DestroyFixture(myFixtureToDestroy) įor ( int i = 0 i mOwningFixture = myFixtureToDestroy ) Then when you destroy the fixture, you can remove the associated instance of MyFixtureUserDataType. MFixtureUserData.emplace_back( std::move(myUserData) ) MyUserData->mOwningFixture = body->CreateFixture( fixtureDef ) MyUserData->mObjectType = 3 // whatever this fixture is aboutį = reinterpret_cast(myUserData.get()) Then when you create your fixtures, you also create the data: // Store this somewhere in your game managing classes
#GIDEROS BOX2D KEEP BODY UPRIGHT CODE#
So somewhere in your code you need to create your own class: struct MyFixtureUserDataType Since the userData deals with "opaque pointers" (Box2d does not know what those are nor what they're for, only you do), and the example uses raw pointers, Box2d will not clean the memory associated to those once the Box2d objects are destroyed, you need to take care of cleaning those.Ī way to do it is to wrap those into smart pointers. The user data is an object that you control and you need hold alive for the whole life of the box2d object.

This part of the official documentation illustrates how this should be done now, however it lacks an important information, IMHO. So it may explain why you see tutorials around that "do no work" with a more recent version of Box2d. They went from storing void pointers to storing an actual object. It seems that box2d recently changed the way they manage userData. When I did something like this, the program is crashing: struct B2_API b2FixtureUserData Next to the structure definition, it is written "You can define this to inject whatever data you want in b2Fixture". Previously, it was possible, as I understood, to create a body and write: tUserData = smth īut this is not possible because the method returns a structure: b2BodyUserData& b2Body::GetUserData() Void* fixtureUserData = contact->GetFixtureA()->GetUserData() įixturesUnderfoot.erase(contact->GetFixtureB()) //A is foot so B is groundįixturesUnderfoot.erase(contact->GetFixtureA()) //B is foot so A is ground Std::string fixtureUserData = contact->GetFixtureA()->getmy() įixturesUnderfoot.insert(contact->GetFixtureB()) //A is foot so B is groundįixtureUserData = contact->GetFixtureB()->GetUserData() įixturesUnderfoot.insert(contact->GetFixtureA()) //B is foot so A is ground This is what I want to do: class M圜ontactListener : public b2ContactListener
#GIDEROS BOX2D KEEP BODY UPRIGHT HOW TO#
But I don't understand how to do this correctly, because just adding a line to the structure of the program crashes. I found out that it is possible to edit the b2FixtureDef structure to add my new data there. After digging through the Box2d files for three days, I realized that I could not cope alone. Now there is no such function and userData should be set in a different way.
#GIDEROS BOX2D KEEP BODY UPRIGHT MANUALS#
But with the source of Box2d I have, I don't see the SetUserData functions in the code as I see them in the guides.Īfter understanding it, I realized that these manuals were outdated, and
