Simple Collision Detection? - Java/Slick2D

Started by
0 comments, last by Tiblanc 13 years, 4 months ago
So I'm trying to learn how to program with Java and the Slick2D library, and I'm having a lot of trouble with collisions. This is the first time I've tinkered with collisions outside of using GameMaker (which simplifies the process quite a bit) so I know I'm missing something. Here's the code I'm trying to use:
if (input.isKeyDown(Input.KEY_W)){	if (!Block.getMask().contains(mask.getCenterX(),mask.getY()-1))	{		y-=.2;	}}

All I'm trying to do is check in a single direction whether or not there's a block in my way. Block is the class used to create my block objects. I gave it the method getMask() which simply returns the rectangle used for collisions. contains is a method bundled with the Slick library. It checks whether or not a point lies within a given shape (in this case, the Block's mask). The rest of it is pretty self explanatory. I'm pretty sure my problem is in using Block as my comparison. I was hoping this would reference all instances of the Block class, but apparently this is not true. Can anyone help me out?
____________________________I'm 15, and beginning C#, and maybe Python. I'm fairly experienced in GML as well, and would be happy to help in GM related questions.
Advertisement
It does not work that way. Unless you keep all your blocks in a static collection, you won't be able to get the mask for all blocks with that call. Instead, you need to loop through all the blocks and check them individually for collision. It's the brute force approach and not the optimal method, but should work just fine for a low amount of blocks and is very easy to implement.
Developer for Novus Dawn : a [s]Flash[/s] Unity Isometric Tactical RPG - Forums - Facebook - DevLog

This topic is closed to new replies.

Advertisement