void PathBehavior::LoadPath(RuntimeScene & scene) { if(localePaths.count(pathName) == 0) { if (runtimeScenesPathDatas == NULL) runtimeScenesPathDatas = static_cast<RuntimeScenePathDatas*>(scene.GetBehaviorSharedData(name).get()); if(runtimeScenesPathDatas != NULL && runtimeScenesPathDatas->globalPaths.count(pathName) != 0) path = std::vector<sf::Vector2f>(runtimeScenesPathDatas->globalPaths.at(pathName)); } else { path = std::vector<sf::Vector2f>(GetPath(pathName)); } isPathLoaded = true; }
/** * Prepare Box2D body, and set up also runtimeScenePhysicsDatasPtr. */ void PhysicsBehavior::CreateBody(const RuntimeScene & scene) { if ( runtimeScenesPhysicsDatas == NULL ) runtimeScenesPhysicsDatas = static_cast<RuntimeScenePhysicsDatas*>(scene.GetBehaviorSharedData(name).get()); //Create body from object b2BodyDef bodyDef; bodyDef.type = dynamic ? b2_dynamicBody : b2_staticBody; bodyDef.position.Set((object->GetDrawableX()+object->GetWidth()/2)*runtimeScenesPhysicsDatas->GetInvScaleX(), -(object->GetDrawableY()+object->GetHeight()/2)*runtimeScenesPhysicsDatas->GetInvScaleY()); bodyDef.angle = -object->GetAngle()*b2_pi/180.0f; //Angles are inverted bodyDef.angularDamping = angularDamping > 0.0f ? angularDamping : 0.0f; bodyDef.linearDamping = linearDamping > 0.0f ? linearDamping : 0.0f; bodyDef.bullet = isBullet; bodyDef.fixedRotation = fixedRotation; body = runtimeScenesPhysicsDatas->world->CreateBody(&bodyDef); body->SetUserData(this); //Setup body if ( shapeType == Circle) { b2FixtureDef fixtureDef; b2CircleShape circle; circle.m_radius = (object->GetWidth()*runtimeScenesPhysicsDatas->GetInvScaleX()+ object->GetHeight()*runtimeScenesPhysicsDatas->GetInvScaleY())/4; //Radius is based on the average of height and width if ( circle.m_radius <= 0 ) circle.m_radius = 1; fixtureDef.shape = &circle; fixtureDef.density = massDensity; fixtureDef.friction = averageFriction; fixtureDef.restitution = averageRestitution; body->CreateFixture(&fixtureDef); } else if( shapeType == CustomPolygon && polygonCoords.size() > 2) { //Make a polygon triangulation to make possible to use a concave polygon and more than 8 edged polygons std::vector<sf::Vector2f> resultOfTriangulation; Triangulate::Process(polygonCoords, resultOfTriangulation); //Iterate over all triangles for(std::size_t i = 0; i < resultOfTriangulation.size() / 3; i++) { b2FixtureDef fixtureDef; b2PolygonShape dynamicBox; //Create vertices b2Vec2 vertices[3]; std::size_t b = 0; for(int a = 2; a >= 0; a--) //Box2D use another direction for vertices { if(polygonPositioning == OnOrigin) { vertices[b].Set((resultOfTriangulation.at(i*3 + a).x * GetPolygonScaleX() - object->GetWidth()/2 - (object->GetDrawableX() - object->GetX())) * runtimeScenesPhysicsDatas->GetInvScaleX(), (((object->GetHeight() - (resultOfTriangulation.at(i*3 + a).y * GetPolygonScaleY())) - object->GetHeight()/2 + (object->GetDrawableY() - object->GetY())) * runtimeScenesPhysicsDatas->GetInvScaleY())); } /*else if(polygonPositioning == OnTopLeftCorner) { vertices[b].Set((resultOfTriangulation.at(i*3 + a).x * GetPolygonScaleX() - object->GetWidth()/2 ) * runtimeScenesPhysicsDatas->GetInvScaleX(), (((object->GetHeight() - (resultOfTriangulation.at(i*3 + a).y * GetPolygonScaleY())) - object->GetHeight()/2) * runtimeScenesPhysicsDatas->GetInvScaleY())); }*/ else if(polygonPositioning == OnCenter) { vertices[b].Set((resultOfTriangulation.at(i*3 + a).x * GetPolygonScaleX()) * runtimeScenesPhysicsDatas->GetInvScaleX(), (((object->GetHeight() - (resultOfTriangulation.at(i*3 + a).y * GetPolygonScaleY())) - object->GetHeight()) * runtimeScenesPhysicsDatas->GetInvScaleY())); } b++; } dynamicBox.Set(vertices, 3); fixtureDef.shape = &dynamicBox; fixtureDef.density = massDensity; fixtureDef.friction = averageFriction; fixtureDef.restitution = averageRestitution; body->CreateFixture(&fixtureDef); } } else { b2FixtureDef fixtureDef; b2PolygonShape dynamicBox; dynamicBox.SetAsBox((object->GetWidth() > 0 ? object->GetWidth() : 1.0f)*runtimeScenesPhysicsDatas->GetInvScaleX()/2, (object->GetHeight() > 0 ? object->GetHeight() : 1.0f)*runtimeScenesPhysicsDatas->GetInvScaleY()/2); fixtureDef.shape = &dynamicBox; fixtureDef.density = massDensity; fixtureDef.friction = averageFriction; fixtureDef.restitution = averageRestitution; body->CreateFixture(&fixtureDef); } objectOldWidth = object->GetWidth(); objectOldHeight = object->GetHeight(); }