Collision detection in a parallax-scrolling game

I’m working on a “simple” game engine of which the display is already working.
It works as follows:

  • A map can consist of multiple layers
  • Each layer can contain sprites and tiles
  • Sprites and tiles both can be either solid or non-solid
  • Sprites and tiles both can be multiple layers “tall”
  • the top-left corner of all layers is at 0,0 and the parallax effect is achieved using a different scroll factor for each layer.

Assumed I have a game consisting of two layers.
Layer 1 has a scroll factor of 0.5 and contains a solid tile which is 2 layers tall (layer 1 and 2)
Layer 2 has a scroll factor of 1 and contains a solid sprite which is 1 layer tall (only layer 2)
How do I compute if the sprite collides with the tile on the “projection layer” as seen by the player (in this case it’s layer 2 since it has a scroll factor of 1)?

I was really confused about this until I had a Dr. Pepper and an epiphany.

 I think you could do something along the lines of, If sprite was to occupy the same spot as another solid object return him to the tile he previously occupying. Like for instance: If I was to run into a wall at full speed I would fall flat on my ass if it wasn't a paper mache wall. Now think of it in tile space if your sprite was to run into a wall at full speed then you could make him fall flat on his ass by setting his X or Y or whatever into the tile he was in. Perhaps maybe right in the middle of the tile, to the top left, or whatever looks natural.

 Now to actually calculate the collision you could do something like this. Make a method like this:
 sO would be the thing you don't want your sprite in
 You pass it your sprites information


return boolean CheckCollision(int X, int Y, int Height, int Width,int Layer){
     int sOX, sOY, sOHeight, sOWidth;
     int sOLayer;
     boolean hit = false;
     if(((X+Width >= sOX && Y+Height >= sOHeight) || (x <= sOX+sOWidth && y <= sOY+sOHeight))&&Layer == sOLayer)
     {
          hit = true;
     }

return hit;
}

Those parenthesis are extremely important. If this does work like I want it to it should return true when your sprite gets within the solid object. But then again I wrote it so it might just be powered by my voodoo magic brain and this method doesn’t work anywhere else haha. Tell me if this works I would like to know. (Also parallax games are cool.)

First of all, thank you for your answer :slight_smile:
Secondly, your code does work - but it’s just what I already have. I think even with (or because?) your Dr. Pepper and Epiphany you misunderstood me.
The problem I’m stuck to is: A Sprite on Layer 2 can collide with a Tile on Layer 1. That wouldn’t be a problem yet. Now since the whole map is parallax-scrolling, I have the problem that the coordinates (50,50) on Layer 1 aren’t at the same place as the coordinates (50,50) on Layer 2, so I have to calculate which pixels on Layer 1 are just below/in front of the Sprite on Layer 2. The whole thing is kinda 3D. Well, almost.

couldnt you just multiply your x/y by your scroll factor?

if x collide with layer1X //or whatever your x/y hit code is)
if y collide with layer1Y
hitLayer1();

if 0.5x collide with layer2X
if 0.5
y collide with layer2Y
hitLayer2();

or something like this? ?

How do you translate the points to screen coordinates? I suggest converting them to a common coordinate system then checking for collision.

Hmm, I think I get it maybe.

Cool top down view:
__
Layer1: / /
Layer2 and sprite: o/_/

and that poorly drawn solid object is what you are supposed to be colliding with as of now but instead you are colliding with it only when the sprites x,y is at the layer1’s x,y of the Solid Object.
If so I suggest that you try and see if you can make each solid object if it takes up more than one layer have multiple x,y’s and change or modify some code that you already have to check the object in question on a layer specific level.

Hopefully that might be what you’re talking about if it’s not yell at me and call me stupid :smiley: and then explain one more time haha.