• Register
Forum Thread
  Posts  
Need help with Blitz 3D coding (Forums : Coding & Scripting : Need help with Blitz 3D coding) Locked
Thread Options
Oct 27 2014 Anchor

So, i've been learning blitz 3d, been going good so far. Ran across a few problems i haven't been able to solve.
i made simple game, where you can use either a tank or an airplane to shoot at targets. You can switch between the two at any time.
At first i made the controllable vehicles as individual objects, not in a type. The targets and projectiles were types though. Worked ok.

Now i'm trying to make the vehicles types too.
Main issue i'm dealing with right now is i'm not sure how to get the projectiles to shoot from the right place again.

The second thing is this;
i'm using CountCollisions for everything right now. But, this means if i try to make my own plane or tank destroyable then it will destroy itself, either due to contact with the ground or with it's own projectiles. i think what i need is a way to ask what hit my object, but i'm not sure. What are my options on fixing this?

Third is that my tank's turret and barrel no longer turn, although it is coded the same as the hull. i'm guessing it has to do with the tank being a type now, and the child objects don't like that for some reason

i'll put the code below. i can also put the whole game up as a download if that's wanted.
Also, i wouldn't be surprised if someone brings this up, so i'll go ahead and mention that i don't have access to the Blitz
forums because i got the free download of Blitz 3D and forgot the download code, and so can't join the forum. i don't want to
go back to the site's download because there was adware everywhere and i got a virus from the popups.

Any help is appreciated:]

edit; updated code some.
Game

;Game Format
;--------------------
;[setup             ]
;[                  ]
;[game loop         ]
;[  call functions  ]
;[  draw            ]
;[end loop          ]
;[                  ]
;[functions         ]
;--------------------


;///////////////////
;SETUP;=============
;\\\\\\\\\\\\\\\\\\\
AppTitle "Tanks and Planes"
Global ChooseVehicle = 0
Print "Please select a vehicle."
Print "Hit 1 for a tank"
Print "Hit 2 for a plane"
While ChooseVehicle = 0
  If KeyHit(2)
    ChooseVehicle = 1
  ElseIf KeyHit(3)
    ChooseVehicle = 2
  EndIf
  If KeyHit(1) = True Then End
Wend

Graphics3D 640,480
SetBuffer BackBuffer()

;Stuff for collisions.
ColTypVehicle = 1
ColTypGround = 1
ColTypShell = 1
Collisions ColTypVehicle,ColTypGround,2,2
Collisions ColTypGround,ColTypShell,2,2
Collisions ColTypVehicle,ColTypShell,2,2

;Keys
Const ESCKEY = 1, WKEY = 17, RKEY = 19, PKEY = 25, SKEY = 31, DKEY = 32, AKEY=30, ZKEY=44, LEFTKEY=203, RIGHTKEY=205, UPKEY=200, DOWNKEY=208, NKEY=49, MKEY=50, SPACEKEY=57

;Create the tank and position it.
;>>>NOTE:The base object must be scaled evenly, otherwise
;the camera and projectile angles will be warped.
;Nevermind, apparently it's all obejcts attached to the tank..

;Make all pieces together to hopefully avoid scaling issues?

Global Hull = LoadMesh("Hull.3ds")
Global Turret = LoadMesh("Turret.3ds",hull)
Global Barrel = LoadMesh("Barrel.3ds",Turret)
Global Airplane = LoadMesh("Airplane.3ds")
Global Barrel1 = LoadMesh("Barrel.3ds",Airplane)
Global Barrel2 = LoadMesh("Barrel.3ds",Airplane)
EntityType Hull, ColTypVehicle;What things are able to collide
EntityRadius Hull, 0.5;How close can other objects come
EntityType Airplane, ColTypVehicle
EntityRadius Airplane, 0.5
;Turret
PositionEntity Turret,0,.7,-.3:ScaleEntity Turret,.5,.5,.5:EntityColor Turret,000,000,255
RotateEntity Turret, EntityPitch=0, EntityYaw=0, EntityRoll=0
;Barrel
PositionEntity Barrel,0,0,2:EntityColor Barrel,255,000,000
RotateEntity Barrel, EntityPitch=0, EntityYaw=0, EntityRoll=0
;Airplane barrels
PositionEntity Barrel1,-.8,-0.1,-.3:EntityColor Barrel,000,255,000
PositionEntity Barrel2,.8,-0.1,-.3:EntityColor Barrel,000,255,000

