1

我需要创建一个以弧线形式在屏幕上跳跃的 pogo 棒。我在想最好的办法是把它移到一个罪波上。如果波的顶部是 1,地面是 0,波的底部是 -1,那么每次它达到 0 时,我都会重置这些值以再次启动正弦波。因此,它不会跟随典型的正弦波(0、1、0、-1、0 等),而是会走 0、1、0、1、0 等。

不幸的是,我的数学很糟糕,我已经尝试了几个小时来开发一个公式。目前,我只是在尝试制作一个正常的罪波,上半部分模拟弹簧棒的跳跃,似乎无法达到那么远。我最接近的是:

m_vel.x++;
float f = PI / 30 / 2;
m_vel.y = 200 * sin(f * m_vel.x);
m_vel.y = -m_vel.y;

我需要波浪非常窄,高点非常高。上面的公式在第一次迭代开始时没问题,但随后波浪变宽,高点和低点相互靠近。任何人都可以帮助一个数学菜鸟吗?

4

4 回答 4

6

不确定你的数学,你的物理需要一些复习!pogostick是抛物运动的一个例子,它的轨迹形成一个抛物线,用一个二次方程来描述。

但是,如果您坚持使用不正确的正弦模型:正弦波的“上半部分”(或正)部分从 0 到 pi 弧度。正弦只代表 y 项(高度),你不应该有一个 x 项,它只是确定每个点的水平步长。如果你有 200,那代表 pogo 棒将达到的最大高度:

height = max_height * sin( theta ) ;

其中 0 <= theta <= pi,并随时间递增。增量的大小将由前进速度或总跳跃距离决定。

theta_step = pi / jump_distance ;

这样当您达到 pi 弧度时,您将移动 jump_distance。在跳跃的瞬时距离(因此图中的 x 值)将是:

 distance = jump_distance / theta ;
于 2010-11-05T10:21:35.540 回答
2

只需取正弦波的绝对值即可。所以消极的部分变成了积极的。

float f = abs( sin( <input here> ) );
于 2010-11-05T10:16:04.350 回答
1

锤子有票:

double a = 100.0; // amplitude controls the height
double f = 10.0;  // frequency controls the width
double t = 0.0;   // time is the independent variable.
abs(a*sin(2.0*PI*f*t)) 

不要忘记正弦函数需要弧度,因此作为参数传入的值必须采用正确的单位。

于 2010-11-05T10:16:06.927 回答
0

这是正弦波和抛物线波的新编写的参数代码。

#define _USE_MATH_DEFINES // need this to get M_PI defined under VS2008
#include <math.h>

[...]

// user parameters
float screen_width = 640.0f;
float number_of_cycles_per_screen = 2.0f;
float min_wave_value = 0.0f;
float max_wave_value = 1.0f;

// sinus wave characteristics
float half_amplitude = 0.5f*(max_wave_value-min_wave_value);
float offset = half_amplitude+min_wave_value;
float f0 = 2.0f*M_PI*number_of_cycles_per_screen/screen_width;
// compute sinus wave on the whole screen width
for (float x=0.0f;x<screen_width;x+=1.0f)
{
    float sin_wave_value = half_amplitude*sin(f0*x)+offset;
    // use the value here
}

// parabola
float amplitude = 0.5*(max_wave_value-min_wave_value);
float root1 = 0.0;
float root2 = 1.0f/number_of_cycles_per_screen;
// compute parabolic wave on the whole screen width
for (float x=0.0f;x<screen_width;x+=1.0f)
{
    float xm = fmod(x,screen_width/number_of_cycles_per_screen)/screen_width;
    float para_wave_value = -amplitude*(xm-root1)*(xm-root2);
    // use the value here
}
于 2010-11-05T10:29:01.340 回答