Sign in to follow this  

Simple Collision Detection? - Java/Slick2D

This topic is 2548 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 2548 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this