Fixing collision (Recap of my day)

Honestly, my collision code is still a mess. It could still use some TLC. But hey: At least it works now. I apwnr the whole day working on collision. The initial problem was so dumb, I can’t believe i didn’t notice it sooner. The problem was, it would cycle through each tile until it found one that it collided with, push the player outside of the box, then break. And I understand why I did it that way. Because it worked, most of the time. But there were two cases where it didn’t work as shown below:

Note: video missing

One case is actually kind of difficult to notice. It’s in corner of the wall and the ground. If you notice, the player kind of jitters. I suggest comparing with the second video, if you don’t see it. Also, the video is blurry, so it’s hard to see anyway. And the second problem is more obvious, and much more of a problem. See, the way I was colliding with tiles worked when there was only one tile to collide with, but it didn’t work with multiple tiles, AKA corners. It would comepletely ignore the second collision because it would break out of the cycle. So, I simply removed the break statement.

But it wasn’t done after that. Oooh no. I understand why it wasn’t removed initially. Because when I fixed that, other things broke, too. I tried several things:

Note: video missing

Programming collision is actually fairly difficult. Well, it was for me. In fact, there’s still a bug in my code but I’m happy with the way it is now. If I want to have collision for the right side of the tile (the left side of the player), then I’m going to have to add to the collision. I’m gonna want to do that, because going backwards is going to be a part of some of the level design.

There are other changes I made since yesterday, too. Here is a brief list of them:

  • When you hold down the jump button, (how I found the bug in the code that I have yet to find)
  • I put smooth corners around the land, instead of the square, jagged corners
  • When you jump, the state successfully changes to to JUMPING, instead of RUNNING. This means that the animation frame stands still when you’re jumping and the animation runs when you’re on the ground. (Check it out in the videos above!)

For your convenience, I’ve included a copy of the player collision code below:

        for (Rectangle tile : tiles) {
            if (this.rect.overlaps(tile)) {
                hasTouchedTile = true;
                boolean hasUpper = false;
                boolean hasLower = false;
                boolean hasLeft = false;
                boolean hasRight = false;

                
                for( HiddenTile hiddenTile : hiddenTiles )
                {
                    if( hiddenTile.x == tile.x && hiddenTile.y == tile. y && hiddenTile.isUpper)
                        hasUpper = true;
                    if( hiddenTile.x == tile.x && hiddenTile.y == tile. y && !hiddenTile.isUpper)
                        hasLower = true;
                }
                for( HiddenTile hiddenTileX : hiddenTilesX )
                {
                    if( hiddenTileX.x == tile.x && hiddenTileX.y == tile. y && !hiddenTileX.isUpper)
                        hasLeft = true;
                    if( hiddenTileX.x == tile.x && hiddenTileX.y == tile. y && hiddenTileX.isUpper)
                        hasRight = true;
                }
                //of the tile
                float left= Math.abs(tile.x - (this.rect.x+this.rect.width));
                float top = Math.abs(tile.y+tile.height - this.rect.y);
                float right = Math.abs(tile.x+tile.width - this.rect.x);
                float bottom= Math.abs(tile.y - (this.rect.y+this.rect.height));
                //colliding with top
                
                if( tile.contains(this.rect.x+this.rect.width,this.rect.y)  && !hasUpper )
                {
                    this.state = State.RUNNING;
                    if( left > top )
                    {
                        this.y = (tile.y + tile.height);
                        if(this.yVelocity<=0)
                        {
                            this.isOnGround=true;
                            this.yVelocity = 0;
                        }
                    }
                    else
                    {
                        isBeingPushed = true;
                    }
                }
                else if( tile.contains(this.rect.x+this.rect.width,this.rect.y+this.rect.height) && !hasLeft && !hasLower )
                {
                    if(left > bottom)
                    {
                        this.y = (tile.y -1);
                        //hit upwards
                        if(this.yVelocity>0)
                            this.yVelocity = 0;
                    }
                    else
                        isBeingPushed = true;
                }
                else if( tile.contains(this.rect.x,this.rect.y+this.rect.height)  && !hasLower )//bottom
                {
                    this.y = (tile.y -1);
                    //hit upwards
                    if(this.yVelocity>0)
                        this.yVelocity = 0;
                }
                else if( tile.contains(this.rect.x,this.rect.y) && !hasUpper )//top
                {
                    this.y = (tile.y + tile.height);
                    if(this.yVelocity<=0)
                    {
                        this.isOnGround=true;
                        this.yVelocity = 0;
                    }
                }
                else
                {
                    isBeingPushed = true;
                }
                if(isBeingPushed && !hasLeft && !hasBeenPushed )
                {
                    this.x=tile.x-tempScroll-0.90f;
                    this.x-=scrollSpeed*time;
                    hasBeenPushed= true;
                }            
            }
        }
        if(!hasTouchedTile)
            this.isOnGround=false;

