C#XNA 4避免碰撞 [英] C# XNA 4 collision avoidance

查看:82
本文介绍了C#XNA 4避免碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

This is my code

public Vector2 theclickedpoint;
      public Boolean clicked = false;
       protected override void Update(GameTime gameTime)
       {
           // Allows the game to exit
           if (Keyboard.GetState().IsKeyDown(Keys.Escape))
               Exit();







           //press left button down
           MouseState mouseState = Mouse.GetState();
           if (mouseState.LeftButton == ButtonState.Pressed)
           {
               //get the clicked point area
               theclickedpoint.X = mouseState.X;
               theclickedpoint.Y = mouseState.Y;
               clicked = true;
           }




           //move the redmouse sprite the the clicked mouse location
           if (clicked == true)
           {
               if (redmousePosition == theclickedpoint)
               {
                   clicked = false;
               }
               else
               {
                   // the X
                   //greater then
                   if (redmousePosition.X <= theclickedpoint.X)
                   {
                       redmousePosition.X += 10;
                   }
                   //less then
                   if (redmousePosition.X >= theclickedpoint.X)
                   {
                       redmousePosition.X -= 10;
                   }

                   // the Y
                   //greater then
                   if (redmousePosition.Y <= theclickedpoint.Y)
                   {
                       redmousePosition.Y += 10;
                   }
                   //less then
                   if (redmousePosition.Y >= theclickedpoint.Y)
                   {
                       redmousePosition.Y -= 10;
                   }
               }

           }




           // TODO: Add your update logic here

           base.Update(gameTime);
       }



现在它转到我单击的鼠标位置,(是的,我知道代码看起来很尴尬),但是我希望它能够避免对象的出现,比如说它只会绕过一个简单的texture2d精灵,任何人都可以帮助我实现



right now it goes to my clicked mouse location, (yeh i know the code looks awkward)but i want it to be able the avoid objects if its in its way, say it will just go around a simple texture2d sprite can anyone help me achieve this goal?

推荐答案

不幸的是,您所要求的并不是游戏开发中最简单的事情,因此我无法提供一个很好的例子.

假设您的精灵位于起始位置,一旦建立了新目标(您的接触点),您就需要运行计算以确定路径.

您可以将该路径存储为许多Vector2(用于2D),然后让精灵开始向每个点连续移动,直到到达终点为止.

不幸的是,有很多变量,例如,如果某些东西进入您已建立的路径,该怎么办?您将不得不重新计算路径.

这是一个不错的示例(请参阅第一个答案) [ ^ ]
Unfortunately what you are asking for isn''t the easiest thing in game development, so I can''t provide an excellent example.

Assuming your sprite is at a starting location, as soon as a new target is established (your touch point), you need to run a calculation to determine the path.

You can store that path as a number of Vector2''s (for 2D) and then have the sprite begin to move toward each point in succession until it reaches the end.

Unfortunately there are lots of variables, such as what if something moves into the path you''ve established? You would have to recalculate the path.

Here''s a decent example (See first answer)[^]


这篇关于C#XNA 4避免碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