![]() ![]() Pygame, on the other hand, increases in the # in high school, with positive x and y directions going # But wait! It's upside-down! Pygame and Box2D orient their # vertex, and then convert from meters to pixels with the scale # We take the body's transform and multiply it with each # Naively assume that this is a polygon shape. # The fixture holds information like density and friction, # The body gives us the position and angle of its shapes # The user closed the window or pressed escape If event.type = QUIT or (event.type = KEYDOWN and event.key = K_ESCAPE): Screen = _mode((SCREEN_WIDTH, SCREEN_HEIGHT), 0, 32, display=1) In issues like this: mouseWorld Coordinates Box2D there is a method: ordPixelsToWorld(x,y) but I can't find this method in the Python library for box2d. I've created a small complete-ish example here on coliru of this workflow.I can't figure out the proper way to get the world coordinates from the mouse coords. 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. |