Answer the question
In order to leave comments, you need to log in
Why does SFML rotate the square incorrectly?
The square falls up (box2d does everything upside down since SFML counts coordinates from the top left corner).
The square falls with an angle of 10 degrees, it seems to become true (10 degrees clockwise), they fall together evenly and synchronously.
However, the square does not bounce off the floor at an angle, but as if the floor is higher, but when it jumps at the corners, it lands normally. The problem is jumping at the corners, they are not correct in height.
#include <iostream>
#include <SFML/Graphics.hpp>
#include <Box2D/Box2D.h>
#include <Windows.h>
using namespace std;
using namespace sf;
int main()
{
//в отчёт идёт сверху внихз, левый верхний угол это (0.0)
RenderWindow window(VideoMode(800, 600), "Box2D + SFML");
window.clear(Color::Cyan);
RectangleShape rect;
rect.setOrigin(25, 25);
rect.setFillColor(Color::Black);
rect.setSize(Vector2f(50, 50));
rect.setPosition(Vector2f(0, 0));
//устанавливаем гравитацию
b2Vec2 gravity(0.0f, 50.0f);
//создаём мир
b2World world(gravity);
//создаём определение земли
b2BodyDef groundBodyDef;
//устанавливаем позицию, 0 по x и 600 по высоте
groundBodyDef.position.Set(0.0f, 625.0f);
//создаём тело земли на основе определения
b2Body* groundBody = world.CreateBody(&groundBodyDef);
//создаём фигуру земли
b2PolygonShape groundBox;
//делаем её прямоугольником и скрепляем тело с фигурой
groundBox.SetAsBox(100000.0f, 0.0f);
groundBody->CreateFixture(&groundBox, 0.0f);
b2PolygonShape shape;
shape.SetAsBox(50.0f, 50.0f);
b2BodyDef bodyDef;
bodyDef.position.Set(350, 0);
bodyDef.type = b2_dynamicBody;
bodyDef.angle = (10.0f * b2_pi) / 180.0; //угол в радианах
b2Body* body = world.CreateBody(&bodyDef);
b2FixtureDef fd;
fd.shape = &shape;
fd.density = 1.0f;
fd.restitution = 0.75f;
body->CreateFixture(&fd);
while (window.isOpen())
{
window.clear(Color::Cyan);
Event event;
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
window.close();
}
cout << "1: x - " << body->GetPosition().x << " y - " << body->GetPosition().y << " " << (body->GetAngle() / b2_pi) * 180.0f << "\n";
world.Step(1.0f / 60.0f, 6, 2);
rect.setPosition(Vector2f(body->GetPosition().x, body->GetPosition().y)); //позиция
rect.setRotation((body->GetAngle() / b2_pi) * 180.0f); //угол в градусах
Sleep(10);
window.draw(rect);
window.display();
}
return 0;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question