XNA Creators Club Online
Page 1 of 1 (4 items)
Sort Posts: Previous Next

Diangle Collision with ViewPort

Last post 01/05/2009 15:42 by Jay24w. 3 replies.
  • 01/05/2009 6:23

    Diangle Collision with ViewPort

    Hello, this is my first post here on XNA, and I'm super new to XNA and C#, been programming in VB.NET for a bit now, no games, just applications. But I decided to dive in to Game Development with C#. So here's my problem:

    I have collision detection for my ViewPort (Window) when ever I go Left, Right, Top, Down with my Player, but say I go Top Left, my collision doesn't accept it when the player for example hits the TOP WALL but not the LEFT wall, It will how ever accept the collision when it Hits both of them at the same time. Here's my exact code that I'm using for it.

    protected override void Update(GameTime gameTime) 
            { 
                keyState = Keyboard.GetState(); 
                if (keyState.IsKeyDown(Keys.Escape)) 
                { 
                    this.Exit(); 
                } 
                if (keyState.IsKeyDown(Keys.D)) { player.velocity = new Vector2(player.speed, 0); } 
                if (keyState.IsKeyDown(Keys.A)) { player.velocity = new Vector2(-player.speed, 0); } 
                if (keyState.IsKeyDown(Keys.W)) { player.velocity = new Vector2(0, -player.speed); } 
                if (keyState.IsKeyDown(Keys.S)) { player.velocity = new Vector2(0, player.speed); } 
     
                if (keyState.IsKeyDown(Keys.D) && keyState.IsKeyDown(Keys.W)) 
                { 
                    if (right == false || top == false
                    { 
                        player.velocity = new Vector2(player.speed, -player.speed); 
                    } 
                    else { right = true; top = truereturn; } 
     
                } 
                if (keyState.IsKeyDown(Keys.D) && keyState.IsKeyDown(Keys.S)) 
                {  
                    player.velocity = new Vector2(player.speed, player.speed);  
                } 
                if (keyState.IsKeyDown(Keys.A) && keyState.IsKeyDown(Keys.W)) 
                {  
                    player.velocity = new Vector2(-player.speed, -player.speed);  
                } 
                if (keyState.IsKeyDown(Keys.A) && keyState.IsKeyDown(Keys.S)) 
                {  
                    player.velocity = new Vector2(-player.speed, player.speed); 
                } 
                if (right == false && keyState.IsKeyDown(Keys.D) || 
                    left == false && keyState.IsKeyDown(Keys.A) || 
                    top == false && keyState.IsKeyDown(Keys.W) || 
                    bottom == false && keyState.IsKeyDown(Keys.S)) 
                { 
                    MovePlayer(); 
                } 
                base.Update(gameTime);  
            } 
    //COLLISION
    private void MovePlayer() 
            { 
                keyState = Keyboard.GetState(); 
                if (player.alive) 
                { 
                    Rectangle playerRect = new Rectangle((int)player.position.X, (int)player.position.Y, player.sprite.Width, player.sprite.Height); 
                    if (player.position.X <= (viewportRect.Left - 10)) 
                    { left = true; } 
                    else { left = false; } 
                    if ((player.position.X + player.sprite.Width) >= (viewportRect.Right)) 
                    { right = true; } 
                    else { right = false; } 
                    if (player.position.Y <= (viewportRect.Top)) 
                    { top = true; } 
                    else { top = false; } 
                    if ((player.position.Y + player.sprite.Height) >= (viewportRect.Bottom)) 
                    { bottom = true; } 
                    else { bottom = false; } 
     
                    player.position += player.velocity; 
                } 
                else 
                { 
                    player.alive = true
                } 
            } 

  • 01/05/2009 14:55 In reply to

    Re: Diangle Collision with ViewPort

    Constantly Update your MovePlayer even if the key isn't down.

      if (keyState.IsKeyDown(Keys.D) && keyState.IsKeyDown(Keys.S))  
                {   
                    player.velocity = new Vector2(player.speed, player.speed);   
                }  
                if (keyState.IsKeyDown(Keys.A) && keyState.IsKeyDown(Keys.W))  
                {   
                    player.velocity = new Vector2(-player.speed, -player.speed);   
                }  
                if (keyState.IsKeyDown(Keys.A) && keyState.IsKeyDown(Keys.S))  
                {   
                    player.velocity = new Vector2(-player.speed, player.speed);  
                }  
              
    //Constantly Updated.
                MovePlayer();  

    If the payers position goes outside the viewport area, just assign the position to the viewport area and reset your velocity to zero.

     if (player.alive)  
                {  
                    Rectangle playerRect = new Rectangle((int)player.position.X, (int)player.position.Y, player.sprite.Width, player.sprite.Height);  
                     
                    if (player.position.X < (viewportRect.Left - 10))  
                    {  
                        player.position.X = viewportRect.Left - 10; 
                        player.velocity = Vector2.Zero; 
                    }  
                 
                    if ((player.position.X + player.sprite.Width) > (viewportRect.Right))  
                    {  
                        player.position.X = viewportRect.Right + player.sprite.Width; 
                        player.velocity = Vector2.Zero;                     
                    }  
     
                    if (player.position.Y < (viewportRect.Top))  
                    {  
                        player.position.Y = viewportRect.Top; 
                        player.velocity = Vector2.Zero;                     
                    }  
     
                    if ((player.position.Y + player.sprite.Height) > (viewportRect.Bottom))  
                    {  
                        player.position.Y =  viewportRect.Bottom + player.sprite.Height; 
                        player.velocity = Vector2.Zero;                     
                    }  
      
                    player.position += player.velocity;  
                } 

    That basically stops your players position going of screen is its outside your viewing area. I think you were over complicating it with all the flags.

    Hope this helps!

    If you need any more help just hola haha.

    Jay24w
    Winner Of x48 Game Camp.
    1ST PLACE - SEAVOLUTION
    PERLMANIA - In Play Test
    TRAILER
  • 01/05/2009 15:33 In reply to

    Re: Diangle Collision with ViewPort

    Hey. I tried this, but when I get to one of the sides, it's just getting stuck their because I'm at the total Left or Top or what ever, and if I do

    player.position.Y = viewportRect.Top + 1;

    I'm getting this constant fast up and down movement from the sprite, is their a way to stop this?
  • 01/05/2009 15:42 In reply to

    Re: Diangle Collision with ViewPort

    Try removing any reference to the sprite width or hiegth to start with. And just set the position to the viewport edge.

     if (player.alive)   
                {   
                    Rectangle playerRect = new Rectangle((int)player.position.X, (int)player.position.Y, player.sprite.Width, player.sprite.Height);   
                      
                    if (player.position.X < viewportRect.Left)   
                    {   
                        player.position.X = viewportRect.Left;  
                        player.velocity = Vector2.Zero;  
                    }   
                  
                    if (player.position.X > viewportRect.Right)   
                    {   
                        player.position.X = viewportRect.Right;  
                        player.velocity = Vector2.Zero;                      
                    }   
      
                    if (player.position.Y < viewportRect.Top)   
                    {   
                        player.position.Y = viewportRect.Top;  
                        player.velocity = Vector2.Zero;                      
                    }   
      
                    if (player.position.Y > viewportRect.Bottom)   
                    {   
                        player.position.Y =  viewportRect.Bottom;  
                        player.velocity = Vector2.Zero;                      
                    }   
       
                    player.position += player.velocity;   
     

    I think the fast movement is your position is being reset constantly because the sprite width part is still overlapping the screen.

    Get it working with out checking the width / height.  Hopefully the code above will do that.

    If not you could post a link to your code and i'll be more than happy to take a quick look.

    Jay24w
    Winner Of x48 Game Camp.
    1ST PLACE - SEAVOLUTION
    PERLMANIA - In Play Test
    TRAILER
Page 1 of 1 (4 items) Previous Next