Cube OpenGL collision qt

New to FreeBASIC? Post your questions here.
Post Reply
Löwenherz
Posts: 145
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Cube OpenGL collision qt

Post by Löwenherz »

Hello.. I have No big experience with fbgfx.bi and have a question how to handle a Setup for a Cube collision?

Its necessary to bind every glVertex3f Point into collision function?

Help is Welcome thanks

Code: Select all

' question about openGL and collision
'
#include "gl/glu.bi"
#include "fbgfx.bi"
' Screencontrol fb.SET_GL_2D_MODE ,fb.OGL_2D_MANUAL_SYNC
' Screencontrol doesnt work here, don't know why?
'
' how I can make a simple collision between these two cubes?
' via pushing up,down,left,right arrow keys? multikey() ?
'
Sub glsetup(xres As Long,yres As Long)
    glShadeModel(GL_SMOOTH)                 ' Enables Smooth Color Shading
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
    glBlendFunc(GL_ONE,GL_ONE)
    glEnable GL_ALPHA
    glEnable GL_BLEND
    glViewport(0, 0, xres, yres)       ' Set the viewport
    glMatrixMode(GL_PROJECTION)        ' Change Matrix Mode to Projection
    glLoadIdentity                     ' Reset View
    gluPerspective(45.0, xres/yres, 1.0, 100.0)
    glMatrixMode(GL_MODELVIEW)         ' Return to the modelview matrix
    glLoadIdentity                     '  Reset View
    glclearcolor .2,.2,.2,1 'optional
End Sub

'Rotate and draw the cube with texturing on each face
Sub DrawGlCube(Byref rotangle As Single)
    glLoadIdentity()
    glTranslatef(2,0,-7)
    glRotatef(rotangle,1,.5,.25)           ' Rotate
    glBegin(GL_QUADS)
    
    glcolor3ub 255,0,0
    glVertex3f( 1.0, 1.0,-1.0)            ' Top right of the quad (top)
    glVertex3f(-1.0, 1.0,-1.0)            ' Top left of the quad (top)
    glVertex3f(-1.0, 1.0, 1.0)            ' Bottom left of the quad (top)
    glVertex3f( 1.0, 1.0, 1.0)            ' Bottom right of the quad (top)
    
    glcolor3ub 255,100,0
    glVertex3f( 1.0,-1.0, 1.0)            ' Top right of the quad (bottom)
    glVertex3f(-1.0,-1.0, 1.0)            ' Top left of the quad (bottom)
    glVertex3f(-1.0,-1.0,-1.0)            ' Bottom left of the quad (bottom)
    glVertex3f( 1.0,-1.0,-1.0)            ' Bottom right of the quad (bottom)
    
    glcolor3ub 255,0,255
    glVertex3f( 1.0, 1.0, 1.0)            ' Top right of the quad (front)
    glVertex3f(-1.0, 1.0, 1.0)            ' Top left of the quad (front)
    glVertex3f(-1.0,-1.0, 1.0)            ' Bottom left of the quad (front)
    glVertex3f( 1.0,-1.0, 1.0)            ' Bottom right of the quad (front)
    
    glcolor3ub 0,0,200
    glVertex3f( 1.0,-1.0,-1.0)            ' Bottom left of the quad (back)
    glVertex3f(-1.0,-1.0,-1.0)            ' Bottom right of the quad (back)
    glVertex3f(-1.0, 1.0,-1.0)            ' Top right of the quad (back)
    glVertex3f( 1.0, 1.0,-1.0)            ' Top left of the quad (back)
    
    glcolor3ub 0,255,0
    glVertex3f(-1.0, 1.0, 1.0)            ' Top right of the quad (left)
    glVertex3f(-1.0, 1.0,-1.0)            ' Top left of the quad (left)
    glVertex3f(-1.0,-1.0,-1.0)            ' Bottom left of the quad (left)
    glVertex3f(-1.0,-1.0, 1.0)            ' Bottom right of the quad (left)
    
    glcolor3ub 255,0,100
    glVertex3f( 1.0, 1.0,-1.0)            ' Top right of the quad (right)
    glVertex3f( 1.0, 1.0, 1.0)            ' Top left of the quad (right)
    glVertex3f( 1.0,-1.0, 1.0)            ' Bottom left of the quad (right)
    glVertex3f( 1.0,-1.0,-1.0)
    glend
End Sub

Sub DrawGlCube2(Byref rotangle As Single)
    glLoadIdentity()
    glTranslatef(-2,0,-7)
    glRotatef(rotangle,1,.5,.25)           ' Rotate
    glBegin(GL_QUADS)
    
    glcolor3ub 255,0,0
    glVertex3f( 1.0, 1.0,-1.0)            ' Top right of the quad (top)
    glVertex3f(-1.0, 1.0,-1.0)            ' Top left of the quad (top)
    glVertex3f(-1.0, 1.0, 1.0)            ' Bottom left of the quad (top)
    glVertex3f( 1.0, 1.0, 1.0)            ' Bottom right of the quad (top)
    
    glcolor3ub 255,100,0
    glVertex3f( 1.0,-1.0, 1.0)            ' Top right of the quad (bottom)
    glVertex3f(-1.0,-1.0, 1.0)            ' Top left of the quad (bottom)
    glVertex3f(-1.0,-1.0,-1.0)            ' Bottom left of the quad (bottom)
    glVertex3f( 1.0,-1.0,-1.0)            ' Bottom right of the quad (bottom)
    
    glcolor3ub 255,0,255
    glVertex3f( 1.0, 1.0, 1.0)            ' Top right of the quad (front)
    glVertex3f(-1.0, 1.0, 1.0)            ' Top left of the quad (front)
    glVertex3f(-1.0,-1.0, 1.0)            ' Bottom left of the quad (front)
    glVertex3f( 1.0,-1.0, 1.0)            ' Bottom right of the quad (front)
    
    glcolor3ub 0,0,200
    glVertex3f( 1.0,-1.0,-1.0)            ' Bottom left of the quad (back)
    glVertex3f(-1.0,-1.0,-1.0)            ' Bottom right of the quad (back)
    glVertex3f(-1.0, 1.0,-1.0)            ' Top right of the quad (back)
    glVertex3f( 1.0, 1.0,-1.0)            ' Top left of the quad (back)
    
    glcolor3ub 0,255,0
    glVertex3f(-1.0, 1.0, 1.0)            ' Top right of the quad (left)
    glVertex3f(-1.0, 1.0,-1.0)            ' Top left of the quad (left)
    glVertex3f(-1.0,-1.0,-1.0)            ' Bottom left of the quad (left)
    glVertex3f(-1.0,-1.0, 1.0)            ' Bottom right of the quad (left)
    
    glcolor3ub 255,0,100
    glVertex3f( 1.0, 1.0,-1.0)            ' Top right of the quad (right)
    glVertex3f( 1.0, 1.0, 1.0)            ' Top left of the quad (right)
    glVertex3f( 1.0,-1.0, 1.0)            ' Bottom left of the quad (right)
    glVertex3f( 1.0,-1.0,-1.0)
    glend
End Sub


