Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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 1856 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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
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
hitLayer1();

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

or something like this? ?

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

JGO Kernel


Medals: 356
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  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (33 views)
2014-12-15 09:26:44

Mr.CodeIt (24 views)
2014-12-14 19:50:38

BurntPizza (51 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (46 views)
2014-12-05 12:39:02

SHC (60 views)
2014-12-03 16:27:13

CopyableCougar4 (61 views)
2014-11-29 21:32:03

toopeicgaming1999 (124 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org 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‑gaming.org
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!