Collide with rotation of the object

Posted by Lahiru on Game Development See other posts from Game Development or by Lahiru
Published on 2013-09-26T12:53:10Z Indexed on 2013/10/27 4:16 UTC
Read the original article Hit count: 449

Filed under:
|

I'm developing a mirror for lazer beam(Ball sprite). There I'm trying to redirect the laze beam according to the ration degree of the mirror(Rectangle). How can I collide the ball to the correct angle if the colliding object is with some angle(45 deg) rather than colliding back.

here is an screen shot of my work enter image description here

here is my code

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace collision
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D ballTexture;
        Rectangle ballBounds;
        Vector2 ballPosition;
        Vector2 ballVelocity;
        float ballSpeed = 30f;
        Texture2D blockTexture;
        Rectangle blockBounds;
        Vector2 blockPosition;
        private Vector2 origin;
        KeyboardState keyboardState;

        //Font
        SpriteFont Font1;
        Vector2 FontPos;
        private String displayText;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        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();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        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);

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        ///
        private float RotationAngle;
          float circle = MathHelper.Pi * 2;
          float angle;

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            //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);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            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);
        }
    }
}

© Game Development or respective owner

Related posts about XNA

Related posts about xna-4.0