Sub drawstring(xpos As Integer,ypos As Integer,text As String,colour As Ulong,size As Single,textangle As Single=0,charangle As Single=0,xres As Long=0,yres As Long=0)
    glMatrixMode GL_PROJECTION 'save projection
    glPushMatrix
    glMatrixMode GL_MODELVIEW
    glPushMatrix
    
    glMatrixMode GL_PROJECTION 'make ortho
    glLoadIdentity
    glOrtho 0, xres, yres, 0,-1, 1
    glMatrixMode GL_MODELVIEW
    glLoadIdentity
    #define Red(c) ((c) Shr 16 And 255)
    #define Green(c) ((c) Shr  8 And 255)
    #define Blue(c) ((c) And 255)
    #define Alph(c) ((c) Shr 24)
    glColor4ub Red(colour),Green(colour),Blue(colour),alph(colour)
    glend
    glpointsize(1.1*size)
    glBegin (GL_POINTS)
    Type point2d
        As Single x,y
        As Ulong col
    End Type
    Dim As Integer flag,codenum=256
    If Instr(text,Chr(10)) Then flag=1
    Static As Long runflag
    Static As point2d infoarray()
    Redim Preserve As point2d infoarray(128,codenum)
    If runflag=0 Then                   
        Dim As Ulong background=0
        Screen 8
        Width 640\8,200\16 
        Dim count As Long
        For ch As Integer=1 To codenum
            Cls
            Draw String(1,1),Chr(ch)
            For x As Long=1 To 8 
                For y As Long=1 To 16
                    If Point(x,y)<>background Then
                        count=count+1
                        infoarray(count,ch)=Type<point2d>(x,y)
                    End If
                Next y
            Next x
            count=0
        Next ch
        runflag=1
    End If
    If size=0 Then Exit Sub
    Dim As point2d temp(1 To 128,codenum),np
    Dim As Single cr= 0.01745329 'degs to radians
    #macro rotate(p1,p2,a,d)
    np.col=p2.col
    np.x=d*(Cos(a*cr)*(p2.x-p1.x)-Sin(a*cr)*(p2.y-p1.y)) +p1.x
    np.y=d*(Sin(a*cr)*(p2.x-p1.x)+Cos(a*cr)*(p2.y-p1.y)) +p1.y
    #endmacro
    
    Dim As point2d cpt(1 To 128),c=Type<point2d>(xpos,ypos),c2
    Dim As Integer dx=xpos,dy=ypos
    For z6 As Integer=1 To Len(text)
        Var asci=text[z6-1]
        If asci=10 Then
            If charangle<>0 Then xpos=xpos+12*Sin(charangle*cr)
            dx=xpos:dy=dy+16:Goto skip 'chr(10) for new line
        End If
        For _x1 As Integer=1 To 128
            temp(_x1,asci).x=infoarray(_x1,asci).x+dx
            temp(_x1,asci).y=infoarray(_x1,asci).y+dy
            temp(_x1,asci).col=colour
            rotate(c,temp(_x1,asci),textangle,size)
            cpt(_x1)=np
            Var copyy=np.y
            If charangle<>0 Then
                Dim As Long p
                If flag Then  p=1 Else  p=(z6-1)
                c2=Type<point2d>(xpos+(size*8)*p*(Cos(textangle*cr)),ypos+(size*8)*p*(Sin(textangle*cr)))
                rotate(c2,cpt(_x1),charangle,1)
                If flag Then np.y=copyy
                cpt(_x1)=np
            End If
            If infoarray(_x1,asci).x<>0 Then
                If Abs(size)>1 Then
                    glVertex3f (cpt(_x1).x,(cpt(_x1).y),0)
                End If
            End If
        Next _x1
        dx=dx+9+4*(Sin(charangle*cr))*flag
        skip:
    Next z6
    glend
    glMatrixMode GL_PROJECTION 'restore 
    glPopMatrix
    glMatrixMode GL_MODELVIEW
    glPopMatrix
End Sub

Sub init Constructor
    drawstring(0,0," ",0,0,0,0,0,0)
End Sub

Function Regulate(Byval MyFps As Long,Byref fps As Long) As Long
    Static As Double timervalue,lastsleeptime,t3,frames
    Dim As Double t=Timer
    frames+=1
    If (t-t3)>=1 Then t3=t:fps=frames:frames=0
    Dim As Long sleeptime=lastsleeptime+((1/myfps)-T+timervalue)*1000
    If sleeptime<1 Then sleeptime=1
    lastsleeptime=sleeptime
    timervalue=T
    Return sleeptime
End Function


Screenres 800,600,32,,2 
width 800\8,600\16 'larger dos fonts
glsetup(800,600)
Dim As Single angle
Dim As Long fps
Do
    'angle+=1
    glClear(GL_COLOR_BUFFER_BIT)
    
    glEnable (GL_CULL_FACE)
    DrawGlcube(angle)
    DrawGlCube2(angle)
    
    gldisable (GL_CULL_FACE) 'disable to use fb draw string
    
    Draw String(5,5),"This is fb draw string, line 3 is set  screencontrol fb.SET_GL_2D_MODE ,fb.OGL_2D_MANUAL_SYNC",rgb(255,255,255)
    
    drawstring(20,20,"Test font with opengl",Rgb(200,100,0),3,0,0,800,600)
    drawstring(550,80,"fps = " & fps,Rgb(0,100,200),2,10,0,800,600)
    drawstring(200,100,"Free"+Chr(10)+"BASIC (rotated 90 degrees)",Rgb(200,0,0),2,90,0,800,600)
    drawstring(200,560,"Press esc to end",Rgb(0,200,0),2,0,10,800,600)
    
    glcolor3ub 255,255,255  'reset the opengl colour
    Flip
    Sleep regulate(35,fps),1
Loop Until Multikey(1)'esc key
Sleep
' ends
2024, July 11, I used the example of dodicat as Basis

PS please wait perhaps I have the solution! :-)
Löwenherz
Posts: 145
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Cube OpenGL collision qt

Post by Löwenherz »

I Made this Progress and Looks much better but its Not perfect :-)

Updated Code example See next Post

Code: Select all


Last edited by Löwenherz on Sep 06, 2024 6:21, edited 3 times in total.
Löwenherz
Posts: 145
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Cube OpenGL collision qt

Post by Löwenherz »

Updated Code example have forgotten the Check collision Function..

But its still Not perfect ..compiled and No movements of the cubes by arrow Keys possible

Code: Select all

' question about openGL and collision two cubes
' demo part two, loewenherz, 05-09-2024
'
#include "gl/glu.bi"
#include "fbgfx.bi"
' Screencontrol fb.SET_GL_2D_MODE ,fb.OGL_2D_MANUAL_SYNC
' Screencontrol doesnt work here, don't know why?
'
Type BoundingBox
    minX As Single
    maxX As Single
    minY As Single
    maxY As Single
    minZ As Single
    maxZ As Single
End Type

Dim cube1 As BoundingBox
Dim cube2 As BoundingBox

Dim cube1Pos As Single = 2
Dim cube2Pos As Single = -2

Sub glsetup(xres As Long,yres As Long)
    glShadeModel(GL_SMOOTH)                 ' Enables Smooth Color Shading
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
    glBlendFunc(GL_ONE,GL_ONE)
    glEnable GL_ALPHA
    glEnable GL_BLEND
    glViewport(0, 0, xres, yres)       ' Set the viewport
    glMatrixMode(GL_PROJECTION)        ' Change Matrix Mode to Projection
    glLoadIdentity                     ' Reset View
    gluPerspective(45.0, xres/yres, 1.0, 100.0)
    glMatrixMode(GL_MODELVIEW)         ' Return to the modelview matrix
    glLoadIdentity                     '  Reset View
    glclearcolor .2,.2,.2,1 'optional
End Sub

'Rotate and draw the cube with texturing on each face
Sub DrawGlCube(Byref rotangle As Single,Byval pos1 As Single)
    glLoadIdentity()
    'glTranslatef(2,0,-7)
    glTranslatef(pos1, 0, -7)
    glRotatef(rotangle,1,.5,.25)           ' Rotate
    glBegin(GL_QUADS)
    
    glcolor3ub 255,0,0
    glVertex3f( 1.0, 1.0,-1.0)            ' Top right of the quad (top)
    glVertex3f(-1.0, 1.0,-1.0)            ' Top left of the quad (top)
    glVertex3f(-1.0, 1.0, 1.0)            ' Bottom left of the quad (top)
    glVertex3f( 1.0, 1.0, 1.0)            ' Bottom right of the quad (top)
    
    glcolor3ub 255,100,0
    glVertex3f( 1.0,-1.0, 1.0)            ' Top right of the quad (bottom)
    glVertex3f(-1.0,-1.0, 1.0)            ' Top left of the quad (bottom)
    glVertex3f(-1.0,-1.0,-1.0)            ' Bottom left of the quad (bottom)
    glVertex3f( 1.0,-1.0,-1.0)            ' Bottom right of the quad (bottom)
    
    glcolor3ub 255,0,255
    glVertex3f( 1.0, 1.0, 1.0)            ' Top right of the quad (front)
    glVertex3f(-1.0, 1.0, 1.0)            ' Top left of the quad (front)
    glVertex3f(-1.0,-1.0, 1.0)            ' Bottom left of the quad (front)
    glVertex3f( 1.0,-1.0, 1.0)            ' Bottom right of the quad (front)
    
    glcolor3ub 0,0,200
    glVertex3f( 1.0,-1.0,-1.0)            ' Bottom left of the quad (back)
    glVertex3f(-1.0,-1.0,-1.0)            ' Bottom right of the quad (back)
    glVertex3f(-1.0, 1.0,-1.0)            ' Top right of the quad (back)
    glVertex3f( 1.0, 1.0,-1.0)            ' Top left of the quad (back)
    
    glcolor3ub 0,255,0
    glVertex3f(-1.0, 1.0, 1.0)            ' Top right of the quad (left)
    glVertex3f(-1.0, 1.0,-1.0)            ' Top left of the quad (left)
    glVertex3f(-1.0,-1.0,-1.0)            ' Bottom left of the quad (left)
    glVertex3f(-1.0,-1.0, 1.0)            ' Bottom right of the quad (left)
    
    glcolor3ub 255,0,100
    glVertex3f( 1.0, 1.0,-1.0)            ' Top right of the quad (right)
    glVertex3f( 1.0, 1.0, 1.0)            ' Top left of the quad (right)
    glVertex3f( 1.0,-1.0, 1.0)            ' Bottom left of the quad (right)
    glVertex3f( 1.0,-1.0,-1.0)
    glend
