Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (789)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (864)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Collision detection in a parallax-scrolling game  (Read 3947 times)
0 Members and 1 Guest are viewing this topic.
Offline zilti

Junior Newbie

« Posted 2011-08-21 17:21:07 »

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)?
Offline WhenNDoubtGTFO

Junior Newbie

« Reply #1 - Posted 2011-08-22 17:58:32 »

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.)
Offline zilti

Junior Newbie

« Reply #2 - Posted 2011-08-22 19:42:44 »

First of all, thank you for your answer   Smiley
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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline namrog84

JGO Ninja

Medals: 46
Projects: 4

Keep programming!

« Reply #3 - Posted 2011-08-23 00:38:05 »

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

if 0.5*x collide with layer2X
if 0.5*y collide with layer2Y

or something like this? ?

"Experience is what you get when you did not get what you wanted"
Offline ra4king

JGO Kernel

Medals: 508
Projects: 3
Exp: 5 years

I'm the King!

« Reply #4 - Posted 2011-08-23 01:48:16 »

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

Offline WhenNDoubtGTFO

Junior Newbie

« Reply #5 - Posted 2011-08-24 03:03:31 »

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 Cheesy and then explain one more time haha.
Pages: [1]
  ignore  |  Print  

hadezbladez (2606 views)
2018-11-16 13:46:03

hadezbladez (932 views)
2018-11-16 13:41:33

hadezbladez (2560 views)
2018-11-16 13:35:35

hadezbladez (502 views)
2018-11-16 13:32:03

EgonOlsen (3680 views)
2018-06-10 19:43:48

EgonOlsen (4032 views)
2018-06-10 19:43:44

EgonOlsen (2443 views)
2018-06-10 19:43:20

DesertCoockie (3249 views)
2018-05-13 18:23:11

nelsongames (3295 views)
2018-04-24 18:15:36

nelsongames (4266 views)
2018-04-24 18:14:32
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!