Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 17 Nov 2007
Offline Last Active Oct 02 2013 05:17 PM

Topics I've Started

8-Direction 2D Animations Flicker

22 September 2013 - 11:06 AM

Hi guys,


I'm working on a 2.5D game (think top down dungeon crawler/side view like Binding of Isaac) where characters can walk in all eight directions. The player animations work fine, but the AI animations have been flickering quite a bit between movement states. The AI moves and selects an animation based on its direction:

            //Select which animation we should currently be in:
            if (velocity != Vector2.Zero)
                if (direction.X < 0 && direction.Y < 0 && currentAnimation.name != "Walk Up Left")
                else if (direction.X > 0 && direction.Y < 0 && currentAnimation.name != "Walk Up Right")
                else if (direction.X < 0 && direction.Y > 0 && currentAnimation.name != "Walk Down Left")
                else if (direction.X > 0 && direction.Y > 0 && currentAnimation.name != "Walk Down Right")
                else if (direction.X < 0 && direction.Y == 0 && currentAnimation.name != "Walk Left")
                else if (direction.X > 0 && direction.Y == 0 && currentAnimation.name != "Walk Right")
                else if (direction.Y > 0 && direction.X == 0 && currentAnimation.name != "Walk Down")
                else if (direction.Y < 0 && direction.X == 0 && currentAnimation.name != "Walk Up")
            else if (velocity == Vector2.Zero)
                if (currentAnimation.name != "Idle")

The problem is that there are times when the AI has to move diagonally, and after doing so he will move straight down for a few frames to correct himself so he's fully on the grid... it's only for a few frames so visually it looks very glitchy going between the two directions.


The direction variable never has floating point values... The AI movement code is like this:


1. Calculate the next tile we need to move to.

2. Make our direction variable look at this tile.

3. Round so the direction is always a whole number, to avoid jerky movement from floating point numbers.


The code I use to round is this:

        private void TrimDirection()
            //Snap our direction to one of the eight cardinal directions:
            direction.X = (float)Math.Round(MathHelper.PiOver4 * (float)Math.Round(direction.X / MathHelper.PiOver4));
            direction.Y = (float)Math.Round(MathHelper.PiOver4 * (float)Math.Round(direction.Y / MathHelper.PiOver4));

Movement code is like this:

                if (direction.X > 0)
                else if (direction.X < 0)

                if (direction.Y > 0)
                else if (direction.Y < 0)

I've been trying to figure out how to approach this issue and I've hit a wall.... do I need to implement some type of animation hysteresis to avoid animations from changing so fast? Or is this issue deeper down inside how I make the AI move?


Thanks in advance!

Dynamic 2D Shadows Optimization

13 February 2013 - 06:56 PM

Hi guys, I've implemented this shadow sample into my engine: http://www.catalinzima.com/2013/01/dynamic-2d-shadows-for-windows-phone-7/ Everything was working fine until I ran my game on the Xbox 360.... I get 30 FPS when my game runs at 1920 x 1080, but if I run it at 1280 x 720 or below it runs at 60 FPS. If you run the stand alone sample the same thing happens.


I don't know exactly why the resolution makes a difference, but I did some further profiling and found that this call gets called for every hull:


game.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, shadowVertices, 0, shadowVertexCount * 2 - 2);


When I draw just one hull my FPS goes back up to 60, so for the last week or so I've been trying to add all of the verts and indices to a list and then make this call one time after iterating through the hulls. The problem is that after doing this the shadows are drawing really whacky... I'm not sure where I'm going wrong, this should be a simple fix but it seems I'm overlooking something small that is screwing me up.


Any help would be appreciated! I attached the modified sample to this post.


