我正在尝试创建一个效果,其中两个射弹将以给定的实体在给定的玩家偏移处射出。一个快速的实现是这样的;
sf::Vector2f offset = m_owner->GetSprite().getPosition();
offset.y -= 5;
createProjectile(offset, m_owner->GetSprite().getRotation());
offset.y += 10;
createProjectile(offset, m_owner->GetSprite().getRotation());
如果您的实体只想在 .x 轴上发射,这非常有效,但是一旦您旋转播放器,它就会中断,因为偏移量没有脱离播放器当前的旋转。
我尝试了很多实现,但似乎都没有奏效,而且由于我的数学非常糟糕,我自己无法解决。
void Weapon::createProjectile(const sf::Vector2f& position, float angle)
{
m_owner->_state->addEntity(new Projectile(m_owner, position, angle,*m_texture, m_velocity, m_damage));
}
Projectile::Projectile(Entity* emitter, const sf::Vector2f& position, float angle,
const sf::Texture& image, int speed, int damage) :
Entity(emitter->_state, true, entityName::entityBullet, speed)
{
Load(_state->getApp()->gettextureManager().Get("Content/ball.png"));
GetSprite().setRotation(angle);
SetPosition(position.x,position.y);
GetSprite().setScale(0.4f,0.4);
}
回答:
float rad;
offset = m_owner->GetSprite().getPosition();
rad = math::to_rad(m_owner->GetSprite().getRotation());
offset.x += 5*sin(rad);
offset.y += -5*cos(rad);
createProjectile(offset,m_owner->GetSprite().getRotation());
offset = m_owner->GetSprite().getPosition();
offset.x += -5*sin(rad);
offset.y += 5*cos(rad);
createProjectile(offset,m_owner->GetSprite().getRotation());