wood burning stoves 2.0*
The moose likes Game Development and the fly likes Best way to work with interaction of 2D images? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "Best way to work with interaction of 2D images?" Watch "Best way to work with interaction of 2D images?" New topic
Author

Best way to work with interaction of 2D images?

Caleb Kemper
Greenhorn

Joined: Feb 08, 2012
Posts: 27

I'm making my second game, this time a side-scroller so I can have more expandable playing areas. I have all the moving and KeyListening worked out, but I'm not sure how to attack the detection of the character hitting the ground/landscape. I understand I could use pixel color detection or something similar, but that seems a bit complex for something so simple.

What's the usual technique for doing something like this? I want it to be flexible too, so that it would work regardless of what the terrain looked like.

Thanks,
Caleb
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
If you create the landscape image within your program then just record the height(s) of the ground for each step along the x axis. Then check your ships height against this landscape height each time the ship moves. If you're using an image created in another program then I guess the simplest method is to just examine the image and manually store the height at each stage - this will be boring to do and might take a while but at least flat sections will be easy. Writing a program to do a one time analysis of a single image isn't going to save you any time and will probably introduce a lot of bugs. If it was me I'd just create the image within the program.


Arcade : Alien Swarm
Board : Chess - Checkers - Connect 4 - Othello
Caleb Kemper
Greenhorn

Joined: Feb 08, 2012
Posts: 27

First off, thanks for the reply!

I see what you're saying about doing a manual rundown of the landscape image and going from there, however I was hoping for something a bit more versatile.
In particular, I have in mind the game Terraria. The maps are randomly generated and this is still achieved. I know Terraria was done in the XNA game studio in C++, so maybe it's not as easily achievable in Java?

My bet is a test for transparency under the centered entity. Too bad that involves some more knowledge on the color class and its methods. More reading for me! haha

Thanks,
Caleb
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
Could you not use the same map files that this game uses and take the height values from the original map? I did a quick google and found various map viewers and editors for this game so it must be possible to work out the heights from them? The advantage of doing it this way is you can run the maps directly and the game would be closer to the original. You could also use the user built maps as well. There may be copyright issues of course (not sure).

I haven't ever played the game or examined these map files so I might be talking ****. I'm pretty sure that working on the images directly is not the best way to go. If it was me then I think I'd just stump up the £6 and play the game using their software

PS They seem to have a Terraria database/file format that you could read and process.
Caleb Kemper
Greenhorn

Joined: Feb 08, 2012
Posts: 27

Well I'm not trying to recreate the game. It's just one source of my inspiration. I think they did a great job on the mapping, and that's why I was looking into it.

I did some pondering and think I figured it out though. Terraria creates randomly generated maps out of varying types of blocks (like Minecraft). Thus, the blocks must be entities and have their own drawing instance data. These probably include shapes which could then utilize the intersects() method to do what I'm trying to do.

This sounds a lot more efficient than transparency as well.

And I do actually own Terraria
Great game; you should look into it!

Thanks,
Caleb
Antonio Principato
Greenhorn

Joined: May 21, 2012
Posts: 10
Not sure if this is the best way to go about it but the Rectangle class has an intersects method. If your landscape and your player extend Rectangle you can just use an if statement to see if they intersect.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best way to work with interaction of 2D images?