• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Mekuri

Collision Jitters on some occasions

6 posts in this topic

Hey there.

I recently began on a new project, inspired by my Ludum Dare entry, and I decided to some more solid collision detection than in my other project.

 

I've finally reached a result that I am fairly happy with, but I experience jitters, that I don't fully understand. When I move right and collide with something, my sprite jitters.. But when going left, it works exactly as intended. Also there are a few corner cases where there's some small jitter.

 

I do the collision detection and seperation like this:

 

1. Do horizontal movement.

2. Check and resolve horizontal collisions.

3. Do vertical movement.

4. Check and resolve vertical movement.

 

For both the horizontal and vertical step, I first check for rectangular collision, and then pixel based within the rectangle.

Since I divide both movement and collision handling up in horizontal and vertical, I have a hard time understanding why it jitters.

 

The code I am using is the following:

 

Player class handles movement and velocity

private void Movement()
        {
            PreviousPosition = position;
            //TODO: Add custom controls, by saving keys in a list or something, and then compare pressed keys to that list, and act accordingly.
            if (InputController.CurrentKeyboardState.IsKeyDown(Keys.A) || InputController.CurrentKeyboardState.IsKeyDown(Keys.Left))
                movement = -1.0f;
            else if (InputController.CurrentKeyboardState.IsKeyDown(Keys.D) || InputController.CurrentKeyboardState.IsKeyDown(Keys.Right))
                movement = 1.0f;
            else
                movement = 0f;

            //For testing jump strength - Final version will have a fixed strength!
            if(InputController.IsKeyPressed(Keys.OemPlus))
                BASE_JUMP_POWER++;
            else if (InputController.IsKeyPressed(Keys.OemMinus))
                BASE_JUMP_POWER--;
            
            //Velocity
            velocity.X += movement * BASE_SPEED;
            velocity.X *= AIR_DRAG;

            velocity.Y += GRAVITY;
            velocity.X = MathHelper.Clamp((float)Math.Round(velocity.X, 4, MidpointRounding.ToEven), -MAX_MOVE_SPEED, MAX_MOVE_SPEED);

            if (isOnGround)
            {
                velocity.Y = MathHelper.Clamp(-velocity.Y * .8f, -MAX_JUMP_SPEED, -BASE_JUMP_POWER);
                isOnGround = false;
            }
            velocity.Y = MathHelper.Clamp((float)Math.Round(velocity.Y, 4, MidpointRounding.ToEven), -MAX_JUMP_SPEED, MAX_FALL_SPEED);
        }

        public override void ApplyHorizontalMovement()
        {
            position.X += velocity.X;
            position.X = (float)Math.Round(position.X);
        }

        public override void ApplyVerticalMovement()
        {
            position.Y += velocity.Y;
            position.Y = (float)Math.Round(position.Y);        
        }

Movement() is called every update cycle.

Notice the apply movement methods, those are called by the "playfield" class, during collision handling, as you can see in the following code:

