Sunday, June 5, 2022
HomeGame Developmentc++ - Generally participant strikes an excessive amount of

c++ – Generally participant strikes an excessive amount of


I am engaged on a easy platform recreation.
I’ve the participant – a dynamic physique, which strikes by ApplyLinearImpulse.
Usually it really works very properly, however generally the participant strikes to a lot.

For instance – I am making brief clicks on the keyboard, each click on the participant strikes for instance 50 pixels, however as soon as in a number of clicks it’ll transfer 200-300 pixels.

The code of the motion:

void GameObject::move_phisical_body_horizontally(const float x)
{
    float velChange = x - m_phisical_body->GetLinearVelocity().x;
    float h_impulse = m_phisical_body->GetMass() * velChange;
    m_phisical_body->ApplyLinearImpulse(b2Vec2(h_impulse, 0), m_phisical_body->GetWorldCenter(), true);
}

the code of the bottom object: (the bottom is a group of floor tiles)

auto& texture = Assets::occasion().getTexture(GroundTexture);
auto width = texture.getSize().x;
auto hieght = texture.getSize().y;
auto place = this->convert_position(position_vec, { width, hieght });

b2BodyDef bodyDef;
bodyDef.kind = b2_staticBody;
bodyDef.userData.pointer = reinterpret_cast<uintptr_t>(this);
b2Body* physique = world->CreateBody(&bodyDef);


b2Vec2 v0((place.x - width) / SCALE, place.y / SCALE);
b2Vec2 v1(place.x / SCALE, place.y / SCALE);
b2Vec2 v2((place.x + width) / SCALE, place.y / SCALE);
b2Vec2 v3((place.x + width * 2) / SCALE, place.y / SCALE);

b2EdgeShape edge1;
b2EdgeShape edge2;

edge1.SetOneSided(v0, v1, v2, v3);
edge2.SetTwoSided(v1, v2);

b2FixtureDef FixtureDef1;
b2FixtureDef FixtureDef2;
FixtureDef1.density = 0.f;
FixtureDef1.form = &edge1;
FixtureDef2.friction = 1;
FixtureDef2.form = &edge2;
body->CreateFixture(&FixtureDef1);
body->CreateFixture(&FixtureDef2);

m_phisical_body = physique;

The code of the participant:

auto width = 32u;
auto hieght = 32u;
sf::Sprite sprite(Assets::occasion().getTexture(PlayerTexture));
sprite.setPosition(position_vec.x * width, position_vec.y * hieght);
sprite.setTextureRect(sf::IntRect({ 0, 0, (int)width, (int)hieght }));
sprite.setOrigin(16.f, 16.f);

auto player_obj = std::make_unique<Participant>(sprite, std::vector<int>{ 11, 12, 1, 1, 1 }, "participant");
participant = *player_obj.get();

auto place = player_obj->convert_position(position_vec, { width, hieght });

b2BodyDef BodyDef;
BodyDef.place = b2Vec2(place.x / SCALE, place.y / SCALE);
BodyDef.kind = b2_dynamicBody;
BodyDef.userData.pointer = reinterpret_cast<uintptr_t>(&participant);
b2Body* physique = world->CreateBody(&BodyDef);
body->SetFixedRotation(true);


b2PolygonShape Form;
Form.SetAsBox((width / 2.f - 1) / SCALE, (hieght / 2.f - 1) / SCALE);
b2FixtureDef FixtureDef;
FixtureDef.density = 1.f;
FixtureDef.friction = 1.f;
FixtureDef.form = &Form;
body->CreateFixture(&FixtureDef);
participant.set_phisical_body(physique);

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments