• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

C# intersect problem in XNA

Status
Not open for further replies.

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,183
hello, I am making my first 2d game and I got an odd problem with intersects.

JASS:
Rectangle rect = new Rectangle(0,0,16,62)
Rectangle rect1 = new Rectangle(790,0,16,62)
Rectangle rect2 = new Rectangle(395,0,200,200)
//some  random code that I wont show here
if (rect.Intersects(rect2))
{
    //do stuff
}
if (rect1.Intersects(rect2))
{
    //do stuff
}

since I made the rectangle of rect 2 huge it should be close to impossible that I can avoid detecting collision.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,204
How could we know what Intersects do?
By looking up the XNA framework specifications? It is a standard function after all. This also means it is safe to assume the functions will work perfectly for any reasonable value passed to them as they have been produced by a reputable source and probably used heavily.

It might have something to do with the fact the Rectangles are nowhere near each other.

Code:
public Rectangle (
         int x,
         int y,
         int width,
         int height
)
rect is at 0 with width 16
rect1 is at 790 with width 16
rect2 is at 395 with width 200

rect and rect2 are over 216 units apart so cannot possibly intersect.
rect1 and rect2 are over 216 units apart so cannot possibly intersect.

For a Rectangle to intersect, it must overlap in both the X and Y axes by some amount. Yes all your Rectangles overlap the Y axis but they are separated by the X axis so have no area touching each other.
 

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,183
fine. Here is the whole code
JASS:
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 WindowsGame1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D img;
        Texture2D img2;
        Texture2D img3;
        Texture2D back;
        
        Vector2 pos1 = new Vector2(0,0);
        Vector2 pos2 = new Vector2(780,0);
        Vector2 pos3 = new Vector2(395,0);
        Vector2 pos4 = new Vector2(0, 0);
        Vector2 speed = new Vector2(5, 2);
        Song music;
        Song sound;
        Random rnd = new Random();
        int musicCount;
        SpriteFont font;
        SpriteFont font2;
        SpriteFont font3;
        int score1 = 0;
        int score2 = 0;
        Rectangle rect = new Rectangle(0, 0, 16, 62);
        Rectangle rect1 = new Rectangle(790, 0, 16, 62);
        Rectangle rect2 = new Rectangle(395, 0, 200, 200);
        
        
        
        
        KeyboardState key;

        //pos1.X
        //pos2.Y
        
        
        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
            
            base.Initialize();
            musicCount = 0;

        }

        /// <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);
            back = Content.Load<Texture2D>("background");
            sound = Content.Load<Song>("bop");
            
            img = Content.Load<Texture2D>("paddle1");
            img2 = Content.Load<Texture2D>("paddle1");
            img3 = Content.Load<Texture2D>("ball");
            
            music = Content.Load<Song>("I cant stop");
            font = Content.Load<SpriteFont>("SpriteFont1");
            font2 = Content.Load<SpriteFont>("score 1");
            font3 = Content.Load<SpriteFont>("score2");

            // TODO: use this.Content to load your game content here
        }

        /// <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>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit




            


            key = Keyboard.GetState();
            if (key.IsKeyDown(Keys.Up))
            {
                
                if (pos2.Y + 3 > 0)
                {
                pos2.Y -= 8;
                rect.Y -= 8;
                }
            }
            if (key.IsKeyDown(Keys.Down))
            {    
            if (pos2.Y < 365)
            
                {
                    pos2.Y += 8;
                    rect.Y += 8;
                }
            
                
            }

            if (key.IsKeyDown(Keys.W))
            {
                if (pos1.Y + 3 > 0)
                {
                    pos1.Y -= 8;
                    rect1.Y -= 8;
                }
            }
            if (key.IsKeyDown(Keys.S))
            {
                if (pos2.Y < 365)
                {
                    pos1.Y += 8;
                    rect1.Y += 8;
                }
            }
            pos3.Y += speed.Y * 2;
            pos3.X += speed.X * 2;

            if (rect.Intersects(rect2))
            {
                
                    speed.X = -speed.X;
                
                
            }
            if (rect1.Intersects(rect2))
            {
                
                    speed.X = -speed.X;
                
            }
             


            
            if (pos3.Y <= 0)
            {
                speed.Y = -speed.Y;
            }

            if (pos3.Y > 450)
            {
                speed.Y = -speed.Y;
            }
            
           if (pos3.X <= -770)
            {
                score2 += 1;
                pos3.X = 400;
                pos3.Y = 400;
                speed.X = rnd.Next(1, 3);
                if (speed.X == 2)
                {
                    speed.X = 5;
                }
                else
                {
                    speed.X = -5;
                }

                speed.Y = rnd.Next(-2, 3);
            }

            if (pos3.X > 1550)
            {
                score1 += 1;
                
                pos3.X = 400;
                pos3.Y = 400;
                
                
                speed.X = rnd.Next(1, 3);
                if (speed.X == 2)
                {
                    speed.X = 5;
                }
                else
                {
                    speed.X = -5;
                }
                
                speed.Y = rnd.Next(-2, 3);
            }
            /*


           if (pos3.X >= pos2.X - 5 && pos3.Y > pos2.Y - 100 && pos3.Y < pos2.Y + 100)
            {
                speed.X = -speed.X;
                
            }

           if (pos3.X <= pos1.X - 5 && pos3.Y > pos1.Y - 100 && pos3.Y < pos1.Y + 100)
            {
                speed.X = -speed.X;
                
            }*/
              
              

            musicCount -= 17;
            if (musicCount <= 0)
            {
                MediaPlayer.Play(music);
                musicCount = 34000;
            }
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            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);
            spriteBatch.Begin();
            spriteBatch.Draw(back, pos4, Color.White);
            spriteBatch.Draw(img, pos1, Color.White);
            spriteBatch.Draw(img2, pos2, Color.White);
            spriteBatch.Draw(img3, pos3, Color.White);
            spriteBatch.DrawString(font, "X: " + speed.X + "\n Y: " + speed.Y, new Vector2(400,50), Color.Black);
            spriteBatch.DrawString(font, "player 1; " + score1,new Vector2(300, 400), Color.Black);
            spriteBatch.DrawString(font, "player 2; " + score2, new Vector2(500, 450), Color.Black);
            
            
            spriteBatch.End();

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}

I got a solution in the comment text but its bugging and I want to use the intersect to replace it with.
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
You are not updating the rectangle of the ball.
Why are you using both rectangles and vectors in the first place? SpriteBatch::Draw can take either of them as the second parameter.

The logic of moving the paddles befuddles me.
E.g.
C#:
if (key.IsKeyDown(Keys.W))
{
    if (pos1.Y + 3 > 0)
    {
        pos1.Y -= 8;
        rect1.Y -= 8;
    }
}
Are you sure that's logical? I would imagine the check should be (pos1.Y - 8) > 0
 
Status
Not open for further replies.
Top