C# 如何检测玩家是否与世界的一部分相交

C# 如何检测玩家是否与世界的一部分相交,c#,xna,intersection,intersect,C#,Xna,Intersection,Intersect,如果题目不清楚,我很抱歉,但由于我的英语水平很低,我找不到一个办法来清楚地问 但我可以解释得很清楚 所以我意识到如果我设计我的世界(和世界,我的意思是整个游戏,它将是一个级别)10.000x10.000。。。这将是非常足够的,除了少数其他雪碧(我的意思是像4或5最大50x50,没有什么大的) 所以我想,为什么我不把我的整个地图做成10.000x10.000(或者说512x512吨)的图片呢? 但我有一个问题,你能“互动”的东西很少。他们(我指的是我的“world.jpg”中的东西)会一直呆在同一

如果题目不清楚,我很抱歉,但由于我的英语水平很低,我找不到一个办法来清楚地问

但我可以解释得很清楚

所以我意识到如果我设计我的世界(和世界,我的意思是整个游戏,它将是一个级别)10.000x10.000。。。这将是非常足够的,除了少数其他雪碧(我的意思是像4或5最大50x50,没有什么大的)

所以我想,为什么我不把我的整个地图做成10.000x10.000(或者说512x512吨)的图片呢? 但我有一个问题,你能“互动”的东西很少。他们(我指的是我的“world.jpg”中的东西)会一直呆在同一个地方,但玩家(实际上是你知道的精灵)会移动,因此我的10.000x10.000会“移动”

所以看下面的图片,有一个黑点是“玩家”,一个红点是门

世界总是以黑点为中心,除非他走到世界的尽头。正如你们所看到的,(看图片第1部分和第2部分)当他向东移动一点时,红点看起来移动了。但我刚刚移动了我的10.000x10.000图像。这就是我的意思,10kx10k图片上的东西会移动

不管怎样,但正如你们在图的最后一部分所看到的,当他走近红点时,我想要我的“动作”

怎么做

-下面的部分实际上与主要问题无关

当他移动时,使用10kx10图片代替世界上出现的另一个精灵有用吗?但如果我想这样做,我不仅会检查他是否在附近,而且还会检查他的位置,以了解我是否应该或不应该给他看精灵

当他谈到我想要的坐标时,如果我展示我的东西会更有用吗,或者使用一张大图就可以了

谢谢


我建议地图的结构有点像这样

public class Map
{
     public MapPoint[,] mapPoints;       //the map
     public Player player;               //the player/user object
     public Vector2 DrawHeroPosition;    
     //where at the screen the player is going to be drawn
     public Vector2 RangeStart;          
     //what part of the map who is going to be drawn
     public int SizeX;     //number of mapPoints the screen can contain at one time 
     public int SizeY;     //number of mapPoints the screen can contain at one time 

     //MapPoint represents a specific 512x512 point (mapPoint) its position at
     //the map but also includes the sprite that is going to be drawn and objects
     //that the player can interact with at that place (like the door)

     //the player object includes reference to where in the world it is place

     public Map(ContentManager theContentManager, int x, int y)
     {
        MapSizeX = x;
        MapSizeY = y;
        int ScreenSizeX = 9;
        int ScreenSizeY = 9;
        mapPoints = new MapPoint[MapSizeX , MapSizeY];

        //ad code for generating/creating map...
        //important that you store the MapPoints position inside each mapPoint

        player = new Player(mapPoints[0,0]);  //crate a player who knows where he is
    }