End Sub

Sub DrawGlCube2(Byref rotangle As Single,Byval pos1 As Single)
    glLoadIdentity()
    'glTranslatef(-2,0,-7)
    glTranslatef(pos1, 0, -7)
    glRotatef(rotangle,1,.5,.25)           ' Rotate
    glBegin(GL_QUADS)
    
    glcolor3ub 255,0,0
    glVertex3f( 1.0, 1.0,-1.0)            ' Top right of the quad (top)
    glVertex3f(-1.0, 1.0,-1.0)            ' Top left of the quad (top)
    glVertex3f(-1.0, 1.0, 1.0)            ' Bottom left of the quad (top)
    glVertex3f( 1.0, 1.0, 1.0)            ' Bottom right of the quad (top)
    
    glcolor3ub 255,100,0
    glVertex3f( 1.0,-1.0, 1.0)            ' Top right of the quad (bottom)
    glVertex3f(-1.0,-1.0, 1.0)            ' Top left of the quad (bottom)
    glVertex3f(-1.0,-1.0,-1.0)            ' Bottom left of the quad (bottom)
    glVertex3f( 1.0,-1.0,-1.0)            ' Bottom right of the quad (bottom)
    
    glcolor3ub 255,0,255
    glVertex3f( 1.0, 1.0, 1.0)            ' Top right of the quad (front)
    glVertex3f(-1.0, 1.0, 1.0)            ' Top left of the quad (front)
    glVertex3f(-1.0,-1.0, 1.0)            ' Bottom left of the quad (front)
    glVertex3f( 1.0,-1.0, 1.0)            ' Bottom right of the quad (front)
    
    glcolor3ub 0,0,200
    glVertex3f( 1.0,-1.0,-1.0)            ' Bottom left of the quad (back)
    glVertex3f(-1.0,-1.0,-1.0)            ' Bottom right of the quad (back)
    glVertex3f(-1.0, 1.0,-1.0)            ' Top right of the quad (back)
    glVertex3f( 1.0, 1.0,-1.0)            ' Top left of the quad (back)
    
    glcolor3ub 0,255,0
    glVertex3f(-1.0, 1.0, 1.0)            ' Top right of the quad (left)
    glVertex3f(-1.0, 1.0,-1.0)            ' Top left of the quad (left)
    glVertex3f(-1.0,-1.0,-1.0)            ' Bottom left of the quad (left)
    glVertex3f(-1.0,-1.0, 1.0)            ' Bottom right of the quad (left)
    
    glcolor3ub 255,0,100
    glVertex3f( 1.0, 1.0,-1.0)            ' Top right of the quad (right)
    glVertex3f( 1.0, 1.0, 1.0)            ' Top left of the quad (right)
    glVertex3f( 1.0,-1.0, 1.0)            ' Bottom left of the quad (right)
    glVertex3f( 1.0,-1.0,-1.0)
    glend
End Sub


Sub drawstring(xpos As Integer,ypos As Integer,text As String,colour As Ulong,size As Single,textangle As Single=0,charangle As Single=0,xres As Long=0,yres As Long=0)
    glMatrixMode GL_PROJECTION 'save projection
    glPushMatrix
    glMatrixMode GL_MODELVIEW
    glPushMatrix
    
    glMatrixMode GL_PROJECTION 'make ortho
    glLoadIdentity
    glOrtho 0, xres, yres, 0,-1, 1
    glMatrixMode GL_MODELVIEW
    glLoadIdentity
    #define Red(c) ((c) Shr 16 And 255)
    #define Green(c) ((c) Shr  8 And 255)
    #define Blue(c) ((c) And 255)
    #define Alph(c) ((c) Shr 24)
    glColor4ub Red(colour),Green(colour),Blue(colour),alph(colour)
    glend
    glpointsize(1.1*size)
    glBegin (GL_POINTS)
    Type point2d
        As Single x,y
        As Ulong col
    End Type
    Dim As Integer flag,codenum=256
    If Instr(text,Chr(10)) Then flag=1
    Static As Long runflag
    Static As point2d infoarray()
    Redim Preserve As point2d infoarray(128,codenum)
    If runflag=0 Then                   
        Dim As Ulong background=0
        Screen 8
        Width 640\8,200\16 
        Dim count As Long
        For ch As Integer=1 To codenum
            Cls
            Draw String(1,1),Chr(ch)
            For x As Long=1 To 8 
                For y As Long=1 To 16
                    If Point(x,y)<>background Then
                        count=count+1
                        infoarray(count,ch)=Type<point2d>(x,y)
                    End If
                Next y
            Next x
            count=0
        Next ch
        runflag=1
    End If
    If size=0 Then Exit Sub
    Dim As point2d temp(1 To 128,codenum),np
    Dim As Single cr= 0.01745329 'degs to radians
    #macro rotate(p1,p2,a,d)
    np.col=p2.col
    np.x=d*(Cos(a*cr)*(p2.x-p1.x)-Sin(a*cr)*(p2.y-p1.y)) +p1.x
    np.y=d*(Sin(a*cr)*(p2.x-p1.x)+Cos(a*cr)*(p2.y-p1.y)) +p1.y
    #endmacro
    
    Dim As point2d cpt(1 To 128),c=Type<point2d>(xpos,ypos),c2
    Dim As Integer dx=xpos,dy=ypos
    For z6 As Integer=1 To Len(text)
        Var asci=text[z6-1]
        If asci=10 Then
            If charangle<>0 Then xpos=xpos+12*Sin(charangle*cr)
            dx=xpos:dy=dy+16:Goto skip 'chr(10) for new line
        End If
        For _x1 As Integer=1 To 128
            temp(_x1,asci).x=infoarray(_x1,asci).x+dx
            temp(_x1,asci).y=infoarray(_x1,asci).y+dy
            temp(_x1,asci).col=colour
            rotate(c,temp(_x1,asci),textangle,size)
            cpt(_x1)=np
            Var copyy=np.y
            If charangle<>0 Then
                Dim As Long p
                If flag Then  p=1 Else  p=(z6-1)
                c2=Type<point2d>(xpos+(size*8)*p*(Cos(textangle*cr)),ypos+(size*8)*p*(Sin(textangle*cr)))
                rotate(c2,cpt(_x1),charangle,1)
                If flag Then np.y=copyy
                cpt(_x1)=np
            End If
            If infoarray(_x1,asci).x<>0 Then
                If Abs(size)>1 Then
                    glVertex3f (cpt(_x1).x,(cpt(_x1).y),0)
                End If
            End If
        Next _x1
        dx=dx+9+4*(Sin(charangle*cr))*flag
        skip:
    Next z6
    glend
    glMatrixMode GL_PROJECTION 'restore 
    glPopMatrix
    glMatrixMode GL_MODELVIEW
    glPopMatrix
End Sub

Sub init Constructor
    drawstring(0,0," ",0,0,0,0,0,0)
End Sub

Function Regulate(Byval MyFps As Long,Byref fps As Long) As Long
    Static As Double timervalue,lastsleeptime,t3,frames
    Dim As Double t=Timer
    frames+=1
    If (t-t3)>=1 Then t3=t:fps=frames:frames=0
    Dim As Long sleeptime=lastsleeptime+((1/myfps)-T+timervalue)*1000
    If sleeptime<1 Then sleeptime=1
    lastsleeptime=sleeptime
    timervalue=T
    Return sleeptime
End Function

