基于一致不一致的运动
Movement based on constant inconsistant
我制作了一个 class 这是一个角色,在更新方法中我使用了尽可能多的物理以使其逼真地工作并编辑了值以使运动感觉良好。
我最近注意到当我向左和向右移动时它以不同的速度移动,并且不知道这种情况持续了多久。我在开头声明了所有大写命名变量常量,并使用相同的常量进行移动,所以我不知道是什么原因造成的。
我已经尽力找到问题所在并进行修复,但我什么都没有。
我设置常量的开头:
const float GRAVITY = 1f;
const float AIR_SPEED_COEFFICIENT = 0.4f;
const float TERMINAL_AIR_HORIZONTAL_VELOCITY = 10f;
const float AIR_RESISTANCE = 0.97f;
const float FRICTION = 0.64f;
更新函数:
public void Update(Rectangle floor, GameWindow Window)
{
jumpFrameCounter++;
if (Keyboard.GetState().IsKeyDown(Keys.Up) || Keyboard.GetState().IsKeyDown(Keys.W))
{
if (jumpFrameCounter > 11 && jumpsUsed < amountOfJumps)
{
velocity.Y = -GRAVITY * 16 * jumpHeight;
jumpsUsed++;
jumpFrameCounter = 0;
}
}
if (Keyboard.GetState().IsKeyDown(Keys.Left) || Keyboard.GetState().IsKeyDown(Keys.A))
{
if (hitbox.Bottom >= floor.Top)
{
velocity.X -= RUN_SPEED / 4;
}
else
{
velocity.X -= airSpeed * AIR_SPEED_COEFFICIENT;
}
}
if (Keyboard.GetState().IsKeyDown(Keys.Right) || Keyboard.GetState().IsKeyDown(Keys.D))
{
if (hitbox.Bottom >= floor.Top)
{
velocity.X += RUN_SPEED / 4;
}
else
{
velocity.X += airSpeed * AIR_SPEED_COEFFICIENT;
}
}
if (velocity.X > 0.00001f || velocity.X < -0.00001f && !(Keyboard.GetState().IsKeyDown(Keys.Right) || Keyboard.GetState().IsKeyDown(Keys.D) || Keyboard.GetState().IsKeyDown(Keys.Left) || Keyboard.GetState().IsKeyDown(Keys.A)))
{
if (hitbox.Bottom >= floor.Top)
{
velocity.X *= FRICTION;
}
else
{
velocity.X *= AIR_RESISTANCE;
}
}
if (Keyboard.GetState().IsKeyDown(Keys.Down) || Keyboard.GetState().IsKeyDown(Keys.S))
{
fastFall = true;
}
velocity.Y += GRAVITY * unfloatyness;
if (fastFall)
{
velocity.Y += 6 * GRAVITY;
}
if (hitbox.Bottom >= floor.Top)
{
if (velocity.X > RUN_SPEED * groundSpeed)
{
velocity.X = RUN_SPEED * groundSpeed;
}
else if (velocity.X < -RUN_SPEED * groundSpeed)
{
velocity.X = -RUN_SPEED * groundSpeed;
}
}
else
{
if (velocity.X > TERMINAL_AIR_HORIZONTAL_VELOCITY * airSpeed)
{
velocity.X = TERMINAL_AIR_HORIZONTAL_VELOCITY * airSpeed;
}
else if (velocity.X < -TERMINAL_AIR_HORIZONTAL_VELOCITY * airSpeed)
{
velocity.X = -TERMINAL_AIR_HORIZONTAL_VELOCITY * airSpeed;
}
}
position += velocity;
hitbox = new Rectangle((int)position.X, (int)position.Y, fighterTexture.Width, fighterTexture.Height);
if (hitbox.Bottom > floor.Top && velocity.Y > 0)
{
position.Y = floor.Top - fighterTexture.Height;
jumpsUsed = 0;
fastFall = false;
velocity.Y = -1f;
}
if (hitbox.Left < 0)
{
position.X = 0;
}
else if (hitbox.Right > Window.ClientBounds.Width)
{
position.X = Window.ClientBounds.Width - fighterTexture.Width;
}
if (hitbox.Top < 0)
{
position.Y = 0;
}
else if (hitbox.Bottom > Window.ClientBounds.Height)
{
position.Y = Window.ClientBounds.Bottom - fighterTexture.Height;
}
}
这一行令人困惑。
Because AND&&
has higher precedence than OR||
if (velocity.X > 0.00001f || velocity.X < -0.00001f && !(Keyboard.GetState().IsKeyDown(Keys.Right) || Keyboard.GetState().IsKeyDown(Keys.D) || Keyboard.GetState().IsKeyDown(Keys.Left) || Keyboard.GetState().IsKeyDown(Keys.A)))
语句会变成。
if (velocity.X > 0.00001f
||
(velocity.X < -0.00001f && !(Keyboard.GetState().IsKeyDown(Keys.Right) )
|| Keyboard.GetState().IsKeyDown(Keys.D)
|| Keyboard.GetState().IsKeyDown(Keys.Left)
|| Keyboard.GetState().IsKeyDown(Keys.A)))
我假设你真的想要:
if ( ( velocity.X > 0.00001f || (velocity.X < -0.00001f )
&&
(
! (Keyboard.GetState().IsKeyDown(Keys.Right)
|| Keyboard.GetState().IsKeyDown(Keys.D)
|| Keyboard.GetState().IsKeyDown(Keys.Left)
|| Keyboard.GetState().IsKeyDown(Keys.A)))
)
然而你没有留下任何评论,我不知道如何解读。
这条线看起来像 "if velocity of x is nearly zero and Left
,D
, orA
is pressed or Right
is NOT pressed" 然后将 X 设置为更大的值。
但是这行和你描述的有冲突
I recently noticed when i move left and right it moves at different speeds, and have no idea how long that has been true.
您希望 Left 和 Right 做同样的事情,但不是在代码中。
我的建议是"use parenthesis for readability"和"use bool if there is more than 3 conditions to check in a line"。
Louis Go 提供的解决方案,
事实证明,将 if 语句更改为 2 个嵌套语句就像 the 的顺序一样简单,并且混淆了计算机。
旧代码:
if (velocity.X > 0.00001f || velocity.X < -0.00001f && !(Keyboard.GetState().IsKeyDown(Keys.Right) || Keyboard.GetState().IsKeyDown(Keys.D) || Keyboard.GetState().IsKeyDown(Keys.Left) || Keyboard.GetState().IsKeyDown(Keys.A)))
{
if (hitbox.Bottom >= floor.Top)
{
velocity.X *= FRICTION;
}
else
{
velocity.X *= AIR_RESISTANCE;
}
}
修改后的代码:
if (velocity.X > 0.00001f || velocity.X < -0.00001f)
{
if (!(keyboardState.IsKeyDown(Keys.Right) || keyboardState.IsKeyDown(Keys.D) || keyboardState.IsKeyDown(Keys.Left) || keyboardState.IsKeyDown(Keys.A)))
{
if (hitbox.Bottom >= floor.Top)
{
velocity.X *= FRICTION;
}
else
{
velocity.X *= AIR_RESISTANCE;
}
}
}
我制作了一个 class 这是一个角色,在更新方法中我使用了尽可能多的物理以使其逼真地工作并编辑了值以使运动感觉良好。
我最近注意到当我向左和向右移动时它以不同的速度移动,并且不知道这种情况持续了多久。我在开头声明了所有大写命名变量常量,并使用相同的常量进行移动,所以我不知道是什么原因造成的。
我已经尽力找到问题所在并进行修复,但我什么都没有。
我设置常量的开头:
const float GRAVITY = 1f;
const float AIR_SPEED_COEFFICIENT = 0.4f;
const float TERMINAL_AIR_HORIZONTAL_VELOCITY = 10f;
const float AIR_RESISTANCE = 0.97f;
const float FRICTION = 0.64f;
更新函数:
public void Update(Rectangle floor, GameWindow Window)
{
jumpFrameCounter++;
if (Keyboard.GetState().IsKeyDown(Keys.Up) || Keyboard.GetState().IsKeyDown(Keys.W))
{
if (jumpFrameCounter > 11 && jumpsUsed < amountOfJumps)
{
velocity.Y = -GRAVITY * 16 * jumpHeight;
jumpsUsed++;
jumpFrameCounter = 0;
}
}
if (Keyboard.GetState().IsKeyDown(Keys.Left) || Keyboard.GetState().IsKeyDown(Keys.A))
{
if (hitbox.Bottom >= floor.Top)
{
velocity.X -= RUN_SPEED / 4;
}
else
{
velocity.X -= airSpeed * AIR_SPEED_COEFFICIENT;
}
}
if (Keyboard.GetState().IsKeyDown(Keys.Right) || Keyboard.GetState().IsKeyDown(Keys.D))
{
if (hitbox.Bottom >= floor.Top)
{
velocity.X += RUN_SPEED / 4;
}
else
{
velocity.X += airSpeed * AIR_SPEED_COEFFICIENT;
}
}
if (velocity.X > 0.00001f || velocity.X < -0.00001f && !(Keyboard.GetState().IsKeyDown(Keys.Right) || Keyboard.GetState().IsKeyDown(Keys.D) || Keyboard.GetState().IsKeyDown(Keys.Left) || Keyboard.GetState().IsKeyDown(Keys.A)))
{
if (hitbox.Bottom >= floor.Top)
{
velocity.X *= FRICTION;
}
else
{
velocity.X *= AIR_RESISTANCE;
}
}
if (Keyboard.GetState().IsKeyDown(Keys.Down) || Keyboard.GetState().IsKeyDown(Keys.S))
{
fastFall = true;
}
velocity.Y += GRAVITY * unfloatyness;
if (fastFall)
{
velocity.Y += 6 * GRAVITY;
}
if (hitbox.Bottom >= floor.Top)
{
if (velocity.X > RUN_SPEED * groundSpeed)
{
velocity.X = RUN_SPEED * groundSpeed;
}
else if (velocity.X < -RUN_SPEED * groundSpeed)
{
velocity.X = -RUN_SPEED * groundSpeed;
}
}
else
{
if (velocity.X > TERMINAL_AIR_HORIZONTAL_VELOCITY * airSpeed)
{
velocity.X = TERMINAL_AIR_HORIZONTAL_VELOCITY * airSpeed;
}
else if (velocity.X < -TERMINAL_AIR_HORIZONTAL_VELOCITY * airSpeed)
{
velocity.X = -TERMINAL_AIR_HORIZONTAL_VELOCITY * airSpeed;
}
}
position += velocity;
hitbox = new Rectangle((int)position.X, (int)position.Y, fighterTexture.Width, fighterTexture.Height);
if (hitbox.Bottom > floor.Top && velocity.Y > 0)
{
position.Y = floor.Top - fighterTexture.Height;
jumpsUsed = 0;
fastFall = false;
velocity.Y = -1f;
}
if (hitbox.Left < 0)
{
position.X = 0;
}
else if (hitbox.Right > Window.ClientBounds.Width)
{
position.X = Window.ClientBounds.Width - fighterTexture.Width;
}
if (hitbox.Top < 0)
{
position.Y = 0;
}
else if (hitbox.Bottom > Window.ClientBounds.Height)
{
position.Y = Window.ClientBounds.Bottom - fighterTexture.Height;
}
}
这一行令人困惑。
Because AND&&
has higher precedence than OR||
if (velocity.X > 0.00001f || velocity.X < -0.00001f && !(Keyboard.GetState().IsKeyDown(Keys.Right) || Keyboard.GetState().IsKeyDown(Keys.D) || Keyboard.GetState().IsKeyDown(Keys.Left) || Keyboard.GetState().IsKeyDown(Keys.A)))
语句会变成。
if (velocity.X > 0.00001f
||
(velocity.X < -0.00001f && !(Keyboard.GetState().IsKeyDown(Keys.Right) )
|| Keyboard.GetState().IsKeyDown(Keys.D)
|| Keyboard.GetState().IsKeyDown(Keys.Left)
|| Keyboard.GetState().IsKeyDown(Keys.A)))
我假设你真的想要:
if ( ( velocity.X > 0.00001f || (velocity.X < -0.00001f )
&&
(
! (Keyboard.GetState().IsKeyDown(Keys.Right)
|| Keyboard.GetState().IsKeyDown(Keys.D)
|| Keyboard.GetState().IsKeyDown(Keys.Left)
|| Keyboard.GetState().IsKeyDown(Keys.A)))
)
然而你没有留下任何评论,我不知道如何解读。
这条线看起来像 "if velocity of x is nearly zero and Left
,D
, orA
is pressed or Right
is NOT pressed" 然后将 X 设置为更大的值。
但是这行和你描述的有冲突
I recently noticed when i move left and right it moves at different speeds, and have no idea how long that has been true.
您希望 Left 和 Right 做同样的事情,但不是在代码中。
我的建议是"use parenthesis for readability"和"use bool if there is more than 3 conditions to check in a line"。
Louis Go 提供的解决方案,
事实证明,将 if 语句更改为 2 个嵌套语句就像 the 的顺序一样简单,并且混淆了计算机。
旧代码:
if (velocity.X > 0.00001f || velocity.X < -0.00001f && !(Keyboard.GetState().IsKeyDown(Keys.Right) || Keyboard.GetState().IsKeyDown(Keys.D) || Keyboard.GetState().IsKeyDown(Keys.Left) || Keyboard.GetState().IsKeyDown(Keys.A)))
{
if (hitbox.Bottom >= floor.Top)
{
velocity.X *= FRICTION;
}
else
{
velocity.X *= AIR_RESISTANCE;
}
}
修改后的代码:
if (velocity.X > 0.00001f || velocity.X < -0.00001f)
{
if (!(keyboardState.IsKeyDown(Keys.Right) || keyboardState.IsKeyDown(Keys.D) || keyboardState.IsKeyDown(Keys.Left) || keyboardState.IsKeyDown(Keys.A)))
{
if (hitbox.Bottom >= floor.Top)
{
velocity.X *= FRICTION;
}
else
{
velocity.X *= AIR_RESISTANCE;
}
}
}