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

发布于 2024-11-26 05:16:20 字数 822 浏览 0 评论 0原文

如果问题标题不清楚,我很抱歉,但以我廉价的英语,我找不到办法清楚地问它。

但我可以用很长的方式解释它。

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

所以我想,为什么我不把整个地图制作为 10.000x10.000(或者可以说大量) 512x512)图片? 但我有一个问题,可以“互动”的东西很少。他们(对于他们来说,我的意思是我的“world.jpg”中的东西)总是停留在同一个地方,但玩家(实际上是一个精灵,如你所知)会移动,因此我的 10.000x10.000 将“移动”。

看下面的图片,有一个黑点是“玩家”,还有一个红点,可以说是一扇门。

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

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

该怎么做?

-下面的部分与主要问题并不真正相关

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

当他找到我想要的坐标时,我展示我的东西会更有用,还是用一张大图就可以了?

谢谢。

I'm sorry if question title was unclear, but with my cheap english, I cant find a way to ask it clearly.

But I can explain it in long way.

So I have realized if I design my world(and with world, I mean ENTIRE game, it will be one level) 10.000x10.000... it will be very enough, other than few another sprite(and I mean like 4 or 5 with maximum of 50x50, nothing big.)

So I thought, why dont I make my entire map as 10.000x10.000(or lets say tons of 512x512) picture ?
But I have one question, there is few things you can "interact". they will(with they, I mean the stuff that is in my "world.jpg") be always stay at same place, but player(which is actually a sprite as you know) will move, therefore my 10.000x10.000 will "move".

So look at picture below, there is black dot which is "player" and red dot, which is lets say, a door.

and world is always centered to black dot unless he goes to end of the world. as you can see, (look at picture part 1 and part 2) when he moves a little bit to east, red dot looks moved. but I just moved my 10.000x10.000 image. Thats what I meant with the stuff on 10kx10k pic will move.

Anyway, but as you can see in last part of pic, when he goes near red dot, I want to my "action"

How to do it ?

-part below is not really related to main question

Is it useful to use 10kx10 pic instead of another sprites appearing on world when he moves ? but If I want to do that, not just I will check if he is nearby, but I will also check his point to realize if I should or shouldnt show him sprite.

Will it be more useful if I show my stuff when he comes to coordinates I want, or is using one big picture is OK ?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半山落雨半山空 2024-12-03 05:16:20

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

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;
        }
    }
}

我如何在 MapPoint 类中绘制...

public void Draw(SpriteBatch theSpriteBatch, Vector2 positonOnScreen)
    {
        positonOnScreen = new Vector2(positonOnScreen.X * base.Scale * 16,
        positonOnScreen.Y * base.Scale * 16);

        //base.Scale is just a variable for have the ability to zoom in/out
        //16 represents the original size of the picture (16x16 pixels)

        theSpriteBatch.Draw(mSpriteTexture, new Rectangle((int)positonOnScreen.X,
        (int)(positonOnScreen.Y), 64, 64),new Rectangle(0, 0, 16, 16), Color.White);
     }

I would suggest a structure of the map somewhat like this..

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;
        }
    }
}

how i draw inside the MapPoint Class...

public void Draw(SpriteBatch theSpriteBatch, Vector2 positonOnScreen)
    {
        positonOnScreen = new Vector2(positonOnScreen.X * base.Scale * 16,
        positonOnScreen.Y * base.Scale * 16);

        //base.Scale is just a variable for have the ability to zoom in/out
        //16 represents the original size of the picture (16x16 pixels)

        theSpriteBatch.Draw(mSpriteTexture, new Rectangle((int)positonOnScreen.X,
        (int)(positonOnScreen.Y), 64, 64),new Rectangle(0, 0, 16, 16), Color.White);
     }
淡忘如思 2024-12-03 05:16:20

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

if( (player.GetPosition() - point.GetPosition()).length() < radius )
{ /* Do code here */ }

这将检测您的玩家是否在您的点的特定半径内,然后您可以执行您想要的任何操作。

希望这有帮助! :)

If you are asking for collision detection within a radius of your red dot. You can simply use the following test (pseudocode, I don't write C# :-)

if( (player.GetPosition() - point.GetPosition()).length() < radius )
{ /* Do code here */ }

This will detect if your player is within a certain radius of your dot, you can then perform whatever action you wish.

Hope this helps! :)

和影子一齐双人舞 2024-12-03 05:16:20

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

老实说,这是一个坏主意,所以我希望我误解了。最好让您的背景图像只是通用的,并将所有交互式对象分类到您的游戏中。如果这样做,那么您可以让对象类包含基于它们的距离(圆形碰撞)或基于您为它们定义的边界框彼此相交的行为。

圆形碰撞:

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

另外,如果您计划制作 10,000 x 10,000 像素的图像,请了解 XNA 内容管道不会导入一侧大于 4,000 像素的图像。

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

Ok, from what I understand of your question, you have a large image which contains different objects you want your player to interact with, yes? By which I mean, the image file itself has doors or hills or other things which the player would interact with.

This is a bad idea, honestly, so I hope I misunderstood. It is far better to have your background image just be something generic and make all interactive objects classes within your game. If you do this, then you can have your object classes contain behavior to intersect with each other either based on their distance (circle collision) or based on a bounding box you define for them.

Circle Collision:

if (Math.Abs(Vector2.Distance(player.Position - obj.Position)) < player.Radius + obj.Radius)
    //do the action

Rectangle Collision:

if (player.Bounds.Intersects(obj.Bounds))
   //do the action

Also, if you are planning on making a 10,000 x 10,000 pixel image, understand that the XNA Content Pipeline will not import an image greater than 4,000 pixels to a side.

If you are set on having the player interact with pixels in the background of the image, you can either make an array of interactive object locations manually or you can use the Texture2D.GetData() method to load in the colors of every single pixel in the image for processing-- but be aware that this will take a long time, especially for large or numerous textures.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文