My changes are in ConvexHull's DrawShadows:

            int currentIndex = startingIndex;
            int svCount = 0;
            while (svCount != shadowVertexCount * 2)
                shadowHullIndices.Add(currentIndex + shadowHullVertices.Count); //Added this
                Vector3 vertexPos = vertices[currentIndex].Position + new Vector3(position, 0);

                //one vertex on the hull
                shadowVertices[svCount] = new VertexPositionColor();
                shadowVertices[svCount].Color = Color.Transparent;
                shadowVertices[svCount].Position = vertexPos;

                //one extruded by the light direction
                shadowVertices[svCount + 1] = new VertexPositionColor();
                shadowVertices[svCount + 1].Color = Color.Transparent;
                Vector3 L2P = vertexPos - new Vector3(lightSource.Position, 0);
                shadowVertices[svCount + 1].Position = new Vector3(lightSource.Position, 0) + L2P * 9000;

                svCount += 2;
                currentIndex = (currentIndex + 1) % primitiveCount;

            drawingEffect.World = Matrix.Identity;

            for (int i = 0; i < shadowVertexCount * 2 - 2; i++) //Added this
//Original call:
//game.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, shadowVertices, 0, shadowVertexCount * 2 - 2); 

and in Game1's DrawLightmap:

                foreach (ConvexHull ch in objects)
                    //draw shadow
                    ch.DrawShadows(light, shadowHullVertices, shadowHullIndices); //Added. For now, pass the lists in and add the appropriate verts and indices.

                //Added this:
                GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, shadowHullVertices.ToArray(), 0, shadowHullVertices.Count, shadowHullIndices.ToArray(), 0, shadowHullIndices.Count / 3);



Different Timing

04 October 2011 - 06:10 PM

Hey guys, my game is using a fixed time step.... so I was kind of under the impression that my game would run at the same speed no matter what platform is runs on. However, I have a cutscene that is based off of timers; when I watch the cutscene on my 360 it ends exactly when it's supposed to, but on my PC it's a second or two off. Is this a difference between Xbox and PC, or could it be that it would have different ending results on different PCs? I'm coding in XNA, and like I said, I'm using fixed time step so I'm kind of confused on why this is happening.


Getting Multiple Effects with Render Targets

26 April 2011 - 02:45 PM

Hey again everyone, I've come to a dead end in my recent coding attempts and thought I'd see if anyone here would be able to assist me. I am still working on a 3D space shooter in XNA, but am wanting to add some shader effects. I have taken the Bloom Sample from the XNA site and implemented it into my game.... with some modifications I have three different effect combinations that I'm wanting to apply. I want a different effect on my bullets, HUD and finally over the rest of the scene.

I have everything working great except getting all of these things to combine.... At first, I was using multiple render targets. One for each effect I wanted, after talking with a member at the XNA Forums, I realized this isn't a good way to do this because I will be losing depth information by using three textures like this. (Bullets would be showing on top of ships etc.). What I'd like to do is some how mark what needs a different effect and what doesn't so I can do all of this in one texture/render target. I have all the right code implemented, I just need some way to mark the bullets etc. How would I go about doing something like this?

Thanks guys!

Targeting HUD/Crosshair in 3D Space Shooter

30 January 2011 - 11:09 AM

Hey everyone, I'm working on another 3D space shooter in XNA and have been stuck on this for about a week now.

Right now I have a ship that can fly around and fire bullets, I also have a chase camera following it. I am trying to implement a targeting HUD similar to what you'd see in Star Wars: Rogue Squadron but I can't get the HUD to accurately line up with where the bullets are fireing. The targeting HUD is lined up maybe half of the time, but when the ship is in certain angles, or the player accelerates, it's totally off. I think my camera might have something to do with it, since it will pull back when the player accelerates and what not.

I'm thinking that maybe I can somehow calculate the bullet's projected path to update the HUD position. Is this the recommended way of approaching this? I've tried googling for samples to see how most people approach this kind of problem but haven't come up with much. I guess what I'm really needing help with is the logic/math to get the correct position.

Let me know if there is some certain code that you'd like to see but I'm not sure what would be helpful for me to post. I'm thinking the chase camera is a big problem with why it's not looking right, because it will pull back and whatnot at certain times. I've tried calculating HUD positions from both my ship and camera with no luck.

Thanks so much for the help, hopefully talking this out with you guys will help clear things up.