PDA

View Full Version : questions "shoot example" (tbgl)



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

Petr Schreiber
14-06-2012, 08:34
Hi Frank!,

Front camera
If you want first person camera on some object, you need to do it this way:

create the object (player)
create the camera and set the object as parent (something like: TBGL_EntityCreateCamera(%sScene, %eCamera, %eMyPlayerEntity))
set the camera position to some offset, otherwise it will be "inside player", you can use for example TBGL_EntitySetPos(%sScene, %eCamera, 0, 0, -10) to set camera watching the players back for start


Making the lasers follow the player orientation
In your code, you render scene using entities, and lasers via other technique - that can complicate the things a bit.
The better approach would be to create lasers dynamically as entities. The best way is to create custom entity, as described here (http://www.thinbasic.com/community/content.php?19-tbgl-user-defined-entities), or to shoot boxes for a start. The key is:

to generate id for each new shot use TBGL_EntityGetFreeID
to synchronize rotation of player and shot use TBGL_EntitySyncAxes


Aaaa, so confusing!
Written in steps, I would recommend:

create new tBasicU based on actor_object template, use this file to represent single laser shot

that means, after file is created, rename all occurences of <object> to for example "LaserShot"
you want the shot to appear at players position and heading the same direction, modify the _Create function to:

take player entity as parameter
after creation set the laser at players position and sync the axes


put the graphic code (tbgl_BeginPoly ... ) to LaserShot_Render, which will be predefined for you


in main code include the tBasicU file
when you want to shoot, just call LaserShot_Create(%sScene, %ePlayer)... yes, with custom entity it will be as simple as that :)
to make the camera follow the player direction of looking, set it the player as parent, push it a bit forward or backward as you need and that's it


Petr

Lionheart008
14-06-2012, 14:13
thank you petr for infos, but "tBasicU" is still too complicate for me at momement.

what do you think about this setup ? one cube to rotate and move and you can shot with it... that's just an idea some minutes before and I like that..



' The entity skeleton for TBGL
' , started on 06-12-2012 by frank brübach (lionheart)
' - part two, 06-14-2012

Uses "TBGL"

Begin Const
' -- Scene IDs
%sScene = 1

' -- Entity IDs
%eCamera = 1
%eLight
%eBox
%eBox2
%eSphere
%ePlayerX
%ePlayerY
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> move: <up> <down> key"

' -- Create and show window
hWnd = TBGL_CreateWindowEx("TBGL Shoot script 1b_exp - press ESC to quit", 740, 580, 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,%ePlayerX)
TBGL_EntitySetPos(%sScene, %eCamera, 1, 1, -34)
'TBGL_EntitySetTargetPos(%sScene, %eCamera,0,0,0)

TBGL_EntityCreateDLSlot(%sScene, %ePlayerX, 0, 0)
TBGL_EntitySetPos(%sScene, %ePlayerX, 0, 0, 0)

TBGL_EntityCreateDLSlot(%sScene, %ePlayerY, 0, 0)
TBGL_EntitySetPos(%sScene, %ePlayerY, 2, 1, 0)

' -- Create point light
TBGL_EntityCreateLight(%sScene, %eLight)
TBGL_EntitySetPos(%sScene, %eLight, 4, 10, -35)

' -- Create something to look at
TBGL_EntityCreateBox(%sScene, %eBox, 0, 2, 2, 2, 0, 255, 0, 0)
TBGL_EntitySetPos(%sScene, %eBox, 0, 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

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, %ePlayerX, 0,-90/FrameRate, 0)
TBGL_EntityTurn(%sScene, %ePlayerX, 0,0, -90/FrameRate)

ElseIf TBGL_GetWindowKeyState(hWnd, %VK_RIGHT) Then
TBGL_EntityTurn(%sScene, %ePlayerX, 0,90/FrameRate, 0)
TBGL_EntityTurn(%sScene, %ePlayerX, 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

If TBGL_GetWindowKeyState( hWnd, %VK_UP) Then
TBGL_EntityPush(%sScene, %eBox2, 5/FrameRate,0, 0)
TBGL_EntityPush(%sScene, %ePlayerX, 5/FrameRate,0, 0)
End If
If TBGL_GetWindowKeyState( hWnd, %VK_DOWN) Then
TBGL_EntityPush(%sScene, %eBox2, -5/FrameRate,0, 0)
TBGL_EntityPush(%sScene, %ePlayerX, -5/FrameRate,0, 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 = %ePlayerX-6
Laser(LaserIndex).y = %ePlayerY-7
Laser(LaserIndex).z = 0'-1
Laser(LaserIndex).s = 15
End Function

'render laser ok ? ----------------
Function RenderLasers() As Long
Local i As Long, j As Long
TBGL_Color 0,255,0
TBGL_LineWidth 3

For i = 1 To %NUMLASERS
If Laser(i).s = 0 Then Exit For
Laser(i).z = Laser(i).z + Laser(i).s / 60 'FrameRate ''tbgl_GetFrameRate
TBGL_PushMatrix
TBGL_Translate Laser(i).x, Laser(i).y, Laser(i).z
TBGL_BeginPoly %GL_LINES
TBGL_Vertex 0,0,0
TBGL_Vertex 0,0,-1
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 ------

thanks, frank

Lionheart008
16-06-2012, 09:45
@petr: I suppose the idea with box and player with same coordinates (position) wasn't very good because there's no feedback ?
the tbgl actor-unit stuff I will check another time, I have some difficulties to grasp it at the moment (sorry, but that's true!).

kind regards, frank

Petr Schreiber
16-06-2012, 11:20
Hi Frank,

there is no good/bad - you are the coder, whatever suits you best - use it :)
I personally prefer the actor based units, because they are reusable in other project and keep the same structure.

I think the time you invest to learn the principle pays back later in other projects.

Fingers crossed for difficulty grasping :)


Petr