Skip to main content
 首页 » 编程设计

c#之雪碧震动。怎么了

2024年02月20日12dudu

我的敌人 Sprite 在到达可变终点位置后一直在振动。怎么了?为什么会震动? 此外,变量 next_position 永远不会为真。但为什么?我希望敌人 Sprite 在到达当前终点位置后移动到一个新的随机终点位置。

public class Game1 : Microsoft.Xna.Framework.Game 
{ 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 
    Texture2D enemy; 
    Vector2 position, endposition; 
    bool next_position = false; 
 
    public Game1() 
    { 
        graphics = new GraphicsDeviceManager(this); 
        Content.RootDirectory = "Content"; 
        graphics.PreferredBackBufferWidth = 1280; 
        graphics.PreferredBackBufferHeight = 720; 
    } 
 
    protected override void Initialize() 
    {           
        base.Initialize(); 
    } 
 
    protected override void LoadContent() 
    {          
        spriteBatch = new SpriteBatch(GraphicsDevice); 
        enemy = Content.Load<Texture2D>("zombie"); 
        Random randomstart = new Random(); 
        position = new Vector2(randomstart.Next(100, 200), randomstart.Next(100, 200)); 
        endposition = new Vector2(randomstart.Next(100, 600), randomstart.Next(100, 400)); 
    } 
 
    protected override void Update(GameTime gameTime) 
    { 
        float delta = (float)gameTime.ElapsedGameTime.TotalSeconds;          
        Random random = new Random(); 
        position.X += 5 * delta; 
        position.Y += 3 * delta; 
 
        if (next_position == true) 
        { 
          endposition = new Vector2(random.Next(100, 600), random.Next(100, 400)); 
          next_position = false; 
        } 
 
        if (Vector2.Dot(endposition - position, endposition - position) > 0 || Vector2.Dot(endposition - position, endposition - position) < 0)  
        { 
          Vector2 enemyDirection = Vector2.Normalize(endposition - position) * 100f; 
          position += enemyDirection * delta; 
        } 
        else 
        { 
            next_position = true; 
        } 
 
        base.Update(gameTime); 
    } 
 
    protected override void Draw(GameTime gameTime) 
    { 
        GraphicsDevice.Clear(Color.CornflowerBlue); 
        spriteBatch.Begin(); 
        spriteBatch.Draw(enemy, position, Color.White); 
        spriteBatch.End(); 
 
        base.Draw(gameTime); 
    } 
} 

请您参考如下方法:

我对这条线有点困惑

if (Vector2.Dot(endposition - position, endposition - position) > 0 || Vector2.Dot(endposition - position, endposition - position) < 0) 

应该完全正确。 从代码的其余部分,你试图用它来实现的是检查 Sprite 的位置是否是敌人的位置。 这条线实际上做的是计算相反方向的向量的点积,它永远不会为零,除非向量为零。所以理论上这可能确实有效,但前提是位置恰好等于敌人的位置。

但是,您每单位时间将 Sprite 移动固定距离,因此它实际上并没有到达敌人,而是越过了敌人。然后发现自己在另一边,它向后移动,回到它来自的地方,并再次超过它。这会继续并导致您描述的振动。

现在,你想要实现的行为通常是计算两个物体( Sprite 和敌人)之间的距离,使用毕达哥拉斯定理,然后在距离低于 a 时接受物体的到达某个阈值(容易但容易出错),或者比对象可以通过此特定更新的距离短(在你的情况下,因为你移动了一个归一化向量时间 100f * delta,这个距离是相等的到 100f * delta)。

希望我的解释对您有所帮助。 当然,请随时要求澄清。

编辑(回复评论):

您正在创建的 limit 向量相对没有意义,因为它只是在 x 和 y 方向上偏移 max_distance 的位置。不需要那样的东西。 然而,另一个 if() 包含了几乎所有你想要的代码,检查一下:

// first you calculate the distance we can move this update 
float max_distance = 100f * delta; 
 
// now, we compare the actual distance to the end position to that maximum 
// if the actual distance is larger, then we are not there yet 
// otherwise, (if the actual distance is smaller) we will overshoot the target, 
// thus we are in fact there! (=close enough to continue) 
// (note: we compare the distances squared to avoid a square root, 
//  which makes this check much faster, which is good practice, 
//  in case we ever scale this up to more objects) 
if ((position - endposition).LengthSquared() > max_distance * max_distance) 
{ 
    /* we are not yet there, continue moving! */ 
} 
else 
{ 
    /* we are there! */ 
} 

真的很简单,不需要其他检查。让我知道这是否适合您!