1
\$\begingroup\$

I realized I put this in the wrong forum, so here it is:

I made a collision detection in the Update() section, and this is the code:

    if (player.playerRect.Intersects(turtleRect)) 
{ 
    turtleRect.X = turtleRect.X - 60; 
} 

When I run the game, and collide with the turtle, nothing happens. I tried changing the

turtleRect.X = turtleRect.X - 60; 

To

turtleRect.X = turtleRect.X - 600; 

Because I thought I wasn't noticing the change. Still, nothing happened. Then I thought it was just the

turtleRect.X = turtleRect.X - 600 

So I put that in the Update() method, outside of the if statement and it worked. Now I realize that it is the

if (player.playerRect.Intersects(turtleRect)) {

} 

But I cannot figure out what is wrong with that. I have the playerRect in a different class, but I imported it into the main class. I know the Rectangles are moving because of the

    turtleRect.X -= 1; 

in the Update() method. The turtle moves across the screen. I have tested many things, and looked it up and I still can't figure it out. This is the rectangle collision, so could somebody also guide me on how to do the per pixel collision? Here is my Draw() method:

protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();
        spriteBatch.Draw(background, viewPortRect, Color.White);
        spriteBatch.Draw(turtle, turtleRect, Color.White);
        spriteBatch.Draw(turtleSmall, turtleRect2, Color.White);
        spriteBatch.Draw(turtleSmall2, turtleRect3, Color.White);
        spriteBatch.Draw(turtleSmall3, turtleRect4, Color.White);
        player.Draw(spriteBatch);
        spriteBatch.DrawString(font, "Level " + score, new Vector2(20, 10), Color.Blue);
        spriteBatch.End();

        base.Draw(gameTime);
    }

Any help is appreciated.

