Here's my Code example for oxygen
Cube collision
Use arrows Up down left right
The example Starts with collision both cubes are laying upon each other so you must move the Cube to undo it
' cube and collision example, july-august 2024, frank bruebach
' 10-august-2024, oxygen Basic
'
' the collision works fine here -> both cubes lays upon each other
' move cube with arrows up,down,left,right
'
'includepath "$\inc\"
$ FileName "t.exe"
$ title "cubes and collision simple"
$ fontA = "Arial",FW_SEMIBOLD
int width=640
int height=480
uses OpenglSceneFrame
indexbase 1
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
Function CheckCollision(ByRef box1 As BoundingBox, ByRef box2 As BoundingBox) As LONG
' 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
sub Initialize(sys hWnd)
'=======================
end sub
'
'----------------------------------------------------------------------------------- //
sub DrawGlCube(ByRef rotangle As Single, ByVal posX As Single, ByVal posY As Single)
glLoadIdentity
'glTranslatef 1.5, 0.0, -6.0
glTranslatef(posX, posY, -7)
'glRotatef ang1,1.0, 1.0, 1.0 'rquad
glBegin GL_QUADS
glColor3f 0.0, 1.0, 0.0
glVertex3f 1.0, 1.0, -1.0
glVertex3f -1.0, 1.0, -1.0
glVertex3f -1.0, 1.0, 1.0
glVertex3f 1.0, 1.0, 1.0
glColor3f 1.0, 0.5, 0.0
glVertex3f 1.0, -1.0, 1.0
glVertex3f -1.0, -1.0, 1.0
glVertex3f -1.0, -1.0, -1.0
glVertex3f 1.0, -1.0, -1.0
glColor3f 1.0, 0.0, 0.0
glVertex3f 1.0, 1.0, 1.0
glVertex3f -1.0, 1.0, 1.0
glVertex3f -1.0, -1.0, 1.0
glVertex3f 1.0, -1.0, 1.0
glColor3f 1.0, 1.0, 0.0
glVertex3f 1.0, -1.0, -1.0
glVertex3f -1.0, -1.0, -1.0
glVertex3f -1.0, 1.0, -1.0
glVertex3f 1.0, 1.0, -1.0
glColor3f 0.0, 0.0, 1.0
glVertex3f -1.0, 1.0, 1.0
glVertex3f -1.0, 1.0, -1.0
glVertex3f -1.0, -1.0, -1.0
glVertex3f -1.0, -1.0, 1.0
glColor3f 1.0, 0.0, 1.0
glVertex3f 1.0, 1.0, -1.0
glVertex3f 1.0, 1.0, 1.0
glVertex3f 1.0, -1.0, 1.0
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 -1.5, 0.0, -7.0
'glRotatef ang1,1.0, 1.0, 1.0 'rquad
glTranslatef(posX, posY, -7)
glBegin GL_QUADS
glColor3f 0.0, 1.0, 0.0
glVertex3f 1.0, 1.0, -1.0
glVertex3f -1.0, 1.0, -1.0
glVertex3f -1.0, 1.0, 1.0
glVertex3f 1.0, 1.0, 1.0
glColor3f 1.0, 0.5, 0.0
glVertex3f 1.0, -1.0, 1.0
glVertex3f -1.0, -1.0, 1.0
glVertex3f -1.0, -1.0, -1.0
glVertex3f 1.0, -1.0, -1.0
glColor3f 1.0, 0.0, 0.0
glVertex3f 1.0, 1.0, 1.0
glVertex3f -1.0, 1.0, 1.0
glVertex3f -1.0, -1.0, 1.0
glVertex3f 1.0, -1.0, 1.0
glColor3f 1.0, 1.0, 0.0
glVertex3f 1.0, -1.0, -1.0
glVertex3f -1.0, -1.0, -1.0
glVertex3f -1.0, 1.0, -1.0
glVertex3f 1.0, 1.0, -1.0
glColor3f 0.0, 0.0, 1.0
glVertex3f -1.0, 1.0, 1.0
glVertex3f -1.0, 1.0, -1.0
glVertex3f -1.0, -1.0, -1.0
glVertex3f -1.0, -1.0, 1.0
glColor3f 1.0, 0.0, 1.0
glVertex3f 1.0, 1.0, -1.0
glVertex3f 1.0, 1.0, 1.0
glVertex3f 1.0, -1.0, 1.0
glVertex3f 1.0, -1.0, -1.0
glEnd
end sub
'------------------ //
sub Scene(sys hWnd)
'==================
'
static single ang1,angi1=1
static Single angle
static int fps
'
glClearColor 0.3, 0.3, 0.5, 0
glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
'
glLoadIdentity
'
glClearColor 0.5, 0, 0, 0
glPushMatrix
'
glLoadIdentity
static int framecount
sys x,y
framecount++
gltranslatef -.5,.25,-4.0
glColor3f .99,.50,.50
glscalef .2,.2,.01 ''*** larger scale from ,06
gprint str(framecount)
'
glpopmatrix
'
glLoadIdentity
glpushMatrix
gltranslatef 2.0, 0.0, -4.0
'glClearColor 0.8, 0.3, 0.5, 0
glscalef .2,.2,.01
glRotatef 90.0,0,0,1
gprint "Hello openGL"
glPopMatrix
'
if not key[16] 'shift'
if key[37] then cube1PosX -= 0.1 ' to right side
if key[39] then cube1PosX += 0.1 ''
if key[38] then cube1PosY += 0.1 '' up ''
if key[40] then cube1PosY -= 0.1 '' down ''
if key[33] then cube1PosY += 0.1 '''page up
if key[34] then cube1PosY -= 0.1 '''page down
end if
' 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
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)
'------------------------------- // solved and corrected ----------- //
' Check for collision
'If not CheckCollision(cube1, cube2) Then
If CheckCollision(cube1, cube2) Then
gltranslatef 4.0, 0.0, -4.0
glscalef .52,.52,.01
glRotatef 90.0,0,0,1
gprint "Collision!"
End If
'------------------------------- // solved and corrected ----------- //
'ang1+=angi1
if ang1>360 then ang1-=360
end sub
sub Release(sys hwnd)
'====================
end sub