Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 要单击的Xna等轴测点移动_C#_Xna_Game Physics - Fatal编程技术网

C# 要单击的Xna等轴测点移动

C# 要单击的Xna等轴测点移动,c#,xna,game-physics,C#,Xna,Game Physics,我正在做一个等距游戏(菱形网格),我偶然发现了一个关于角色移动的小问题 public void Destination(tile destination) { for (int i = 0; i < 8; i++) { if (AdjacentTile[i] == destination) { characterDirection = i; } } animation.changeS

我正在做一个等距游戏(菱形网格),我偶然发现了一个关于角色移动的小问题

public void Destination(tile destination)
{ 
    for (int i = 0; i < 8; i++)
    {
        if (AdjacentTile[i] == destination)
        {
            characterDirection = i; 
        }
    }
    animation.changeSpriteDirection(characterDirection); //After I found which adjacent tile is the next destination I change the character direction based on it's position (1 = North , 2 = Nort Est etc) .. so the Y of the Animation_sourceRectangle it's changed//

    Vector2 Position;
    Position.X = current_characterTile.X - destination.X;
    Position.Y = current_characterTile.Y - destination.Y;

    rotation = (float)Math.Atan2(-Position.X, Position.Y); 

    moveVector = (Vector2.Transform(new Vector2(0, -1), Matrix.CreateRotationZ(rotation))) * characterSpeed;
    movingCommand = 1; // the character is supposed to be moving..
    Move(); //this function moves the sprite until the *tile.i and tile.j* of the character is the same as tile.j and tile.i of the destination

    //something like this  
    if ( characterTile.i == destination.i && characterTile.j == destination.j) 
       movingCommand = 0 //stop
    else 
       character_Position += moveVector; 
}
我用A*在两点之间找到一条路径,然后我想把我的角色从A点移动到B点,穿过形成路径的所有瓷砖,但我找不到一种方法,我是说一种更简单、更精确的方法

public void Destination(tile destination)
{ 
    for (int i = 0; i < 8; i++)
    {
        if (AdjacentTile[i] == destination)
        {
            characterDirection = i; 
        }
    }
    animation.changeSpriteDirection(characterDirection); //After I found which adjacent tile is the next destination I change the character direction based on it's position (1 = North , 2 = Nort Est etc) .. so the Y of the Animation_sourceRectangle it's changed//

    Vector2 Position;
    Position.X = current_characterTile.X - destination.X;
    Position.Y = current_characterTile.Y - destination.Y;

    rotation = (float)Math.Atan2(-Position.X, Position.Y); 

    moveVector = (Vector2.Transform(new Vector2(0, -1), Matrix.CreateRotationZ(rotation))) * characterSpeed;
    movingCommand = 1; // the character is supposed to be moving..
    Move(); //this function moves the sprite until the *tile.i and tile.j* of the character is the same as tile.j and tile.i of the destination

    //something like this  
    if ( characterTile.i == destination.i && characterTile.j == destination.j) 
       movingCommand = 0 //stop
    else 
       character_Position += moveVector; 
}
到目前为止,我已经废弃了这段代码,但它有点“生锈”

