//---------------------------------------- void ofxBox2dEdge::updateShape() { if(body==NULL) return; const b2Transform& xf = body->GetTransform(); ofPolyline::clear(); mesh.clear(); mesh.setUsage(body->GetType()==b2_staticBody?GL_STATIC_DRAW:GL_DYNAMIC_DRAW); mesh.setMode(OF_PRIMITIVE_LINE_STRIP); for (b2Fixture * f = body->GetFixtureList(); f; f = f->GetNext()) { b2EdgeShape * edge = (b2EdgeShape*)f->GetShape(); if(edge) { ofPolyline::addVertex(worldPtToscreenPt(edge->m_vertex2)); ofPolyline::addVertex(worldPtToscreenPt(edge->m_vertex1)); mesh.addVertex(ofVec3f(worldPtToscreenPt(edge->m_vertex2))); mesh.addVertex(ofVec3f(worldPtToscreenPt(edge->m_vertex1))); } } bFlagShapeUpdate = true; // Temporary hack to ensure it's flagged as changed, until we // switch to OF 0.8.0. setClosed(isClosed()); }
//---------------------------------------- vector <ofPoint>& ofxBox2dPolygon::getPoints() { if(body == NULL) { return ofPolyline::getVertices(); } bool wasClosed = isClosed(); const b2Transform& xf = body->GetTransform(); ofPolyline::clear(); ofPolyline::setClosed(wasClosed); for (b2Fixture * f = body->GetFixtureList(); f; f = f->GetNext()) { b2PolygonShape * poly = (b2PolygonShape*)f->GetShape(); if(poly) { for(int i=0; i<poly->GetVertexCount(); i++) { ofPolyline::addVertex( worldPtToscreenPt(b2Mul(xf, poly->GetVertex(i))) ); } if(isClosed()) ofPolyline::close(); } } return ofPolyline::getVertices(); }
//---------------------------------------------- void ttChar::drawBox2dObject(){ ofSetColor(255, 30, 220,100); character.draw(); if (step>0) { start.draw(); joint.draw(); } //draw sensor ofPolyline shape; const b2Transform& xf = character.body->GetTransform(); for (b2Fixture* f = character.body->GetFixtureList(); f; f = f->GetNext()) { if (f->IsSensor()) { ofSetColor(255,30,230,100); // ofCircle(sensor.getPosition(),f->GetShape()->m_radius*OFX_BOX2D_SCALE); b2PolygonShape* poly = (b2PolygonShape*)f->GetShape(); if(poly) { for(int i=0; i<poly->m_vertexCount; i++) { b2Vec2 pt = b2Mul(xf, poly->m_vertices[i]); shape.addVertex(worldPtToscreenPt(pt)); } } } } shape.setClosed(true); ofPath path; for (int i=0; i<shape.size(); i++) { if(i==0)path.moveTo(shape[i]); else path.lineTo(shape[i]); } path.setColor(ofGetStyle().color); path.setFilled(ofGetStyle().bFill); path.draw(); }
//------------------------------------------------ ofPolyline& ofxBox2dRect::getRectangleShape() { if(isBody()) { shape.clear(); const b2Transform& xf = body->GetTransform(); for (b2Fixture* f = body->GetFixtureList(); f; f = f->GetNext()) { b2PolygonShape* poly = (b2PolygonShape*)f->GetShape(); if(poly) { for(int i=0; i<poly->m_count; i++) { b2Vec2 pt = b2Mul(xf, poly->m_vertices[i]); shape.addVertex(worldPtToscreenPt(pt)); } } } } // we are a rectangle so close it shape.setClosed(true); return shape; }