Question

Hi I have a box2d compound body which is intended to be a vehicle seen in profile. when it goes backwards i'd like it to face the other way ie flip/reflect on the x axis. is there an easy way to so this? I find nothing in google though much false hits due to other uses of word reflection.

I could recreate the body but I'd have to do the whole out-of-time delete/add thing? and it seems a bit cumbersome. i tried body.getFixtureList and setting the vertices but tyhe method is flagged don't change the list. does that mean don't add remove? or don't edit? Anyways nothing changes on screen. Off to double-check my code...

Am I missing something?

Was it helpful?

Solution

Changing the vertices won't work. Only possibility to do that is to recreate the body as you said. And don't think that it will be slow just because you will have to delete it and then create it again. Box2d could probably create 100 of your vehicles in one frame without having too much of a struggle.

And for the whole flipping process you would scale your vertices x component by -1. And if your vertices origin wasn't in center of shape you would have to adjust the x coordinate of the whole vehicle (or just have the origin properly placed in the first place).

Also when recreating the body you would probably want to save the linear and angular velocity of your original body. And then once you create the new one apply those velocities to the new body. If you don't do that you would make your vehicle freeze once you do the flip.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top