2013-09-26 157 views
0

我正在開發一個拉澤梁(球精靈)的鏡子。在那裏,我試圖根據鏡子的比例(Rectangle)重定向延遲光束。如果碰撞的物體有一定的角度(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); 
} 
+2

你看過Vector2.Reflect嗎?它內置於xna –

+2

你不應該多次問同一個問題。 – user1306322

回答

0

你應該知道的是,如果一個物體表面上碰撞,物體速度矢量將反映在表面的法向矢量上。

您需要計算接觸點處的法向量。沿着法線的速度分量將切換方向,而垂直於法線的速度分量將保持不變。

This link應該是有用的。