游戏开发中的插补

游戏开发中的插补

插补

插值是图形编程中非常基本的操作。熟悉它是很好的,以扩大您作为图形开发人员的视野。

基本思想是要从A过渡到B。A值t表示中间的状态。

例如,如果t为0,则状态为A。如果t为1,则状态为B。介于两者之间的是插值。

在两个实数(浮点数)之间,通常将简单插值描述为:

interpolation = A * (1 - t) + B * t

通常简化为:

interpolation = A + (B - A) * t

这种插值的名称是“线性的”,它以恒定速度将一个值转换为另一个值。因此,当您了解线性插值法时,您会知道他们是在指这个简单的公式。

还有其他类型的插值,此处将不介绍。推荐的后续内容是Bezier页面。

向量插值
向量类型(Vector2和Vector3)也可以插值,它们带有方便的函数来实现 Vector2.linear_interpolate()和Vector3.linear_interpolate()。

对于三次插值,还有Vector2.cubic_interpolate()和Vector3.cubic_interpolate(),它们执行Bezier样式插值。

这是使用插值从A点到B点的简单伪代码:

func _physics_process(delta):
    t += delta * 0.4

    $Sprite.position = $A.position.linear_interpolate($B.position, t)

它将产生以下运动:

在这里插入图片描述

变换插值

也可以插值整个变换(确保它们具有统一的比例尺,或者至少具有相同的非统一比例尺)。为此,可以使用函数Transform.interpolate_with()。

这是将猴子从Position1转换为Position2的示例:

../../_images/interpolation_positions.png

使用以下伪代码:

var t = 0.0

func _physics_process(delta):
    t += delta

    $Monkey.transform = $Position1.transform.interpolate_with($Position2.transform, t)

再次,它将产生以下运动:

在这里插入图片描述

平滑运动

可以使用插值来平滑移动,旋转等。这是鼠标使用平滑运动跟随圆的示例:

const FOLLOW_SPEED = 4.0

func _physics_process(delta):
    var mouse_pos = get_local_mouse_position()

    $Sprite.position = $Sprite.position.linear_interpolate(mouse_pos, delta * FOLLOW_SPEED)

外观如下:

在这里插入图片描述

这对于使摄像机移动,跟随您的盟友(确保它们保持在一定范围内)以及许多其他常见的游戏模式非常有用。

猜你喜欢

转载自blog.csdn.net/qq_44273429/article/details/111179307