Post by smc44 on Mar 18, 2009 19:10:37 GMT -5
This is some real 3D collision, if you need any explanations then please tell me, but take a look at this code, hope you enjoy!
Struc sPlayer
Dim PosX#, PosY#, PosZ#
Dim Speed#
Dim HitX#, HitY#, HitZ#, OnGround#, Gravity#
Dim Angle#(2)
Dim Drop#
Dim Health#
Dim OnLadder#(10)
EndStruc
Dim sPlayer Player
Player.PosX# = 0: Player.PosY# = 45: Player.PosZ# = 90: Player.Speed# = 2
Player.Health# = 100: Player.OnGround# = TRUE
Struc sPlane
Dim PosX#, PosY#, PosZ#
Dim SizeX#, SizeY#, SizeZ#
Dim Texture
EndStruc
Dim sPlane Plane(1)
Player.Gravity# = 0.75
glEnable(GL_TEXTURE_2D)
Plane(0).Texture = LoadTex("textures/Floor01.jpg")
Struc sLight
Dim Ambient#(3), Diffuse#(3), Specular#(3)
Dim Pos#(3)
EndStruc
Dim sLight Light(8)
Light(0).Ambient# = Vec4(0.4, 0.4, 0.4, 1)
Light(0).Diffuse# = Vec4(0.4, 0.3, 0.3, 1)
Light(0).Specular# = Vec4(0.4, 0.5, 0.4, 1)
Light(0).Pos# = Vec4(1, 7, 0, 1)
glLightfv(GL_LIGHT0, GL_AMBIENT, Light(0).Ambient#)
glLightfv(GL_LIGHT0, GL_DIFFUSE, Light(0).Diffuse#)
glLightfv(GL_LIGHT0, GL_SPECULAR, Light(0).Specular#)
glLightfv(GL_LIGHT0, GL_POSITION, Light(0).Pos#)
glEnable(GL_LIGHT0)
''''
Light(1).Ambient# = Vec4(0.5, 0.5, 0.4, 1)
Light(1).Diffuse# = Vec4(0.4, 0.5, 0.5, 1)
Light(1).Specular# = Vec4(0.4, 0.5, 0.4, 1)
Light(1).Pos# = Vec4(8, -9, 0, 1)
glLightfv(GL_LIGHT1, GL_AMBIENT, Light(1).Ambient#)
glLightfv(GL_LIGHT1, GL_DIFFUSE, Light(1).Diffuse#)
glLightfv(GL_LIGHT1, GL_SPECULAR, Light(1).Specular#)
glLightfv(GL_LIGHT1, GL_POSITION, Light(1).Pos#)
glEnable(GL_LIGHT1)
''''
Light(2).Ambient# = Vec4(0.4, 0.5, 0.5, 1)
Light(2).Diffuse# = Vec4(0.4, 0.5, 0.5, 1)
Light(2).Specular# = Vec4(0.4, 0.5, 0.5, 1)
Light(2).Pos# = Vec4(8, -9, 0, 1)
glLightfv(GL_LIGHT2, GL_AMBIENT, Light(2).Ambient#)
glLightfv(GL_LIGHT2, GL_DIFFUSE, Light(2).Diffuse#)
glLightfv(GL_LIGHT2, GL_SPECULAR, Light(2).Specular#)
glLightfv(GL_LIGHT2, GL_POSITION, Light(2).Pos#)
glEnable(GL_LIGHT2)
''''
Light(3).Ambient# = Vec4(10, 10, 0.5, 1)
Light(3).Diffuse# = Vec4(10, 10, 10, 1)
Light(3).Specular# = Vec4(10, 0.5, 0.5, 1)
Light(3).Pos# = Vec4(0, 90, 0, 1)
glLightfv(GL_LIGHT3, GL_AMBIENT, Light(3).Ambient#)
glLightfv(GL_LIGHT3, GL_DIFFUSE, Light(3).Diffuse#)
glLightfv(GL_LIGHT3, GL_SPECULAR, Light(3).Specular#)
glLightfv(GL_LIGHT3, GL_POSITION, Light(3).Pos#)
glEnable(GL_LIGHT3)
''''
Light(4).Ambient# = Vec4(1, 3, 6, 1)
Light(4).Diffuse# = Vec4(1, 3, 6, 1)
Light(4).Specular# = Vec4(1, 3, 6, 1)
Light(4).Pos# = Vec4(8, -9, 0, 1)
glLightfv(GL_LIGHT4, GL_AMBIENT, Light(4).Ambient#)
glLightfv(GL_LIGHT4, GL_DIFFUSE, Light(4).Diffuse#)
glLightfv(GL_LIGHT4, GL_SPECULAR, Light(4).Specular#)
glLightfv(GL_LIGHT4, GL_POSITION, Light(4).Pos#)
glEnable(GL_LIGHT4)
glEnable(GL_NORMALIZE)
glShadeModel(GL_SMOOTH)
Struc sBox
Dim Vert#(23)(2)
Dim Norm#(5)(2)
Dim Angle#
Dim PosX#, PosY#, PosZ#
Dim SizeX#, SizeY#, SizeZ#
Dim HitX#, HitY#, HitZ#
EndStruc
Dim sBox Box(10)
Box(0).PosX# = 0: Box(0).PosY# = -1.501: Box(0).PosZ# = -0.001
Box(0).SizeX# = 0.1: Box(0).SizeY# = 0.5: Box(0).SizeZ# = 1
Box(1).PosX# = 0.24: Box(1).PosY# = -5: Box(1).PosZ# = 10.5
Box(1).SizeX# = 0.04: Box(1).SizeY# = 0.5: Box(1).SizeZ# = 0.05
Box(0).Vert#(0) = Vec3(-1, -1, -1)
Box(0).Vert#(1) = Vec3(1, -1, -1)
Box(0).Vert#(2) = Vec3(1, 1, -1)
Box(0).Vert#(3) = Vec3(-1, 1, -1)
''
Box(0).Vert#(4) = Vec3(-1, -1, 1)
Box(0).Vert#(5) = Vec3(-1, -1, 1)
Box(0).Vert#(6) = Vec3(1, 1, 1)
Box(0).Vert#(7) = Vec3(-1, 1, 1)
''
Box(0).Vert#(8) = Vec3(1, -1, 1)
Box(0).Vert#(9) = Vec3(1, -1, -1)
Box(0).Vert#(10) = Vec3(1, 1, -1)
Box(0).Vert#(11) = Vec3(1, 1, 1)
''
Box(0).Vert#(12) = Vec3(-1, -1, -1)
Box(0).Vert#(13) = Vec3(1, -1, -1)
Box(0).Vert#(14) = Vec3(1, -1, 1)
Box(0).Vert#(15) = Vec3(-1, -1, 1)
''
Box(0).Vert#(16) = Vec3(-1, -1, 1)
Box(0).Vert#(17) = Vec3(1, -1, 1)
Box(0).Vert#(18) = Vec3(1, 1, 1)
Box(0).Vert#(19) = Vec3(-1, 1, 1)
''
Box(0).Vert#(20) = Vec3(-1, 1, 1)
Box(0).Vert#(21) = Vec3(-1, 1, -1)
Box(0).Vert#(22) = Vec3(1, 1, -1)
Box(0).Vert#(23) = Vec3(1, 1, 1)
Box(0).Norm#(0) = Vec3(0, 2, 2)
Box(0).Norm#(1) = Vec3(1, 2, 0)
Box(0).Norm#(2) = Vec3(0, 2, 2)
Box(0).Norm#(3) = Vec3(1, 2, 0)
Box(0).Norm#(4) = Vec3(0, 2, 2)
Box(0).Norm#(5) = Vec3(1, 2, 0)
Sub CreateBox(PosX#, PosY#, PosZ#, SizeX#, SizeY#, SizeZ#)
glPushMatrix()
glTranslatef(PosX#, PosY#, PosZ#)
glScalef(SizeX#, SizeY#, SizeZ#)
glRotatef(90, 0, 0, 1)
glBegin(Gl_QUADS)
glNormal3fv(Normalize(CrossProduct(Box(0).Norm#(0), Box(0).Norm#(5))))
glVertex3fv(Box(0).Vert#(0))
glVertex3fv(Box(0).Vert#(1))
glVertex3fv(Box(0).Vert#(2))
glVertex3fv(Box(0).Vert#(3))
glNormal3fv(Box(0).Norm#(1))
glVertex3fv(Box(0).Vert#(4))
glVertex3fv(Box(0).Vert#(5))
glVertex3fv(Box(0).Vert#(6))
glVertex3fv(Box(0).Vert#(7))
glNormal3fv(Box(0).Norm#(2))
glVertex3fv(Box(0).Vert#(8))
glVertex3fv(Box(0).Vert#(9))
glVertex3fv(Box(0).Vert#(10))
glVertex3fv(Box(0).Vert#(11))
glNormal3fv(Box(0).Norm#(3))
glVertex3fv(Box(0).Vert#(12))
glVertex3fv(Box(0).Vert#(13))
glVertex3fv(Box(0).Vert#(14))
glVertex3fv(Box(0).Vert#(15))
glNormal3fv(Box(0).Norm#(4))
glVertex3fv(Box(0).Vert#(16))
glVertex3fv(Box(0).Vert#(17))
glVertex3fv(Box(0).Vert#(18))
glVertex3fv(Box(0).Vert#(19))
glNormal3fv(Box(0).Norm#(5))
glVertex3fv(Box(0).Vert#(20))
glVertex3fv(Box(0).Vert#(21))
glVertex3fv(Box(0).Vert#(22))
glVertex3fv(Box(0).Vert#(23))
glEnd()
glPopMatrix()
EndSub
Sub CreateCrate(PosX#, PosY#, PosZ#, SizeX#, SizeY#, SizeZ#)
glTranslatef(PosX#, PosY#, PosZ#)
glScalef(SizeX#, SizeY#, SizeZ#)
glBegin(Gl_QUADS)
glcolor3f(1,0,0)
glVertex3f(-1,-1,-1)
glVertex3f( 1,-1,-1)
glVertex3f( 1, 1,-1)
glVertex3f(-1, 1,-1)
glcolor3f(0,1,0)
glVertex3f(-1,-1,-1)
glVertex3f(-1,-1, 1)
glVertex3f(-1, 1, 1)
glVertex3f(-1, 1,-1)
glcolor3f(0,0,1)
glVertex3f( 1,-1, 1)
glVertex3f( 1,-1,-1)
glVertex3f( 1, 1,-1)
glVertex3f( 1, 1, 1)
glcolor3f(1,1,0)
glVertex3f(-1,-1,-1)
glVertex3f( 1,-1,-1)
glVertex3f( 1,-1, 1)
glVertex3f(-1,-1, 1)
glcolor3f(0,1,1)
glVertex3f(-1,-1, 1)
glVertex3f( 1,-1, 1)
glVertex3f( 1, 1, 1)
glVertex3f(-1, 1, 1)
glColor3f(1,1,1)
glVertex3f(-1, 1, 1)
glVertex3f(-1, 1,-1)
glVertex3f( 1, 1,-1)
glVertex3f( 1, 1, 1)
glend()
EndSub
' Plane 1
Plane(0).PosX# = 0: Plane(0).PosY# = 9: Plane(0).PosZ# = 0
Plane(0).SizeX# = 50: Plane(0).SizeY# = 10: Plane(0).SizeZ# = 50
' Plane 2
Plane(1).PosX# = 0: Plane(1).PosY# = 0.4: Plane(1).PosZ# = -0.4
Plane(1).SizeX# = 0.5: Plane(1).SizeY# = 1: Plane(1).SizeZ# = 0.5
Sub CreatePlane(PosX#, PosY#, PosZ#, SizeX#, SizeY#, SizeZ#, Texture)
glTranslatef(PosX#, PosY#, PosZ#)
glScalef(SizeX#, SizeY#, SizeZ#)
glBindTexture(GL_TEXTURE_2D, Texture)
glBegin (GL_QUADS)
glNormal3f(1.0, 0.0, 0.0)
glTexCoord3f(0, 10, 0): glVertex3f(-1, -1, -1)
glTexCoord3f(10, 10, 0): glVertex3f(1, -1, -1)
glTexCoord3f(10, 0, 0): glVertex3f(1, -1, 1)
glTexCoord3f(0, 0, 0): glVertex3f(-1, -1, 1)
glEnd()
EndSub
Sub CreateLadder(PosX#, PosY#, PosZ#, SizeX#, SizeY#, SizeZ#, Texture)
glTranslatef(PosX#, PosY#, PosZ#)
glScalef(SizeX#, SizeY#, SizeZ#)
glBindTexture(GL_TEXTURE_2D, Texture)
glBegin(GL_QUADS)
glNormal3f(1.0, 0.0, 0.0)
glTexCoord3f(5, 5, 0): glVertex3f(2, -1, 5)
glTexCoord3f(0, 5, 0): glVertex3f(2, 3, 5)
glTexCoord3f(0, 0, 0): glVertex3f(-2, 3, 5)
glTexCoord3f(5, 0, 0): glVertex3f(-2, -1, 5)
glEnd()
EndSub
Dim BoxSX# = 1, BoxSY# = 7, BoxSZ# = 1
Dim BoxPosX# = 0, BoxPosY# = 1, BoxPosZ# = 0
Dim BoxTex = LoadTex("textures/wall01.jpg")
Dim PrevCount = TickCount()
Dim Count, Time#
Dim Level = glGenLists(1)
glNewList(Level, GL_COMPILE)
glPushMatrix(): CreateLadder(BoxPosX#, BoxPosY#, BoxPosZ#, BoxSX#, BoxSY#, BoxSZ#, BoxTex)
CreatePlane(Plane(0).PosX#, Plane(0).PosY#, Plane(0).PosZ#, Plane(0).SizeX#, Plane(0).SizeY#, Plane(0).SizeZ#, Plane(0).Texture)
CreatePlane(Plane(1).PosX#, Plane(1).PosY#, Plane(1).PosZ#, Plane(1).SizeX#, Plane(1).SizeY#, Plane(1).SizeZ#, Plane(0).Texture)
CreateBox(Box(0).PosX#, Box(0).PosY#, Box(0).PosZ#, Box(0).SizeX#, Box(0).SizeY#, Box(0).SizeZ#)
glPopMatrix()
glEndList()
Dim Bullet = glGenLists(1)
glNewList(Bullet, GL_COMPILE)
glPushMatrix()
CreateBox(Box(1).PosX#, Box(1).PosY#, Box(1).PosZ#, Box(1).SizeX#, Box(1).SizeY#, Box(1).SizeZ#)
glPopMatrix()
glEndList()
TextMode(TEXT_OVERLAID)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
While TRUE
glClear(GL_DEPTH_BUFFER_BIT or GL_COLOR_BUFFER_BIT)
glLoadIdentity()
glClearColor(.1, 0.5, .3, 1)
glRotatef(Player.Angle#(2), 0, 0, 1)
glRotatef(Player.Angle#(1), 1, 0, 0)
glRotatef(Player.Angle#(0), 0, 1, 0)
glTranslatef(-Player.PosX#, Player.PosY#, -Player.PosZ#)
glScalef(10, 10, 10)
' Draw Objects
glCallList(Level)
Locate 0, 0: Printr "Health:"; Player.Health#
DrawText()
SwapBuffers()
GoSub Collision
GoSub PlayerControl
Wend
PlayerControl:
Player.HitX# = Player.PosX#: Player.HitY# = Player.PosY#: Player.HitZ# = Player.PosZ#
Player.Angle#(0) = Player.Angle#(0) + Mouse_XD()*60.0
Player.Angle#(1) = Player.Angle#(1) + Mouse_YD()*60.0
if KeyDown("W") Then
Player.PosX# = Player.PosX# + Cosd(Player.Angle#(0)-90)/Player.Speed#
Player.PosZ# = Player.PosZ# + Sind(Player.Angle#(0)-90)/Player.Speed#
Endif
if KeyDown("S") Then
Player.PosX# = Player.PosX# - Cosd(Player.Angle#(0)-90)/Player.Speed#
Player.PosZ# = Player.PosZ# - Sind(Player.Angle#(0)-90)/Player.Speed#
Endif
if KeyDown("D") Then
Player.PosX# = Player.PosX# + Cosd(Player.Angle#(0))/Player.Speed#
Player.PosZ# = Player.PosZ# + Sind(Player.Angle#(0))/Player.Speed#
Endif
if KeyDown("A") Then
Player.PosX# = Player.PosX# - Cosd(Player.Angle#(0))/Player.Speed#
Player.PosZ# = Player.PosZ# - Sind(Player.Angle#(0))/Player.Speed#
Endif
Player.PosY# = Player.PosY# + 0.75
Return
Collision:
if Box(1).PosX# <= 0.24 Then Box(1).PosX# = 0.24
Endif
if Box(1).PosY# > 1 Then Player.PosX# = 1
Endif
if Box(1).PosY# < -498 Then Player.PosX# = -498
Endif
if Box(1).PosY# > 498 Then Player.PosZ# = 498
Endif
if Box(1).PosY# < -498 Then Player.PosZ# = -498
Endif
' Ladder Collision
if Player.PosX# < BoxPosX#+(BoxSX#+2.25*10) And Player.PosX# > BoxPosX#-(BoxSX#+2.25*10) Then
if Player.PosZ# < BoxPosZ#+(BoxSZ#+6*10) And Player.PosZ# > BoxPosZ#-(BoxSZ#-6*10) Then
if Player.PosY# < BoxPosY#+(BoxSY#*10) And Player.PosY# > BoxPosY#-(BoxSY#+23*10) Then
Player.PosZ# = Player.HitZ#: Player.OnLadder#(0) = TRUE
if Player.Angle#(1) <= -70 And Player.OnLadder#(0) = TRUE Then
Player.PosY# = Player.PosY# - 1.25
Endif
Endif
Endif
Endif
' Plane 2 Collision
if Player.PosY# < Plane(1).PosY#+(Plane(1).SizeY#-23*10) And Player.PosY# > Plane(1).PosY#-(Plane(1).SizeY#+23*10) Then
if Player.PosZ# < Plane(1).PosZ#+(Plane(1).SizeZ#+5.5*10) And Player.PosZ# > Plane(1).PosZ#-(Plane(1).SizeZ#+45*10) Then
if Player.PosX# < Plane(1).PosX#+(Plane(1).SizeX#+25*10) And Player.PosX# > Plane(1).PosX#-(Plane(1).SizeX#+25*10) Then
Player.PosY# = Player.HitY#
Endif
Endif
Endif
' Divider Collision
if Player.PosX# < Box(0).PosX#+(Box(0).SizeX#+3.7*10) And Player.PosX# > Box(0).PosX#-(Box(0).SizeX#+3.7*10) Then
if Player.PosZ# < Box(0).PosZ#+(Box(0).SizeZ#+6*10) And Player.PosZ# > Box(0).PosZ#-(Box(0).SizeZ#+45*10) Then
if Player.PosY# < Box(0).PosY#+(Box(0).SizeY#+6*10) And Player.PosY# > Box(0).PosY#-(Box(0).SizeY#+1*10) Then
Player.PosX# = Player.HitX#: Player.PosZ# = Player.PosZ# + 0.01
Endif
Endif
Endif
if Player.PosZ# < Box(0).PosZ#+(Box(0).SizeZ#+4.5*10) And Player.PosZ# > Box(0).PosZ#-(Box(0).SizeZ#+45.3*10) Then
if Player.PosX# < Box(0).PosX#+(Box(0).SizeX#+3.7*10) And Player.PosX# > Box(0).PosX#-(Box(0).SizeX#+3.7*10) Then
if Player.PosY# < Box(0).PosY#+(Box(0).SizeY#+6*10) And Player.PosY# > Box(0).PosY#-(Box(0).SizeY#+17.2*10) Then
Player.PosZ# = Player.HitZ#: Player.PosX# = Player.PosX# + Sind(Player.Angle#(0))*0.01
Endif
Endif
Endif
if Player.PosX# > 498 Then Player.PosX# = 498
Endif
if Player.PosX# < -498 Then Player.PosX# = -498
Endif
if Player.PosZ# > 498 Then Player.PosZ# = 498
Endif
if Player.PosZ# < -498 Then Player.PosZ# = -498
Endif
if Player.PosY# > 49 Then Player.PosY# = 49
Endif
Return