box2d triangulation of static body

浪子不回头ぞ 提交于 2019-12-24 07:29:12

问题


I have posted on this before, but to no avail. Sorry for slightly repeating myself but I have tried to figure this out and I haven't been able to. I am probably not understanding something in box2d. I therefore wrote a very simplified example to highlight the issue.

In my code I simply create a rectangle. I triangulate it and then I add it to a box2d world. If I define the object as a dynamic object (density > 0) it is reacting to the walls and objects within the scene see images here.

If I declare it as a static (density=0) then the triangulation is messed up, as you can see in the link above. Basically it's the setPhysics line that makes all the difference between the two scenarios.

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup()
{
    // Box2d
    box2d.init();
    box2d.createGround();
    box2d.setFPS(30.0);
}

//--------------------------------------------------------------
void ofApp::update()
{
    shape.clear();

    //A rectangle with a chunk taken out, making a concave shape
    shape.addVertex(ofPoint(mouseX, mouseY));
    shape.addVertex(ofPoint(mouseX+0, mouseY+200));
    shape.addVertex(ofPoint(mouseX+200, mouseY+200));
    shape.addVertex(ofPoint(mouseX+200, mouseY));

    shape.simplify();
    ofPolyline outline = shape;
    ofPolyline resampled = shape.getResampledBySpacing(45);

    vector <TriangleShape> tris;
    tris = triangulatePolygonWithOutline(resampled, outline);

    polyShapes.clear();

    // now loop through all the triangles and make a box2d triangle
    for (int i=0; i<tris.size(); i++)
    {
          ofPtr<ofxBox2dPolygon> triangle = ofPtr<ofxBox2dPolygon>(new ofxBox2dPolygon);
          triangle.get()->addTriangle(tris[i].a, tris[i].b, tris[i].c);
          triangle.get()->setPhysics(1, 0, 0); //density, bounce, friction
          triangle.get()->create(box2d.getWorld());
          //triangle.get()->body->SetType(b2_staticBody);
          polyShapes.push_back(triangle);
    }

    box2d.update();
}

//--------------------------------------------------------------
void ofApp::draw()
{
    ofBackground(125);

    ofSetColor(255);
    ofNoFill();
    for (int i=0; i<polyShapes.size(); i++)
    {
        polyShapes[i].get()->draw();
        ofCircle(polyShapes[i].get()->getPosition(), 3);
    }
}

The code for the above program can be downloaded here (for openFrameworks). Any help very much appreciated. I have to deliver something to a client and have been hitting my head against the wall with this one.

来源:https://stackoverflow.com/questions/23386433/box2d-triangulation-of-static-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!