platform collision

stopping on platform

Page 1 of 1

2 Replies - 2352 Views - Last Post: 06 July 2010 - 06:02 AM

#1 Guest_cameron*


Reputation:

platform collision

Posted 06 July 2010 - 02:13 AM

I'm trying to make a platform game but can't seem to figure out how to get the play to stop descending when it comes into contact with a platform. I've got it set up currently where if they collide then the player position equals the top of the platform rectangle until the player walks off. I'd like to get it where the player can jump off the platform but I figured if I could get the platform detection correct then that will just follow suit. Here's what I've got, I commented out some of the stuff so that only the player and the platform are being drawn. Any constructive help would be appreciated.

namespace Projcmk02
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D Background;
        Texture2D Background2;
        Texture2D RM1;
        Texture2D RM2;
        Texture2D RM3;
        Texture2D bowser;
        Texture2D supamario;
        Texture2D platform;

        Vector2 pos1= new Vector2(0,300);
        Vector2 pos2= new Vector2(0,0);
        Vector2 pos3= new Vector2(0,500);
        Vector2 pos4= new Vector2(300,460);
        Vector2 pos5= new Vector2(100,0);
        Vector2 pos6 = new Vector2(250,200);

        //float sp0 = 0f;
        float sp1 = 2f;
        float sp2 = 3f;
        float sp3 = 4f;
        float sp4 = 3f;
        float sp5 = 4f;
        float sp6 = 5f;
        float sp7 = 3f;
        float sp8 = 6f;
        float sp9 = 2f;
        float sp10 = 2f;

        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();
        }

        /// <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);
            bowser = Content.Load<Texture2D>(@"Images/smwbowser");
            Background = Content.Load<Texture2D>(@"Images/Background");
            Background2 = Content.Load<Texture2D>(@"Images/mario-and-peach");
            supamario = Content.Load<Texture2D>(@"Images/supamario");
            RM1 = Content.Load<Texture2D>(@"Images/mred");
            RM2 = Content.Load<Texture2D>(@"Images/mred");
            RM3 = Content.Load<Texture2D>(@"Images/mred");
            platform = Content.Load<Texture2D>(@"Images/platform");
           

            // 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
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            KeyboardState keyboardState = Keyboard.GetState();
            if (keyboardState.IsKeyDown(Keys.Left))
                pos4.X -= sp1;
            if (keyboardState.IsKeyDown(Keys.Right))
                pos4.X += sp1;
            if (keyboardState.IsKeyDown(Keys.Up))
                pos4.Y -= sp1;
            else
                pos4.Y += sp1;
            if (keyboardState.IsKeyDown(Keys.Down))
                pos4.Y += sp1;




            if (pos4.X < 0)
                pos4.X = 0;
            if (pos4.Y < 0)
                pos4.Y = 0;
            if (pos4.X > window.ClientBounds.Width - 114)
                pos4.X = window.ClientBounds.Width - 114;
            if (pos4.Y > window.ClientBounds.Height - 150)
                pos4.Y = window.ClientBounds.Height - 150;

            pos5.X += sp2;
            if (pos5.X > window.ClientBounds.Width) pos5.X = -135;
            pos5.Y += sp3;
            if (pos5.Y > window.ClientBounds.Height) pos5.Y = -202;


            pos1.X += sp4;
            if (pos1.X > window.ClientBounds.Width - 100 || pos1.X < 0) sp4 *= -1;
            pos1.Y += sp5;
            if (pos1.Y > window.ClientBounds.Height) pos1.Y = -100;

            pos2.X += sp6;
            if (pos2.X > window.ClientBounds.Width - 100 || pos2.X < 0) sp6 *= -1;
            pos2.Y += sp7;
            if (pos2.Y > window.ClientBounds.Height) pos2.Y = -100;

            pos3.X += sp8;
            if (pos3.X > window.ClientBounds.Width - 100 || pos3.X < 0) sp8 *= -1;
            pos3.Y += sp9;
            if (pos3.Y > window.ClientBounds.Height) pos3.Y = -100;


            GamePadState gamepadState = GamePad.GetState(PlayerIndex.One);
            pos4.X += sp10 * gamepadState.ThumbSticks.Left.X;

            if (Collideplatform())
            {
                pos4.Y = 128;
                pos6 = new Vector2(250, 200);
            }

            else 

          /*  if (CollideRM1())
            {
                pos4.Y -= 50;
                pos1 = new Vector2(0, 100);
                GamePad.SetVibration(PlayerIndex.One, 0f, .02f);
            }
            if (CollideRM2())
            {
                pos4.Y -= 50;
                pos2 = new Vector2(0, 500);
                GamePad.SetVibration(PlayerIndex.One, 0f, .02f);
            }
            if (CollideRM3())
            {
                pos4.Y -= 50;
                pos3 = new Vector2(0, 300);
                GamePad.SetVibration(PlayerIndex.One, 0f, .02f);
            }
            if (Collidebowser())
            {
                pos4.Y += 50;
                pos5 = new Vector2(0, 0);
                GamePad.SetVibration(PlayerIndex.One, 0.25f, .02f);
            }*/
            
           
            // 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();
            if (pos4.Y > 0)
                spriteBatch.Draw(Background, Vector2.Zero, Color.White);
            if (pos4.Y < 2)
                spriteBatch.Draw(Background2, Vector2.Zero, Color.White);
           // spriteBatch.Draw(RM1, pos1, Color.White);
           // spriteBatch.Draw(RM2, pos2, Color.White);
           // spriteBatch.Draw(RM3, pos3, Color.White);
            spriteBatch.Draw(supamario, pos4, Color.White);            
           // spriteBatch.Draw(bowser, pos5, Color.White);
            spriteBatch.Draw(platform, pos6, Color.White);
            spriteBatch.End();

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
        protected bool CollideRM1()
        {
            Rectangle supamario = new Rectangle((int)pos4.X, (int)pos4.Y, 114, 150);
            Rectangle RM1 = new Rectangle((int)pos1.X, (int)pos3.Y, 100, 100);

            return supamario.Intersects(RM1);
        }
        protected bool CollideRM2()
        {
            Rectangle supamario = new Rectangle((int)pos4.X, (int)pos4.Y, 114, 150);
            Rectangle RM2 = new Rectangle((int)pos2.X, (int)pos2.Y, 100, 100);

            return supamario.Intersects(RM2);
        }
        protected bool CollideRM3()
        {
            Rectangle supamario = new Rectangle((int)pos4.X, (int)pos4.Y, 114, 150);
            Rectangle RM3 = new Rectangle((int)pos3.X, (int)pos3.Y, 100, 100);

            return supamario.Intersects(RM3);
        }
        protected bool Collidebowser()
        {
            Rectangle supamario = new Rectangle((int)pos4.X, (int)pos4.Y, 114, 150);
            Rectangle bowser = new Rectangle((int)pos5.X, (int)pos5.Y, 135, 202);

            return supamario.Intersects(bowser); 
        }
        protected bool Collideplatform()
        {
            Rectangle supamario = new Rectangle((int)pos4.X, (int)pos4.Y, 60, 75);
            Rectangle platform = new Rectangle((int)pos6.X, (int)pos6.Y, 180, 25);

            return supamario.Intersects(platform);
        }
    }
}



Mod Edit:
Added in code tags. When posting code please use code tags like below.
:code:

Is This A Good Question/Topic? 0

Replies To: platform collision

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 930
  • View blog
  • Posts: 6,316
  • Joined: 18-October 08

Re: platform collision

Posted 06 July 2010 - 03:38 AM

Moving to the XNA forum.
Was This Post Helpful? 0
  • +
  • -

#3 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,276
  • Joined: 01-June 09

Re: platform collision

Posted 06 July 2010 - 06:02 AM

Well what happens now when the player reaches the end of the platform? Does he just like keep walking in a straight line or something?

If so, do you have some kind of downward force such as gravity?

This post has been edited by eZACKe: 06 July 2010 - 06:02 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1