blic void WorldCollision(Sprite sprite, bool verticalMovement)
        {
            if (verticalMovement)
                sprite.ApplyVerticalMovement();
            else
                sprite.ApplyHorizontalMovement();
            for (int x = sprite.Bounds.X / TILE_SIZE - 1; x < sprite.Bounds.X / TILE_SIZE + 2; x++)
            {
                for (int y = sprite.Bounds.Y / TILE_SIZE - 1; y < sprite.Bounds.Y / TILE_SIZE + 2; y++)
                {
                    Rectangle tmp = new Rectangle(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);                    
                    if (sprite.Bounds.Intersects(tmp))
                   {
                       if (StaticTileInfo.GetCollisionType(_world[x, y].Type) != Collision.Passable)
                       {
                           if (PixelbasedCollision(sprite.Bounds, sprite.GetTextureData, tmp, GetTileColorData(x, y)))
                           {
                               if (StaticTileInfo.GetCollisionType(_world[x, y].Type) == Collision.Spike)
                               {
                                   sprite.IsDying = true;
                                   return;
                               }
                               else
                               {
                                   if (verticalMovement)
                                   {
                                       float intersectionDepth = RectangleExtensions.GetVerticalIntersectionDepth(sprite.Bounds, tmp);
                                       sprite.Position += new Vector2(0, intersectionDepth);
                                       if (StaticTileInfo.GetCollisionType(_world[sprite.Bounds.Center.X / TILE_SIZE, (sprite.Bounds.Bottom + 1) / TILE_SIZE].Type) == Collision.Impassable ||
                                           StaticTileInfo.GetCollisionType(_world[sprite.Bounds.Left / TILE_SIZE, (sprite.Bounds.Bottom + 1) / TILE_SIZE].Type) == Collision.Impassable ||
                                           StaticTileInfo.GetCollisionType(_world[sprite.Bounds.Right / TILE_SIZE, (sprite.Bounds.Bottom + 1) / TILE_SIZE].Type) == Collision.Impassable)
                                           sprite.IsOnGround = true;
                                       else
                                           sprite.Velocity = new Vector2(sprite.Velocity.X, 0);
                                   }
                                   else
                                   {
                                       float intersectionDepth = RectangleExtensions.GetHorizontalIntersectionDepth(sprite.Bounds, tmp); 
                                       sprite.Position += new Vector2(intersectionDepth, 0);
                                       sprite.Velocity = new Vector2(0, sprite.Velocity.Y);
                                   }
                                   break;
                               }
                           }
                       }
                       else
                           continue;
                   }
                }
            }
            if(!verticalMovement)
                WorldCollision(sprite, true);
        }

private bool PixelbasedCollision(Rectangle rect1, Color[] data1, Rectangle rect2, Color[] data2)
        {
            int top = Math.Max(rect1.Top, rect2.Top);
            int bottom = Math.Min(rect1.Bottom, rect2.Bottom);
            int left = Math.Max(rect1.Left, rect2.Left);
            int right = Math.Min(rect1.Right, rect2.Right);

            for (int y = top; y < bottom; y++)
            {
                for (int x = left; x < right; x++)
                {
                    Color color1 = data1[(x - rect1.Left) + (y - rect1.Top) * rect1.Width];
                    Color color2 = data2[(x - rect2.Left) + (y - rect2.Top) * rect2.Width];

                    if (color1.A != 0 && color2.A != 0)
                        return true;
                }
            }
            
            return false;
        }

        private Color[] GetTileColorData(int x, int y)
        {
            Color[] tmp = new Color[TILE_SIZE * TILE_SIZE];
            _tileSheet.GetData<Color>(0 * TILE_SIZE, new Rectangle(_world[x,y].Variant * TILE_SIZE, (int)(_world[x,y].Type - 1) * TILE_SIZE, TILE_SIZE, TILE_SIZE), tmp, 0, tmp.Length);
            return tmp;
        }

 

I've also made a small video showing the issues I am having:

http://www.youtube.com/watch?v=FC8eYy1LRGQ

 

I hope I provided all the information necessary. I know there's a lot of code, but I was hoping someone with experience might immediately know the error just from seeing the movie :-)

 

Thanks for reading!

Edited by Mekuri
0

Share this post


Link to post
Share on other sites

This looks suspect:

 

            if (velocity.X > -.1f && velocity.X < .1f) velocity.X = 0;

 

I removed the above- It was some code I forgot to remove, from when I played around with the physics, earlier. It did not change anything though.

Thanks for the input.

0

Share this post


Link to post
Share on other sites

                                    if (verticalMovement) { float intersectionDepth = RectangleExtensions.GetVerticalIntersectionDepth(sprite.Bounds, tmp); sprite.Position += new Vector2(0, intersectionDepth); if (StaticTileInfo.GetCollisionType(_world[sprite.Bounds.Center.X / TILE_SIZE, (sprite.Bounds.Bottom + 1) / TILE_SIZE].Type) == Collision.Impassable || StaticTileInfo.GetCollisionType(_world[sprite.Bounds.Left / TILE_SIZE, (sprite.Bounds.Bottom + 1) / TILE_SIZE].Type) == Collision.Impassable || StaticTileInfo.GetCollisionType(_world[sprite.Bounds.Right / TILE_SIZE, (sprite.Bounds.Bottom + 1) / TILE_SIZE].Type) == Collision.Impassable) sprite.IsOnGround = true; else sprite.Velocity = new Vector2(sprite.Velocity.X, 0); } else { float intersectionDepth = RectangleExtensions.GetHorizontalIntersectionDepth(sprite.Bounds, tmp); sprite.Position += new Vector2(intersectionDepth, 0); sprite.Velocity = new Vector2(0, sprite.Velocity.Y); } 

 

