我正在使用SDL2在C中编写一个游戏引擎,我正在尝试使用bit twiddling来存储我当前的移动方向。
我不确定如何重现它,但这是一个显示错误的视频:
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;
}
编辑:改变存储方向的方法后,我确定问题与bit twiddling无关。我怀疑它是加速方法。
答案 0 :(得分:0)
你的加速方法有点模糊(双关语),这里有2条建议:
可以将位测试简化为单个测试,用2位屏蔽播放器speed
并检查单个位。
减速情况也应该使用abs
功能,并调整减速情况。
如果fabs()
和abs()
成员是浮点值,xspeed
函数似乎比yspeed
更合适。
为播放器使用中间指针可提高可读性:
以下是生成的代码:
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);