![]() First of all I'm new to gaming, and in that i'm working with lua, which is another new thing for me. I'm not getting any idea what has to be done. Now when I throw the pumpkin(box2d body) and when it falls on the spring top, the top of the spring falls down. ![]() I don't want any mouse event on Spring so I've not registered any mouse event for spring, in fact when i throw pumpkin(box2d body) and if it falls on the spring top it must show normal spring behavior. Stage:addChild (spring ) local radius = spring:getWidth ( ) / 2 local body = self.world:createBody Spring = Bitmap.new (Texture.new (img ) ) Self.springTop = self:CreateSpring (x, 190, "images/SpringTOp.png" ) - Creating Distance Joint local jointDef = b2.createDistanceJointDef (,, x, 250, x, 250 ) local distanceJoint = self.world:createJoint (jointDef )ĭistanceJoint:setLength ( 35 ) function LoadGame:CreateSpring (x, y, img ) I've created a small complete-ish example here on coliru of this = self:CreateSpring (x, 250, "images/Springs.png" ) 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 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 ![]() 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. 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 ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |