Old 04-13-2009, 03:59 PM   #1 (permalink)
Zuner
 
Join Date: Jun 2008
Posts: 96
MercifulBoss is on a distinguished road
Default Please Help with Collisions!

I am still struggling to accomplish collisions. If some is willing to help please post. My current code for collisions is here.

Code:
 public void DrawScreen(SpriteBatch spriteBatch, ContentManager content, Sprite sprite, GameTime gameTime)
        {
            int y = 0;
            int x = 0;
            int index = 0;
            for (int row = 0; row < Rows; row++)
            {
                y = row * Grid.CellHeight;
                Y = y;
                for (int col = 0; col < Columns; col++)
                {
                    x = col * Grid.CellWidth;
                    location = new Vector2(x, y);
                    X = x;
                    index = row * this.Columns + col;
                    Index = index;
               
                    if (this[index] == 1)
                    {
                        sprite.AllowMove = true;
                        sprite.Move = "false";
                        rec = new Rectangle(x, y, CellWidth, CellHeight);
                        spriteBatch.Draw(grass, rec, Color.White);
                       
                        //ground = new Vector2(x, y);
                        //GroundRec = new Rectangle(x, y, CellWidth, CellHeight);
                    }
                    if (this[index] == 2)
                    {
                        sprite.AllowMove = false;
                        sprite.AllowMoveLeft = false;
                        sprite.AllowMoveRight = false;
                        sprite.AllowMoveUp = false;
                        //rec = new Rectangle(x, y, CellWidth, CellHeight);
                        //spriteBatch.Draw(sky, location, Color.White);
                        sprite.CanMove = "false";
                        //sprite.CanMove = "";
                    }
                    if (this[index] == 3)
                    {
                        rec = new Rectangle(x, y, CellWidth, CellHeight);
                        spriteBatch.Draw(grass, rec, Color.White);
                        sprite.AllowMoveRight = false;
                        sprite.AllowMoveLeft = false;
                        sprite.res = false;
                    }
                    //x = X;
                }
                //y = GroundDistance;
                
            }
            
        }
Please help!



MercifulBoss is offline   Reply With Quote

Advertisement [Remove Advertisement]
Old 04-13-2009, 04:06 PM   #2 (permalink)
lost in paradise with rae
Support Team
Moderator
Expert Zuner
 
Red Sky's Avatar
 
Join Date: Nov 2007
Location: Pennsylvania
Posts: 2,662
Red Sky is just really niceRed Sky is just really niceRed Sky is just really nice
Send a message via MSN to Red Sky
Default

First of all, you shouldn't be updating things in your draw function. That's bad programming practice.

Second, I see absolutely no collision code in there.

You're defining rectangles ('rec'). You have to see if they intersect each other. If they intersect each other, than you disallow whatever movement you don't want.





Red Sky is offline   Reply With Quote
Old 04-13-2009, 06:21 PM   #3 (permalink)
Zuner
 
Join Date: Jun 2008
Posts: 96
MercifulBoss is on a distinguished road
Default

Quote:
Originally Posted by Red Sky View Post
First of all, you shouldn't be updating things in your draw function. That's bad programming practice.

Second, I see absolutely no collision code in there.

You're defining rectangles ('rec'). You have to see if they intersect each other. If they intersect each other, than you disallow whatever movement you don't want.
Sorry

I have tried the intersection code, however my index for obstacles in another position.