Function CheckCollision(ByRef box1 As BoundingBox, ByRef box2 As BoundingBox) As Boolean ' ulong ?
    ' Check if the bounding boxes overlap in the X dimension
    If box1.maxX < box2.minX Or box1.minX > box2.maxX Then
        Return False
    End If

    ' Check if the bounding boxes overlap in the Y dimension
    If box1.maxY < box2.minY Or box1.minY > box2.maxY Then
        Return False
    End If

    ' Check if the bounding boxes overlap in the Z dimension
    If box1.maxZ < box2.minZ Or box1.minZ > box2.maxZ Then
        Return False
    End If

    ' If all dimensions overlap, there is a collision
    Return True
End Function


Screenres 800,600,32,,2 
width 800\8,600\16 'larger dos fonts
glsetup(800,600)
Dim As Single angle
Dim As Long fps
Do
    'angle+=1
    ' Update cube1 position based on arrow keys
    If Multikey(200) Then cube1Pos += 0.1 ' Left arrow
    If Multikey(208) Then cube1Pos -= 0.1 ' Right arrow
    If Multikey(203) Then cube1Pos += 0.1 ' Up arrow
    If Multikey(205) Then cube1Pos -= 0.1 ' Down arrow

    ' Update cube2 position based on arrow keys
    If Multikey(200) Then cube2Pos += 0.1 ' Left arrow
    If Multikey(208) Then cube2Pos -= 0.1 ' Right arrow
    If Multikey(203) Then cube2Pos += 0.1 ' Up arrow
    If Multikey(205) Then cube2Pos -= 0.1 ' Down arrow

    ' Update bounding boxes
    cube1.minX = cube1Pos - 1
    cube1.maxX = cube1Pos + 1
    cube1.minY = -1
    cube1.maxY = 1
    cube1.minZ = -7 - 1
    cube1.maxZ = -7 + 1

    cube2.minX = cube2Pos - 1
    cube2.maxX = cube2Pos + 1
    cube2.minY = -1
    cube2.maxY = 1
    cube2.minZ = -7 - 1
    cube2.maxZ = -7 + 1    

    ' Clear the screen
    glClear(GL_COLOR_BUFFER_BIT)

    ' Draw the cubes
    glEnable(GL_CULL_FACE)
    DrawGlCube(angle, cube1Pos)    
    DrawGlCube2(angle, cube2Pos)
    glDisable(GL_CULL_FACE)

    ' Draw strings
 Draw String(5,5),"This is fb draw string, line 3 is set  screencontrol fb.SET_GL_2D_MODE ,fb.OGL_2D_MANUAL_SYNC",rgb(255,255,255)
 '   DrawString(5, 5), "This is fb draw string, line 3 is set screencontrol fb.SET_GL_2D_MODE ,fb.OGL_2D_MANUAL_SYNC", rgb(255, 255, 255)
    drawstring(20, 20, "Test font with opengl", Rgb(200, 100, 0), 3, 0, 0, 800, 600)
    drawstring(550, 80, "fps = " & fps, Rgb(0, 100, 200), 2, 10, 0, 800, 600)
    drawstring(200, 100, "Free" + Chr(10) + "BASIC (rotated 90 degrees)", Rgb(200, 0, 0), 2, 90, 0, 800, 600)
    drawstring(200, 560, "Press esc to end", Rgb(0, 200, 0), 2, 0, 10, 800, 600)

    ' Check for collision ' 
    If CheckCollision(cube1, cube2) Then
        drawstring(400, 300, "Collision!", Rgb(255, 0, 0), 2, 0, 0, 800, 600)
    End If

    glColor3ub 255, 255, 255  ' Reset the OpenGL color
    Flip
    Sleep Regulate(35, fps), 1
Loop Until Multikey(1) ' Esc key
Sleep
' ends
I am getting No reaction from multikey ()
hhr
Posts: 237
Joined: Nov 29, 2019 10:41

Re: Cube OpenGL collision qt

Post by hhr »

Try this:

Code: Select all

    ' Update cube1 position based on arrow keys
    If Multikey(FB.SC_LEFT) Then cube1Pos += 0.1 ' Left arrow
    If Multikey(FB.SC_RIGHT) Then cube1Pos -= 0.1 ' Right arrow
    If Multikey(FB.SC_UP) Then cube1Pos += 0.1 ' Up arrow
    If Multikey(FB.SC_DOWN) Then cube1Pos -= 0.1 ' Down arrow

    ' Update cube2 position based on arrow keys
    If Multikey(FB.SC_LEFT) Then cube2Pos += 0.1 ' Left arrow
    If Multikey(FB.SC_RIGHT) Then cube2Pos -= 0.1 ' Right arrow
    If Multikey(FB.SC_UP) Then cube2Pos += 0.1 ' Up arrow
    If Multikey(FB.SC_DOWN) Then cube2Pos -= 0.1 ' Down arrow
You can test Multikey with:

Code: Select all

dim as ubyte i
do
   if multikey(i) then print i, hex(i)
   i += 1
loop
Löwenherz
Posts: 145
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Cube OpenGL collision qt

Post by Löwenherz »

Thanks for Help hhr..

The collision already Works.. but the Up down arrows doesn't Work AS aspected.. they are using Same behaviour Like left right arrows..

And i must declare what Cube1 or cube2 is selected to move otherwise both cubes are moving for example in both left or right directions ..

Code: Select all

Do
...
    ' Update cube1 position based on arrow keys
    ' ok for left and right arrow
    '
    If Multikey(FB.SC_RIGHT) Then cube1Pos += 0.1 ' right arrow
    If Multikey(FB.SC_LEFT) Then cube1Pos -= 0.1 ' Left arrow
    
    ' up and down key doesn't work --> uses like movement left and right behaviour
    
    'FB.SC_UP ' FB.SC_DOWN
    If Multikey(FB.SC_UP) Then cube1Pos += 0.1 ' Up arrow 
    If Multikey(FB.SC_DOWN) Then cube1Pos -= 0.1 ' Down arrow 

' Update bounding boxes
    cube1.minX = cube1Pos - 1
    cube1.maxX = cube1Pos + 1
    cube1.minY = -1
    cube1.maxY = 1
    cube1.minZ = -7 - 1
    cube1.maxZ = -7 + 1

' how to pick cube2 as separately cube?

' Update cube2 position based on arrow keys
    '' If Multikey(FB.SC_RIGHT) Then cube2Pos += 0.1 ' right arrow
    '' If Multikey(FB.SC_LEFT) Then cube2Pos -= 0.1 ' left arrow
    '' If Multikey(FB.SC_UP) Then cube2Pos += 0.1 ' Up arrow
    '' If Multikey(FB.SC_DOWN) Then cube2Pos -= 0.1 ' Down arrow

    cube2.minX = cube2Pos - 1
    cube2.maxX = cube2Pos + 1
    cube2.minY = -1
    cube2.maxY = 1
    cube2.minZ = -7 - 1
    cube2.maxZ = -7 + 1    
....

Loop Until Multikey(1) ' Esc key
Sleep
hhr
Posts: 237
Joined: Nov 29, 2019 10:41

Re: Cube OpenGL collision qt

Post by hhr »

You must define cube1Pos and cube2Pos for horizontal and vertical.
e.g. cube1PosX, cube1PosY, cube2PosX, cube2PosY.

For example:

Code: Select all

If Multikey(FB.SC_1) And Multikey(FB.SC_RIGHT) Then cube1PosX += 0.1 ' Key "1" and right arrow: Cube1
If Multikey(FB.SC_2) And Multikey(FB.SC_RIGHT) Then cube2PosX += 0.1 ' Key "2" and right arrow: Cube2
Look at Multikey and DOS-Keyboard-Scancodes in the Documentation.
Löwenherz
Posts: 145
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Cube OpenGL collision qt

Post by Löwenherz »

Thanks hhr again..

I have Got it I am so Glad :-)
If you are starting the Scene both cubes are laying upon each other.. then you can use left right Up down Key to move Second Cube and collision Font Text appears If there IS a collision

Code: Select all

#include "gl/glu.bi"
#include "fbgfx.bi"

' Screencontrol fb.SET_GL_2D_MODE ,fb.OGL_2D_MANUAL_SYNC
' doesnt work here, dont know why or if I need it really?

' demo part three, loewenherz, 05+06-09-2024, 15:10 mez
' collision works, up, down, left, right arrows works too :-)
' thanks for help hhr
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' if you are starting the scene both cubes are laying upon each other
' use left, right, up or down arrow to move cube and see when collision
' is detected, font output appears "collision"
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Type BoundingBox
    minX As Single
    maxX As Single
    minY As Single
    maxY As Single
    minZ As Single
    maxZ As Single
