移动精灵会导致运动不平稳

发布于 2024-08-26 10:17:03 字数 1037 浏览 2 评论 0原文

我的精灵动作有些不稳定。

基本上,当用户触摸屏幕上的某个点时,精灵应该移动到该点。这基本上工作得很好......它甚至考虑到了增量 - 因为帧速率可能不一致。

然而,我注意到 y 运动通常在 x 运动之前完成(即使行进距离相同),因此看起来精灵正在以“L”形状而不是平滑的对角线移动。

垂直和水平速度(vx、vy)都设置为 300。有什么问题吗?我怎样才能让我的精灵沿着平滑的对角线移动?

 - (void)update:(ccTime)dt
{
int x = self.position.x;
int y = self.position.y;

    //if ball is to the left of target point
if (x<targetx)
{
            //if movement of the ball won't take it to it's target position
    if (x+(vx *dt) < targetx)
    {
        x += vx * dt;
    }
    else {
        x = targetx;
    }

} else if (x>targetx) //same with x being too far to the right
{
    if (x-(vx *dt) > targetx)
    {
        x -= vx * dt;
    }
    else {
        x = targetx;
    }

}
if (y<targety)
{
    if (y+(vy*dt)<targety)
    {
        y += vy * dt;
    }
    else {
        y = targety;
    }

} else if (y>targety)
{
    if (y-(vy*dt)>targety)
    {
        y -= vy * dt;
    }
    else {
        y = targety;
    }

}
self.position = ccp(x,y);
}

I've got some jerky movement of my sprite.

Basically, when the user touches a point on the screen, the sprite should move to that point. This is working mostly fine... it's even taking into account a delta - because frame rate may not be consistant.

However, I notice that the y movement usually finishes before the x movement (even when the distances to travel are the same), so it appears like the sprite is moving in an 'L' shape rather than a smooth diagonal line.

Vertical and horizontal velocity (vx, vy) are both set to 300. Any ideas what's wrong? How can I go about getting my sprite to move in a smooth diagonal line?

 - (void)update:(ccTime)dt
{
int x = self.position.x;
int y = self.position.y;

    //if ball is to the left of target point
if (x<targetx)
{
            //if movement of the ball won't take it to it's target position
    if (x+(vx *dt) < targetx)
    {
        x += vx * dt;
    }
    else {
        x = targetx;
    }

} else if (x>targetx) //same with x being too far to the right
{
    if (x-(vx *dt) > targetx)
    {
        x -= vx * dt;
    }
    else {
        x = targetx;
    }

}
if (y<targety)
{
    if (y+(vy*dt)<targety)
    {
        y += vy * dt;
    }
    else {
        y = targety;
    }

} else if (y>targety)
{
    if (y-(vy*dt)>targety)
    {
        y -= vy * dt;
    }
    else {
        y = targety;
    }

}
self.position = ccp(x,y);
}

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

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

发布评论

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

评论(1

玻璃人 2024-09-02 10:17:03

您想要从任意 (x,y) 移动到 (targetx, targety) 并同时到达两个坐标(以避免“狗腿”)。因此,假设 x 速度为 vx 并且您在 t 秒内到达那里。这意味着 vx = (targetx - x)/t。如果您希望同时平滑地移动到同一点,则 t 的 y 坐标必须相同,这意味着 t = (targetx - x)/vx 且 vy 实际上必须为 (targety - y)*vx/(targetx - x)。

也就是说,你无法分别设置vx和vy并得到你想要的结果。

You want to move to (targetx, targety) from any (x,y) and arrive at both coordinates at the same time (to avoid the "dogleg"). So, suppose the x velocity is vx and you get there in t seconds. That means vx = (targetx - x)/t. t must be the same for the y coordinate if you want smooth movement to the same point at the same time, so that means t = (targetx - x)/vx and vy must actually be (targety - y)*vx/(targetx - x).

In other words, you can't set vx and vy separately and get the result you want.

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