Code:
  public void CalculateCollisions(GameTime gameTime, Grid grid, Sprite sprite)
        {
            //sprite.MoveChar(gameTime, grid);
            //sprite.AllowMove = false;
            //int idx = GetIndex(pt, grid, sprite);
            int x = 0;
            int y = 0;
            int index = 0;
            int idx = GetIndex(grid, sprite);
            sprite.Y = grid.rec.Top;
            for (int row = 0; row < grid.Rows; row++)
            {
                y = row * Grid.CellHeight;
                for(int col = 0; col < grid.Columns; col++)
                {
                    x = col * Grid.CellWidth;
                    index = row * grid.Columns + col;
                    
                    if (grid[index] == 1)
                    {
                    }
                     if (grid[index] == 2)
                     {
                             if ((grid.rec.Intersects(sprite.rec) && sprite.rec.Left >= grid.rec.Right))
                             {
                                 sprite.AllowMoveRight = false;
                                 //sprite.X = grid.rec.Right;
                                 //sprite.X = grid.rec.Right;
                                 sprite.X = 0;
                                 sprite.Y = 0;
                             }
                        
                             //if (sprite.rec.X >= x)
                             //{
                             //    sprite.rec.X = grid.rec.X - sprite.Width - Grid.CellWidth;
                             //    sprite.AllowMoveRight = false;
                             //}
                             if (sprite.rec.Bottom >= grid.rec.Top - sprite.rec.Height - Grid.CellHeight)
                             {
                                 sprite.AllowMove = false;
                                 sprite.Y = grid.rec.Top;
                                 sprite.rec.Y = grid.rec.Top;
                                 if (sprite.rec.Bottom < grid.rec.Top)
                                 {
                                     sprite.AllowMove = true;
                                 }
                                 //grid.Y = sprite.rec.Bottom;
                             }
                             //sprite.MoveChar(gameTime, grid);
                             //sprite.Position.Y = (int)grid.location.Y - Grid.CellHeight * 2 - sprite.Height;
                         

                     }
                }
            }
        }
Here is the collisions I tried to do, they do not function.



MercifulBoss is offline   Reply With Quote
Old 06-12-2009, 12:07 PM   #4 (permalink)
Squirt
 
Join Date: May 2009
Posts: 19
Extron is on a distinguished road
Default

I have not had a real chance to look at your code thoroughly, but from the looks of it, you are trying to test collisions from several directions in the same method. Now, I am very new to collision programming, as well, but i would separate this method into several smaller methods that check specific directions of collision, like checking to see if it can move to the right, or if it can move down. That way, if you have a function that just needed to know if it can move right, then you can call the method to check if it can move right. Also, when programming for collision, there are two main things that need to be programmed, which is to check if anything is colliding, and to correct the collision. It would be best if you separated this method into two methods, one that calculates the collisions, and one that corrects the sprite's movements. I think then you will be able to see and visualize the code a lot easier, and then you can change one method without having to rewrite everything. By the way, what game is this for?




Extron is offline   Reply With Quote
Old 06-16-2009, 11:14 AM   #5 (permalink)
Zuner
 
Join Date: Jun 2008
Posts: 96
MercifulBoss is on a distinguished road
Default

Quote:
Originally Posted by Extron View Post
I have not had a real chance to look at your code thoroughly, but from the looks of it, you are trying to test collisions from several directions in the same method. Now, I am very new to collision programming, as well, but i would separate this method into several smaller methods that check specific directions of collision, like checking to see if it can move to the right, or if it can move down. That way, if you have a function that just needed to know if it can move right, then you can call the method to check if it can move right. Also, when programming for collision, there are two main things that need to be programmed, which is to check if anything is colliding, and to correct the collision. It would be best if you separated this method into two methods, one that calculates the collisions, and one that corrects the sprite's movements. I think then you will be able to see and visualize the code a lot easier, and then you can change one method without having to rewrite everything. By the way, what game is this for?
Thanks for your tips, or help. It is on a game I am working on albeit incredibly slowly, a platformer. Now that summer is starting, perhaps I will make more progress.



MercifulBoss is offline   Reply With Quote
Old 06-28-2009, 03:33 PM   #6 (permalink)
Squirt
 
Join Date: May 2009
Posts: 19
Extron is on a distinguished road
Default

I have yet to make any platformer games, but it sounds like it would be fun. Mostly I deal with arcade games and top down shooter games, so there is limited collision in dealing with gravity and advanced physics, but I am glad I was able to help. I learned all of that just from experience, and if you keep working at it, you will continue to grow as a good programmer.




Extron is offline   Reply With Quote
Reply

Bookmarks

Thread Tools