2012-02-07 113 views
2

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

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的一圈。

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

我试过其他一些东西,比如1维行波和另一件涉及调整正常正弦波的东西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 
} 
+0

很难看出来你真的想要什么功能。函数给出的'vec'的值是多少?它的返回值应该代表什么? – leftaroundabout 2012-02-07 21:56:51

回答

3

伪代码,你需要做的是以下几点:

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); 
} 
+0

只需跟进 - 这是否回答您的问题,还是我脱离了标记? – hatboyzero 2012-03-06 18:12:25