    public void Update()
    {
       //in the update method you do a lot of things like movement and so
       //set what part of the map the game should draw if the game for example
       //can show 9x9 512points at a single time

       //give range value from the players position
        RangeStart.X = player.PositionX;

        //test if the maps position is in the left corner of the map
        //if it is draw the map from the start..(RangeStart.X = 0)
        if (player.PositionX - (ScreenSizeX / 2) < 0) { RangeStart.X = 0; }
        //if not draw the hero in the mitle of the screen
        else
        {
            RangeStart.X = player.PositionX - (ScreenSizeX / 2);
        }
        //if the hero is in the right corer, fix his position
        while (RangeStart.X + ScreenSizeX > MapSizeX)
        {
            RangeStart.X--;
        }

        //the same thing for the Y axle
        RangeStart.Y = player.PositionY;
        if (player.PositionY - (ScreenSizeY / 2) < 0) { RangeStart.Y = 0; }
        else
        {
            RangeStart.Y = player.PositionY - (ScreenSizeY / 2);
        }
        while (RangeStart.Y + ScreenSizeY > MapSizeY)
        {
            RangeStart.Y--;
        }

        //time to set the position of the hero...
        //he works like the opposite of the range, if you move what part of the map
        //you draw you dont change the heros draw position, if you dont move the range
        //you have to move the hero to create the illusion of "moment"

        //if you are in the left part you have to move the heros draw position..
        if (player.PositionX - (ScreenSizeX / 2) < 0) 
        { DrawHeroPosition.X = player.PositionX; }

        //if you are in the right part
        else if (player.PositionX+1 > MapSizeX - (ScreenSizeX / 2))
        {
            DrawHeroPosition.X = player.PositionX - (MapSizeX - ScreenSizeX);
        }

        //if you aint in a corner, just place the hero in the middle of the map
        else
        {
            DrawHeroPosition.X = (ScreenSizeX / 2);
        }


        //the same thing for Y
        if (player.PositionY - (ScreenSizeY / 2) < 0) 
        { DrawHeroPosition.Y = player.PositionY; }
        else if (player.PositionY+1 > MapSizeY - (ScreenSizeY / 2))
        {
            DrawHeroPosition.Y = player.PositionY - (MapSizeY - ScreenSizeY);
        }
        else
        {
            DrawHeroPosition.Y = (ScreenSizeY / 2);
        }

    }

    public void Draw()
    {

        int x = (int)RangeStart.X;
        int y = (int)RangeStart.Y;

        for(int counterX = 0; x < ((MapSizeX)); x++, counterX++)
        {
            for (int counterY = 0; y < (MapSizeY); y++, counterY++)
            {
               if (mapPoints[x, y] != null)
               {
                 mapPoints[x, y].Draw(spriteBatch, mapPoints[counterX,counterY].positonInMatrix);
                 //mapPoints[counterX,counterY] = where to draw
                 //mapPoints[x, y] = what to draw
               }
            }
            y = (int)RangeStart.Y;
        }
    }
}

如果您要求在红点半径范围内进行碰撞检测。您可以简单地使用以下测试(伪代码,我不写C:-)

if((player.GetPosition()-point.GetPosition()).length()
这将检测玩家是否在圆点的某个半径内,然后你可以执行任何你想要的动作


希望这有帮助!:)

好的,从我对你的问题的理解来看,你有一个大图像,其中包含你希望你的玩家与之交互的不同对象,是吗?我的意思是,图像文件本身有门、山或其他玩家可以与之交互的东西

老实说,这是个坏主意,所以我希望我误解了。最好让你的背景图像只是一些通用的东西,并在游戏中创建所有交互式对象类。如果执行此操作,则可以使对象类包含彼此相交的行为,该行为基于对象类的距离(圆碰撞)或为对象类定义的边界框

圆碰撞:

if (Math.Abs(Vector2.Distance(player.Position - obj.Position)) < player.Radius + obj.Radius)
    //do the action
if (player.Bounds.Intersects(obj.Bounds))
   //do the action
此外,如果您计划制作10000 x 10000像素的图像,请了解XNA内容管道不会将大于4000像素的图像导入到一个侧面


如果设置让播放器与图像背景中的像素交互,则可以手动创建一个交互对象位置数组,也可以使用Texture2D.GetData()方法加载图像中每个像素的颜色进行处理,但请注意,这需要很长时间,特别是对于大的或多个纹理。

理解您的问题确实很困难。我建议试着让人帮你翻译。如果我误解了你或有什么不清楚的地方,请留下评论=)如果我将此代码更改为使用10.000x10.000图片,是否可以,或者我应该使用大量512x512图片?另外,如果玩家(精灵)触摸,比如说100x、150y部分,它会实现哪一部分?做了一些大的改变,这个新版本可以工作,包括一种处理玩家位置和绘图的可能方法。
if (Math.Abs(Vector2.Distance(player.Position - obj.Position)) < player.Radius + obj.Radius)
    //do the action
if (player.Bounds.Intersects(obj.Bounds))
   //do the action