与角度物体碰撞

时间:2013-09-26 11:52:16

标签: c# xna xna-4.0 xna-math-library xnanimation

我正在开发一种用于激光束(Ball sprite)的镜子。在那里,我试图根据镜子的比例度(矩形)重定向laze光束。如果碰撞物体与某个角度(45度)相撞而不是碰撞,我怎么能将球碰撞到正确的角度。

这是我工作的屏幕截图 enter image description here

这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

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

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

This link应该有用。