1

2 つの発射体がプレイヤーに対して特定のオフセットで特定のエンティティから出てくるエフェクトを作成しようとしています。簡単な実装はこれでした。

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());
4

1 に答える 1

2

勉強しなければならない数学は三角法です。とても便利です!

キャラクターがtheta「真上」から時計回りにラジアン回転されている場合、上部のオフセットは次のようになります。

offset.x += 5*sin(theta);
offset.y += -5*cos(theta);

もう1つは

offset.x += -5*sin(theta);
offset.y += 5*cos(theta);

これは「円の数学」(三角法)であり、5 はあなたが話すオフセットであり、円の半径とも呼ばれます。この種の計算を行うときの簡単な健全性チェックは、0 ラジアンで何が起こるかを検討することです。sinとのいくつかの簡単な事実cos:

sin(0) = 0
cos(0) = 1
sin'(0) = 1
cos'(0) = 0

(2 番目のものは導関数です。sin は最初は増加し、cos は最初はフラットです。グラフを確認してください。) これともう少し直感があれば、グラフィックス コードを書くのに役立ちますtheta = 0。1 つの例外: 最初のオフセットに対する 2 番目のオフセットを取得しています: 私はそれを避け、代わりに 2 つのコピーを作成するか、最初の発射体を作成した後にm_owner->GetSprite().getPosition();リセットします。offset

于 2013-04-11T15:04:55.603 に答える