Hello everyone im new to c++, box2d, and SFML and haven't done anything more than small amount of visual basic in high school. I took a week to learn the basics of c++ and SFML and finally got a grasp on box2d until now.
Currently i have a small world with a few static bodies and one player body that moves via impulses around the screen. I have now started to attach a sprite to my player body and it works fine when my body is at full speed. During acceleration my sprite doesn't match up with my body until it hits full acceleration.
b2Vec2 pvel = pBody->GetLinearVelocity();
b2Vec2 desiredVel (2,2);
float velChangeXR = desiredVel.x - pvel.x;
float velChangeYD = desiredVel.y - pvel.y;
float velChangeXL = desiredVel.x + pvel.x;
float velChangeYU = desiredVel.y + pvel.y;
impulseXR = velChangeXR;
impulseYD = velChangeYD;
impulseXL = velChangeXL;
impulseYU = velChangeYU;
b2Vec2 movement(0.f, 0.f);
if (pMovingUp)
{
movement.y -= impulseYU;
pBody->ApplyLinearImpulse(b2Vec2(0,movement.y), pBody->GetWorldCenter() );
source.y = Direction::Up;
std::cout << pvel.y << std::endl;
}
if (pMovingDown)
{
movement.y = impulseYD;
pBody->ApplyLinearImpulse(b2Vec2(0,movement.y), pBody->GetWorldCenter() );
source.y = Direction::Down;
std::cout << pvel.y << std::endl;
}
if (pMovingLeft)
{
movement.x -= impulseXL ;
pBody->ApplyLinearImpulse(b2Vec2(movement.x,0), pBody->GetWorldCenter() );
source.y = Direction::Left;
std::cout << pvel.x << std::endl;
}
if (pMovingRight)
{
movement.x = impulseXR;
pBody->ApplyLinearImpulse(b2Vec2(movement.x,0), pBody->GetWorldCenter() );
source.y = Direction::Right;
std::cout << pvel.x << std::endl;
}
if (keyR == true)
{
pBody->SetLinearVelocity(b2Vec2(0,0));
pBody->SetAngularVelocity(0);
}
//sprite to pbody glue//////////////////////////////////////////////////////////////////
{
pbos = pBody->GetPosition();
pSprite.setPosition((pbos.x * sfdd::SCALE) - 16,(pbos.y * sfdd::SCALE) - 16);
//animation rect
pSprite.setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32));
}
}
//render//
worldA.Step(timeStep,velocityIterations,positionIterations);
worldA.DrawDebugData();
//mWindow.draw(ground);
mWindow.draw(pSprite);
mWindow.display();
Here is the code feel free to tell me im an idiot but please be helpful for others new to this
.