End Type

Dim cube1 As BoundingBox
Dim cube2 As BoundingBox

Dim cube1Pos As Single = 2
Dim cube2Pos As Single = -2

Dim cube1PosX As Single = 2
Dim cube1PosY As Single = -2

Dim cube2PosX As Single = 2
Dim cube2PosY As Single = -2

Sub glsetup(xres As Long, yres As Long)
    glShadeModel(GL_SMOOTH)                 ' Enables Smooth Color Shading
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
    glBlendFunc(GL_ONE, GL_ONE)
    glEnable GL_ALPHA
    glEnable GL_BLEND
    glViewport(0, 0, xres, yres)       ' Set the viewport
    glMatrixMode(GL_PROJECTION)        ' Change Matrix Mode to Projection
    glLoadIdentity                     ' Reset View
    gluPerspective(45.0, xres / yres, 1.0, 100.0)
    glMatrixMode(GL_MODELVIEW)         ' Return to the modelview matrix
    glLoadIdentity                     '  Reset View
    glClearColor .2, .2, .2, 1 'optional
End Sub

' Rotate and draw the cube with texturing on each face
Sub DrawGlCube(ByRef rotangle As Single, ByVal posX As Single, ByVal posY As Single)
    glLoadIdentity()
    glTranslatef(posX, posY, -7)
    glRotatef(rotangle, 1, .5, .25)           ' Rotate
    glBegin(GL_QUADS)

    glColor3ub 255, 0, 0
    glVertex3f( 1.0, 1.0, -1.0)            ' Top right of the quad (top)
    glVertex3f(-1.0, 1.0, -1.0)            ' Top left of the quad (top)
    glVertex3f(-1.0, 1.0, 1.0)            ' Bottom left of the quad (top)
    glVertex3f( 1.0, 1.0, 1.0)            ' Bottom right of the quad (top)

    glColor3ub 255, 100, 0
    glVertex3f( 1.0, -1.0, 1.0)            ' Top right of the quad (bottom)
    glVertex3f(-1.0, -1.0, 1.0)            ' Top left of the quad (bottom)
    glVertex3f(-1.0, -1.0, -1.0)            ' Bottom left of the quad (bottom)
    glVertex3f( 1.0, -1.0, -1.0)            ' Bottom right of the quad (bottom)

    glColor3ub 255, 0, 255
    glVertex3f( 1.0, 1.0, 1.0)            ' Top right of the quad (front)
    glVertex3f(-1.0, 1.0, 1.0)            ' Top left of the quad (front)
    glVertex3f(-1.0, -1.0, 1.0)            ' Bottom left of the quad (front)
    glVertex3f( 1.0, -1.0, 1.0)            ' Bottom right of the quad (front)

    glColor3ub 0, 0, 200
    glVertex3f( 1.0, -1.0, -1.0)            ' Bottom left of the quad (back)
    glVertex3f(-1.0, -1.0, -1.0)            ' Bottom right of the quad (back)
    glVertex3f(-1.0, 1.0, -1.0)            ' Top right of the quad (back)
    glVertex3f( 1.0, 1.0, -1.0)            ' Top left of the quad (back)

    glColor3ub 0, 255, 0
    glVertex3f(-1.0, 1.0, 1.0)            ' Top right of the quad (left)
    glVertex3f(-1.0, 1.0, -1.0)            ' Top left of the quad (left)
    glVertex3f(-1.0, -1.0, -1.0)            ' Bottom left of the quad (left)
    glVertex3f(-1.0, -1.0, 1.0)            ' Bottom right of the quad (left)

    glColor3ub 255, 0, 100
    glVertex3f( 1.0, 1.0, -1.0)            ' Top right of the quad (right)
    glVertex3f( 1.0, 1.0, 1.0)            ' Top left of the quad (right)
    glVertex3f( 1.0, -1.0, 1.0)            ' Bottom left of the quad (right)
    glVertex3f( 1.0, -1.0, -1.0)
    glEnd
End Sub

Sub DrawGlCube2(ByRef rotangle As Single, ByVal posX As Single, ByVal posY As Single)
    glLoadIdentity()
    glTranslatef(posX, posY, -7)
    glRotatef(rotangle, 1, .5, .25)           ' Rotate
    glBegin(GL_QUADS)

    glColor3ub 255, 0, 0
    glVertex3f( 1.0, 1.0, -1.0)            ' Top right of the quad (top)
    glVertex3f(-1.0, 1.0, -1.0)            ' Top left of the quad (top)
    glVertex3f(-1.0, 1.0, 1.0)            ' Bottom left of the quad (top)
    glVertex3f( 1.0, 1.0, 1.0)            ' Bottom right of the quad (top)

    glColor3ub 255, 100, 0
    glVertex3f( 1.0, -1.0, 1.0)            ' Top right of the quad (bottom)
    glVertex3f(-1.0, -1.0, 1.0)            ' Top left of the quad (bottom)
    glVertex3f(-1.0, -1.0, -1.0)            ' Bottom left of the quad (bottom)
    glVertex3f( 1.0, -1.0, -1.0)            ' Bottom right of the quad (bottom)

    glColor3ub 255, 0, 255
    glVertex3f( 1.0, 1.0, 1.0)            ' Top right of the quad (front)
    glVertex3f(-1.0, 1.0, 1.0)            ' Top left of the quad (front)
    glVertex3f(-1.0, -1.0, 1.0)            ' Bottom left of the quad (front)
    glVertex3f( 1.0, -1.0, 1.0)            ' Bottom right of the quad (front)

    glColor3ub 0, 0, 200
    glVertex3f( 1.0, -1.0, -1.0)            ' Bottom left of the quad (back)
    glVertex3f(-1.0, -1.0, -1.0)            ' Bottom right of the quad (back)
    glVertex3f(-1.0, 1.0, -1.0)            ' Top right of the quad (back)
    glVertex3f( 1.0, 1.0, -1.0)            ' Top left of the quad (back)

    glColor3ub 0, 255, 0
    glVertex3f(-1.0, 1.0, 1.0)            ' Top right of the quad (left)
    glVertex3f(-1.0, 1.0, -1.0)            ' Top left of the quad (left)
    glVertex3f(-1.0, -1.0, -1.0)            ' Bottom left of the quad (left)
    glVertex3f(-1.0, -1.0, 1.0)            ' Bottom right of the quad (left)

    glColor3ub 255, 0, 100
    glVertex3f( 1.0, 1.0, -1.0)            ' Top right of the quad (right)
    glVertex3f( 1.0, 1.0, 1.0)            ' Top left of the quad (right)
    glVertex3f( 1.0, -1.0, 1.0)            ' Bottom left of the quad (right)
    glVertex3f( 1.0, -1.0, -1.0)
    glEnd
End Sub


