c++ - 如何在接触时重置我的角色位置?
问题描述
我的 2d 平台游戏中有一个角色,还有一个在两点之间移动的敌人方块。
在接触时,我想将角色的位置重置为其原始位置,因为它已经死亡。
使用我当前的代码,玩家的位置仅在与敌人固定装置接触时设置为原始位置,并且不会像预期的那样停留在那里。
非常感谢任何帮助,因为我无法弄清楚需要做些什么来解决这个问题。
字符.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;
}
解决方案
从我能读到的,看看这个块:
if (damage)
{
setPosition(originPosition);
}
您移动了角色,但没有相应地设置它的新位置。这就是为什么它在碰撞后回到原来的位置。
你的意思是沿着这些路线做一些事情(我将细节留给你,你知道你在做什么):
if (damage)
{
pos.x = originPosition.x;
pos.y = originPosition.y;
}
希望能帮助到你。玩得开心!