This is my first post here....
-> so "Hi" to everyone :D
I am currently writing a platformer like game, which I want to be playable for two or more persons.
Because I need good physics for it (Players should be able to push each other into traps etc.) I replaced my own poor physics with Box2D.
Unfortunally it behaves a bit weird, because the Box2D world seems always to be a bit shifted to the rendered world, so that the characters are half in the ground (see image).
Besides that i use a tilemap and the characters are sometimes stuck between two tiles, although they are on the same level....
The function which converts my map object into a b2World looks like this:
Piece #1:
(out of map converter)
Given Variables:
b2World world;
std::vector<extendedSprite *> const & tiles; //<- extendedSprite inherits from sf::Sprite
std::vector<playerClass *> const & players; //<- playerClass inherits from extendedSprite
Code Piece:
size_t i;
extendedSprite * eSpritePtr = NULL;
//Convert Tiles
for (i = 0; i < tiles.size(); i++)
{
eSpritePtr = tiles.at(i);
eSpritePtr->b2_init(world, b2_staticBody);
eSpritePtr->physBody->SetSleepingAllowed(true);
eSpritePtr->physBody->SetAwake(false);
}
//Convert players
for (i = 0; i < players.size(); i++)
{
eSpritePtr = players.at(i);
eSpritePtr->b2_init(world, b2_dynamicBody);
eSpritePtr->physBody->SetFixedRotation(true);
}
Piece #2:
(out of extendedSprite Class)
Given variables:
b2Body * physBody;
float xVelocity; //Of the Sprite
float yVelocity;
Code piece:
void extendedSprite::b2_init(b2World & world, b2BodyType bodyType)
{
b2BodyDef def;
def.position = SF_B2_Translator::toB2Vector(getPosition()); //"Converts" Pixels to Meters by dividing them by 48
def.type = bodyType;
physBody = world.CreateBody(&def);
b2FixtureDef fixtureDef;
b2PolygonShape shape;
shape.SetAsBox( SF_B2_Translator::toMeters(getGlobalBounds().width / 2), SF_B2_Translator::toMeters(getGlobalBounds().height / 2)); //Again pixel to meter conversion
fixtureDef.density = 70.f;
fixtureDef.friction = 0.7f;
fixtureDef.shape = &shape;
physBody->CreateFixture(&fixtureDef);
if (bodyType == b2_dynamicBody)
{
physBody->SetLinearVelocity(b2Vec2( SF_B2_Translator::toMeters(xVelocity), SF_B2_Translator::toMeters(yVelocity)));
}
}
...
Later i update the player like this:
xVelocity = SF_B2_Translator::toPixels(physBody->GetLinearVelocity().x); //"Converts" Meters to Pixels by multiplying by 48
yVelocity = SF_B2_Translator::toPixels(physBody->GetLinearVelocity().y);
setPosition( SF_B2_Translator::toSFVector(physBody->GetPosition()));
...
(Hope that wasn't to much code...)
I can't find my mistakes..... Do you have any ideas?
Is Box2D a good choice at all, if i want my game to be playable over network later?
Thanks in advance,
realloc()
P.S. Sry for my bad English......