Пример #1
0
    Info(sf::String& text)
    {
        box.setPosition(100,100);
        box.setFillColor(sf::Color(245,248,6));
        box.setSize(sf::Vector2f(300,300));
        box.setOutlineColor(sf::Color::Red);
        box.setOutlineThickness(3);

        InfoText.setString(text);
        InfoText.setPosition(110,110);
        InfoText.setFillColor(sf::Color::White);
    }
Пример #2
0
void Init()
{
	GameWin.create(sf::VideoMode(800, 600, 32), "Physics Test - [SFML & Box2D]");

	if (!font.loadFromFile("arial.ttf"))
	{
		exit(0);
	}

	PauseText.setFont(font);

	pauseText = to_string(Paused);

	PauseText.setCharacterSize(10);
	PauseText.setString("PAUSED:" + pauseText);
	PauseText.setPosition(sf::Vector2f(700, 10));
	PauseText.setFillColor(sf::Color::Yellow);
	
	box.setPosition(sf::Vector2f(15, 5));
	box.setSize(sf::Vector2f(10, 10));
	box.setFillColor(sf::Color(255, 0, 0));

	box2.setPosition(sf::Vector2f(50, 5));
	box2.setSize(sf::Vector2f(15, 15));
	box2.setFillColor(sf::Color(0, 255, 0));

	ground.setPosition(sf::Vector2f(0, 540));
	ground.setSize(sf::Vector2f(800, 560));
	ground.setFillColor(sf::Color(0, 0,255));

	wall.setPosition(sf::Vector2f(1, 1));
	wall.setSize(sf::Vector2f(10, 580));
	wall.setFillColor(sf::Color(0, 0, 255));

	wall2.setPosition(sf::Vector2f(790, 1));
	wall2.setSize(sf::Vector2f(10, 580));
	wall2.setFillColor(sf::Color(0, 0, 255));

	World = new b2World(gravity);
	World->SetGravity(gravity);

	groundBodyDef.type = b2_staticBody;
	groundBodyDef.position.Set(0, 540);
	groundBody = World->CreateBody(&groundBodyDef);

	wallBodyDef.type = b2_staticBody;
	wallBodyDef.position.Set(10, 580);
	wallBody = World->CreateBody(&wallBodyDef);

	wallBodyDef2.type = b2_staticBody;
	wallBodyDef2.position.Set(790, 1);
	wallBody2 = World->CreateBody(&wallBodyDef2);

	ballBodyDef.type = b2_dynamicBody;
	ballVector.Set(10, 10);
	ballBodyDef.angularVelocity = 0.0f;
	ballBodyDef.linearVelocity = ballVector;

	ballBodyDef2.type = b2_dynamicBody;
	ballVector2.Set(15, 15);
	ballBodyDef2.angularVelocity = 0.0f;
	ballBodyDef2.linearVelocity = ballVector2;

	ballBodyDef.position.Set(15, 0);
	ballBodyDef.awake = true;
	Body = World->CreateBody(&ballBodyDef);

	ballBodyDef2.position.Set(30, 0);
	ballBodyDef2.awake = true;
	Body2 = World->CreateBody(&ballBodyDef2);

	boxShapeDef.shape = &groundBox;
	boxShapeDef.density = 2.0f;
	boxShapeDef.restitution = 0.5f;
	groundBox.SetAsBox(800, 0);

	groundBody->CreateFixture(&groundBox, 0);

	wallBoxDef.shape = &wallBox;
	wallBoxDef.density = 2.0f;
	wallBoxDef.restitution = 0.5f;
	wallBox.SetAsBox(1, 600);

	wallBody->CreateFixture(&wallBox, 0);

	wallBoxDef2.shape = &wallBox2;
	wallBoxDef2.density = 2.0f;
	wallBoxDef2.restitution = 0.5f;
	wallBox2.SetAsBox(1, 600);

	wallBody2->CreateFixture(&wallBox2, 0);

	dynamicBox.SetAsBox(10.0f, 10.0f);

	dynamicBox2.SetAsBox(10.0f, 10.0f);

	fixtureDef.shape = &dynamicBox;
	fixtureDef.density = 2.0f;
	fixtureDef.friction = 1.5f;
	fixtureDef.restitution = 0.9f;

	Body->CreateFixture(&fixtureDef);

	fixtureDef2.shape = &dynamicBox2;
	fixtureDef2.density = 5.0f;
	fixtureDef2.friction = 5.0f;
	fixtureDef2.restitution = 1.0f;

	Body2->CreateFixture(&fixtureDef2);

	timeStep = 1.0f / 600.0f;
	velIter = 1;
	posIter = 1;

	World->Step(timeStep, velIter, posIter);

	b2Vec2 pos = Body->GetPosition();
	float angle = Body->GetAngle();

	box.setPosition(pos.x, pos.y);
	box.setRotation(angle);

	b2Vec2 pos2 = Body2->GetPosition();
	float angle2 = Body2->GetAngle();

	box2.setPosition(pos2.x, pos2.y);
	box2.setRotation(angle2);
}