;Create the camera
Global Camera = CreateCamera():PositionEntity Camera,0,3,-8:RotateEntity Camera,0,0,0

;Lights
Global Light = CreateLight()

Type Shelltype ;set up the bullet type
  Field entityhandle ;create a field to contain the handle of the shell's mesh
End Type
Global Shellmesh = CreateCone():RotateMesh Shellmesh,90,0,0:ScaleEntity Shellmesh,.1,.1,.1
EntityType Shellmesh, ColTypShell				;set up collision type for the shell
EntityRadius Shellmesh, .01					;set up collision radius for the shell

;This section creates the ground. Current scaled ground size is 128x128, so default should be 256 unless scaling acts wierd.
Ground = LoadTerrain("hightmap1.bmp");hightmap controls hight
Texture = LoadTexture ("texture.bmp");the texture we'll see
EntityTexture Ground, Texture;put the texture on the ground
ScaleEntity Ground,.5,2,.5;scaling
EntityType Ground, ColTypGround
EntityRadius Ground, 0.5

;Create the targets
Type Targettype ;set up the target type
  Field Hull ;create a field to contain the handle of the target's mesh
  Field Turret
  Field Barrel
  Field Selected
End Type
Type Target2type ;set up the target type
  Field Airplane ;create a field to contain the handle of the target's mesh
  Field Barrel1 ;create a field to contain the handle of the target's mesh
  Field Barrel2 ;create a field to contain the handle of the target's mesh
  Field Selected
End Type
X#=0
Y#=TerrainY(Ground,x#,y#,z#)
Z#=0
SeedRnd MilliSecs();don't put this in loop, repeats numbers for some reason.
For Counter = 1 To 40
  X = Rand(1,128)
  Z = Rand(1,128)
  Target.Targettype = New Targettype				;create a target
  Target\Hull = CopyEntity(Hull)		;create the target mesh
  Target\Turret = CopyEntity(Turret)		;create the target mesh
  Target\Barrel = CopyEntity(Barrel)		;create the target mesh
  Target\Selected=0
  ;PositionMesh Target\entityhandle, EntityX(X,1), EntityY(Y,1), EntityZ(Z,1)	;place the target
  Y#=TerrainY(Ground,x#,y#,z#)
  Y=Y/2
  MoveEntity Target\Hull, X,Y,Z	;place the target
  TranslateEntity Target\Hull,0,Y,0
  ResetEntity Target\Hull				;otherwise target could hit bullet while moving from 0,0,0 to current position
Next
For Counter = 1 To 10
  X = Rand(1,128)
  Z = Rand(1,128)
  Target2.Target2type = New Target2type				;create a target
  Target2\Airplane = CopyEntity(Airplane)		;create the target mesh
  Target2\Barrel1 = CopyEntity(Barrel1)
  Target2\Barrel2 = CopyEntity(Barrel2)
  Target\Selected=0
  ;PositionMesh Target\entityhandle, EntityX(X,1), EntityY(Y,1), EntityZ(Z,1)	;place the target
  Y#=TerrainY(Ground,x#,y#,z#)
  Y=Y/2
  MoveEntity Target2\Airplane, X,Y+Rand(10,35),Z	;place the target
  TranslateEntity Target2\Airplane ,0,Y,0
  TurnEntity Target2\Airplane ,0,Rand(0,360),0
  ResetEntity Target2\Airplane 				;otherwise target could hit bullet while moving from 0,0,0 to current position
Next

;Explosion trash
Type TrashType
  Field entityhandle
  Field X#,Y#,Z#
  Field KillTimer
  Field KillTime
