我如何重置接触时的角色位置?

问题描述 投票:0回答:1

我在2D平台游戏中有一个角色,并且我也有一个在两点之间移动的敌人方块。

在联系时,我想将角色的位置重置为原来的位置,因为它已经死亡。

使用我当前的代码,玩家的位置仅在与敌人的固定装置接触时被设置为原始位置,而没有像预期的那样停留在该位置。

非常感谢您的帮助,因为我无法弄清解决该问题需要采取的措施。

character.cpp

/*!
\file character.cpp
*/

#include "character.h"

Character::Character(b2World * world, const sf::Vector2f& position, const sf::Vector2f &size, const float orientation, sf::Texture *texture)
{
    b2BodyDef l_bodyDef;
    b2PolygonShape l_shape;
    b2FixtureDef l_fixtureDef;

    l_bodyDef.position.Set(position.x, position.y);
    l_bodyDef.angle = orientation * DEG2RAD;
    l_bodyDef.type = b2_dynamicBody;

    m_body = world->CreateBody(&l_bodyDef);
    m_body->SetFixedRotation(true);
    m_body->SetUserData(this); // used by our contact listener

    l_shape.SetAsBox(size.x * 0.5f, size.y * 0.5f);
    l_shape.m_radius = 0.0f;

    l_fixtureDef.density = mk_fDensity;
    l_fixtureDef.friction = mk_fFriction;
    l_fixtureDef.restitution = 0.f;
    l_fixtureDef.shape = &l_shape;

    m_body->CreateFixture(&l_fixtureDef);

    l_shape.SetAsBox(size.x * 0.4, size.y * 0.001f, b2Vec2(0.f, size.y * 0.5f), 0.f);
    b2Fixture* bodyFixture = m_body->CreateFixture(&l_fixtureDef);
    bodyFixture->SetUserData((void *)PhysicalThing::CHARACTER);

    l_shape.SetAsBox(size.x * 0.4f, size.y * 0.001f, b2Vec2(0.f, size.y * 0.5f), 0.f);
    l_fixtureDef.isSensor = true; //Make it a sensor
    b2Fixture* footSensorFixture = m_body->CreateFixture(&l_fixtureDef);
    footSensorFixture->SetUserData((void *)PhysicalThing::CHARACTER);

    setPosition(position);
    setSize(size);
    setOrigin(size * 0.5f);
    setTexture(texture);
    setTextureRect(sf::IntRect(0, 0, 28, 68));
    setRotation(orientation);
    setOutlineThickness(0.f);

    originPosition = position;
}

void Character::update()
{
    b2Vec2 pos = m_body->GetPosition();
    setPosition(pos.x, pos.y);
    float angle = m_body->GetAngle()* RAD2DEG;
    setRotation(angle);

    b2Vec2 vel = m_body->GetLinearVelocity();
    float velChange = mySpeed - vel.x;
    float impulse = m_body->GetMass() * velChange;
    m_body->ApplyLinearImpulseToCenter(b2Vec2(impulse, 0.f), true);

    if (m_movingStates[LEFT])
    {
        mySpeed = -2.0f;
        direction = -1;
        setTextureRect(sf::IntRect(81, 0, 54, 66));
        setScale(direction * 2, 1);
    }

    if (m_movingStates[RIGHT])
    {
        mySpeed = 2.0f;
        direction = 1;
        setTextureRect(sf::IntRect(81, 0, 54, 66));
        setScale(direction * 2, 1);
    }

    if (m_sit)
    {
        if((int)sitType->GetUserData() == PhysicalThing::KINEMATICBLOCK)
        {
            m_body->SetLinearVelocity(b2Vec2(m_body->GetLinearVelocity().x + sitType->GetBody()->GetLinearVelocity().x, m_body->GetLinearVelocity().y + sitType->GetBody()->GetLinearVelocity().y));
        }
    }

    if (damage)
    {
        setPosition(originPosition);
    }

    if (m_movingStates[JUMP] && m_sit)
    {
        m_body->ApplyLinearImpulseToCenter(b2Vec2(0.f, -0.35f), true);
        setTextureRect(sf::IntRect(81, 0, 54, 66));
        setScale(direction * 2, 1);
    }

    if (!m_movingStates[LEFT] && !m_movingStates[RIGHT])
    {
        mySpeed = 0.f;
        setTextureRect(sf::IntRect(0, 0, 28, 68));
        setScale(direction, 1);
    }
}

void Character::onKeyPress(sf::Event event)
{
    if (event.key.code == sf::Keyboard::Left)m_movingStates[LEFT] = true;
    if (event.key.code == sf::Keyboard::Right)m_movingStates[RIGHT] = true;
    if (event.key.code == sf::Keyboard::Space)m_movingStates[JUMP] = true;
}

void Character::onKeyRelease(sf::Event event)
{
    if (event.key.code == sf::Keyboard::Left) m_movingStates[LEFT] = false;
    if (event.key.code == sf::Keyboard::Right) m_movingStates[RIGHT] = false;
    if (event.key.code == sf::Keyboard::Space) m_movingStates[JUMP] = false;
}

void Character::sitOn(b2Fixture * other)
{
    m_sit = true;
    sitType = other;
}

void Character::sitOff(b2Fixture * other)
{
    m_sit = false;
    sitType = nullptr;
}

void Character::onDamage(b2Fixture * other)
{
    damage = true;
    lives--;
}

void Character::offDamage(b2Fixture * other)
{
    damage = false;
}
c++ box2d sfml
1个回答
0
投票

据我所见,请看以下内容:

if (damage)
{
    setPosition(originPosition);
}

您移动了角色,但没有相应地设置它的新位置。这就是为什么碰撞后它又回到原来的位置。

您的意思是按照这些方针行事(我将细节留给您,您知道自己在做什么):

if (damage)
{
    pos.x = originPosition.x;
    pos.y = originPosition.y;
}

希望有帮助。玩得开心!

© www.soinside.com 2019 - 2024. All rights reserved.