1

有人可以给我一些关于如何在一个圆圈中产生敌人的方向(XNA 编程)吗?

我希望敌人沿着窗口边界外的圆周随机生成。我希望它们沿直线穿过窗口的中心,然后移到它们开始位置的另一侧(或尽可能靠近该位置)。

理想情况下,这将创造一个敌人从看似四面八方随机出现的环境。

到目前为止,这是我的敌人(“坏人”)课程。我在 SetupTraveling 游戏状态中处理位置。我正在做的并不是真正的工作,所以任何帮助将不胜感激。

public class Baddies : Sprite
{
    public enum State
    {
        Inactive,
        SetupTraveling,
        Traveling,
        SetupInactive,
    }

    public State CurrentState
    {
        set
        {
            currentState = value;
            framesInStateCount = 0;
        }
        get
        {
            return currentState;
        }
    }

    int framesInStateCount = 0;
    State currentState = State.SetupInactive;    

    public Baddies()
    {
        Image = getImage("Bad");
        Scale = .2f;
        Rotation = 0f;
        DRotation = .05f;
        TurnedOn = true;
        BounceOn = false;
        WrapOn = false;
        Gravity = 0f;
    }

    public override void Update()
    {
        framesInStateCount++;
        switch (currentState)
        {
            case State.Inactive:
                if (RandOneIn(100)) CurrentState = State.SetupTraveling;
                break;
            case State.SetupTraveling:
                        PositionX = ((Game1.vGameWidth + 100)/2) * (float)Math.Cos(Glob.rnd(0.0, 2.0 * Math.PI));
                        PositionY = ((Game1.vGameHeight + 100)/2) * (float)Math.Sin(Glob.rnd(0.0, 2.0 * Math.PI));                        
                        DDPositionY = 0;
                        DPositionY = -1;
                        DPositionX = 1f;
                        DDPositionX = 0f;                          

                CurrentState = State.Traveling;
                break;
            case State.Traveling:
                if (PositionX > Game1.vGameWidth + (Image.Width / 2) * Scale)
                {
                    currentState = State.SetupInactive;
                }
                if (PositionX < -500f - (Image.Width / 2) * Scale)
                {
                    currentState = State.SetupInactive;
                }
                if (PositionY > Game1.vGameHeight + (Image.Height / 2) * Scale)
                {
                    currentState = State.SetupInactive;
                }
                if (PositionY < 0 - (Image.Height / 2) * Scale)
                {
                    currentState = State.SetupInactive;
                }
                break;
            case State.SetupInactive:
                PositionX = -300f;
                DPositionX = 0f;
                DPositionY = 0f;
                DDPositionX = 0f;
                CurrentState = State.Inactive;

                break;                
        }     

        base.Update();
    } 
}
4

2 回答 2

4

三角函数的其他方法:

  1. 决定你的圆半径是多少……通常是:

    raqdius = sqrt(Viewport.Size.Width^2 + Viewport.Size.Height^2) / 2;
    
  2. 生成随机角度

    angle = (float) Random.NextDouble() * MathHelper.PI * 2; 
    
  3. 你的坐标是

    x = ViewPort.Center.X + radius * Math.Cos(angle);
    y = ViewPort.Center.Y + radius * Math.Sin(angle);
    
于 2012-12-10T22:36:24.457 回答
1

这个怎么样:

    1. 决定你圈子的raidius是什么,称之为r
    2. 从 [-r, r] 生成 X 值
    3. Y = sqrt(r^2 - X^2)
    4. 将 Y 随机设置为 Y 或 -Y。你的坐标是 X,Y

于 2012-12-10T20:50:44.590 回答