End Type
Global Trashmesh = CreateSphere():ScaleEntity Trashmesh,.5,.5,.5

PlaceTankandAirplane();Made this a function to make switching vehicles easier
Global FireGunXYZ_PYR#[5]
Global Velocity# = 0.0
Global Score = 0
Global ShotsFired = 0
Global Paused = 0
Global SideOfPlane = 1;Used by FireGun, for the airplane. Switches which side the gun fires from.
Global Timer = MilliSecs()
;///////////////////
;MAIN LOOP;=========
;///////////////////
While Not KeyHit(ESCKEY)
  KeepShellsMoving()
  KeepTrashMoving()
  KeepTargetsMoving()
  CheckTargets()
  UpdateWorld
  RenderWorld
  Textme();Must be after RenderWorld otherwise it dosn't show
  Flip
Wend
End
;///////////////////
;END MAIN LOOP;=====
;\\\\\\\\\\\\\\\\\\\

;///////////////////
;FUNCTIONS;=========
;\\\\\\\\\\\\\\\\\\\

;Check all the keys.
Function KeyHitStuffs()
End Function

;Go here To fire the gun
Function FireGun()
    ShotsFired=ShotsFired+1
    Shell.Shelltype = New Shelltype				;create a bullet
    Shell\entityhandle = CopyEntity(Shellmesh)		;create the bullet mesh
    PositionEntity Shell\entityhandle,FireGunXYZ_PYR[0],FireGunXYZ_PYR[1],FireGunXYZ_PYR[2];place the bullet at the guns position
    RotateEntity Shell\entityhandle,FireGunXYZ_PYR[3],FireGunXYZ_PYR[4],FireGunXYZ_PYR[5];orientate the bullet with the gun
    ResetEntity Shell\entityhandle				;otherwise bullet could hit enemy while moving from 0,0,0 to current position
End Function

;If i'm not repeated, the shells stop moving. So call me maybe.
Function KeepShellsMoving()
  For thisShell.Shelltype = Each Shelltype			;iterate through all of the bullets
    MoveEntity thisShell\entityhandle, 0, 0, 2			;move the bullet forward along the bullets Z axis
    TurnEntity thisShell\entityhandle,0.1,0,0 ;Gravity
;Obselete code. Left here incase needed later for future projectiles.
;    If (EntityY(thisShell\entityhandle, 1)) >= EntityY(Hull, 1)+5	;check if the type is close/far away from a certain object
;      FreeEntity thisShell\entityhandle
;      Delete thisShell

    ;check if the bullet is out of bounds. Not sure what the Abs thing was after If, but it was useless and broke stuff.
    If (EntityY(thisShell\entityhandle, 1)) < -5 Or (EntityY(thisShell\entityhandle, 1)) > 100 Or  (EntityX(thisShell\entityhandle, 1)) < -5 Or (EntityZ(thisShell\entityhandle, 1)) < -5 Or (EntityX(thisShell\entityhandle, 1)) > 133 Or (EntityZ(thisShell\entityhandle, 1)) > 133
      FreeEntity thisShell\entityhandle			;delete the bullet mesh
      Delete thisShell					;delete the bullet
    ElseIf CountCollisions (thisShell\entityhandle);This line must be an ElseIf, otherwise if the shell is deleted by out of bounds check then
      For Count = 1 To 3
        Trash.TrashType = New TrashType				;create trash
        Trash\entityhandle = CopyEntity(Trashmesh)	;create the trash mesh
        PositionEntity Trash\entityhandle, EntityX(thisShell\entityhandle, 1), EntityY(thisShell\entityhandle, 1), EntityZ(thisShell\entityhandle, 1)
        Trash\X#=Rand(0,10)/10
        Trash\Y#=Rand(3,10)/10
        Trash\Z#=Rand(0,10)/10
        Trash\KillTimer = MilliSecs()
        Trash\KillTime = Rand(10,75)
      Next
      FreeEntity thisShell\entityhandle            ;the shell will no longer exist and this will throw an error.
      Delete thisShell                             ;have to wait until after placing the trash to delete the shell
    EndIf
  Next
