0

我正在为激光束(球精灵)开发一面镜子。在那里,我试图根据镜子(矩形)的配给度来重定向延迟光束。如果碰撞物体以某个角度(45 度)而不是向后碰撞,我如何将球碰撞到正确的角度。

这是我工作的屏幕截图 在此处输入图像描述

这是我的代码:

protected override void Initialize()
{
    // TODO: Add your initialization logic here
    ballPosition = new Vector2(this.GraphicsDevice.Viewport.Width / 2,
                   this.GraphicsDevice.Viewport.Height * 0.25f);
    blockPosition = new Vector2(this.GraphicsDevice.Viewport.Width / 2,
                               this.GraphicsDevice.Viewport.Height /2);
    ballVelocity = new Vector2(0, 1);

    base.Initialize();
}

protected override void LoadContent()
{
    // Create a new SpriteBatch, which can be used to draw textures.
    spriteBatch = new SpriteBatch(GraphicsDevice);

    ballTexture = Content.Load<Texture2D>("ball");
    blockTexture = Content.Load<Texture2D>("mirror");

    //create rectangles based off the size of the textures
    ballBounds = new Rectangle((int)(ballPosition.X - ballTexture.Width / 2),
    (int)(ballPosition.Y - ballTexture.Height / 2), ballTexture.Width, ballTexture.Height);

    blockBounds = new Rectangle((int)(blockPosition.X - blockTexture.Width / 2),
    (int)(blockPosition.Y - blockTexture.Height / 2), blockTexture.Width, blockTexture.Height);

    origin.X = blockTexture.Width / 2;
    origin.Y = blockTexture.Height / 2;
    // TODO: use this.Content to load your game content here
    Font1 = Content.Load<SpriteFont>("SpriteFont1");
    FontPos = new Vector2(graphics.GraphicsDevice.Viewport.Width - 100, 20);

}

private float RotationAngle;
  float circle = MathHelper.Pi * 2;
  float angle;

protected override void Update(GameTime gameTime)
{
    //check for collision between the ball and the block, or if the ball is outside the bounds of the screen

    if (ballBounds.Intersects(blockBounds) || !GraphicsDevice.Viewport.Bounds.Contains(ballBounds))
    {
        //we have a simple collision!
        //if it has hit, swap the direction of the ball, and update it's position
        ballVelocity = -ballVelocity;
        ballPosition += ballVelocity * ballSpeed;
    }
    else
    {
        //move the ball a bit
        ballPosition += ballVelocity * ballSpeed;
    }

    //update bounding boxes
    ballBounds.X = (int)ballPosition.X;
    ballBounds.Y = (int)ballPosition.Y;

    blockBounds.X = (int)blockPosition.X;
    blockBounds.Y = (int)blockPosition.Y;

    keyboardState = Keyboard.GetState();

    float val = 1.568017f/90;

    if (keyboardState.IsKeyDown(Keys.Space))
        RotationAngle = RotationAngle + (float)Math.PI;

    if (keyboardState.IsKeyDown(Keys.Left))
        RotationAngle = RotationAngle - val;

    angle = (float)Math.PI / 4.0f;  // 90 degrees
    RotationAngle = angle;
    //   RotationAngle = RotationAngle % circle;
        displayText = RotationAngle.ToString();
    base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);

    spriteBatch.Begin();

    // Find the center of the string
    Vector2 FontOrigin = Font1.MeasureString(displayText) / 2;
    spriteBatch.DrawString(Font1, displayText, FontPos, Color.White, 0, FontOrigin, 1.0f, SpriteEffects.None, 0.5f);
    spriteBatch.Draw(ballTexture, ballPosition, Color.White);
    spriteBatch.Draw(blockTexture, blockPosition,null, Color.White, RotationAngle,origin, 1.0f, SpriteEffects.None, 0f);

    spriteBatch.End();
    base.Draw(gameTime);
}
4

1 回答 1

0

你应该知道,如果一个物体在一个表面上发生碰撞,物体的速度矢量将反映在表面的法向量上。

您需要计算接触点的法线向量。沿法线的速度分量将切换方向,而垂直于法线的速度分量将保持不变。

这个链接应该很有用。

于 2013-09-26T23:21:38.633 回答