Hey guys,
I need your help.
Currently I am figuring out to migrate Box2D to an SDL2 project. There are several problems I cam across an solved:
- changing the “pixel coordinates” to cartesian coordinates and
- setting a scale factor (Pixels to Meters)
- drawing the whole thing
And it actually works, but for some reason the collision does not work anymore. I tried the Box2D Code in the testbed and there it works. That means the failure has to be located in SDL - in the rendering or in the translating from MKS to Pixels. So I thought you guys could help me out.
Normally it should look like this (Testbed):
But in SDL, it just “falls through” and doesn’t collide at all (ignore the low framerate, my laptop is just to weak to record something):
Here is my source code:
#include <iomanip>
#include <Box2D/Box2D.h>
#include <iostream>
#include <SDL2/SDL.h>
b2World* world;
const int MET2PIX = 80; // 640 / 80 = 8
const int WIDTH = 640;
const int HEIGHT = 480;
const int SCALED_WIDTH = WIDTH / MET2PIX; // 4 | 3
const int SCALED_HEIGHT = HEIGHT / MET2PIX;
using namespace std;
int main()
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window *window = SDL_CreateWindow("FirstGame", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, SDL_WINDOW_SHOWN);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
world = new b2World(b2Vec2(0.0f, 9.81f));
// LineGround
b2BodyDef myGroundDef;
myGroundDef.type = b2_staticBody;
myGroundDef.position.Set(0, 0); // set the starting position x and y cartesian
myGroundDef.angle = 0;
b2Body* groundLineBody = world->CreateBody(&myGroundDef);
b2EdgeShape edgeShape;
edgeShape.Set( b2Vec2(-1.5,0), b2Vec2(1.5,0) ); // length -> coordinate vector from to vector
b2FixtureDef edgeFixtureDef;
edgeFixtureDef.shape = &edgeShape;
groundLineBody->CreateFixture(&edgeFixtureDef);
// Box
SDL_Rect box;
b2Body* Body;
b2BodyDef boxBodyDef;
boxBodyDef.type = b2_dynamicBody;
boxBodyDef.angle = 45;
boxBodyDef.position.Set(0, 2.5);
Body = world->CreateBody(&boxBodyDef);
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(0.25, 0.25); // will be 0.5 x 0.5
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 1;
fixtureDef.friction = 0.3f;
fixtureDef.restitution = 0.5f;
Body->CreateFixture(&fixtureDef);
// box: convert Metres back to Pixels for width and height
box.w = 0.25 * MET2PIX;
box.h = 0.25 * MET2PIX;
bool close_game = false;
SDL_Event event;
// The game Loop
while(close_game != true)
{
b2Vec2 pos = Body->GetPosition(); // Body = Body from box
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
close_game = true;
else if(event.key.keysym.sym == SDLK_ESCAPE)
close_game = true;
}
box.x = ((SCALED_WIDTH / 2.0f) + pos.x) * MET2PIX;
box.y = (-(SCALED_HEIGHT / 2.0f) + pos.y) * MET2PIX;
cout << "X of box:" << setprecision(20) << box.x << endl;
cout << "Y of box:" << setprecision(20) << box.y << endl;
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 255, 0, 0);
// Draw ground platform
SDL_SetRenderDrawColor(renderer, 255, 255, 0, 0);
SDL_RenderDrawLine(renderer, ((SCALED_WIDTH / 2.0f) + edgeShape.m_vertex1.x) * MET2PIX, ((SCALED_HEIGHT / 2.0f) + edgeShape.m_vertex1.y) * MET2PIX, ((SCALED_WIDTH / 2.0f) + edgeShape.m_vertex2.x) * MET2PIX, ((SCALED_HEIGHT / 2.0f) + edgeShape.m_vertex2.y) * MET2PIX);
// Draw our Box
SDL_RenderDrawRect(renderer, &box);
SDL_RenderFillRect(renderer, &box);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderPresent(renderer);
world->Step(1.0f / 60.0f, 6.0f, 2.0f); // update
}
delete world;
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_SUCCESS;
}
I appreciate any support!
eder