Sub drawstring(xpos As Integer,ypos As Integer,text As String,colour As Ulong,size As Single,textangle As Single=0,charangle As Single=0,xres As Long=0,yres As Long=0)
    glMatrixMode GL_PROJECTION 'save projection
    glPushMatrix
    glMatrixMode GL_MODELVIEW
    glPushMatrix
    
    glMatrixMode GL_PROJECTION 'make ortho
    glLoadIdentity
    glOrtho 0, xres, yres, 0,-1, 1
    glMatrixMode GL_MODELVIEW
    glLoadIdentity
    #define Red(c) ((c) Shr 16 And 255)
    #define Green(c) ((c) Shr  8 And 255)
    #define Blue(c) ((c) And 255)
    #define Alph(c) ((c) Shr 24)
    glColor4ub Red(colour),Green(colour),Blue(colour),alph(colour)
    glend
    glpointsize(1.1*size)
    glBegin (GL_POINTS)
    Type point2d
        As Single x,y
        As Ulong col
    End Type
    Dim As Integer flag,codenum=256
    If Instr(text,Chr(10)) Then flag=1
    Static As Long runflag
    Static As point2d infoarray()
    Redim Preserve As point2d infoarray(128,codenum)
    If runflag=0 Then                   
        Dim As Ulong background=0
        Screen 8
        Width 640\8,200\16 
        Dim count As Long
        For ch As Integer=1 To codenum
            Cls
            Draw String(1,1),Chr(ch)
            For x As Long=1 To 8 
                For y As Long=1 To 16
                    If Point(x,y)<>background Then
                        count=count+1
                        infoarray(count,ch)=Type<point2d>(x,y)
                    End If
                Next y
            Next x
            count=0
        Next ch
        runflag=1
    End If
    If size=0 Then Exit Sub
    Dim As point2d temp(1 To 128,codenum),np
    Dim As Single cr= 0.01745329 'degs to radians
    #macro rotate(p1,p2,a,d)
    np.col=p2.col
    np.x=d*(Cos(a*cr)*(p2.x-p1.x)-Sin(a*cr)*(p2.y-p1.y)) +p1.x
    np.y=d*(Sin(a*cr)*(p2.x-p1.x)+Cos(a*cr)*(p2.y-p1.y)) +p1.y
    #endmacro
    
    Dim As point2d cpt(1 To 128),c=Type<point2d>(xpos,ypos),c2
    Dim As Integer dx=xpos,dy=ypos
    For z6 As Integer=1 To Len(text)
        Var asci=text[z6-1]
        If asci=10 Then
            If charangle<>0 Then xpos=xpos+12*Sin(charangle*cr)
            dx=xpos:dy=dy+16:Goto skip 'chr(10) for new line
        End If
        For _x1 As Integer=1 To 128
            temp(_x1,asci).x=infoarray(_x1,asci).x+dx
            temp(_x1,asci).y=infoarray(_x1,asci).y+dy
            temp(_x1,asci).col=colour
            rotate(c,temp(_x1,asci),textangle,size)
            cpt(_x1)=np
            Var copyy=np.y
            If charangle<>0 Then
                Dim As Long p
                If flag Then  p=1 Else  p=(z6-1)
                c2=Type<point2d>(xpos+(size*8)*p*(Cos(textangle*cr)),ypos+(size*8)*p*(Sin(textangle*cr)))
                rotate(c2,cpt(_x1),charangle,1)
                If flag Then np.y=copyy
                cpt(_x1)=np
            End If
            If infoarray(_x1,asci).x<>0 Then
                If Abs(size)>1 Then
                    glVertex3f (cpt(_x1).x,(cpt(_x1).y),0)
                End If
            End If
        Next _x1
        dx=dx+9+4*(Sin(charangle*cr))*flag
        skip:
    Next z6
    glend
    glMatrixMode GL_PROJECTION 'restore 
    glPopMatrix
    glMatrixMode GL_MODELVIEW
    glPopMatrix
End Sub

Sub init Constructor
    drawstring(0,0," ",0,0,0,0,0,0)
End Sub

Function Regulate(Byval MyFps As Long,Byref fps As Long) As Long
    Static As Double timervalue,lastsleeptime,t3,frames
    Dim As Double t=Timer
    frames+=1
    If (t-t3)>=1 Then t3=t:fps=frames:frames=0
    Dim As Long sleeptime=lastsleeptime+((1/myfps)-T+timervalue)*1000
    If sleeptime<1 Then sleeptime=1
    lastsleeptime=sleeptime
    timervalue=T
    Return sleeptime
End Function

Function CheckCollision(ByRef box1 As BoundingBox, ByRef box2 As BoundingBox) As Boolean
    ' Check if the bounding boxes overlap in the X dimension
    If box1.maxX < box2.minX Or box1.minX > box2.maxX Then
        Return False
    End If

    ' Check if the bounding boxes overlap in the Y dimension
    If box1.maxY < box2.minY Or box1.minY > box2.maxY Then
        Return False
    End If

    ' Check if the bounding boxes overlap in the Z dimension
    If box1.maxZ < box2.minZ Or box1.minZ > box2.maxZ Then
        Return False
    End If

    ' If all dimensions overlap, there is a collision
    Return True
End Function

Screenres 800, 600, 32, , 2
Width 800 \ 8, 600 \ 16 'larger dos fonts
glsetup(800, 600)
Dim As Single angle
Dim As Long fps
Do
    ' Update cube1 position based on arrow keys
    If Multikey(FB.SC_RIGHT) Then cube1PosX += 0.1 ' right arrow
    If Multikey(FB.SC_LEFT) Then cube1PosX -= 0.1 ' left arrow
    If Multikey(FB.SC_UP) Then cube1PosY += 0.1 ' up arrow
    If Multikey(FB.SC_DOWN) Then cube1PosY -= 0.1 ' down arrow

    ' Update bounding boxes
    cube1.minX = cube1PosX - 1
    cube1.maxX = cube1PosX + 1
    cube1.minY = cube1PosY - 1
    cube1.maxY = cube1PosY + 1
    cube1.minZ = -7 - 1
    cube1.maxZ = -7 + 1

    ' Update cube2 position based on arrow keys
    ' If Multikey(FB.SC_RIGHT) Then cube2Pos += 0.1 ' right arrow
    ' If Multikey(FB.SC_LEFT) Then cube2Pos -= 0.1 ' left arrow
    ' If Multikey(FB.SC_UP) Then cube2Pos += 0.1 ' Up arrow
    ' If Multikey(FB.SC_DOWN) Then cube2Pos -= 0.1 ' Down arrow

    cube2.minX = cube2PosX - 1
    cube2.maxX = cube2PosX + 1
    cube2.minY = cube2PosY - 1
    cube2.maxY = cube2PosY + 1
    cube2.minZ = -7 - 1
    cube2.maxZ = -7 + 1

    ' Clear the screen
    glClear(GL_COLOR_BUFFER_BIT)

    ' Draw the cubes
    glEnable(GL_CULL_FACE)
    DrawGlCube(angle, cube1PosX, cube1PosY)
    DrawGlCube2(angle, cube2PosX, cube2PosY)
    glDisable(GL_CULL_FACE)

    ' Draw strings
    Draw String(5,5),"This is fb draw string, line 3 is set  screencontrol fb.SET_GL_2D_MODE ,fb.OGL_2D_MANUAL_SYNC",rgb(255,255,255)
    DrawString(20, 20, "Test font with opengl", Rgb(200, 100, 0), 3, 0, 0, 800, 600)
    DrawString(550, 80, "fps = " & fps, Rgb(0, 100, 200), 2, 10, 0, 800, 600)
    DrawString(200, 100, "Free" + Chr(10) + "BASIC (rotated 90 degrees)", Rgb(200, 0, 0), 2, 90, 0, 800, 600)
    DrawString(200, 560, "Press esc to end", Rgb(0, 200, 0), 2, 0, 10, 800, 600)

    ' Check for collision
    If CheckCollision(cube1, cube2) Then
        DrawString(300, 100, "Collision!", Rgb(255, 0, 0), 2, 0, 0, 800, 600)
    End If

    glColor3ub 255, 255, 255  ' Reset the OpenGL color
    Flip
    Sleep Regulate(35, fps), 1
Loop Until Multikey(1) ' Esc key
Sleep
' ends
hhr
Posts: 237
Joined: Nov 29, 2019 10:41

Re: Cube OpenGL collision qt

Post by hhr »

I tried this. You have to press "1" or "2" in addition:

Code: Select all

   ' Update cube1 position based on arrow keys
   If Multikey(FB.SC_1) And Multikey(FB.SC_RIGHT) Then cube1PosX += 0.1 ' Key "1" and right arrow
   If Multikey(FB.SC_1) And Multikey(FB.SC_LEFT) Then cube1PosX -= 0.1  ' Key "1" and left arrow
   If Multikey(FB.SC_1) And Multikey(FB.SC_UP) Then cube1PosY += 0.1    ' Key "1" and up arrow
   If Multikey(FB.SC_1) And Multikey(FB.SC_DOWN) Then cube1PosY -= 0.1  ' Key "1" and down arrow
   
   ' Update cube2 position based on arrow keys
   If Multikey(FB.SC_2) Then                         ' Key "2"
      If Multikey(FB.SC_RIGHT) Then cube2PosX += 0.1 ' right arrow
      If Multikey(FB.SC_LEFT) Then cube2PosX -= 0.1  ' left arrow
      If Multikey(FB.SC_UP) Then cube2PosY += 0.1    ' up arrow
      If Multikey(FB.SC_DOWN) Then cube2PosY -= 0.1  ' down arrow
   End If
Löwenherz
Posts: 145
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Cube OpenGL collision qt

Post by Löwenherz »

Hello again..
' Update cube1 position based on arrow keys
If Multikey(FB.SC_1) And Multikey(FB.SC_RIGHT) Then cube1PosX += 0.1
@hhr: Works but If you are using instead of "and" Here "or"..

Need a little Help.. want to include a Star Objekt to include in my OpenGL Scene by pushing space bar..

