C++弹簧单高跷跳跃的正弦波公式

发布于 2024-09-30 14:27:14 字数 438 浏览 0 评论 0 原文

我需要创建一个以弧线跳跃穿过屏幕的弹簧单高跷。我认为最好的方法就是将其移动到正弦波上。如果波的顶部为 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;

我需要波浪非常窄,高点非常高。上面的公式在第一次迭代中开始时没问题,但随后波浪变得更宽,高点和低点彼此接近。谁能帮助数学菜鸟吗?

I need to create a pogo stick that jumps across the screen in arcs. I was thinking the best way to do this would be to move it on a sin wave. If the top of the wave is 1, the ground is 0 and the bottom of the wave is -1, then every time it hits 0 I would reset the values to start the sin wave again. So instead of following the typical sin wave (0, 1, 0, -1, 0 etc) it would go 0, 1, 0, 1, 0 etc.

Unfortunately my math is pretty terrible and I've been trying for hours to develop a formula. At the moment I'm just trying to make a normal sin wave where the top half emulates a pogo stick jumping, can't seem to even get that far. The closest I have is:

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

I need the waves to be quite narrow, and the high point to be quite high. The above formula starts off ok for the first iteration but then the waves get wider and the high and low points close in on each other. Can anyone help a math noob out?

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

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

发布评论

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

评论(4

錯遇了你 2024-10-07 14:27:14

不确定你的数学,你的物理需要复习!弹簧单高跷是抛射运动及其轨迹形式的一个示例a 抛物线,由 二次方程

但是,您是否应该坚持使用不正确的正弦模型:正弦波的“上半部分”(或正)部分从 0 到 pi 弧度运行。正弦仅代表 y 项(高度),不应有 x 项,它仅决定每个点的水平步长。其中 200 表示弹簧单高跷将达到的最大高度:

height = max_height * sin( theta ) ;

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

theta_step = pi / jump_distance ;

这样,当您达到 pi 弧度时,您将移动 jump_distance。在跳跃过程中,瞬时距离(以及图中的 x 值)将为:

 distance = jump_distance / theta ;

Not sure about your math, your physics needs some brushing up! The pogo stick is an example of projectile motion and its trajectory forms a parabola, which is described by a quadratic equation.

However should you persist with the incorrect sinusoidal model: The "top half" (or positive) part of a sine wave runs from 0 to pi radians. The sine represents only the y term (height), you should not have an x term there, that simply determines the horizontal step for each point. Where you have 200, that represents the maximum height the pogo stick will reach:

height = max_height * sin( theta ) ;

where 0 <= theta <= pi, and is incremented over time. The size of the increment will be determined by the forward speed, or total jump distance.

theta_step = pi / jump_distance ;

so that by the time you have reached pi radians, you will have moved by jump_distance. During the jump instantaneous distance (and therefore the x value in a plot) will be:

 distance = jump_distance / theta ;
Bonjour°[大白 2024-10-07 14:27:14

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

float f = abs( sin( <input here> ) );

Just take the absolute value of a sin wave.
So the parts that are negative are turned positive.

float f = abs( sin( <input here> ) );
ι不睡觉的鱼゛ 2024-10-07 14:27:14

Hammerite 有票:

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)) 

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

Hammerite has the ticket:

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)) 

Don't forget that the sine function requires radians, so the value you pass in as a parameter must be in the right units.

毁我热情 2024-10-07 14:27:14

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

#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
}

Here is the fresh-written, parametric code for both a sinus wave and a parabolic wave.

#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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文