二维波矢

发布于 2025-01-03 14:44:37 字数 993 浏览 0 评论 0原文

所以我试图让玩家发射一颗子弹,以波浪状的方式射向鼠标。我可以让子弹以波浪状移动(尽管不是我预测的那样),但不能朝鼠标移动。

Vector2 BulletFun::sine(Vector2 vec) {
    float w = (2 * PI) / 1000; // Where 1000 is the period
    float waveNum = (2 * PI) / 5; // Where 5 is the wavelength

    Vector2 k(0.0F, waveNum);

    float t = k.dot(vec) - (w * _time);

    float x = 5 * cos(t); // Where 5 is the amplitude
    float y = 5 * sin(t);

    Vector2 result(x, y);

    return result;
}

现在速度并不是太重要,一旦我弄清楚了这一点,这就不应该是太大的问题。我确实得到了一些角度变化,但似乎是相反的,而且只有 1/8 圆。

我可能在某个地方计算错误了。我刚刚了解了波矢。

我尝试了一些其他的东西,例如一维行波和另一个涉及通过 vec 调整正常正弦波的东西。其结果或多或少相同。

谢谢!

编辑:

vec 是从玩家位置到鼠标点击位置的位移。返回的是一个新的向量,该向量被调整为遵循波浪模式,每次子弹接收和更新时都会调用 BulletFun::sine 。

设置是这样的:

void Bullet::update() {
    _velocity = BulletFun::sine(_displacement);

    _location.add(_velocity); // add is a property of Tuple
                              // which Vector2 and Point2 inherit
}

So I'm trying to make the player shoot a bullet that goes towards the mouse in a wavey pattern. I can get the bullet to move in a wavey pattern (albeit not really how I predicted), but not towards the mouse.

Vector2 BulletFun::sine(Vector2 vec) {
    float w = (2 * PI) / 1000; // Where 1000 is the period
    float waveNum = (2 * PI) / 5; // Where 5 is the wavelength

    Vector2 k(0.0F, waveNum);

    float t = k.dot(vec) - (w * _time);

    float x = 5 * cos(t); // Where 5 is the amplitude
    float y = 5 * sin(t);

    Vector2 result(x, y);

    return result;
}

Right now the speed isn't much of a concern, that shouldn't be too much of a problem once I have this figured out. I do get some angle change, but it seems to be reversed and only 1/8th a circle.

I'm probably miscalculating something somewhere. I just kind of learned about wave vectors.

I've tried a few other things, such as 1 dimensional travelling waves and another thing involving adjusting a normal sine wave by vec. Which had more or less the same result.

Thanks!

EDIT:

vec is the displacement from the player's location to the mouse click location. The return is a new vector that is adjusted to follow a wave pattern, BulletFun::sine is called each time the bullet receives and update.

The setup is something like this:

void Bullet::update() {
    _velocity = BulletFun::sine(_displacement);

    _location.add(_velocity); // add is a property of Tuple
                              // which Vector2 and Point2 inherit
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伏妖词 2025-01-10 14:44:37

在伪代码中,您需要做的是:

waveVector = Vector2(travelDistance,amplitude*cos(2*PI*frequency*travelDistance/unitDistance);

cosTheta = directionVector.norm().dot(waveVector.norm());
theta = acos(cosTheta);

waveVector.rotate(theta);
waveVector.translate(originPosition);

应该在传统坐标系中计算波矢量,然后将其旋转到方向矢量的局部坐标系(其中方向矢量是局部x轴),然后相对于您想要的波束原点位置或其他位置平移波矢量...

这将产生一个非常类似于

Vector2
BulletFun::sine(Bullet _bullet, float _amplitude, float _frequency, float _unitDistance)
{
    float displacement = _bullet.getDisplacement();
    float omega = 2.0f * PI * _frequency * _displacement / _unitDistance;

    // Compute the wave coordinate on the traditional, untransformed
    // Cartesian coordinate frame.
    Vector2 wave(_displacement, _amplitude * cos(omega));

    // The dot product of two unit vectors is the cosine of the 
    // angle between them.
    float cosTheta = _bullet.getDirection().normalize().dot(wave.normalize());
    float theta = acos(cosTheta);

    // Translate and rotate the wave coordinate onto
    // the direction vector.
    wave.translate(_bullet.origin());
    wave.rotate(theta);
}

In pseudocode, what you need to do is the following:

waveVector = Vector2(travelDistance,amplitude*cos(2*PI*frequency*travelDistance/unitDistance);

cosTheta = directionVector.norm().dot(waveVector.norm());
theta = acos(cosTheta);

waveVector.rotate(theta);
waveVector.translate(originPosition);

That should compute the wave vector in a traditional coordinate frame, and then rotate it to the local coordinate frame of the direction vector (where the direction vector is the local x-axis), and then translate the wave vector relative to your desired origin position of the wave beam or whatever...

This will result in a function very similar to

Vector2
BulletFun::sine(Bullet _bullet, float _amplitude, float _frequency, float _unitDistance)
{
    float displacement = _bullet.getDisplacement();
    float omega = 2.0f * PI * _frequency * _displacement / _unitDistance;

    // Compute the wave coordinate on the traditional, untransformed
    // Cartesian coordinate frame.
    Vector2 wave(_displacement, _amplitude * cos(omega));

    // The dot product of two unit vectors is the cosine of the 
    // angle between them.
    float cosTheta = _bullet.getDirection().normalize().dot(wave.normalize());
    float theta = acos(cosTheta);

    // Translate and rotate the wave coordinate onto
    // the direction vector.
    wave.translate(_bullet.origin());
    wave.rotate(theta);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文