//-------------------------------------------------------------- void ofApp::mouseReleased(int x, int y, int button) { if(breakupIntoTriangles) { // This is the manual way to triangulate the shape // you can then add many little triangles // first simplify the shape shape.simplify(); // save the outline of the shape ofPolyline outline = shape; // resample shape ofPolyline resampled = shape.getResampledBySpacing(25); // trangleate the shape, return am array of traingles vector <TriangleShape> tris = triangulatePolygonWithOutline(resampled, outline); // add some random points inside addRandomPointsInside(shape, 255); // now loop through all the trainles and make a box2d triangle for(int i=0; i<tris.size(); i++) { auto triangle = std::make_shared<ofxBox2dPolygon>(); triangle->addTriangle(ofDefaultVertexType(tris[i].a.x, tris[i].a.y, 0), ofDefaultVertexType(tris[i].b.x, tris[i].b.y, 0), ofDefaultVertexType(tris[i].c.x, tris[i].c.y, 0)); triangle->setPhysics(1.0, 0.3, 0.3); triangle->create(box2d.getWorld()); polyShapes.push_back(triangle); } } else { auto poly = std::make_shared<ofxBox2dPolygon>(); poly->addVertices(shape.getVertices()); poly->setPhysics(1.0, 0.3, 0.3); poly->triangulatePoly(); poly->create(box2d.getWorld()); polyShapes.push_back(poly); } // done with shape clear it now shape.clear(); }
Player::Player(TileMapLand* land) : Entity() , hitEnemy(false) , m_sprite_view(new AnimatedSpriteView(*this,"Animation/player.txt")) , m_plateforme_physics(new PlateformerPhysic(*this,land)) { name = "Player"; setBody(new Body(*this)); body()->setSize(sf::Vector2f(16,16)); body()->setOrigin(sf::Vector2f(8,8)); body()->setPosition(sf::Vector2f(38,38)); setPhysics(m_plateforme_physics); m_plateforme_physics->gravity = sf::Vector2f(0,0.3); m_plateforme_physics->max_speed = sf::Vector2f(2.0,8.0); m_plateforme_physics->jump_power = 8.0; m_plateforme_physics->walk_speed = 2.0; m_plateforme_physics->run_speed = 3.0; m_plateforme_physics->walk_deceleration = 0.3; setGamepad(new KeyBoardGamePad(*this)); setView(m_sprite_view); }
void JSRestoredVisibleData::updateFrom(const IPresencePropertiesRead& orig) { setLocation(orig.location()); setOrientation(orig.orientation()); setBounds(orig.bounds()); setMesh(orig.mesh()); setPhysics(orig.physics()); }
//-------------------------------------------------------------- void ofApp::update() { // add some circles every so often if((int)ofRandom(0, 10) == 0) { auto circle = std::make_shared<ofxBox2dCircle>(); circle->setPhysics(0.3, 0.5, 0.1); circle->setup(box2d.getWorld(), (ofGetWidth()/2)+ofRandom(-20, 20), -20, ofRandom(10, 20)); circles.push_back(circle); } // remove shapes offscreen ofRemove(circles, shouldRemove); // ofRemove(polyShapes, shouldRemove); box2d.update(); }
//-------------------------------------------------------------- void ofApp::keyPressed(int key) { if(key == '1') { auto circle = std::make_shared<ofxBox2dCircle>(); circle->setPhysics(0.3, 0.5, 0.1); circle->setup(box2d.getWorld(), mouseX, mouseY, ofRandom(10, 20)); circles.push_back(circle); } if(key == 't') breakupIntoTriangles = !breakupIntoTriangles; if(key == 'c') { shape.clear(); polyShapes.clear(); circles.clear(); } }
//-------------------------------------------------------------- void ofApp::setup() { ofDisableAntiAliasing(); ofBackgroundHex(0xfdefc2); ofSetLogLevel(OF_LOG_NOTICE); ofSetVerticalSync(true); // Box2d box2d.init(); box2d.setGravity(0, 10); box2d.createGround(); box2d.setFPS(30.0); breakupIntoTriangles = true; // load the shape we saved... auto pts = loadPoints("shape.dat"); auto poly = std::make_shared<ofxBox2dPolygon>(); poly->addVertices(pts); poly->setPhysics(1.0, 0.3, 0.3); poly->triangulatePoly(); poly->create(box2d.getWorld()); polyShapes.push_back(poly); }