End Function

;Check all the targets to see if they've been hit
Function CheckTargets()
  For thisTarget.Targettype = Each Targettype    ;iterate through all of the targets
;  TranslateEntity thisTarget\entityhandle,0,-.1,0;gravity
    KeyHitStuffs()
   If thisTarget\Selected = 1;Tank stuffs
      If KeyDown(DKEY) = True And Velocity >= 0 Then TurnEntity thisTarget\Hull,0,-.4,0
      If KeyDown(AKEY) = True And Velocity >= 0 Then TurnEntity thisTarget\Hull,0,.4,0 ;Turning is separated out so turning while going backwards isn't weird
      If KeyDown(DKEY) = True And Velocity <  0 Then TurnEntity thisTarget\Hull,0,.4,0
      If KeyDown(AKEY) = True And Velocity <  0 Then TurnEntity thisTarget\Hull,0,-.4,0
      If KeyDown(WKEY) And Velocity < 0.1 Then Velocity=Velocity+0.002
      If KeyDown(SKEY) And Velocity > -0.1 Then Velocity=Velocity-0.002
      If Velocity > 0 Then Velocity=Velocity-0.001;friction
      If Velocity < 0 Then Velocity=Velocity+0.001;friction
      If Velocity > 0.001 Or Velocity < -0.001 Then MoveEntity thisTarget\Hull,0,0, Velocity ;Moving the hull must be in an if statement like this, otherwise it randomly moves foward.
      If KeyDown(RIGHTKEY) = True Then TurnEntity thisTarget\Turret,0,-5,0
      If KeyDown(LEFTKEY) = True Then TurnEntity thisTarget\Turret,0,5,0
      If KeyDown(UPKEY) And EntityPitch#(thisTarget\Barrel) > -10 Then TurnEntity thisTarget\Barrel,-1,0,0
      If KeyDown(DOWNKEY) And EntityPitch#(thisTarget\Barrel) < 5 Then TurnEntity thisTarget\Barrel,1,0,0
      If KeyHit(PKEY) = True Then Paused = 1
      If KeyHit(SPACEKEY) = True 
        FireGunXYZ_PYR[0] = EntityX(thisTarget\Barrel)
        FireGunXYZ_PYR[1] = EntityY(thisTarget\Barrel)
        FireGunXYZ_PYR[2] = EntityZ(thisTarget\Barrel)
        FireGunXYZ_PYR[3] = EntityPitch#(thisTarget\Barrel)
        FireGunXYZ_PYR[4] = EntityYaw#(thisTarget\Barrel)
        FireGunXYZ_PYR[5] = EntityRoll#(thisTarget\Barrel)
        FireGun()
      EndIf
    EndIf
    If (EntityY(thisTarget\Hull)) < -5;If i fall off the map, make me go back to start
        Velocity = 0.0
        FreeEntity thisTarget\Hull
        FreeEntity thisTarget\Turret
        FreeEntity thisTarget\Barrel
        Delete thisTarget
    EndIf
    If CountCollisions (thisTarget\Hull)
      FreeEntity thisTarget\Hull
      FreeEntity thisTarget\Turret
      FreeEntity thisTarget\Barrel
      Delete thisTarget
      Score = Score+10
    EndIf
  Next
  For thisTarget2.Target2type = Each Target2type    ;iterate through all of the targets
