Author Topic: Collision Detection in OpenGL  (Read 4094 times)

0 Members and 1 Guest are viewing this topic.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Collision Detection in OpenGL
« on: March 30, 2019, 09:54:40 am »
Hi everyone!
I have found an easy way to detect collision detection of cursor with object rendered with OpenGL. (https://www.opengl.org/archives/resources/faq/technical/selection.htm)
Move your cursor over the shapes.
Code: QB64: [Select]
  1. _TITLE "Move cursor on shape(s)."
  2. SCREEN _NEWIMAGE(600, 600, 32)
  3.  
  4. DECLARE LIBRARY 'external function(s)
  5.     SUB drawCube ALIAS glutSolidCube (BYVAL dsize AS DOUBLE)
  6.         SUB gluPickMatrix (BYVAL x AS SINGLE, BYVAL y AS SINGLE, BYVAL delX AS SINGLE, BYVAL delY AS SINGLE, viewport AS LONG)
  7.  
  8. type vec3
  9.         x as single
  10.         y as single
  11.         z as single
  12.  
  13. type cube
  14.         pos as vec3 'position
  15.         col as vec3 'color
  16.         rot as vec3 'rotation
  17.  
  18. DIM SHARED mouseX, mouseY, gl, collision, obj_type
  19. Dim shared cubes(10) as cube
  20. DIM SHARED selectBuffer(4) AS _UNSIGNED LONG, hits&, viewport&(4)
  21.  
  22. for i = 0 to ubound(cubes)
  23.         cubes(i).pos.x = p5random(-1 ,1)
  24.         cubes(i).pos.y = p5random(-1, 1)
  25.         cubes(i).pos.z = p5random(-0.8,0)
  26.         cubes(i).col.x = rnd : cubes(i).col.y = rnd : cubes(i).col.z = rnd
  27.         cubes(i).rot.x = p5random(0,360)
  28.         cubes(i).rot.y = p5random(0,360)
  29.         cubes(i).rot.z = p5random(0,360)
  30.  
  31.  
  32. gl = -1
  33.     mouseX = _MOUSEX: mouseY = _MOUSEY
  34.     _LIMIT 40
  35.     CLS , 1
  36.     COLOR _RGB(255, 0, 0), 1
  37.     IF hits& >= 1 THEN
  38.                 collision = selectBuffer(3)
  39.                 ? collision, hits&
  40.         ELSE
  41.                 collision = 0
  42.         end if
  43.  
  44. SUB _GL ()
  45.         static buffer(4) as _unsigned long, tmp
  46.  
  47.     IF gl = 0 THEN EXIT SUB
  48.  
  49.  
  50.     _glEnable _GL_LIGHTING
  51.     _glEnable _GL_LIGHT0
  52.     _glEnable _GL_DEPTH_TEST
  53.     _glEnable _GL_COLOR_MATERIAL
  54.  
  55.     _glLightfv _GL_LIGHT0, _GL_AMBIENT, glVec4(0.1, 0.1, 0.1, 0)
  56.     _glLightfv _GL_LIGHT0, _GL_SPECULAR, glVec4(0.8, 0.8, 0.8, 0)
  57.     _glLightfv _GL_LIGHT0, _GL_POSITION, glVec4(1, 1, 0, 0)
  58.  
  59.  
  60.     _glGetIntegerv _GL_VIEWPORT, _OFFSET(viewport&())
  61.  
  62.     _glSelectBuffer 4, _OFFSET(buffer())
  63.     dummy = _glRenderMode(_GL_SELECT)
  64.     _glPushName 0
  65.  
  66.     _glMatrixMode _GL_PROJECTION
  67.  
  68.         _glPushMatrix
  69.  
  70.     gluPickMatrix mouseX, _HEIGHT - mouseY, 1,1, viewport&()
  71.  
  72.         for i = 0 to ubound(cubes)
  73.                 _glLoadName (i+1)
  74.                 _glPushMatrix
  75.                 _glcolor3f cubes(i).col.x,cubes(i).col.y,cubes(i).col.z
  76.                 _glrotatef cubes(i).rot.x+tmp,1,0,0
  77.                 _glrotatef cubes(i).rot.y+tmp,0,1,0
  78.                 _glrotatef cubes(i).rot.z,0,0,1
  79.                 _gltranslatef cubes(i).pos.x,cubes(i).pos.y,cubes(i).pos.z
  80.                 drawCube 0.22
  81.                 _glPopMatrix
  82.         next
  83.  
  84.         _glPopMatrix
  85.  
  86.     hits& = _glRenderMode(_GL_RENDER)
  87.         selectBuffer(3) = buffer(3)
  88.  
  89.     _glMatrixMode _GL_MODELVIEW
  90.        
  91.         for i = 0 to ubound(cubes)
  92.                 _glPushMatrix
  93.                 if collision = (i+1) then _glColor3f 1,0.5,0 else _glcolor3f cubes(i).col.x,cubes(i).col.y,cubes(i).col.z
  94.                 _glrotatef cubes(i).rot.x+tmp,1,0,0
  95.                 _glrotatef cubes(i).rot.y+tmp,0,1,0
  96.                 _glrotatef cubes(i).rot.z,0,0,1
  97.                 _gltranslatef cubes(i).pos.x,cubes(i).pos.y,cubes(i).pos.z
  98.                 drawCube 0.22
  99.                 _glPopMatrix
  100.         next
  101.        
  102.     _glFlush
  103.    
  104.         tmp=tmp+1
  105.  
  106. FUNCTION p5random! (mn!, mx!)
  107.     IF mn! > mx! THEN
  108.         SWAP mn!, mx!
  109.     END IF
  110.     p5random! = RND * (mx! - mn!) + mn!
  111.  
  112. FUNCTION glVec4%& (x, y, z, w)
  113.     STATIC internal_vec4(3)
  114.     internal_vec4(0) = x
  115.     internal_vec4(1) = y
  116.     internal_vec4(2) = z
  117.     internal_vec4(3) = w
  118.     glVec4%& = _OFFSET(internal_vec4())
  119.  
« Last Edit: March 30, 2019, 09:56:36 am by Ashish »
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Collision Detection in OpenGL
« Reply #1 on: March 30, 2019, 11:06:29 am »
This is a very useful matter. Thank you for sharing, Ashish.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Collision Detection in OpenGL
« Reply #2 on: March 30, 2019, 11:09:55 am »
This is a very useful matter. Thank you for sharing, Ashish.
Thanks Petr! But remember, when I include gluPerspective() with rendering, it doesn't work. This subject need to be studied more.
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Collision Detection in OpenGL
« Reply #3 on: March 30, 2019, 11:22:02 am »
Quote
Thanks Petr! But remember, when I include gluPerspective() with rendering, it doesn't work. This subject need to be studied more.

I'm sure I'll get to it soon. So far, I have been solving collision detection in a Maze program using a two-dimensional array (X/Z) and it works perfectly and quickly, but of course it's not an
OpenGL version.