Lionheart008
13-06-2012, 11:20
hello. I have some simple question about a simple shooter example. I used as basis the funshooter example (old one).
a) how to set a front view for camera (I used entity template) and think it's the perspective view so I must changed laser shoot position (centre of cube)
b) I wanted to rotate the playerX (here: cube with sphere in it) AND the weapon laser should follow him to shoot in all directions (linear)
c) playerX/playerY modus is correct in my example? I don't think so...
d) obstacle part isn't correct because of array dimensions (I will check it next time)
e) "shoot" with "space" key, rotate with "left,right" keys (only for the beginning)
my dummy example:
' The entity skeleton for TBGL
' , started on 06-12-2012 by frank brübach (lionheart)
' questions:
' a) how To Set front view ? b) how To rotate playerX With laser weapon
' to shoot linear in every direction like playerX's rotation ?
Uses "TBGL"
Begin Const
' -- Scene IDs
%sScene = 1
' -- Entity IDs
%eCamera = 1
%eLight
%eBox
%eBox2
%eSphere
End Const
' Game statistics
Dim Score As Long, Life As Long, GameOver As Long
Dim PlayerX, PlayerY As Single
' Speed control
Dim FrameRate As Double
Life = 100
%PLAYERSHIP = 1
' Lasers
Type tLasers
X As Double
Y As Double
Z As Double
S As Double
End Type
%NUMLASERS = 10
Dim Laser(%NUMLASERS) As tLasers
Dim LaserIndex As Long
' Obstacles to shoot
Type tObstacles
X As Double
Y As Double
Z As Double
S As Double
End Type
%NUMOBSTACLES = 5
Dim Obstacle(%NUMOBSTACLES) As tObstacles
'----------- MAIN ----------
Function TBMain() As Long
Local hWnd As DWord, i As Long
Local FrameRate As Double
MsgBox 0, "shoot with <space> key, rotate <left>, <right> key"
' -- Create and show window
hWnd = TBGL_CreateWindowEx("TBGL Shoot script 1a - press ESC to quit", 640, 480, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow
' -- Create scene
TBGL_SceneCreate(%sScene)
' -- Create basic entities
' -- Create camera to look from 15, 15, 15 to 0, 0, 0
TBGL_EntityCreateCamera(%sScene, %eCamera)
TBGL_EntitySetPos(%sScene, %eCamera, 18, 18, 18)
TBGL_EntitySetTargetPos(%sScene, %eCamera, 0, 0, 0)
' perspective view ? - better to use front view into depth (z)
' -- Create point light
TBGL_EntityCreateLight(%sScene, %eLight)
TBGL_EntitySetPos(%sScene, %eLight, 15, 10, 5)
' -- Create something to look at
TBGL_EntityCreateBox(%sScene, %eBox+PlayerX, 0, 2, 2, 2, 0, 255, 0, 0)
TBGL_EntitySetPos(%sScene, %eBox, -8, 1.7, 1.5)
TBGL_EntityCreateBox(%sScene, %eBox2+PlayerY, 0, 6, 6, 6, 0, 0, 255, 0)
TBGL_EntitySetPos(%sScene, %eBox2, 4, 0, 0)
TBGL_EntityCreateSphere(%sScene, %eSphere, 0, 4, 255, 255, 0)
TBGL_EntitySetPos(%sScene, %eSphere, 4, 0, 0)
For i = 1 To %NUMOBSTACLES
initObstacle(i)
Next
' -- Resets status of all keys
TBGL_ResetKeyState()
' -- Main loop
While TBGL_IsWindow(hWnd)
FrameRate = TBGL_GetFrameRate
TBGL_ClearFrame
TBGL_SceneRender(%sScene)
RenderLasers
'RenderObstacles ' doesn't work because of uncorrect initobstacles
TBGL_DrawFrame
If TBGL_GetWindowKeyOnce( hWnd, %VK_SPACE) Then Shoot()
If TBGL_GetWindowKeyState( hWnd, %VK_ESCAPE) Or GameOver = 1 Then Exit While
If TBGL_GetWindowKeyState(hWnd, %VK_LEFT) Then
TBGL_EntityTurn(%sScene, %eBox, 0,-90/FrameRate, 0)
TBGL_EntityTurn(%sScene, %eBox, 0,0,-90/FrameRate)
ElseIf TBGL_GetWindowKeyState(hWnd, %VK_RIGHT) Then
TBGL_EntityTurn(%sScene, %eBox, 0, 90/FrameRate, 0)
TBGL_EntityTurn(%sScene, %eBox, 0,0,90/FrameRate)
End If
If TBGL_GetWindowKeyState(hWnd, %VK_LEFT) Then
TBGL_EntityTurn(%sScene, %eBox2, 0,-90/FrameRate, 0)
ElseIf TBGL_GetWindowKeyState(hWnd, %VK_RIGHT) Then
TBGL_EntityTurn(%sScene, %eBox2, 0, 90/FrameRate, 0)
End If
Wend
TBGL_DestroyWindow
End Function
' Lasers ' ok -------------------------------------------
Function Shoot() As Long
Incr LaserIndex
If LaserIndex > %NUMLASERS Then LaserIndex = 1
Laser(LaserIndex).x = %eBox+PlayerX+6
Laser(LaserIndex).y = %eBox2+PlayerY+1
Laser(LaserIndex).z = 6'-1
Laser(LaserIndex).s = 46'15 ' speed
End Function
'render laser ok ? ----------------
Function RenderLasers() As Long
Local i As Long, j As Long
TBGL_Color 0,255,255
TBGL_LineWidth 4
For i = 1 To %NUMLASERS
If Laser(i).s = 0 Then Exit For
'--------- error with division, I took "60"
Laser(i).x =Laser(i).x - Laser(i).s / 60 'FrameRate ''tbgl_GetFrameRate
'--------- error with division, I took "60"
TBGL_PushMatrix
TBGL_Translate Laser(i).x-1.65, Laser(i).y-3.15, Laser(i).z-4.55
'TBGL_Rotate 90,1,0,0
TBGL_Rotate 90,0,1,0 'ok
TBGL_BeginPoly %GL_LINES
TBGL_Vertex 0,0,0
TBGL_Vertex 0,0,-2
TBGL_EndPoly
TBGL_PopMatrix
' Very simple collison detection with the boxes
For j = 1 To %NUMOBSTACLES
If Laser(i).x < Obstacle(j).x+1 And Laser(i).x > Obstacle(j).x-1 And _
Laser(i).z < Obstacle(j).z+1 And Laser(i).z > Obstacle(j).z-1 And _
Laser(i).y < Obstacle(j).y+1 And Laser(i).y > Obstacle(j).y-1 Then
initObstacle(j)
Score = Score + 100
End If
Next
Next
End Function
'############################################################################
'---------------------------------------------------- obstacles not ok ------
'Obstacles ''
Function InitObstacle( index As Long ) As Long
'Obstacle(index).x = Rnd(-2,2)
'Obstacle(index).y = Rnd(-2,2)
'Obstacle(index).z = Rnd(-10,+20)
'Obstacle(index).s = Rnd(5,7)
End Function
Function RenderObstacles() As Long
Local i As Long,hwnd As Long
'TBGL_PolygonLook IIf(TBGL_GetWindowKeyState(hWnd, %VK_TAB), %GL_LINE, %GL_FILL)
'---Script will run on different PCs so we must assure
'---constant speed of movement by scaling movement relative to framerate
'FrameRate = TBGL_GetFrameRate
TBGL_Color 128,0,0
For i = 1 To %NUMOBSTACLES
Obstacle(i).z = Obstacle(i).z + Obstacle(i).s / 60'FrameRate
If Obstacle(i).z > 0 Then initObstacle(i)
' Very simple collison detection with the player
If Obstacle(i).x < PlayerX+1 And Obstacle(i).x > PlayerX-1 And _
Obstacle(i).y < PlayerY+1 And Obstacle(i).y > PlayerY-1 And _
Obstacle(i).z > -2 Then
initObstacle(i)
Life = Life - 10
If Life <= 0 Then GameOver = 1
End If
TBGL_PushMatrix
TBGL_Translate Obstacle(i).x, Obstacle(i).y, Obstacle(i).z
TBGL_Rotate Timer*100,1,1,1
TBGL_Box 1,1,1
TBGL_PopMatrix
Next
End Function
'############################################################################
'---------------------------------------------------- obstacles not ok ------
any help is welcome here. the idea is to shoot down some enemies (coming up from all directions in space) with one rotating playerX.
best regards, frank
a) how to set a front view for camera (I used entity template) and think it's the perspective view so I must changed laser shoot position (centre of cube)
b) I wanted to rotate the playerX (here: cube with sphere in it) AND the weapon laser should follow him to shoot in all directions (linear)
c) playerX/playerY modus is correct in my example? I don't think so...
d) obstacle part isn't correct because of array dimensions (I will check it next time)
e) "shoot" with "space" key, rotate with "left,right" keys (only for the beginning)
my dummy example:
' The entity skeleton for TBGL
' , started on 06-12-2012 by frank brübach (lionheart)
' questions:
' a) how To Set front view ? b) how To rotate playerX With laser weapon
' to shoot linear in every direction like playerX's rotation ?
Uses "TBGL"
Begin Const
' -- Scene IDs
%sScene = 1
' -- Entity IDs
%eCamera = 1
%eLight
%eBox
%eBox2
%eSphere
End Const
' Game statistics
Dim Score As Long, Life As Long, GameOver As Long
Dim PlayerX, PlayerY As Single
' Speed control
Dim FrameRate As Double
Life = 100
%PLAYERSHIP = 1
' Lasers
Type tLasers
X As Double
Y As Double
Z As Double
S As Double
End Type
%NUMLASERS = 10
Dim Laser(%NUMLASERS) As tLasers
Dim LaserIndex As Long
' Obstacles to shoot
Type tObstacles
X As Double
Y As Double
Z As Double
S As Double
End Type
%NUMOBSTACLES = 5
Dim Obstacle(%NUMOBSTACLES) As tObstacles
'----------- MAIN ----------
Function TBMain() As Long
Local hWnd As DWord, i As Long
Local FrameRate As Double
MsgBox 0, "shoot with <space> key, rotate <left>, <right> key"
' -- Create and show window
hWnd = TBGL_CreateWindowEx("TBGL Shoot script 1a - press ESC to quit", 640, 480, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow
' -- Create scene
TBGL_SceneCreate(%sScene)
' -- Create basic entities
' -- Create camera to look from 15, 15, 15 to 0, 0, 0
TBGL_EntityCreateCamera(%sScene, %eCamera)
TBGL_EntitySetPos(%sScene, %eCamera, 18, 18, 18)
TBGL_EntitySetTargetPos(%sScene, %eCamera, 0, 0, 0)
' perspective view ? - better to use front view into depth (z)
' -- Create point light
TBGL_EntityCreateLight(%sScene, %eLight)
TBGL_EntitySetPos(%sScene, %eLight, 15, 10, 5)
' -- Create something to look at
TBGL_EntityCreateBox(%sScene, %eBox+PlayerX, 0, 2, 2, 2, 0, 255, 0, 0)
TBGL_EntitySetPos(%sScene, %eBox, -8, 1.7, 1.5)
TBGL_EntityCreateBox(%sScene, %eBox2+PlayerY, 0, 6, 6, 6, 0, 0, 255, 0)
TBGL_EntitySetPos(%sScene, %eBox2, 4, 0, 0)
TBGL_EntityCreateSphere(%sScene, %eSphere, 0, 4, 255, 255, 0)
TBGL_EntitySetPos(%sScene, %eSphere, 4, 0, 0)
For i = 1 To %NUMOBSTACLES
initObstacle(i)
Next
' -- Resets status of all keys
TBGL_ResetKeyState()
' -- Main loop
While TBGL_IsWindow(hWnd)
FrameRate = TBGL_GetFrameRate
TBGL_ClearFrame
TBGL_SceneRender(%sScene)
RenderLasers
'RenderObstacles ' doesn't work because of uncorrect initobstacles
TBGL_DrawFrame
If TBGL_GetWindowKeyOnce( hWnd, %VK_SPACE) Then Shoot()
If TBGL_GetWindowKeyState( hWnd, %VK_ESCAPE) Or GameOver = 1 Then Exit While
If TBGL_GetWindowKeyState(hWnd, %VK_LEFT) Then
TBGL_EntityTurn(%sScene, %eBox, 0,-90/FrameRate, 0)
TBGL_EntityTurn(%sScene, %eBox, 0,0,-90/FrameRate)
ElseIf TBGL_GetWindowKeyState(hWnd, %VK_RIGHT) Then
TBGL_EntityTurn(%sScene, %eBox, 0, 90/FrameRate, 0)
TBGL_EntityTurn(%sScene, %eBox, 0,0,90/FrameRate)
End If
If TBGL_GetWindowKeyState(hWnd, %VK_LEFT) Then
TBGL_EntityTurn(%sScene, %eBox2, 0,-90/FrameRate, 0)
ElseIf TBGL_GetWindowKeyState(hWnd, %VK_RIGHT) Then
TBGL_EntityTurn(%sScene, %eBox2, 0, 90/FrameRate, 0)
End If
Wend
TBGL_DestroyWindow
End Function
' Lasers ' ok -------------------------------------------
Function Shoot() As Long
Incr LaserIndex
If LaserIndex > %NUMLASERS Then LaserIndex = 1
Laser(LaserIndex).x = %eBox+PlayerX+6
Laser(LaserIndex).y = %eBox2+PlayerY+1
Laser(LaserIndex).z = 6'-1
Laser(LaserIndex).s = 46'15 ' speed
End Function
'render laser ok ? ----------------
Function RenderLasers() As Long
Local i As Long, j As Long
TBGL_Color 0,255,255
TBGL_LineWidth 4
For i = 1 To %NUMLASERS
If Laser(i).s = 0 Then Exit For
'--------- error with division, I took "60"
Laser(i).x =Laser(i).x - Laser(i).s / 60 'FrameRate ''tbgl_GetFrameRate
'--------- error with division, I took "60"
TBGL_PushMatrix
TBGL_Translate Laser(i).x-1.65, Laser(i).y-3.15, Laser(i).z-4.55
'TBGL_Rotate 90,1,0,0
TBGL_Rotate 90,0,1,0 'ok
TBGL_BeginPoly %GL_LINES
TBGL_Vertex 0,0,0
TBGL_Vertex 0,0,-2
TBGL_EndPoly
TBGL_PopMatrix
' Very simple collison detection with the boxes
For j = 1 To %NUMOBSTACLES
If Laser(i).x < Obstacle(j).x+1 And Laser(i).x > Obstacle(j).x-1 And _
Laser(i).z < Obstacle(j).z+1 And Laser(i).z > Obstacle(j).z-1 And _
Laser(i).y < Obstacle(j).y+1 And Laser(i).y > Obstacle(j).y-1 Then
initObstacle(j)
Score = Score + 100
End If
Next
Next
End Function
'############################################################################
'---------------------------------------------------- obstacles not ok ------
'Obstacles ''
Function InitObstacle( index As Long ) As Long
'Obstacle(index).x = Rnd(-2,2)
'Obstacle(index).y = Rnd(-2,2)
'Obstacle(index).z = Rnd(-10,+20)
'Obstacle(index).s = Rnd(5,7)
End Function
Function RenderObstacles() As Long
Local i As Long,hwnd As Long
'TBGL_PolygonLook IIf(TBGL_GetWindowKeyState(hWnd, %VK_TAB), %GL_LINE, %GL_FILL)
'---Script will run on different PCs so we must assure
'---constant speed of movement by scaling movement relative to framerate
'FrameRate = TBGL_GetFrameRate
TBGL_Color 128,0,0
For i = 1 To %NUMOBSTACLES
Obstacle(i).z = Obstacle(i).z + Obstacle(i).s / 60'FrameRate
If Obstacle(i).z > 0 Then initObstacle(i)
' Very simple collison detection with the player
If Obstacle(i).x < PlayerX+1 And Obstacle(i).x > PlayerX-1 And _
Obstacle(i).y < PlayerY+1 And Obstacle(i).y > PlayerY-1 And _
Obstacle(i).z > -2 Then
initObstacle(i)
Life = Life - 10
If Life <= 0 Then GameOver = 1
End If
TBGL_PushMatrix
TBGL_Translate Obstacle(i).x, Obstacle(i).y, Obstacle(i).z
TBGL_Rotate Timer*100,1,1,1
TBGL_Box 1,1,1
TBGL_PopMatrix
Next
End Function
'############################################################################
'---------------------------------------------------- obstacles not ok ------
any help is welcome here. the idea is to shoot down some enemies (coming up from all directions in space) with one rotating playerX.
best regards, frank