If you need the whole class, here is Game1.cs:

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 Game1
{

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D turtle;
        Texture2D turtleSmall;
        Texture2D turtleSmall2;
        Texture2D turtleSmall3;
        Texture2D background;
        Rectangle viewPortRect;
        Rectangle turtleRect;
        Rectangle turtleRect2;
        Rectangle turtleRect3;
        Rectangle turtleRect4;
        public Texture2D texture;
        public Rectangle playerRect;
        public Vector2 position;
        public Vector2 origin;
        public Vector2 velocity;
        SpriteFont font;
        Character player;
        int score = 1;
        public int currentFrame;
        public int frameHeight;
        public int frameWidth;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

        }

        protected override void Initialize()
        {
            player = new Character(Content.Load<Texture2D>("flame"), new Vector2(100, 100), 47, 44);



              base.Initialize();
            }
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            background = Content.Load<Texture2D>("grass");
            turtle = Content.Load<Texture2D>("turtle");
            font = Content.Load<SpriteFont>("SpriteFont1");
            turtleSmall = Content.Load<Texture2D>("turtleSmall");
            turtleSmall2 = Content.Load<Texture2D>("turtleSmall");
            turtleSmall3 = Content.Load<Texture2D>("turtleSmall");
            viewPortRect = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
            turtleRect = new Rectangle((GraphicsDevice.Viewport.Width) - (turtle.Width + 200), (GraphicsDevice.Viewport.Height - 66) - (turtle.Height / 2), 100, 75);
            turtleRect2 = new Rectangle((GraphicsDevice.Viewport.Width) - (turtle.Width), (GraphicsDevice.Viewport.Height - 40) - (turtle.Height / 2), 50, 38);
            turtleRect3 = new Rectangle((GraphicsDevice.Viewport.Width) - (turtle.Width -50), (GraphicsDevice.Viewport.Height - 40) - (turtle.Height / 2), 50, 38);
            turtleRect4 = new Rectangle((GraphicsDevice.Viewport.Width) - (turtle.Width - 100), (GraphicsDevice.Viewport.Height - 40) - (turtle.Height / 2), 50, 38);

        }

        protected override void UnloadContent()
        {

        }
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            //Turtle Movement
            turtleRect.X -= 1;
            turtleRect2.X -= 1;
            turtleRect3.X -= 1;
            turtleRect4.X -= 1;
            //Turtle Collision
            if (player.playerRect.Intersects(turtleRect))
            {
                turtleRect.X -= 500;
            }

            player.Update(gameTime);
            base.Update(gameTime);
        }

        private Vector2 CenterTexture(Texture2D turtle)
        {
            return new Vector2((GraphicsDevice.Viewport.Width) - (turtle.Width), (GraphicsDevice.Viewport.Height - 66) - (turtle.Height / 2));
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();
            spriteBatch.Draw(background, viewPortRect, Color.White);
            spriteBatch.Draw(turtle, turtleRect, Color.White);
            spriteBatch.Draw(turtleSmall, turtleRect2, Color.White);
            spriteBatch.Draw(turtleSmall2, turtleRect3, Color.White);
            spriteBatch.Draw(turtleSmall3, turtleRect4, Color.White);
            player.Draw(spriteBatch);
            spriteBatch.DrawString(font, "Level " + score, new Vector2(20, 10), Color.Blue);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

Here is the Character.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Game1
{
    class Character
    {
        public Texture2D texture;
        public Rectangle playerRect;
        public Vector2 position;
        public Vector2 origin;
        public Vector2 velocity;

        public int currentFrame;
        public int frameHeight;
        public int frameWidth;

        float timer;
        float interval = 75;

        bool hasJumped;

        public Character(Texture2D newTexture, Vector2 NewPosition, int newFrameHeight, int newFrameWidth)
        {
            position = NewPosition;
            texture = newTexture;
            frameHeight = newFrameHeight;
            frameWidth = newFrameWidth;
            hasJumped = true;
        }

        public void Update(GameTime gameTime)
        {
            position += velocity;

            playerRect = new Rectangle(currentFrame * frameWidth, 0, frameWidth, frameHeight);
            origin = new Vector2(playerRect.Width / 2, playerRect.Height / 2);
            position = position + velocity;

            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                velocity.X = 2f;
                AnimateRight(gameTime);
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                velocity.X = -2f;
                AnimateLeft(gameTime);
            }
            else
            {
                velocity.X = 0f;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Space) && hasJumped == false)
            {
                position.Y -= 3f;
                velocity.Y = -3.5f;
                hasJumped = true;
            }

            if (hasJumped == true)
            {
                float i = 1;
                velocity.Y += 0.15f * i;
            }

            if (position.Y + texture.Height >= 450)
            {
                hasJumped = false;
            }

            if (hasJumped == false)
            {
                velocity.Y = 0f;
            }

        }

        public void AnimateRight(GameTime gameTime)
        {
            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 2;
            if (timer > interval)
            {
                currentFrame++;
                timer = 0;
                if (currentFrame > 3)
                {
                    currentFrame = 0;
                }
            }
        }

        public void AnimateLeft(GameTime gameTime)
        {
            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 2;
            if (timer > interval)
            {
                currentFrame++;
                timer = 0;
                if (currentFrame > 7 || currentFrame < 4)
                {
                    currentFrame = 4;
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, position, playerRect, Color.White, 0f, origin, 1.0f, SpriteEffects.None, 0);
        }

    }
}
\$\endgroup\$
5
  • \$\begingroup\$ Are you using the XNA Rectangle? or your own? If the if statement is not evaluating to true, then it means they are not intersecting. If you visually see them intersect then that means you are using another variable to represent the position of your turtle. \$\endgroup\$ Mar 10, 2012 at 3:30
  • \$\begingroup\$ I am clearly seeing them intersect, but nothing happens. If I am using a different variable for the turtle's position, then why does the turtle move right to left when I do turtleRect.X -= 1; I use the same variable for the movement as the intersection. Do you need more code? If you do, which parts do you want? I can give you the class or part of it :) \$\endgroup\$
    – CJ Cohorst
    Mar 10, 2012 at 3:45
  • \$\begingroup\$ Are you using your own Rectangle class? or XNA's? Yes, can you post your drawing code? and what do you mean by "I have the playerRect in a different class, but I imported it into the main class." \$\endgroup\$ Mar 10, 2012 at 3:51
  • \$\begingroup\$ I used XNA's Rectangle class. The Character.cs is a different class, and that is what the playerRect variable is in. I imported the playerRect in the main class. I will also put the drawing code in the post right now. \$\endgroup\$
    – CJ Cohorst
    Mar 10, 2012 at 3:54
  • \$\begingroup\$ I am sorry but could you pastebin your code? The Character.cs and the Game.cs. \$\endgroup\$ Mar 10, 2012 at 4:02

1 Answer 1

1
\$\begingroup\$

The player.playerRect is just a rectangle that represent which portion of the texture to draw. Which is not what you want to use when doing the intersection.

So to get this to work use the playerRect (which is not currently being used) in your Game class to create the bounds of the player:

playerRect = new Rectangle(player.position.X, player.position.Y, player.playerRect.Width, player.playerRect.Height);
if (playerRect.Intersects(turtleRect))
{
  turtleRect.X -= 500;
}
\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .