简体   繁体   中英

Easiest way to do collision LWJGL JAVA

Ok so I have a ArrayList which contains all the tiles that need to be drawn to the screen and I have the player that is moved with the arrow keys, but I am unsure how I could implement collision detection with this, Would it be efficient to check if the current tile is filled and if so stop, This is my first time creating a game from scratch so any help is greatly appreciated!

EDIT: The tiles are 32x32 images which are held in a ArrayList basically if you guys can help me with the y-axis collision i should be fine with the rest. Tile class contains x, y, and the tile type

EDIT2:I havent tried anything yet, I don't really know where to start but basically i need some sort of Collision on tile, eg While player is moving if collide on tile do something but i dont know how to check if i have collided or not

Below I have an example of a bouncing ball, which will test for the end of the JPanel, and do something

  //x = X-Axis Location //This tests for a collision of the JPanel on the left side or the right side
public boolean isLeavingSideToSide(JPanel jp)
{
    if( x <=0 || x+ width >= jp.getWidth())
    {
        BounceEvent be = new BounceEvent(this);
        notifyListeners(be);            
        return true;
    }
    else
        return false;
}

BounceEvent is created to allow for the notifyListener, which calls the bounced method.

public void notifyListeners(BounceEvent be)
{
    for(BounceListener bl : listeners)
    {
        bl.bounced(be);
    }
}

My Bounced method changes the color of the ball (Does some action)

public void bounced(BounceEvent be) {
    Ball b = be.getBall();
    Color c = b.getColor();
    int x = 256;
    int newColor1 = (int) (Math.random() * x);
    int newColor2 = (int) (Math.random() * x);
    int newColor3 = (int) (Math.random() * x);
    b.setColor(new Color(newColor1,newColor2,newColor3));
}

Hopefully this will help you get started.

Collision detection isn't matter for beginners, nor can it be fully explained in a Stackoverflow post; if you're still struggling with arrays, try to make simple games, where collision is detected by calculating the distance between objects like circles .

There are many ways to handle collision detection, but you have to be more specific:
what kind of tile-based game are you planning to develop?

I strongly suggest you to read this article , by Katy Coe, which gives a summary of a few implementations. The following methods are explained in the aforementioned blog.

The simplest and most intuitive method is the Pixel Colour Approach , where players can move on tiles with a specific color value, but it can be used only with a limited color palette. The Mask Approach is essentially the same as PCA, but it hides black and white layers behind custom graphics; the latter is computationally expensive and not generally recommended.

The Grid-Based Approach is probably what you're looking for. Each element of the map corresponds to a tile of the grid (described by a 2D array). The player moves in a discrete space, although animation might fake fluid movements.

The Pixel Tile Method describes collisions by sorrounding characters with n control points; this approach let the developer use non-squared characters in a tiled world. The Bounding Box Method is a simpler implementation of PTM, where the player is wrapped in a rectangle. The program checks whether any of the four corners of the rectangle intersect with any wall.

Discrete Collision Detection , or its improved version, Speculative Contacts , are used in more complex games and you surely don't need those now.

Don't try to learn all at once, start with the simplest implementation you can think of . Otherwise, you would soon get discouraged and would not appreciate the pro/cons of each technique.

I've recently found this great tutorial about tile-based games with Macromedia Flash, but you can easily apply those techniques using your language of choice. READ IT! If you want an alternative to Katy's articles, Rodrigo Monteiro comes in help .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM