fr3sh

Hi

When I try to flip X a character character flips ok but the BB are not. The vertices are the same when I try to getVertices.

Before flip X
bbfoot: nazwa
[0.2565152, -0.31099072, 0.7490966, -0.33414736, 1.1143589, -0.2804874, 0.6752386, 0.3776338, 0.3620068, 0.28933164, 0.09143463, 0.14965858]

After flip X
bbfoot: nazwa
[0.2565152, -0.31099072, 0.7490966, -0.33414736, 1.1143589, -0.2804874, 0.6752386, 0.3776338, 0.3620068, 0.28933164, 0.09143463, 0.14965858]
fr3sh
  • Messaggi: 32

Søren

Which runtime?
Avatar utente
Søren

Shiu
  • Messaggi: 2453

fr3sh

libgdx :)
fr3sh
  • Messaggi: 32

fr3sh

Is there someone to help? do I must to recreate the box2d polygons?
fr3sh
  • Messaggi: 32

fr3sh

OK I made a simple example to better understand the problem.

Here is an arrow with bb perfectly aligned.


When i set flipX every thing is ok except the bb



Here is the code of create metod
public void create() {

kartka = new SpriteBatch();
shapeRenderer = new ShapeRenderer();

swiat = new World(new Vector2(0, -9.81f), true );
debugRenderer = new Box2DDebugRenderer();

camera = new OrthographicCamera(Gdx.graphics.getWidth() /20, Gdx.graphics.getHeight() /20);

renderer = new SkeletonRenderer();
debugRenderer2 = new SkeletonRendererDebug();

atlas = new TextureAtlas(Gdx.files.internal("spineboy/skeleton.atlas"));

SkeletonJson json = new SkeletonJson(atlas);

json.setScale(0.09f); // scale
debugRenderer2.setScale(json.getScale());
SkeletonData skeletonData = json.readSkeletonData(Gdx.files.internal("spineboy/skeleton.json"));

skeleton = new Skeleton(skeletonData);
skeleton.setFlipX(true);

AnimationStateData stateData = new AnimationStateData(skeletonData);
state = new AnimationState(stateData);

state.apply(skeleton); // Poses skeleton using current animations. This sets the bone's local SRT.
skeleton.updateWorldTransform();

bounds = new SkeletonBounds();
bounds.update(skeleton, true);

PolygonShape boxPoly1;
for (Slot slot : skeleton.getSlots()) {
if (slot.getAttachment() != null){
String s = slot.getAttachment().getName();

if ( slot.getAttachment() instanceof BoundingBoxAttachment ){
Gdx.app.log(slot.getAttachment().getName(), " nazwa ");

Box2dAttachment box2 = new Box2dAttachment(slot.getAttachment().getName());
box2.att = (BoundingBoxAttachment)slot.getAttachment();
//s.equals(a.nazwa

boxPoly1 = new PolygonShape();
boxPoly1.set(box2.att.getVertices());

float a[] = box2.att.getVertices();
System.out.println(Arrays.toString(a));

float x = skeleton.getX() + slot.getBone().getWorldX() ;
float y = skeleton.getY() + slot.getBone().getWorldY();

float rotation = slot.getBone().getWorldRotation() ;

BodyDef cos = new BodyDef();
FixtureDef fizyka = new FixtureDef();
cos.type = BodyType.StaticBody;
cos.angularDamping = 1.8f;
fizyka.density = 2.5f;
fizyka.friction = 0.4f;
fizyka.restitution = 0.6f;

fizyka.shape = boxPoly1;
fizyka.filter.groupIndex = -1;

box2.body =swiat.createBody(cos);
box2.body.createFixture(fizyka);
box2.body.setTransform(x , y , rotation * MathUtils.degRad);

boxPoly1.dispose();
}}}}
Vertices are the same before and after flip:
bb: nazwa
[-0.3073149, -1.0631596, 3.3688884, -1.034258, 3.3532636, 0.9528781, -0.29007906, 0.9573554]
bb2: nazwa
[3.344621, -2.160562, 6.67297, -0.014652501, 3.3444107, 2.0789216]

The debug renderer works ok and flips the bb.

---

Does any one use flip X and BB? I'm alone with this problem or what?

Nate where are you? ;)
fr3sh
  • Messaggi: 32

Nate

The local vertices for a bounding box don't change when the skeleton is flipped. Flipping the skeleton only changes the world vertices which are computed from the local vertices and bone transform. Flipping is accomplished by flipping the bone transform, so when it is applied the flip occurs. You'll need to flip your Box2D body.
Avatar utente
Nate

Nate
  • Messaggi: 12210

fr3sh

But for the debug rendering it's works perfectly why? do you use world vertices there?
fr3sh
  • Messaggi: 32

Nate

Yep.
Avatar utente
Nate

Nate
  • Messaggi: 12210

fr3sh

Nate as far as I know there is now way to flip box2d body. It's can be done only by destroying body and recreate. Could you give me a hint how to compute local vertices after flip. I have tried to use the code below but I failed:


float m00 = slot.getBone().getM00();
float m01 = slot.getBone().getM01();
float m10 = slot.getBone().getM10();
float m11 = slot.getBone().getM11();
float[] vertices = box2.att.getVertices(); //BoundingBoxAttachment
float[] worldVertices = new float[vertices.length];


for (int i = 0, n = vertices.length; i < n; i += 2) {
float px = vertices[i];
float py = vertices[i + 1];
worldVertices[i] = px * m00 + py * m01 + xi;
worldVertices[i + 1] = px * m10 + py * m11 + yi;
}
PolygonShape boxPoly1;
boxPoly1 = new PolygonShape();
boxPoly1.set(worldVertices);
fr3sh
  • Messaggi: 32

Nate

I guess you can destroy and recreate the body, or you could have two bodies and use the correct one based on the flip (more if you are flipping on both x and y).

Flipping occurs around the origin, so I believe it would be correct for the local vertices to simply inverse the signs.
float[] localVertices = box2.att.getVertices(); //BoundingBoxAttachment
for (int i = 0, n = vertices.length; i < n; i += 2) {
float px = localVertices[i];
float py = localVertices[i + 1];
if (flipX) px = -px;
if (flipY) py = -py;
// do something with local vertex
}
Avatar utente
Nate

Nate
  • Messaggi: 12210

fr3sh

No way it's so simple. Why I didn't figure it out. It's working! Thanks NATE
fr3sh
  • Messaggi: 32

Nate

Haha, great! :)
Avatar utente
Nate

Nate
  • Messaggi: 12210


Torna a Bugs