# A Few OpenGL Collision Detection Questions

## Recommended Posts

MrLolEthan    106
Is there any way I can make an invisible cube (Like a Bounding Box) and use .intersects() or something on it like a rectangle? If not then whats the easiest way to do Collision Detection (3D) in Java?

Thanks,
Ethan.

EDIT:
I got the collision [u]detection[/u] working but now i'm wondering if anyone has an algorithm or way of doing the "stop letting me walk into the object on collision" stuff (what I tried made me go to the same spot each time (x -= x - 0.3749997f same for z really)). Edited by MrLolEthan

##### Share on other sites
VildNinja    789
This is not related to OpenGL

if you just want to detect if two AABBs overlaps, you can fairly easily code it yourself:

for (AABB other : allAABBs)
sweep for collisions on x-axis: if other.min.x < this.max.x && other.max.x > this.min.x
if true sweep for collisions on y-axis: -
if true sweep for collisions on y-axis: -
if true yes they do collide.

If you have an insane amount of objects you could put them into an oct-tree, but the above approach is [u]very[/u] fast

If you want some thing more you should look into one of these: http://www.google.dk/search?q=3D+physics+java&aq=f&oq=3D+physics+java&sugexp=chrome,mod=9&sourceid=chrome&ie=UTF-8

##### Share on other sites
MrLolEthan    106
[quote name='VildNinja' timestamp='1355533494' post='5010800']
This is not related to OpenGL

if you just want to detect if two AABBs overlaps, you can fairly easily code it yourself:

for (AABB other : allAABBs)
sweep for collisions on x-axis: if other.min.x < this.max.x &amp;&amp; other.max.x > this.min.x
if true sweep for collisions on y-axis: -
if true sweep for collisions on y-axis: -
if true yes they do collide.

If you have an insane amount of objects you could put them into an oct-tree, but the above approach is [u]very[/u] fast

If you want some thing more you should look into one of these: [url="http://www.google.dk/search?q=3D+physics+java&amp;aq=f&amp;oq=3D+physics+java&amp;sugexp=chrome,mod=9&amp;sourceid=chrome&amp;ie=UTF-8"]http://www.google.dk...chrome&amp;ie=UTF-8[/url]

[/quote] Could you please give me a snippet of code to show how its done? (Sorry i'm new to Java Game Developement) (Btw i'm using Lwjgl and I looked into JBullet but JBullet didn't look like it's compatible with Lwjgl) Edited by MrLolEthan

##### Share on other sites
VildNinja    789
[quote name='MrLolEthan' timestamp='1355574212' post='5010921']
Could you please give me a snippet of code to show how its done? (Sorry i'm new to Java Game Developement) (Btw i'm using Lwjgl and I looked into JBullet but JBullet didn't look like it's compatible with Lwjgl)
[/quote]
Yes it is. But you have to make the binding yourself, i.e. if you create a mesh of a cube in 3D, you need to get the position and rotation of it from an equivalent cube in your physics model. You can take a look on the source of the JBullet demos. It uses jogl, but the openGL code should be the same.

If you don´t want to do it yourself you can take a look at http://jmonkeyengine.com

##### Share on other sites
metsfan    679
You seem to have a fundamental misunderstanding of what OpenGL is. OpenGL is just a graphics driver. It provides an interface between your program and the pipeline used by your graphics card to output information to screen. Collision detection is a completely separate process that uses bounding volumes created from the geometry of your objects to find collisions. I would suggest learning more about game engines in general. Once you understand how game engines and subsystems work (collision detection is just one of many subsystems in a game), I would then focus your efforts on learning about collision detection. You may find that the best option for you is to use an already-made library.

As for Bullet and OpenGL not being compatible: once again, this is a fundamental misunderstanding of what these two pieces of software do. OpenGL and Bullet are completely separate entities that provides two completely different functions. However, the key between them is that you can use the same geometry to power both engines. They just process them differently: Bullet uses the geometry to create an abstraction of a physics simulation, and OpenGL uses the geometry to color pixels on the screen.

Here are some books on the subject that you may find interesting:
Game Coding Complete - This book will give you some insight into how game engines work.
http://www.amazon.com/Game-Coding-Complete-Fourth-Edition/dp/1133776574/ref=sr_1_1?ie=UTF8&qid=1355671800&sr=8-1&keywords=game+coding+complete

Game Engine Architecture - This book will teach you how to build a commercial-grade game engine.
http://www.amazon.com/Game-Engine-Architecture-Jason-Gregory/dp/1568814135/ref=sr_1_1?s=books&ie=UTF8&qid=1355671868&sr=1-1&keywords=game+engine+architecture

Real Time Collision Detection - This book will become your bible for collision detection
http://www.amazon.com/Real-Time-Collision-Detection-Interactive-Technology/dp/1558607323/ref=sr_1_1?s=books&ie=UTF8&qid=1355671841&sr=1-1&keywords=real+time+collision+detection%5C

Good luck.