Code: Select all

'
' code snippet
'
sub draw_star(x as double,y as double,z as double,s as double)
  glLoadIdentity()
  'glTranslatef(posX, posY, -7)
 
  glTranslatef(x,y,-7,s) ' doesnt work here because of four parameter  

  glbegin GL_TRIANGLES
  'glcolor4f starcolour
  glVertex3f x, y + (0.5 * s), z
  glVertex3f x - (0.5 * s), y - (0.5 * s), z - (0.5 * s)
  glVertex3f x + (0.5 * s), y - (0.5 * s), z - (0.5 * s)
  
  glVertex3f x, y + (0.5 * s), z
  glVertex3f x - (0.5 * s), y - (0.5 * s), z - (0.5 * s)
  glVertex3f x, y - (0.5 * s), z + (0.5 * s)
  
  glVertex3f x, y + (0.5 * s), z
  glVertex3f x + (0.5 * s), y - (0.5 * s), z - (0.5 * s)
  glVertex3f x, y - (0.5 * s), z + (0.5 * s)
  
  glVertex3f x - (0.5 * s), y - (0.5 * s), z - (0.5 * s)
  glVertex3f x + (0.5 * s), y - (0.5 * s), z - (0.5 * s)
  glVertex3f x, y - (0.5 * s), z + (0.5 * s)
  glend
end sub


' Update cube1 position based on arrow keys ' GO if it's "or" not "and"
    If Multikey(FB.SC_1) OR MultiKey(FB.SC_RIGHT) Then cube1PosX += 0.1 ' right arrow 'GO
    '...
    ' want to shoot the star with pushing space bar
    If Multikey(FB.SC_SPACE) Then draw_star(0.5,0.5,0.5,0.5)
paul doe
Moderator
Posts: 1793
Joined: Jul 25, 2017 17:22
Location: Argentina
Contact:

Re: Cube OpenGL collision qt

Post by paul doe »

@Löwenherz: I moved this topic to 'Beginners', as this is not Windows-specific (it works perfectly fine on Linux as well)
dodicat
Posts: 8136
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Cube OpenGL collision qt

Post by dodicat »

Here are cube collisions for fb.
Theory
Never mind cross products or projections.
Get the maximum and minimum positions of the corners of a cube.(.x, .y, .z) values as it turns and translates.
if a point lies within these co-ordinates it is inside a cube, otherwise NO.
Hover the mouse over the cube on the right and move it, only this cube is moveable.
Use the mouse wheel to move the cube towards or away.
If you press the left button then movement is de- activated while it is pressed.

Code: Select all



Type v3
    As Double x,y,z
End Type

'standard opengl type cube faces used as the base cube
Dim Shared As V3 basecube(1 To 6,1 To 4)= _
{{(-1,-1,-1),(1,-1,-1),(1,1,-1),(-1,1,-1)},_'front
{(1,-1,-1),(1,-1,1),(1,1,1),(1,1,-1)},_ 'right
{(-1,-1,1),(1,-1,1),(1,1,1),(-1,1,1)},_'back
{(-1,-1,-1),(-1,-1,1),(-1,1,1),(-1,1,-1)},_'left
{(1,1,-1),(1,1,1),(-1,1,1),(-1,1,-1)},_'top
{(1,-1,-1),(1,-1,1),(-1,-1,1),(-1,-1,-1)}}'base

Type cube
    As v3 p(1 To 6,0 To 4)
    As v3 v1,v2 'ends of cube diagonals
    As v3 centre 
    As Long painter(1 To 6)
    Declare Constructor(() As v3)
    As Ulong col(1 To 6)'colour each of six faces
    As boolean active
    size As Double
End Type

Type mouse
    As Long x,y,w,b
End Type


Constructor cube(a() As v3)
For r As Long=1 To 6
    For c As Long=1 To 4
        p(r,c)=a(r,c)
    Next
Next
'two corner diagonals
v1=p(1,1)
v2=p(2,3)
centre=Type<v3>((v1.x+v2.x)/2,(v1.y+v2.y)/2,(v1.z+v2.z)/2)
End Constructor

Sub CubeSort(c() As cube)
    For n As Long=Lbound(c) To Ubound(c)-1
        For m As Long=n+1 To Ubound(c)
            If c(n).centre.z<c(m).centre.z Then 
                Swap c(n),c(m)
            End If
        Next
    Next
End Sub

Function Expand(p() As V3,b As Single,shift As V3,i As Integer) As cube
    For n As Integer=Lbound(p,2) To Ubound(p,2)
        p(i,n).x=b*basecube(i,n).x+shift.x
        p(i,n).y=b*basecube(i,n).y+shift.y
        p(i,n).z=b*basecube(i,n).z+shift.z
    Next n
    Return cube(p())
End Function

Function createcube(size As Double,centre As v3)As cube
    Dim As v3 a(1 To 6,1 To 4)
    For i As Integer=Lbound(basecube,1) To Ubound(basecube,1)
        Expand (a(),size,centre,i)
    Next i
    Var z=cube(a())
    z.size=size
   ' z.centre=centre 'not required here
    For n As Long=1 To 6
        z.col(n)=Rgb(Rnd*255,Rnd*255,Rnd*255)
    Next n
    Return z
End Function

Function Rotate(c As V3,p As V3,angle As V3,scale As V3=Type<V3>(1,1,1)) As V3
    Dim As Single sx=Sin(angle.x),sy=Sin(angle.y),sz=Sin(angle.z)
    Dim As Single cx=Cos(angle.x),cy=Cos(angle.y),cz=Cos(angle.z)
    Dim As Single dx=p.x-c.x,dy=p.y-c.y,dz=p.z-c.z
    Return Type<V3>((scale.x)*((cy*cz)*dx+(-cx*sz+sx*sy*cz)*dy+(sx*sz+cx*sy*cz)*dz)+c.x,_
    (scale.y)*((cy*sz)*dx+(cx*cz+sx*sy*sz)*dy+(-sx*cz+cx*sy*sz)*dz)+c.y,_
    (scale.z)*((-sy)*dx+(sx*cy)*dy+(cx*cy)*dz)+c.z)
End Function

Function perspective(p As V3,eyepoint As V3) As V3
    Dim As Double   w=1+(p.z/eyepoint.z)
    Return Type<V3>((p.x-eyepoint.x)/w+eyepoint.x,_
    (p.y-eyepoint.y)/w+eyepoint.y,_
    (p.z-eyepoint.z)/w+eyepoint.z)
End Function 

Function rotatecube(g1 As cube,angle As v3) As cube
    Dim As v3 fulcrum=Type<v3>((g1.v1.x+g1.v2.x)/2,(g1.v1.y+g1.v2.y)/2,(g1.v1.z+g1.v2.z)/2)
    Dim As cube tmp1=g1
    tmp1.centre=fulcrum
    Dim As v3 eye=Type(tmp1.centre.x,tmp1.centre.y,1000)
    Dim As Double cx,cy,cz
    For m As Integer=1 To 6
        cx=0:cy=0:cz=0
        For n As Integer=1 To 4
            tmp1.p(m,n)=Rotate(fulcrum,g1.p(m,n),angle)
            tmp1.p(m,n)=perspective(tmp1.p(m,n),eye)  'apply the eye (perspective)
            'accumulate cx,cy,cz
            cx+=tmp1.p(m,n).x:cy+=tmp1.p(m,n).y:cz+=tmp1.p(m,n).z
        Next n
        cx=cx/4:cy=cy/4:cz=cz/4
        'get face centroid into zero'th index of 2nd. dimension
        tmp1.p(m,0)=Type<v3>(cx,cy,cz)
    Next m
    'rotate the diagonal ends also
    tmp1.v1=Rotate(fulcrum,g1.v1,angle)
    tmp1.v2=Rotate(fulcrum,g1.v2,angle)
   
    Return tmp1
End Function

Function pointincube(p As v3, c As cube) As Long
    Dim As Double maxx=-1e6,minx=1e6,maxy=-1e6,miny=1e6,maxz=-1e6,minz=1e6
    
    For n As Long =1 To 6
        For m As Long=1 To 4
            If minx>c.p(n,m).x Then minx=c.p(n,m).x
            If maxx<c.p(n,m).x Then maxx=c.p(n,m).x
            
            If miny>c.p(n,m).y Then miny=c.p(n,m).y
            If maxy<c.p(n,m).y Then maxy=c.p(n,m).y
            
            If minz>c.p(n,m).z Then minz=c.p(n,m).z
            If maxz<c.p(n,m).z Then maxz=c.p(n,m).z
        Next m
    Next n
    If p.x<minx Or p.x>maxx Then Return 0
    If p.y<miny Or p.y>maxy Then Return 0
    If p.z<minz Or p.z>maxz Then Return 0
    Return 1