Note November 24 2024: This game was heavily edited and released as Kat’s Dream. The source code is here: https://github.com/xianbaum/katsdream.

Jump And Shoot Man

Hi, I’ve decided to use this blog as my general software development blog. So, not just for Punching Out Cthulhu (which is still in development, albeit slow development), I figure people who follow my personal blog probably don’t care (although I doubt many people really care about what goes on in my normal life, heh) and not even just limited to game development. Here is progress of another game I’ve been making. It’s not much right now, but please check it out.

A video game screenshot of a player jumping over a pit

I’ve named it Jump ‘N’ Shoot Man for now, and it’s a runner game with two buttons, “Jump” and “Shoot”. That’s obviously not what it’s going to be called (especially since the character is a woman). It was originally planned to be a much more terrible game and I was going to release it because I thought “Flappy Bird is popular, so why not this?” but it kind of grew on me.

In addition to this, I’m going to be participating in my first game jam, /agdg/’s MicroGame Jam. I’m very optimistic about this (and the future of my game/software development) so please check this blog out (I mean, if you want).

Note November 24 2024: This game (which I call Jump ‘N’ Shoot Man here) was heavily edited and released as Kat’s Dream. The source code is here: https://github.com/xianbaum/katsdream. The game I mentioned that I was going to make for the Game Jam turned into City Night.

XML Parser

Punching Out Cthulhu isn’t dead. Its code is being ported to C++ and SDL. Here’s a shot very early in development

Compare with older screenshots and bear with me that the current functionality of the game is about the same as these ancient screenshots as in, the enemies are stationary, and the player can move around:

Older screenshots

XML Parser

Today, I finished the majority of the code for the XML parser. Previously, the enemies and levels had to be hard-coded within Java, and making them could be tedious. I felt like that solution was a little too messy, since my files were seriously getting to be a couple thousand lines of code after programming a couple enemies. I still have to translate this code to XML and get some kinks out and debug (not exactly as easy as it sounds, however…), but this looks like a promising future for my game and should make development that much faster!

Here is an example of what it looked like before:

this.texture[8] = SVGBitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBuildableBitmapTextureAtlas, this, monster-head.svg, 256,256);
this.texture[9] = SVGBitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBuildableBitmapTextureAtlas, this, monster-body.svg, 256, 512);
this.texture[10] = SVGBitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBuildableBitmapTextureAtlas, this, monster-arms.svg, 256, 512);

(later in the code)

enemy[8] = new Actor();
enemy[8].limb[1].Y=-4;
enemy[8].limb[0] = new Limb();
enemy[8].limb[0].Sprite = new Sprite(300*(cameraWidth/854),200*(cameraHeight/480), texture[8]);
enemy[8].limb[0].width = 100*(cameraWidth/854);
enemy[8].limb[0].height = 100*(cameraWidth/854);
enemy[8].limb[0].Y=-25;
enemy[8].limb[1] = new Limb();
enemy[8].limb[1].Sprite = new Sprite(300*(cameraWidth/854),200*(cameraHeight/480), texture[10]);
enemy[8].limb[1].width = 450*(cameraWidth/854);
enemy[8].limb[1].height = 400*(cameraWidth/854);
enemy[8].limb[2] = new Limb();
enemy[8].limb[2].Sprite = new Sprite(300*(cameraWidth/854),200*(cameraHeight/480), texture[9]);
enemy[8].limb[2].width = 400*(cameraWidth/854);
enemy[8].limb[2].height = 400*(cameraWidth/854);
enemy[8].X = 100;
enemy[8].Y = 99;
enemy[8].Z = 0;
enemy[8].health = 1000;

Now, both segments of the code can be contained in one file. Here’s an example of what it may look like now, and in a separate file rather in the same file as hundreds of other enemies of different types.

<enemy limbcount = “2” name = “monster”>
    <health>1000</health>
    <position x=“100” y=“99” z=“0” />
    <limb id=“0” name = “monster-head”
    <img width=“100” height=“100”>monster-head.svg</img>
    <options y=“-25” />
    </limb>
    <limb id=“1” name = “monster-body”>
    <img width=“400” height=“400”>monster-body.svg</img>
    </limb>
    <limb id=“2” name = “monster-arms”>
    <img width=“450” height=“400”>monster-arms.svg</img>
    <options y=“-4” />
    </limb>
</enemy>

As you can tell, it’s much cleaner. In future, I want to make a small program that generates this XML code for me to make development even easier.

Note added November 24, 2024: This is in reference to Punching Out Cthulhu.

Screenshots

A cartoon screenshot of two fists facing a large monster

A cartoon screenshot of two fists facing a floating skeleton with two fists

A cartoon screenshot of two fists facing a small flying monster

These in-game screenshots are nearly 11 months old as of June 2nd. Note that these enemies have no AI in this point in development.

Note added November 24, 2024: These are screenshots for Punching Out Cthulhu.