2013-09-26 155 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应该是有用的。