This if (verticalMovement) part also looks a bit suspect, there is a condition in which the position gets updated but not the velocity? That can't happen for horizontal movement though. The position and velocity always get updated symmetrically.

1

Share this post


Link to post
Share on other sites

This is more suspect:

 

velocity.X = MathHelper.Clamp((float)Math.Round(velocity.X, 4, MidpointRounding.ToEven), -MAX_MOVE_SPEED, MAX_MOVE_SPEED);

 

It could round velocity differently for left and right. Log velocities and positions while jittering or not jittering to find out if it is the case.

1

Share this post


Link to post
Share on other sites

                                    if (verticalMovement) { float intersectionDepth = RectangleExtensions.GetVerticalIntersectionDepth(sprite.Bounds, tmp); sprite.Position += new Vector2(0, intersectionDepth); if (StaticTileInfo.GetCollisionType(_world[sprite.Bounds.Center.X / TILE_SIZE, (sprite.Bounds.Bottom + 1) / TILE_SIZE].Type) == Collision.Impassable || StaticTileInfo.GetCollisionType(_world[sprite.Bounds.Left / TILE_SIZE, (sprite.Bounds.Bottom + 1) / TILE_SIZE].Type) == Collision.Impassable || StaticTileInfo.GetCollisionType(_world[sprite.Bounds.Right / TILE_SIZE, (sprite.Bounds.Bottom + 1) / TILE_SIZE].Type) == Collision.Impassable) sprite.IsOnGround = true; else sprite.Velocity = new Vector2(sprite.Velocity.X, 0); } else { float intersectionDepth = RectangleExtensions.GetHorizontalIntersectionDepth(sprite.Bounds, tmp); sprite.Position += new Vector2(intersectionDepth, 0); sprite.Velocity = new Vector2(0, sprite.Velocity.Y); } 

 

This if (verticalMovement) part also looks a bit suspect, there is a condition in which the position gets updated but not the velocity? That can't happen for horizontal movement though. The position and velocity always get updated symmetrically.

 

Nice catch- I need some restructuring there. (Structure is really my weakness :P) - Thanks a lot.

This is more suspect:

 

velocity.X = MathHelper.Clamp((float)Math.Round(velocity.X, 4, MidpointRounding.ToEven), -MAX_MOVE_SPEED, MAX_MOVE_SPEED);

 

It could round velocity differently for left and right. Log velocities and positions while jittering or not jittering to find out if it is the case.

 

This might very well be it. I won't be able to try this until tomorrow, but I can see why this could cause the jittering.

 

Thanks a lot for the input :-)

0

Share this post


Link to post
Share on other sites

Hey again.

 

I just wanted to check back, and say that I managed to solve the problem. The problem was the two for loops in the WorldCollision method. So basically I had to change the maximum range to check within. See below:

 

Before

for (int x = sprite.Bounds.X / TILE_SIZE - 1; x < sprite.Bounds.X / TILE_SIZE + 2; x++)
            {
                for (int y = sprite.Bounds.Y / TILE_SIZE - 1; y < sprite.Bounds.Y / TILE_SIZE + 2; y++)
                {

 After

for (int x = sprite.Bounds.X / TILE_SIZE - 1; x < (sprite.Bounds.X + sprite.Bounds.Width) / TILE_SIZE + 1; x++)
            {
                for (int y = sprite.Bounds.Y / TILE_SIZE - 1; y < (sprite.Bounds.Y + sprite.Bounds.Height) / TILE_SIZE + 1; y++)

I believe that the reason it was jittering is that the player sprite has a different size than the tiles. Each tile is 32X32, while the player sprite varies in size based on the current animation frame (For pixel perfect collision detection). The way I calculate the maximum iteration value (can you say that?) makes sure that it checks the tile exactly next to it, both to the right and downwards, whereas it only did so to the left and upwards before.

 

Anyways- Thanks for your input, just wanted to post the solution in case someone else could use it :-)

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0