LibGDX 指导 - 精灵追踪 2D 无限随机贝塞尔曲线 [英] LibGDX guidance - sprite tracing 2D infinite random bezier curve

查看:32
本文介绍了LibGDX 指导 - 精灵追踪 2D 无限随机贝塞尔曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够将平滑的动画应用到我的精灵并使用加速度计控制它.我的精灵被固定为沿 x-aix 左右移动.

I've been able to apply a smooth animation to my sprite and control it using the accelerometer. My sprite is fixed to move left and right along the x-aixs.

从这里开始,我需要弄清楚如何为精灵创建一条垂直的无限波浪线以尝试追踪.我的游戏的目的是让用户用加速度计控制精灵的左/右运动,以尽可能地追踪永无止境的波浪线,而精灵和相机都在垂直方向移动以模拟移动沿线."如果这条线是随机生成的,那将是理想的.

From here, I need to figure out how to create a vertical infinite wavy line for the sprite to attempt to trace. the aim of my game is for the user to control the sprite's left/right movement with the accelerometer in an attempt to trace the never ending wavy line as best they can, whilst the sprite and camera both move in a vertical direction to simulate "moving along the line." It would be ideal if the line was randomly generated.

我研究过样条曲线、平面、贝塞尔曲线等,但我找不到任何似乎与我想要实现的目标足够接近的东西.

I've researched about splines, planes, bezier curves etc, but I can't find anything that seems to relate close enough to what I'm trying to achieve.

我只是在寻求一些指导,以了解我可以使用哪些方法来实现这一目标.有什么想法吗?

I'm just seeking some guidance as to what methods I could possibly use to achieve this. Any ideas?

推荐答案

您可以使用 4 到 5 个正弦波的总和(每个正弦波具有不同的幅度、波长和相位差).所有这 3 个参数都可以是随机的.

You could use sum of 4 to 5 sine waves (each with different amplitude, wavelength and phase difference). All 3 of those parameters could be random.

生成的曲线将非常平滑(因为它主要是正弦曲线),但它看起来是随机的(它的时间段将是所有 4 到 5 个随机波长的 LCM,这是一个巨大的数字).

The resulting curve would be very smooth (since it is primarily sinusoidal) yet it'll look random (it's time period would be LCM of all 4 to 5 random wavelengths which is a huge number).

所以曲线不会长时间重复,但不会对记忆造成困难.关于计算复杂度,您始终可以通过使用 FPS 更改正弦项的数量来调整它.

So the curve won't repeat for a long time, yet it will not be hard on memory. Concerning computational complexity, you can always tune it by changing number of sine terms with FPS.

它应该看起来像这样.

It should look like this.

它也很容易实现.(即使我可以生成上面的图像..哈哈)

It's really easy to implement too. (even I could generate above image.. haha)

希望这会有所帮助.数学摇滚.:D

Hope this helps. Maths rocks. :D

(这里的基本思想是一个有限的傅里叶级数,我认为它应该是理想的你的用例)

(The basic idea here is a finite Fourier series which I think should be ideal for your use case)

您可以像这样创建每个术语并为所有术语分配随机值.

You can create each term like this and assign random values to all terms.

public class SineTerm {

    private float amplitude;
    private float waveLength;
    private float phaseDifference;

    public SineTerm(float amplitude, float waveLength, float phaseDifference) {
        this.amplitude = amplitude;
        this.waveLength = waveLength;
        this.phaseDifference = phaseDifference;
    }

    public float evaluate(float x) {
        return amplitude * (float) Math.sin(2 * Math.PI * x / waveLength + phaseDifference);
    }

}

现在创建一个 SineTerm 数组并添加 evaluate(x) 返回的所有值(使用精灵的一个坐标作为输入).使用输出作为精灵的其他坐标.你应该很高兴.

Now create an array of SineTerms and add all values returned by evaluate(x) (use one coordinate of sprite as input). Use the output as other coordinate of sprite. You should be good to go.

真正的诀窍在于调整这些随机数.

The real trick would be in tuning those random numbers.

祝你好运.

这篇关于LibGDX 指导 - 精灵追踪 2D 无限随机贝塞尔曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