2013-09-26 6 views
0

저는 레이저 빔 (볼 스프라이트) 용 미러를 개발 중입니다. 거기서 나는 거울의 배합 정도 (직사각형)에 따라 잔잔한 광선을 방향 전환하려고 노력하고 있습니다. 충돌하는 물체가 뒤쪽으로 충돌하는 것이 아니라 약간의 각도 (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이 유용합니다.