;  TranslateEntity thisTarget\entityhandle,0,-.1,0;gravity
    If thisTarget2\Selected = 1;Airplane stuffs
      If KeyDown(AKEY) = True Then TurnEntity thisTarget2\Airplane,0,1,0
      If KeyDown(DKEY) = True Then TurnEntity thisTarget2\Airplane,0,-1,0
      If KeyDown(WKEY) And Velocity < 0.200 Then Velocity=Velocity+0.002
      If KeyDown(SKEY) And Velocity > -0.0 Then Velocity=Velocity-0.002
      If Velocity > 0.001 Or Velocity < -0.001 Then MoveEntity thisTarget2\Airplane,0,0, Velocity ;Moving the hull must be in an if statement like this, otherwise it randomly moves foward.
      If KeyDown(RIGHTKEY) = True Then TurnEntity thisTarget2\Airplane,0,0,-1
      If KeyDown(LEFTKEY) = True Then TurnEntity thisTarget2\Airplane,0,0,1
      If KeyDown(UPKEY) Then TurnEntity thisTarget2\Airplane,1,0,0
      If KeyDown(DOWNKEY) Then TurnEntity thisTarget2\Airplane,-1,0,0
      If KeyHit(PKEY) = True Then Paused = 1
      If KeyDown(SPACEKEY) = True
        If SideOfPlane = 1
          FireGunXYZ_PYR[0] = EntityX#(thisTarget2\Barrel1)
          FireGunXYZ_PYR[1] = EntityY#(thisTarget2\Barrel1)
          FireGunXYZ_PYR[2] = EntityZ#(thisTarget2\Barrel1)
          FireGunXYZ_PYR[3] = EntityPitch#(thisTarget2\Barrel1)
          FireGunXYZ_PYR[4] = EntityYaw#(thisTarget2\Barrel1)
          FireGunXYZ_PYR[5] = EntityRoll#(thisTarget2\Barrel1)
          FireGun()
          SideOfPlane = 2
        ElseIf SideOfPlane = 2
          FireGunXYZ_PYR[0] = EntityX(thisTarget2\Barrel2)
          FireGunXYZ_PYR[1] = EntityY(thisTarget2\Barrel2)
          FireGunXYZ_PYR[2] = EntityZ(thisTarget2\Barrel2)
          FireGunXYZ_PYR[3] = EntityPitch#(thisTarget2\Barrel2)
          FireGunXYZ_PYR[4] = EntityYaw#(thisTarget2\Barrel2)
          FireGunXYZ_PYR[5] = EntityRoll#(thisTarget2\Barrel2)
          FireGun()
          SideOfPlane = 1
        EndIf
      EndIf
    EndIf
    If CountCollisions (thisTarget2\Airplane) And thisTarget2\Selected = 0
      ;If thisTarget2\Selected = 1
      ;  thisTarget2\Selected = 0
      ;  PlaceTankandAirplane()
      ;EndIf
      FreeEntity thisTarget2\Airplane  
      FreeEntity thisTarget2\Barrel1
      FreeEntity thisTarget2\Barrel2
      Delete thisTarget2
      Score = Score+10
    EndIf
  Next
End Function

;Move the airborn targets
Function KeepTargetsMoving()
  For thisTarget2.Target2Type = Each Target2Type			;iterate through all of the bullets
    If thisTarget2\Selected = 0
      MoveEntity thisTarget2\Airplane , 0, 0, .15			;move the bullet forward along the bullets Z axis
      If (EntityZ(thisTarget2\Airplane)) > 130 And EntityYaw#(thisTarget2\Airplane) < 110 Or EntityYaw#(thisTarget2\Airplane) < -110;north, 0
        TurnEntity (thisTarget2\Airplane),0,Rand(110,340),0
      EndIf
      If (EntityX(thisTarget2\Airplane)) > 130 And EntityYaw#(thisTarget2\Airplane) < 20 Or EntityYaw#(thisTarget2\Airplane) > 160;east, -90
        TurnEntity (thisTarget2\Airplane),0,Rand(110,340),0
       EndIf
      If (EntityX(thisTarget2\Airplane)) < -2 And EntityYaw#(thisTarget2\Airplane) > -20 Or EntityYaw#(thisTarget2\Airplane) < -160;west, 90
        TurnEntity (thisTarget2\Airplane),0,Rand(110,340),0
     EndIf
      If (EntityZ(thisTarget2\Airplane)) < -2 And EntityYaw#(thisTarget2\Airplane) > 70 Or EntityYaw#(thisTarget2\Airplane) < -70;south, 180
        TurnEntity (thisTarget2\Airplane),0,Rand(110,340),0
      EndIf
    EndIf
  Next
