Side Scrolling Action Game

Hello,

I am trying to make a 2D side scrolling action game similar to “Dan The Man”. However, I am stuck as to how to manage the level physics.

Initially I though to create the ground terrain as an image for each level and use that. However, I soon realized that this would be hard because I won’t be able to get the “actual” ground height at certain columns. Instead, it would return me the entire image’s height.

Next, I tried to store the entire map in a 2D array. While this worked for me and allowed for easy collision checking, the animation was not smooth as the player moved in discrete tile steps.
If I make the player move via the pixel coordinates, while the animation was much smoother, I was no longer able to store the player in the tile map as the player could be in two tiles at once making it harder to detect collisions.

I wish to hear your opinions as to how would you approach such a predicament. I would also appreciate it if anyone could link some similar examples for me to look and learn from on my own :slight_smile:.

Thank You in advance.

hello,

unfortunately it is a long time ago, but I once saw a very nice example of doing collision detection with bitmap images.

the point being that if you do not have some sort of geometry, it could be an interesting method. the project (I cannot link here, haha) dropped irregular forms from the ceiling, letting them fall onto the floor.
the nice thing was that it was working for any kind of form since it checked for pixels rather than distances and the like.

in fact, if you know the lowest pixel (or line of pixels) of your moving object, you do not need to check against your complete terrain but only against a smaller masked out part.

probably, if your moving object is not very regular you might need to check agains all its pixels, or, with some work involved the pixels showing its contour.

if the terrain is not too fancy, I think you could get away with limiting the pixels to detect collisions with to a small rectangle (from the highest to the lowest).

if any of your objects pixels is on a “black” pixel -> collision. you could use a contour too, but that may mean that you step over the boundary because your step size is too big.

so I think its fine to use contours for one object and the pixel area for the second - whichever.

maybe this helps to illustrate my strange words a little:

I know it is an not often used method for collisions, yet I like the beauty of it that it is independent of the form.

making an internal copy of your terrain in b/w should be easy.

if you have your object and your terrain in separate PGraphics, you could ignore colors completely and check on alpha values too.

Wow. That is an interesting idea. Thank you so much. I’ll give that a go :+1:

Solved the issue I was having.
Here is a quick summary of how I achieve this for anyone who may stumble across this post in the future, with the same problem.
In the end, I decided to go with storing the map in a 2D array. Previously, I had declared a few boolean fields to specify whether or not there is a block on the player’s left or right hand side. As such, as soon the player entered the left or right column of a block, he could no longer be able move in that direction. This usually left a weird gap between the player’s edge and the block.
How I ended up solving this was to change the boolean fields into integer fields. Now, instead of simply setting the fields to true or false, I instead calculated the first wall on either side of the player object by scanning the row the player is in, and stored the wall’s x value (column * tileSize). Then, whenever moving in either direction, I check if the player’s right edge > the right collision x-coordinate, or if the player’s left edge < the left collision x-coordinate.

I used a similar approach to handle jumping. After writing a gravity function which would make the player drop to the ground if he is not already on the ground, I scanned the column the player is on to get the first block under the player. Setting the top of the block as the y coordinate of the ground was all I needed then.

Special thanks to @Hyperion65 for putting me on the right path. Cheers

P.S. Following is the code I used to convert the player’s position in x and y coordinates to row and column integers I could use in the 2D array to get surrounding tiles.

int row = playerY/tileSize;
int col = playerX/tileSize;
2 Likes