public void Destination(tile destination)
{ 
    for (int i = 0; i < 8; i++)
    {
        if (AdjacentTile[i] == destination)
        {
            characterDirection = i; 
        }
    }
    animation.changeSpriteDirection(characterDirection); //After I found which adjacent tile is the next destination I change the character direction based on it's position (1 = North , 2 = Nort Est etc) .. so the Y of the Animation_sourceRectangle it's changed//

    Vector2 Position;
    Position.X = current_characterTile.X - destination.X;
    Position.Y = current_characterTile.Y - destination.Y;

    rotation = (float)Math.Atan2(-Position.X, Position.Y); 

    moveVector = (Vector2.Transform(new Vector2(0, -1), Matrix.CreateRotationZ(rotation))) * characterSpeed;
    movingCommand = 1; // the character is supposed to be moving..
    Move(); //this function moves the sprite until the *tile.i and tile.j* of the character is the same as tile.j and tile.i of the destination

    //something like this  
    if ( characterTile.i == destination.i && characterTile.j == destination.j) 
       movingCommand = 0 //stop
    else 
       character_Position += moveVector; 
}
公共作废目的地(平铺目的地)
{ 
对于(int i=0;i<8;i++)
{
if(相邻分片[i]==目的地)
{
characterDirection=i;
}
}
animation.changespriteddirection(characterDirection);//在我找到下一个目标是哪个相邻的平铺后,我根据它的位置(1=北,2=北等)更改角色方向,因此动画的Y\u source矩形的Y发生了更改//
矢量2位置;
Position.X=当前字符.X-目的地.X;
Position.Y=当前字符.Y-目的地.Y;
旋转=(浮动)数学Atan2(-Position.X,Position.Y);
moveVector=(Vector2.Transform(新Vector2(0,-1),Matrix.CreateRotationZ(rotation))*characterSpeed;
movingCommand=1;//角色应该正在移动。。
Move();//此函数移动精灵,直到角色的*tile.i和tile.j*与目标的tile.j和tile.i*相同
//像这样的
if(characterTile.i==destination.i&&characterTile.j==destination.j)
movingCommand=0//停止
其他的
字符_位置+=移动向量;
}
如果有人能告诉我该做什么或帮助我,我将非常感激。

谢谢。

可能性:

public void Destination(tile destination)
{ 
    for (int i = 0; i < 8; i++)
    {
        if (AdjacentTile[i] == destination)
        {
            characterDirection = i; 
        }
    }
    animation.changeSpriteDirection(characterDirection); //After I found which adjacent tile is the next destination I change the character direction based on it's position (1 = North , 2 = Nort Est etc) .. so the Y of the Animation_sourceRectangle it's changed//

    Vector2 Position;
    Position.X = current_characterTile.X - destination.X;
    Position.Y = current_characterTile.Y - destination.Y;

    rotation = (float)Math.Atan2(-Position.X, Position.Y); 

    moveVector = (Vector2.Transform(new Vector2(0, -1), Matrix.CreateRotationZ(rotation))) * characterSpeed;
    movingCommand = 1; // the character is supposed to be moving..
    Move(); //this function moves the sprite until the *tile.i and tile.j* of the character is the same as tile.j and tile.i of the destination

    //something like this  
    if ( characterTile.i == destination.i && characterTile.j == destination.j) 
       movingCommand = 0 //stop
    else 
       character_Position += moveVector; 
}
  • 在每个平铺上,确定角色的速度向量,并确定角色移动到下一个平铺所需的时间。当时间过去后,立即开始移动到下一个磁贴。(这是我在下面实现的。)
  • 在每个平铺上,确定角色的速度向量。然后,当角色足够接近下一个磁贴时(例如,其X和Y坐标之间的差异小于2像素?),将其捕捉到磁贴并开始移动到下一个磁贴。这将导致伪影,通常不太精确
解决方案:

public void Destination(tile destination)
{ 
    for (int i = 0; i < 8; i++)
    {
        if (AdjacentTile[i] == destination)
        {
            characterDirection = i; 
        }
    }
    animation.changeSpriteDirection(characterDirection); //After I found which adjacent tile is the next destination I change the character direction based on it's position (1 = North , 2 = Nort Est etc) .. so the Y of the Animation_sourceRectangle it's changed//

    Vector2 Position;
    Position.X = current_characterTile.X - destination.X;
    Position.Y = current_characterTile.Y - destination.Y;

    rotation = (float)Math.Atan2(-Position.X, Position.Y); 

    moveVector = (Vector2.Transform(new Vector2(0, -1), Matrix.CreateRotationZ(rotation))) * characterSpeed;
    movingCommand = 1; // the character is supposed to be moving..
    Move(); //this function moves the sprite until the *tile.i and tile.j* of the character is the same as tile.j and tile.i of the destination

    //something like this  
    if ( characterTile.i == destination.i && characterTile.j == destination.j) 
       movingCommand = 0 //stop
    else 
       character_Position += moveVector; 
}
假设您已经运行了寻路算法,并找到了一个瓷砖的链接列表,您必须通过该列表才能到达目标。我们还假设这些瓷砖在移动过程中不会被阻挡(不过,如果可以的话,修改算法很简单)

public void Destination(tile destination)
{ 
    for (int i = 0; i < 8; i++)
    {
        if (AdjacentTile[i] == destination)
        {
            characterDirection = i; 
        }
    }
    animation.changeSpriteDirection(characterDirection); //After I found which adjacent tile is the next destination I change the character direction based on it's position (1 = North , 2 = Nort Est etc) .. so the Y of the Animation_sourceRectangle it's changed//

    Vector2 Position;
    Position.X = current_characterTile.X - destination.X;
    Position.Y = current_characterTile.Y - destination.Y;

    rotation = (float)Math.Atan2(-Position.X, Position.Y); 

    moveVector = (Vector2.Transform(new Vector2(0, -1), Matrix.CreateRotationZ(rotation))) * characterSpeed;
    movingCommand = 1; // the character is supposed to be moving..
    Move(); //this function moves the sprite until the *tile.i and tile.j* of the character is the same as tile.j and tile.i of the destination

    //something like this  
    if ( characterTile.i == destination.i && characterTile.j == destination.j) 
       movingCommand = 0 //stop
    else 
       character_Position += moveVector; 
}
我通常会这样做来处理这个问题:

public void Destination(tile destination)
{ 
    for (int i = 0; i < 8; i++)
    {
        if (AdjacentTile[i] == destination)
        {
            characterDirection = i; 
        }
    }
    animation.changeSpriteDirection(characterDirection); //After I found which adjacent tile is the next destination I change the character direction based on it's position (1 = North , 2 = Nort Est etc) .. so the Y of the Animation_sourceRectangle it's changed//

    Vector2 Position;
    Position.X = current_characterTile.X - destination.X;
    Position.Y = current_characterTile.Y - destination.Y;

    rotation = (float)Math.Atan2(-Position.X, Position.Y); 

    moveVector = (Vector2.Transform(new Vector2(0, -1), Matrix.CreateRotationZ(rotation))) * characterSpeed;
    movingCommand = 1; // the character is supposed to be moving..
    Move(); //this function moves the sprite until the *tile.i and tile.j* of the character is the same as tile.j and tile.i of the destination

    //something like this  
    if ( characterTile.i == destination.i && characterTile.j == destination.j) 
       movingCommand = 0 //stop
    else 
       character_Position += moveVector; 
}
  • 运行寻路算法,如果路径为 存在
  • character.Path=返回的列表
  • character.beginMovingToTarget(character.Path[0])
  • 字符.Path.RemoveAt(0) beginMovingToTarget()方法将确定速度向量,并确定到达平铺所需的时间。到达时间后,我们立即转到下一个磁贴,直到路径为空。让我们调用这个时间变量character.timeToArrival

    public void Destination(tile destination)
    { 
        for (int i = 0; i < 8; i++)
        {
            if (AdjacentTile[i] == destination)
            {
                characterDirection = i; 
            }
        }
        animation.changeSpriteDirection(characterDirection); //After I found which adjacent tile is the next destination I change the character direction based on it's position (1 = North , 2 = Nort Est etc) .. so the Y of the Animation_sourceRectangle it's changed//
    
        Vector2 Position;
        Position.X = current_characterTile.X - destination.X;
        Position.Y = current_characterTile.Y - destination.Y;
    
        rotation = (float)Math.Atan2(-Position.X, Position.Y); 
    
        moveVector = (Vector2.Transform(new Vector2(0, -1), Matrix.CreateRotationZ(rotation))) * characterSpeed;
        movingCommand = 1; // the character is supposed to be moving..
        Move(); //this function moves the sprite until the *tile.i and tile.j* of the character is the same as tile.j and tile.i of the destination
    
        //something like this  
        if ( characterTile.i == destination.i && characterTile.j == destination.j) 
           movingCommand = 0 //stop
        else 
           character_Position += moveVector; 
    }
    
    更新():

    public void Destination(tile destination)
    { 
        for (int i = 0; i < 8; i++)
        {
            if (AdjacentTile[i] == destination)
            {
                characterDirection = i; 
            }
        }
        animation.changeSpriteDirection(characterDirection); //After I found which adjacent tile is the next destination I change the character direction based on it's position (1 = North , 2 = Nort Est etc) .. so the Y of the Animation_sourceRectangle it's changed//
    
        Vector2 Position;
        Position.X = current_characterTile.X - destination.X;
        Position.Y = current_characterTile.Y - destination.Y;
    
        rotation = (float)Math.Atan2(-Position.X, Position.Y); 
    
        moveVector = (Vector2.Transform(new Vector2(0, -1), Matrix.CreateRotationZ(rotation))) * characterSpeed;
        movingCommand = 1; // the character is supposed to be moving..
        Move(); //this function moves the sprite until the *tile.i and tile.j* of the character is the same as tile.j and tile.i of the destination
    
        //something like this  
        if ( characterTile.i == destination.i && characterTile.j == destination.j) 
           movingCommand = 0 //stop
        else 
           character_Position += moveVector; 
    }
    

    确保除法是浮点数,而不是整数。

    我也可以给你发一段我为实现这一点而编写的真实代码,但它不够干净,我想把它贴在这里。非常感谢你的帮助。我刚刚在我的项目中实现了这一点,它工作得很好。谢谢你,谢谢你。我现在使用的方法缺乏平滑性,当与角色旋转和一些对角线运动相结合时,结果并不令人满意。再次感谢你:D
    public void Destination(tile destination)
    { 
        for (int i = 0; i < 8; i++)
        {
            if (AdjacentTile[i] == destination)
            {
                characterDirection = i; 
            }
        }
        animation.changeSpriteDirection(characterDirection); //After I found which adjacent tile is the next destination I change the character direction based on it's position (1 = North , 2 = Nort Est etc) .. so the Y of the Animation_sourceRectangle it's changed//
    
        Vector2 Position;
        Position.X = current_characterTile.X - destination.X;
        Position.Y = current_characterTile.Y - destination.Y;
    
        rotation = (float)Math.Atan2(-Position.X, Position.Y); 
    
        moveVector = (Vector2.Transform(new Vector2(0, -1), Matrix.CreateRotationZ(rotation))) * characterSpeed;
        movingCommand = 1; // the character is supposed to be moving..
        Move(); //this function moves the sprite until the *tile.i and tile.j* of the character is the same as tile.j and tile.i of the destination
    
        //something like this  
        if ( characterTile.i == destination.i && characterTile.j == destination.j) 
           movingCommand = 0 //stop
        else 
           character_Position += moveVector; 
    }