End Function

Function cubeintersect(c1 As cube,c2 As cube) As boolean
    Dim As Long n,m
    For n=1 To 6
        For m=1 To 4
            If pointincube(c1.p(n,m),c2)=1 Then Return 1
        Next m
    Next n
    For n=1 To 6
        For m=1 To 4
            If pointincube(c2.p(n,m),c1)=1 Then Return 1
        Next m
    Next n
    Return false 
End Function

Sub fill(p() As v3,c As Ulong,im As Any Ptr=0)
    #define ub Ubound
    Dim As Long Sy=1e6,By=-1e6,i,j,y,k
    Dim As Single a(Ub(p)+1,1),dx,dy
    For i =0 To Ub(p)
        a(i,0)=p(i).x
        a(i,1)=p(i).y
        If Sy>p(i).y Then Sy=p(i).y
        If By<p(i).y Then By=p(i).y
    Next i
    Dim As Single xi(Ub(a,1)),S(Ub(a,1))
    a(Ub(a,1),0) = a(0,0)
    a(Ub(a,1),1) = a(0,1)
    For i=0 To Ub(a,1)-1
        dy=a(i+1,1)-a(i,1)
        dx=a(i+1,0)-a(i,0)
        If dy=0 Then S(i)=1
        If dx=0 Then S(i)=0
        If dy<>0 Andalso dx<>0 Then S(i)=dx/dy
    Next i
    For y=Sy-1 To By+1
        k=0
        For i=0 To Ub(a,1)-1
            If (a(i,1)<=y Andalso a(i+1,1)>y) Orelse _
            (a(i,1)>y Andalso a(i+1,1)<=y) Then
            xi(k)=(a(i,0)+S(i)*(y-a(i,1)))
            k+=1
        End If
    Next i
    For j=0 To k-2
        For i=0 To k-2
            If xi(i)>xi(i+1) Then Swap xi(i),xi(i+1)
        Next i
    Next j
    For i = 0 To k - 2 Step 2
        Line im,(xi(i)+0,y)-(xi(i+1)+1-0,y),c
    Next i
Next y
End Sub

Sub DrawCubeFace(c1 As cube,c As cube,i As Integer,m As mouse,colour As Ulong) 
    Dim As Double dx,dy,dz
    Static As v3 p0(3)
    
    If c.active =true Then
        c.active=false
        dx=m.x-c.centre.x
        dy=m.y-c.centre.y
        dz=5*m.w-c.centre.z
        'apply the (mouse - cube) vector
        For n As Long=1 To 6
            For m As Long=1 To 4
                c1.p(n,m).x+=dx
                c1.p(n,m).y+=dy
                c1.p(n,m).z+=dz
            Next
        Next
        'adjust the diagonal ends
        c1.v1.x+=dx
        c1.v1.y+=dy
        c1.v1.z+=dz
        c1.v2.x+=dx
        c1.v2.y+=dy
        c1.v2.z+=dz
       c1.centre=Type<v3>((c1.v1.x+c1.v2.x)/2,(c1.v1.y+c1.v2.y)/2,(c1.v1.z+c1.v2.z)/2)
    End If
    
    For n As Long=1 To 4 'p0 is zero based array, so fill it correctly
        p0(n-1).x=c.p(i,n).x
        p0(n-1).y=c.p(i,n).y
        p0(n-1).z=c.p(i,n).z
    Next
    fill(p0(),colour)'colour each face
End Sub

Sub FaceSort(array As cube,painter() As Long)
    For p1 As Integer  = 1 To 5
        For p2 As Integer  = p1 + 1 To 6
            If array.p(p1,0).z<array.p(p2,0).z Then Swap painter(p1),painter(p2):Swap array.p(p1,0),array.p(p2,0)
        Next p2
    Next p1
End Sub

Function Regulate(Byval MyFps As Long,Byref fps As Long) As Long
    Static As Double timervalue,lastsleeptime,t3,frames
    Var t=Timer
    frames+=1
    If (t-t3)>=1 Then t3=t:fps=frames:frames=0
    Var sleeptime=lastsleeptime+((1/myfps)-T+timervalue)*1000
    If sleeptime<1 Then sleeptime=1
    lastsleeptime=sleeptime
    timervalue=T
    Return sleeptime
End Function

Sub drawfaces(c1 As cube,tmp As cube,painter() As  Long,m As mouse,x() As Ulong)
    Dim As Ulong colour
    For z As Integer=Lbound(tmp.p,1)+3 To Ubound(tmp.p,1)'Paint only the closest three faces of each
        Var p=painter(z)
        colour=x(p)
        Select Case p
        Case 1: DrawCubeFace(c1,tmp,p,m,colour)
        Case 2: DrawCubeFace(c1,tmp,p,m,colour)
        Case 3: DrawCubeFace(c1,tmp,p,m,colour)
        Case 4: DrawCubeFace(c1,tmp,p,m,colour)
        Case 5: DrawCubeFace(c1,tmp,p,m,colour)
        Case 6: DrawCubeFace(c1,tmp,p,m,colour)
        End Select
    Next z
End Sub

Dim As cube c(1 To 2)=Any
Dim As cube tmp(1 To 2)=Any
Randomize 1
c(1)=createcube(100,Type<v3>(300,300,10))
c(2)=createcube(100,Type<v3>(650,300,0))

'start setting face painting order to default
For n As Long=1 To 2
    For m As Long=1 To 6
        c(n).painter(m)=m
    Next m
Next n

Dim As Long fps
Dim As Double a
Dim As mouse m
Dim As v3 angle(1 To 2)

#define incircle(cx,cy,radius,x,y) (cx-x)*(cx-x) +(cy-y)*(cy-y)<= radius*radius
Screen 20,32
Do
    Getmouse(m.x,m.y,m.w,m.b)
    a+=.01
    angle(1)=Type<v3>(a/2,1.5*a,-2*a)
    angle(2)=Type<v3>(a/3,a,2.2*a)
    
    tmp(1)= rotatecube(c(1),angle(1))
    tmp(2)= rotatecube(c(2),angle(2))
    If incircle(tmp(2).centre.x,tmp(2).centre.y,100,m.x,m.y)  Then tmp(2).active=true 
    If m.b=1 Then tmp(2).active=false
    
    'reset face painting order
    For n As Long=1 To 2
        For m As Long=1 To 6
            tmp(n).painter(m)=m
        Next m
    Next n
    Screenlock
    Cls
    
    Draw String(10,30),"Frame Rate = " & fps
    Locate 9,3
    Draw String(10,50),"cube 1 "+Str(c(1).centre.x)+","+Str(c(1).centre.y)+","+Str(c(1).centre.z)+"  size = "+Str(c(1).size)
    Draw String(10,80),"cube 2 "+Str(c(2).centre.x)+","+Str(c(2).centre.y)+","+Str(c(2).centre.z)+"  size = "+Str(c(2).size)
    
    Print "intersecting cubes?  ";cubeintersect(tmp(1),tmp(2))
    
    
    'sort the face centriods and cubes by .z value of centriods
    CubeSort(tmp())
    FaceSort(tmp(1),tmp(1).painter())
    FaceSort(tmp(2),tmp(2).painter())
    
    drawfaces(c(2),tmp(1),tmp(1).painter(),m,tmp(1).col()) 'c(2) is the moveable cube, so it is sent for adjustment
    drawfaces(c(2),tmp(2),tmp(2).painter(),m,tmp(2).col()) 'm is the mouse
    
    Screenunlock
    
    Sleep regulate(60,fps),1
Loop Until Inkey=Chr(27)
Sleep


 
Löwenherz
Posts: 145
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Cube OpenGL collision qt

Post by Löwenherz »

Many thanks for quite another approach for rotating cubes and intersection dodicat. Interesting example.

If I Press left Mouse (use only two way Laptop left right Button) the right Cube doesn't Stop movements or activate / des activate movements. Intersection Works fine.

I will create another example If I have more time with my Last Setup to select a Cube or Not by Mouse click. The shoot Option I can create too I have a new Idea in my mind. Nice wednesday so far thanks. Loewenherz
Post Reply