collusion and librarys

Started by
3 comments, last by Serapth 8 years, 4 months ago

Hi, first of all sorry for any grammical mistakes, english isn't my native language.

I am in the process of learning how to build a 2D game in java but I don't have a deep understanding in the subject so I have a few questions.

1. I am having a problem with the preformence in the collusion detection. I am using detection with rectangles that intersect, but in many cases, like the player jumping, there are sometimes glitches like the player image sink a bit into the floor because the detection was too late.

What might be the problem and how can I fix it?

Also, is it reasonable to have different collusion methods for different objects (player, enemy, objects and bullets)?

2. I understood it is worthwhile to use librarys like lwjgl for games. I tried to learn a bit from tutorials but I stopped because I never quite understood what are the advantages of the lwjgl, can someone explain a bit on the subject? Why I should use it and how its best to start learning it?

If you want to see source code of the game look or collusion method I can post it, but the game loop isn't uniqe in any way i think (just a lot of updates and repaint in the end) and the cullosion method checks for every rect if it intersects with the map, objects and so forth. Also in the collusion method there are some unnecessay booleans I think that I created trying to fix the problem I described.

Advertisement
Searching for "collision" instead of "collusion" might be a first step to get better search results, especially since "collusion" is a real word that means something entirely different.

That said, there should be a Java port of Box2D around. It might be interesting for you, either to see how it works or to use it instead.

LWJGL allows you direct interaction with OpenGL. Java always had a little bit OpenGL support but when I had a look at it it was too limiting to be useful and too fragile (as in: do the wrong thing and suddenly everything is pure software without warning).

You say "performance of collisions". Usually, "performance" means speed or efficiency, that is, how quickly can you find the rectangles that your player is colliding with.

The next sentence however continues about intersection done too late. I assume "performance" here means collision detection, which needs to be improved to avoid glitching.

A really simple solution can be to adjust the player position after detecting a collision (untested, direction of adjustment may be wrong):


private void adjustPlayer(Rect player, Rect wall) {
    if (player.top < wall.top) { // Assume the player is approaching the wall from above (you can eg also use direction of player movement to derive this)
        if (player.bottom > wall.top) player.move(0, wall.top - player.bottom);
    } else {
        if (player.top < wall.bottom) player.move(0, wall.bottom - player.top);
    }

    if (player.left < wall.left) { // Assume the player is approaching the wall from left.
        if (player.right > wall.left) player.move(wall.left - player.right, 0);
    } else {
        if (player.left < wall.right) player.move(wall.right - player.left, 0);
    }
}

Searching for "collision" instead of "collusion" might be a first step to get better search results, especially since "collusion" is a real word that means something entirely different.

That said, there should be a Java port of Box2D around. It might be interesting for you, either to see how it works or to use it instead.

LWJGL allows you direct interaction with OpenGL. Java always had a little bit OpenGL support but when I had a look at it it was too limiting to be useful and too fragile (as in: do the wrong thing and suddenly everything is pure software without warning).

This is embarrassing haha in the code I wrote collosion, I don't know why i wrote collusion now.

Anyway your answer isn't very helpful because I don't know much about OpenGL either, so I don't know what are the advantages. I am sorry of the if the question seems dumb, in that case I will try to read more on the subject to have better understanding. My knowledge in programming and java is basically what I learned in highschool (which is very, very little, only basic codes in java) and the little bit I learned on the internet.

You say "performance of collisions". Usually, "performance" means speed or efficiency, that is, how quickly can you find the rectangles that your player is colliding with.

The next sentence however continues about intersection done too late. I assume "performance" here means collision detection, which needs to be improved to avoid glitching.

A really simple solution can be to adjust the player position after detecting a collision (untested, direction of adjustment may be wrong):

Right, it worked. I jut read somewhere that in games there are usually different threads for physics and for drawing so I thought my problem could be that my game runs on one thread. Thank you.

As to OpenGL and Java (and LWJGL), the reality is, Java has no native library capable of creating real time games of any complexity. There were some attempts like Java3D, but they went nowhere in the end.

So to do high performance 3D you need a 3D library of some sorts. That is where OpenGL comes in, its a cross platform 3D api that is supported directly by your video hardware, so it's quite zippy. However, OpenGL is a C api, so you need a translation layer when working in other programming languages. LWJGL is such a translation layer, referred to as BINDINGS, although LWJGL does a lot more than just wrap OpenGL.

Working with LWJGL is still pretty low level, so it's quite common to work at a higher level than that, be it a library or a game engine. jMonkeyEngine is a popular 3D engine that is Java based, while LibGDX is a popular 2D/3D library. Both make use of LWJGL under the hood, to use a car metaphor.

As to detecting collisions, as others said, its common to use a third party library. Common physics engines with Java bindings are Box2D and Bullet. Both LibGDX and jMonkeyEngine have support for one or more built in if you go that route.

However if you are just working in a simple 2D game, all of this can be overkill. You can check for collisions using simple rectangles. If you need to search for the term, use the term "Axis aligned bounding boxes" or AABB and you will get tons of results. Even that might be overkill, as you can often use a simple rectangle class, or a couple if if statements, depending on your level of complexity.

This topic is closed to new replies.

Advertisement