End Function

;Move trash around
Function KeepTrashMoving()
  For thisTrash.TrashType = Each TrashType			;iterate through all of the bullets
    MoveEntity thisTrash\entityhandle,thisTrash\X,thisTrash\Y,thisTrash\Z
    If MilliSecs() > thisTrash\KillTimer + thisTrash\KillTime
      FreeEntity thisTrash\entityhandle
      Delete thisTrash
    EndIf
  Next
End Function

;Deals with most of the text in the game.
Function Textme()
  A# = Velocity
  If A < 0.002 Then A = 0;avoids 0 speed glitch
  A = A*100
  If A > 9.9 And ChooseVehicle = 1 Then A = 10;avoids 9.9/10.0 switchy speed glitch
  Text 0,10,"     Score:" + Score
  Text 0,22,"ShotsFired:" + ShotsFired
  Text 0,34,"     Speed:" + Left$ (Str A, 4)
  If ChooseVehicle = 1 Then Text 0,46,"  Position:X=" + EntityX(Hull) + ",Z=" + EntityZ(Hull) + ",R=" + EntityYaw(Hull)
  If ChooseVehicle = 2 Then Text 0,46,"  Position:X=" + EntityX(Airplane) + ",Z=" + EntityZ(Airplane) +  ",R=" + EntityYaw(Airplane)
  If Paused=1
    Paused=0
    Text 320,240,"[Paused]",True,True
    If ChooseVehicle = 1 Then Text 320,252,"Press 1 to switch to the Plane",True,True
    If ChooseVehicle = 2 Then Text 320,252,"Press 1 to switch to the Tank",True,True
    Flip
    Q = 0
    While Q = 0
      If KeyHit(ESCKEY) = True Then End
      If KeyHit(PKEY) = True Then Q = 1
      If KeyHit(2) = True
        ResetAndSwitchVehicles()
        Q = 1
      EndIf
    Wend
  EndIf
End Function

;Used to switch vehicals mid-game
Function ResetAndSwitchVehicles()
  Velocity = 0.0
    If ChooseVehicle = 1
      ChooseVehicle = 2
      EntityParent Camera, Airplane, False
;      PositionEntity Camera,0,3,-8
    ElseIf ChooseVehicle = 2
      ChooseVehicle = 1
      EntityParent Camera, Turret, False
;      PositionEntity Camera,0,2,-4
    EndIf
  PlaceTankandAirplane()
End Function

;Place stuff the first time and when we switch vehicles. RotateEntity is there for the latter, so it looks better.
Function PlaceTankandAirplane()
  If ChooseVehicle = 1
    Target.Targettype = New Targettype				;create a target
    Target\Hull = CopyEntity(Hull)		;create the target mesh
    Target\Turret = CopyEntity(Turret)		;create the target mesh
    Target\Barrel = CopyEntity(Barrel)		;create the target mesh
    Target\Selected=1
    EntityParent Camera, Target\Hull, False
    PositionEntity Target\Hull,42,.5,50:EntityColor Target\Hull,255,000,000
    ;Otherwise we can get stuck on targets, ground, and other vehicles.
    ResetEntity Target\Hull
  ElseIf ChooseVehicle = 2
    Target2.Target2type = New Target2type				;create a target
    Target2\Airplane = CopyEntity(Airplane)		;create the target mesh
    Target2\Barrel1 = CopyEntity(Barrel1)		;create the target mesh
    Target2\Barrel2 = CopyEntity(Barrel2)		;create the target mesh
    Target2\Selected=1
    EntityParent Camera, Target2\Airplane, False
    PositionEntity Target2\Airplane,42,.5,50:EntityColor Target2\Airplane,255,000,000
    RotateEntity Target2\Airplane, EntityPitch=0, EntityYaw=0, EntityRoll=0
    ResetEntity Target2\Airplane
  EndIf
End Function

Edited by: Rrtaya_Tsamsiyu

Reply to thread
click to sign in and post

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.