gpt4 book ai didi

c - 为什么玩家在改变方向时有时会向错误的方向移动

转载 作者:行者123 更新时间:2023-11-30 16:50:51 25 4
gpt4 key购买 nike

我正在用 SDL2 用 C 语言编写一个游戏引擎,并尝试使用位旋转来存储我当前的移动方向。
我不太确定如何重现它,但这里有一个显示错误的视频:
https://youtu.be/RnPZaUDPElU

这是有问题的代码:

#define BMXSPEED 0x01
#define BMYSPEED 0x04

void BEvent (btree_t *t) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
t->run = 0;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.scancode) {
case SDL_SCANCODE_ESCAPE:
t->run = 0;
break;
case SDL_SCANCODE_D:
if (!event.key.repeat) {
t->player->speed |= BMXSPEED;
}
break;
case SDL_SCANCODE_A:
if (!event.key.repeat) {
t->player->speed |= (BMXSPEED << 1);
}
break;
case SDL_SCANCODE_S:
if (!event.key.repeat) {
t->player->speed |= BMYSPEED;
}
break;
case SDL_SCANCODE_W:
if (!event.key.repeat) {
t->player->speed |= (BMYSPEED << 1);
}
break;
}
break;
case SDL_KEYUP:
switch (event.key.keysym.scancode) {
case SDL_SCANCODE_D:
t->player->speed &= ~BMXSPEED;
break;
case SDL_SCANCODE_A:
t->player->speed &= ~(BMXSPEED << 1);
break;
case SDL_SCANCODE_S:
t->player->speed &= ~BMYSPEED;
break;
case SDL_SCANCODE_W:
t->player->speed &= ~(BMYSPEED << 1);
break;
}
break;
}
}

/* X movement */
if (t->player->speed & BMXSPEED && (t->player->speed & (BMXSPEED << 1)) != (BMXSPEED << 1))
t->player->xspeed += (t->player->xspeedMax - abs(t->player->xspeed)) / t->player->xspeedMax;
else
if (t->player->speed & (BMXSPEED << 1) && (t->player->speed & BMXSPEED) != BMXSPEED)
t->player->xspeed -= (t->player->xspeedMax - abs(t->player->xspeed)) / t->player->xspeedMax;
else
t->player->xspeed += (t->player->xspeedMin - t->player->xspeed) / t->player->xspeedMax;

/* Y movement */
if (t->player->speed & BMYSPEED && (t->player->speed & (BMYSPEED << 1)) != (BMYSPEED << 1))
t->player->yspeed += (t->player->yspeedMax - abs(t->player->yspeed)) / t->player->yspeedMax;
else
if (t->player->speed & (BMYSPEED << 1) && (t->player->speed & BMYSPEED) != BMYSPEED)
t->player->yspeed -= (t->player->yspeedMax - abs(t->player->yspeed)) / t->player->yspeedMax;
else
t->player->yspeed += (t->player->yspeedMin - t->player->yspeed) / t->player->yspeedMax;

t->player->rect.x += (int)t->player->xspeed;
t->player->rect.y += (int)t->player->yspeed;
}

编辑:更改存储方向的方法后,我确定问题与位旋转无关。我怀疑是加速方法。

最佳答案

您的加速方法有点晦涩(双关语),这里有 2 个建议:

  • 位测试可以简化为单个测试,用 2 位屏蔽播放器速度并检查单个位。

  • 减速情况可能也应该使用 abs 函数,并对减速情况进行调整。

  • 如果 xspeedyspeed 函数似乎比 abs() 更合适> 成员是浮点值。

  • 为播放器使用中间指针可以提高可读性:

这是生成的代码:

    Player *p = t->player;

/* X movement */
if (p->speed & (BMXSPEED * 3) == BMXSPEED)
p->xspeed += (p->xspeedMax - fabs(p->xspeed)) / p->xspeedMax;
else
if (p->speed & (BMXSPEED * 3) == BMXSPEED << 1)
p->xspeed -= (p->xspeedMax - fabs(p->xspeed)) / p->xspeedMax;
else
p->xspeed -= copysign((p->xspeedMin - fabs(p->xspeed)) / p->xspeedMax, p->xspeed);

/* Y movement */
if (p->speed & (BMYSPEED * 3) == BMYSPEED)
p->yspeed += (p->yspeedMax - fabs(p->yspeed)) / p->yspeedMax;
else
if (p->speed & (BMYSPEED * 3) == BMYSPEED << 1)
p->yspeed -= (p->yspeedMax - fabs(p->yspeed)) / p->yspeedMax;
else
p->yspeed -= copysign((p->yspeedMin - fabs(p->yspeed)) / p->yspeedMax, p->yspeed);

关于c - 为什么玩家在改变方向时有时会向错误的方向移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42014895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com