ApplyEntityImpulse

Everything related to 3D programming
Philippe-felixer76-3
User
User
Posts: 42
Joined: Mon Dec 30, 2013 10:12 pm

ApplyEntityImpulse

Post by Philippe-felixer76-3 »

Hi,

I'm working on a simple game with tanks and bullets. Every now and then there seems to be a problem with the ApplyEntityImpulse function, i think.

The game has a lot lines of code, so i started to strip down and reduce the code to find the root cause of this problem.

No i have a example of code (with still a lot of lines 950+) that will produce this problem i have.

The problem in a nutshell, i use ApplyEntityImpulse to fire a bullet, EnableWorldCollisions is turned off and EnableWorldPhysics is turned on, with the function EntityCollide i check if a bullet hit's a shield or tank. Bullet, Tank and Shield all have entity body's, and all of them have SetEntityCollisionFilter to 0. In the example code TANK 0 will continue to fire bullets at TANK 1, when the shield of TANK 1 is down it will continu to hit the TANK itself. On my system the time for the bullet to reach the TANK 1 is about 5 to 6 seconds.

When i run the example the first few bullets mostly hit the targets without any problems, but after some random seconds the problem will appear, the bullet hit's the target within 0 or 1 millisecond. Wich is impossible. Just did a test and the problem happend at 95403 milliseconds, second test the problem happend at 119418 milliseconds. It's completely random each time.

What am i doing wrong here? Is there a alernative to ApplyEntityImpulse?

Code: Select all

UseJPEGImageDecoder()

IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

EnableExplicit 

Structure Vector3
  x.f
  y.f
  z.f
EndStructure

Structure weapons
  type.l   
  name.s   
  impact.l 
  amount.l  
  scale.f
  ownerbody.l
EndStructure

Structure shields
  type.l
  name.s
  power.l
  maxpower.l
  amount.l
  mesh.l
  matmesh.l
  mattexture.l
EndStructure

Structure bullets
  bullet.l
  ribbon.l
  tickcount.l
  x.l
  y.l
  bulletinfo.weapons
EndStructure

Structure tank
  entity.l
  name.s
EndStructure

Structure alltanks
  name.s
  koepel.l
  loop.l
  sloop.l
  eloop.l
  schild.l
  schildmat.l
  body.l
  node.l
  pbody.l
  speed.f
  ingame.l 
  activeweapon.l  
  activeshield.l  
  List weapon.weapons()
  List shield.shields()
  List tank.tank()
EndStructure

Structure program
  sw.l                            ; screenwidth
  sh.l                            ; screenheight
  event.l                         ; window event
  dx.l                            ; terrain width
  dz.l                            ; terrain depth
  plane.l                         ; terrain mesh
  eplane.l                        ; terrain entity
  Array t.PB_MeshVertex(0,0)      ; terrain mesh vertex
  mblack.l                        ; tank material
  tblack.l                        ; tank texture
  myellow.l                       ; tank material
  tyellow.l                       ; tank texture
  mred.l                          ; tank material
  tred.l                          ; tank texture
  tentity.l                       ; target entity for the camera
  selectedcamera.l                ; selected camera
  List weapons.weapons()          ; beschikbare wapens
  List shields.shields()          ; beschikbare schilden
  List tanks.alltanks()           ; alle tanks
  List bullet.bullets()           ; flying bullets..
  bulletentity.l                  ; bullet entity (from ball mesh)
  ribbonmaterial.l                ; material for the ribbon
  camdist.f                       ; camera afstand
  camdisto.f                      ; camera afstand
  camposprocent.f                 ; camera position percent
  camrotprocent.f                 ; camera rotation procent
  camheight.f                     ; camera height
  camangle.f                      ; camera angle
  beurt.l                         ; welke speler is er aan de beurt  
  List explosion.bullets()        ; explosions..
  windx.f                         ; wind level x
  windz.f                         ; wind level z
  minx.f                          ; minimal x position tank
  maxx.f                          ; maximal x potition tank
  minz.f                          ; minimal z potition tank
  maxz.f                          ; maximal z position tank
EndStructure

Global prg.program, debugging.l = 1
prg\dx = 512
prg\dz = 512
Dim prg\t(prg\dx, prg\dz)

Macro SubVector3(V, V1, V2)
  V\x = V1\x - V2\x
  V\y = V1\y - V2\y
  V\z = V1\z - V2\z
EndMacro

Procedure Normalize(*V.Vector3)
  Define.f magSq, oneOverMag 
  magSq = *V\x * *V\x + *V\y * *V\y + *V\z * *V\z 
  If magsq > 0
    oneOverMag = 1.0 / Sqr(magSq)
    *V\x * oneOverMag
    *V\y * oneOverMag
    *V\z * oneOverMag
  EndIf  
EndProcedure

Procedure AddMesh(Mesh, x.f=0 , y.f=0, z.f=0, ScaleX.f=1, ScaleY.f=1, ScaleZ.f=1, RotateX.f=0, RotateY.f=0, RotateZ.f=0)
  Protected Dim MeshData.PB_MeshVertex(0), Dim MeshDataInd.PB_MeshFace(0)
  Protected ArrSize, ArrSizeInd, c, i, mdx.f, mdy.f, mdz.f, subMesh.i
  For subMesh = 0 To SubMeshCount(Mesh)-1   
    TransformMesh(Mesh, x, y, z, ScaleX,ScaleY,ScaleZ, RotateX,RotateY,RotateZ, subMesh)
    GetMeshData(Mesh, subMesh, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate | #PB_Mesh_Normal, 0, MeshVertexCount(Mesh, subMesh)-1)
    GetMeshData(Mesh, subMesh, MeshDataInd(), #PB_Mesh_Face , 0, MeshIndexCount(Mesh, subMesh)-1)
    ArrSize = ArraySize(MeshData())
    For c=0 To ArrSize   
      mdx = MeshData(c)\x
      mdy = MeshData(c)\y
      mdz = MeshData(c)\z
      MeshVertexPosition(mdx, mdy, mdz)
      mdx = MeshData(c)\NormalX
      mdy = MeshData(c)\NormalY
      mdz = MeshData(c)\NormalZ
      MeshVertexNormal(mdx, mdy, mdz)
      mdx = MeshData(c)\TangentX
      mdy = MeshData(c)\TangentY
      mdz = MeshData(c)\TangentZ
      MeshVertexTangent(mdx, mdy, mdz)
      MeshVertexTextureCoordinate(MeshData(c)\u, MeshData(c)\v)       
    Next   
    ArrSizeInd = ArraySize(MeshDataInd())
    For i=0 To ArrSizeInd Step 3
      MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index,MeshDataInd(i+2)\Index)
    Next
    AddSubMesh(#PB_Mesh_TriangleList)
  Next
EndProcedure

Procedure.l createwheel(mred.l, mblack.l)
  Protected mesh11.l, ent.l
  mesh11.l = CreateMesh(#PB_Any, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
  AddMesh(CreateTube(#PB_Any, 0.5, 0.4, 2), 0,  0,  0,   2,     3, 2);10,0.05,10)            ;Sub-Mesh 0 to 5
  AddMesh(CreateCylinder(#PB_Any, 1, 4),    0,  0,  0,  0.9, 1.48, 0.9) ;0.1,1,10)           ;Sub-Mesh 6 to 11
  AddMesh(CreateCylinder(#PB_Any, 1, 4),    0,  0,  0,  0.5,  1.5, 0.5)   ;0.1,1,10)            ;Sub-Mesh 12 to 17
  FinishMesh(#True)
  ent.l = CreateEntity(#PB_Any, MeshID(mesh11.l), MaterialID(mred), 0,  1, 0)
  ProcedureReturn ent.l
EndProcedure

Procedure.l DrawCube(mid.l, wi.l=5, he.l=5, de.l=5, umw.l = 0, mat.l=2)
  Protected eid.l
  Define.Vector3 lbv, rbv, lba, rba, lov, rov, loa, roa
  lbv\x = -wi + umw
  lbv\y = he 
  lbv\z = de - umw
  rbv\x = wi - umw
  rbv\y = he 
  rbv\z = de - umw
  lba\x = -wi + umw
  lba\y = he 
  lba\z = -de + umw
  rba\x = wi - umw
  rba\y = he 
  rba\z = -de + umw
  lov\x = -wi
  lov\y = -he
  lov\z = de
  rov\x = wi
  rov\y = -he
  rov\z = de
  loa\x = -wi
  loa\y = -he
  loa\z = -de
  roa\x = wi
  roa\y = -he
  roa\z = -de
  CreateMesh(mid.l, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
  ;back
  MeshVertexPosition(lba\x,lba\y,lba\z)
  MeshVertexNormal(0,0,-1)
  MeshVertexTextureCoordinate(1/6, 0)
  MeshVertexPosition(rba\x, rba\y, rba\z)
  MeshVertexNormal(0,0,-1)
  MeshVertexTextureCoordinate(0,0)
  MeshVertexPosition(roa\x, roa\y, roa\z)
  MeshVertexNormal(0,0,-1)
  MeshVertexTextureCoordinate(0,1)
  MeshVertexPosition(loa\x, loa\y, loa\z)
  MeshVertexNormal(0,0,-1)
  MeshVertexTextureCoordinate(1/6, 1)
  ;front   
  MeshVertexPosition(lbv\x, lbv\y, lbv\z)
  MeshVertexNormal(0,0,1)
  MeshVertexTextureCoordinate(1/6, 0)
  MeshVertexPosition(rbv\x, rbv\y, rbv\z)
  MeshVertexNormal(0,0,1)
  MeshVertexTextureCoordinate(2/6, 0)
  MeshVertexPosition(rov\x, rov\y, rov\z)
  MeshVertexNormal(0,0,1)
  MeshVertexTextureCoordinate(2/6, 1)
  MeshVertexPosition(lov\x, lov\y, lov\z)
  MeshVertexNormal(0,0,1)
  MeshVertexTextureCoordinate(1/6, 1)
  ;left   
  MeshVertexPosition(lba\x, lba\y, lba\z)
  MeshVertexNormal(-1,0,0)
  MeshVertexTextureCoordinate(4/6, 0)
  MeshVertexPosition(lbv\x, lbv\y, lbv\z)
  MeshVertexNormal(-1,0,0)
  MeshVertexTextureCoordinate(5/6, 0)
  MeshVertexPosition(lov\x, lov\y, lov\z)
  MeshVertexNormal(-1,0,0)
  MeshVertexTextureCoordinate(5/6, 1)
  MeshVertexPosition(loa\x, loa\y, loa\z)
  MeshVertexNormal(-1,0,0)
  MeshVertexTextureCoordinate(4/6, 1)
  ;right   
  MeshVertexPosition(rba\x, rba\y, rba\z)
  MeshVertexNormal(1,0,0)
  MeshVertexTextureCoordinate(4/6, 0)
  MeshVertexPosition(roa\x, roa\y, roa\z)
  MeshVertexNormal(1,0,0)
  MeshVertexTextureCoordinate(4/6, 1)
  MeshVertexPosition(rbv\x, rbv\y, rbv\z)
  MeshVertexNormal(1,0,0)
  MeshVertexTextureCoordinate(3/6, 0)
  MeshVertexPosition(rov\x, rov\y, rov\z)
  MeshVertexNormal(1,0,0)
  MeshVertexTextureCoordinate(3/6, 1)
  ;bottom   
  MeshVertexPosition(loa\x, loa\y, loa\z)
  MeshVertexNormal(0,1,0)
  MeshVertexTextureCoordinate(5/6, 1)
  MeshVertexPosition(roa\x, roa\y, roa\z)
  MeshVertexNormal(0,1,0)
  MeshVertexTextureCoordinate(6/6, 1)
  MeshVertexPosition(rov\x, rov\y, rov\z)
  MeshVertexNormal(0,1,0)
  MeshVertexTextureCoordinate(6/6, 0)
  MeshVertexPosition(lov\x, lov\y, lov\z)
  MeshVertexNormal(0,1,0)
  MeshVertexTextureCoordinate(5/6, 0)
  ;top   
  MeshVertexPosition(lba\x,lba\y,lba\z)
  MeshVertexNormal(0,1,0)
  MeshVertexTextureCoordinate(2/6,0)
  MeshVertexPosition(rba\x, rba\y, rba\z)
  MeshVertexNormal(0,1,0)
  MeshVertexTextureCoordinate(3/6,0)
  MeshVertexPosition(rbv\x, rbv\y, rbv\z)
  MeshVertexNormal(0,1,0)
  MeshVertexTextureCoordinate(3/6,1)
  MeshVertexPosition(lbv\x, lbv\y, lbv\z)
  MeshVertexNormal(0,1,0)
  MeshVertexTextureCoordinate(2/6,1)
  ; connecting vertices together
  MeshFace(0, 2, 3)
  MeshFace(0, 1, 2)
  MeshFace(4, 6, 5)
  MeshFace(4, 7, 6)
  MeshFace(8, 10, 9)
  MeshFace(8, 11, 10)
  MeshFace(12, 14, 13)
  MeshFace(13, 14, 15)
  MeshFace(16, 17, 18)
  MeshFace(16, 18, 19)
  MeshFace(20, 23, 21)
  MeshFace(21, 23, 22)   
  FinishMesh(#True)        
  eid.l = CreateEntity(#PB_Any, MeshID(mid.l),  MaterialID(mat) ,0,0,0)
  MoveEntity(eid.l, 0, 4, 0)
  ProcedureReturn eid.l
EndProcedure

Procedure AddShoot(enthi.l, entlo.l) 
  Protected Bullet.l, PosCanon.Vector3, PosShoot.Vector3, Direction.Vector3
  SelectElement(prg\tanks()\weapon(), prg\tanks()\activeweapon)
  If prg\tanks()\weapon()\amount>0 Or prg\tanks()\weapon()\amount=-1
    Bullet = CopyEntity(prg\bulletentity, #PB_Any)
    ScaleEntity(Bullet, 0.3, 0.3, 0.3)
    CreateEntityBody(Bullet, #PB_Entity_SphereBody, 1, -1, -1) 
    SetEntityCollisionFilter(Bullet, 0, 0)
    MoveEntity(Bullet, EntityX(enthi.l), EntityY(enthi.l), EntityZ(enthi.l), #PB_Absolute) 
    AddElement(prg\Bullet())
    prg\Bullet()\bullet = Bullet
    prg\Bullet()\tickcount = ElapsedMilliseconds()
    prg\Bullet()\ribbon = CreateRibbonEffect(#PB_Any, MaterialID(prg\ribbonmaterial), 1, 80, 120)
    prg\tanks()\weapon()\ownerbody = prg\tanks()\body
    CopyStructure( prg\tanks()\weapon(), prg\Bullet()\bulletinfo, weapons)
    If prg\tanks()\weapon()\amount>-1 
      prg\tanks()\weapon()\amount = prg\tanks()\weapon()\amount - 1
      If prg\tanks()\weapon()\amount < 0
        prg\tanks()\weapon()\amount  = 0
      EndIf
    EndIf
    RibbonEffectColor(prg\Bullet()\ribbon, 0, RGBA(255, 50, 0, 255), RGBA(1, 5, 255, 5))
    RibbonEffectWidth(prg\Bullet()\ribbon, 0, 0.3, 1) 
    AttachRibbonEffect(prg\Bullet()\ribbon, EntityParentNode(Bullet))
    PosCanon\x = EntityX(entlo.l)
    PosCanon\y = EntityY(entlo.l)
    PosCanon\z = EntityZ(entlo.l)
    PosShoot\x = EntityX(enthi.l)
    PosShoot\y = EntityY(enthi.l)
    PosShoot\z = EntityZ(enthi.l)
    SubVector3(Direction, PosShoot, PosCanon)
    Normalize(Direction)
    If debugging = 1 
      Debug "Bullet position X: " + Str(EntityX(Bullet))
      Debug "Bullet position Y: " + Str(EntityY(Bullet))
      Debug "Bullet position Z: " + Str(EntityZ(Bullet))
      Debug "ApplyEntityImpulseX: " + Str((Direction\x + prg\windx.f)  * (prg\tanks()\speed + 6 )) 
      Debug "ApplyEntityImpulseY: " + Str((Direction\y * ( prg\tanks()\speed - 10) ))
      Debug "ApplyEntityImpulseZ: " + Str((Direction\z + prg\windz.f) * (prg\tanks()\speed + 6)) 
    EndIf
    ApplyEntityImpulse(Bullet, (Direction\x + prg\windx.f)  * (prg\tanks()\speed + 6 ), Direction\y * ( prg\tanks()\speed - 10), (Direction\z + prg\windz.f) * (prg\tanks()\speed + 6))
    ResetList(prg\tanks())
    While NextElement(prg\tanks())
      If prg\tanks()\ingame = 1
        If IsEntity(prg\tanks()\schild)
          SetEntityCollisionFilter(prg\tanks()\schild, 0, 0) 
        EndIf
      EndIf
    Wend  
    SetEntityCollisionFilter(Bullet, 0, 0) 
    prg\camdisto = prg\camdist
  EndIf 
EndProcedure

Procedure maaktank(mred.l, colgroup.l, colmask.l, ingame.l, rgb.l)
  Protected bm.l, tbody.l, ent.l, ent1.l, ent2.l, ent3.l, ent4.l, ent5.l, sphere.l, ent6.l, mtorus.l, torus.l, bal1.l, bal2.l, cube1.l, Compound.l, box.l, boxe.l
  AddElement(prg\tanks())
  prg\tanks()\name = "TEST"
  NewList prg\tanks()\tank()
  ; Tank body ..
  tbody.l = DrawCube(3, 3, 1, 3, 1, mred)
  AddElement( prg\tanks()\tank() )
  prg\tanks()\tank()\entity = tbody.l
  prg\tanks()\tank()\name  = "tbody"
  If debugging.l=1
    Debug "prg\tanks()\tank()\entity: " + Str(prg\tanks()\tank()\entity)
  EndIf  
  ; WHeel 1
  bm.l = prg\mblack.l
  ent.l = createwheel(mred,  bm.l)
  MoveEntity(ent.l, -3, 1, 0)
  RotateEntity(ent.l, 90, 0, 0, #PB_Absolute)
  AddElement( prg\tanks()\tank() )
  prg\tanks()\tank()\entity = ent.l
  prg\tanks()\tank()\name  = "wheel"
  ; wheel 2
  ent1.l = createwheel(mred,  bm.l)
  MoveEntity(ent1.l, 3, 1, 0)
  RotateEntity(ent1.l, 90, 0, 0, #PB_Absolute)
  AddElement( prg\tanks()\tank() )
  prg\tanks()\tank()\entity = ent1.l
  prg\tanks()\tank()\name  = "wheel1"
  ; Rubs band boven
  ent2.l = CreateEntity(#PB_Any, MeshID(CreateCube(#PB_Any, 2)), MaterialID(mred))
  MoveEntity(ent2.l, 0, 2.9, 0)
  ScaleEntity(ent2.l, 3, 0.1, 3)
  AddElement( prg\tanks()\tank() )
  prg\tanks()\tank()\entity = ent2.l
  prg\tanks()\tank()\name  = "rubsband boven"
  ; Rubs band onder
  ent3.l = CreateEntity(#PB_Any, MeshID(CreateCube(#PB_Any, 2)), MaterialID(mred))
  MoveEntity(ent3.l, 0, 1.1, 0)
  ScaleEntity(ent3.l, 3, 0.1, 3)
  AddElement( prg\tanks()\tank() )
  prg\tanks()\tank()\entity = ent3.l
  prg\tanks()\tank()\name  = "rubsband onder"
  ; wielen midden1
  ent4.l = createwheel(mred,  bm.l)
  MoveEntity(ent4.l, 1, 1, 0)
  ScaleEntity(ent4.l, 1, 0.9, 1)
  RotateEntity(ent4.l, 90, 0, 0, #PB_Absolute)
  AddElement( prg\tanks()\tank() )
  prg\tanks()\tank()\entity = ent4.l
  prg\tanks()\tank()\name  = "wheel3"
  ; wielen midden2
  ent5.l = createwheel(mred,  bm.l)
  MoveEntity(ent5.l, -1, 1, 0)
  ScaleEntity(ent5.l, 1, 0.9, 1)
  RotateEntity(ent5.l, 90, 0, 0, #PB_Absolute)
  AddElement( prg\tanks()\tank() )
  prg\tanks()\tank()\entity = ent5.l
  prg\tanks()\tank()\name  = "wheel4"
  ; koepel
  sphere.l = CreateSphere(#PB_Any, 1)
  ent6.l = CreateEntity(#PB_Any, MeshID(sphere), MaterialID(mred))
  ScaleEntity(ent6.l, 1.6, 1.1, 1.6)
  MoveEntity(ent6.l, 0, 5, 0)
  prg\tanks()\koepel = ent6.l
  AddElement( prg\tanks()\tank() )
  prg\tanks()\tank()\entity = ent6.l
  prg\tanks()\tank()\name  = "koepel"
  mtorus.l = CreateTube(#PB_Any, 0.4, 0.4, 4)
  torus.l = CreateEntity(#PB_Any, MeshID(mtorus.l), MaterialID(mred))
  TransformMesh(mtorus, 0, 3, 0, 1, 1, 1, 0, 0, 0)
  prg\tanks()\loop = torus
  AddElement( prg\tanks()\tank() )
  prg\tanks()\tank()\entity = torus.l
  prg\tanks()\tank()\name  = "loop"
  ; balls
  bal1.l = CreateEntity(#PB_Any, MeshID(CreateSphere(#PB_Any, 1)), MaterialID(prg\myellow),  0,  4, 0) ;10, 0) ; bovenste
  ScaleEntity(bal1.l, 0.3, 0.3, 0.3)
  AddElement( prg\tanks()\tank() )
  prg\tanks()\tank()\entity = bal1.l
  prg\tanks()\tank()\name  = "bal1"
  bal2.l = CreateEntity(#PB_Any, MeshID(CreateSphere(#PB_Any, 1)), MaterialID(prg\myellow),  0,  2.2, 0) ; onderste
  ScaleEntity(bal2.l, 0.3, 0.3, 0.3)
  prg\tanks()\sloop = bal1
  prg\tanks()\eloop = bal2
  AddElement( prg\tanks()\tank() )
  prg\tanks()\tank()\entity = bal2.l
  prg\tanks()\tank()\name  = "bal2"
  AddSubEntity(torus.l, bal1.l, #PB_Entity_SphereBody)
  AddSubEntity(torus.l, bal2.l, #PB_Entity_SphereBody)
  AddSubEntity(ent6.l, torus.l, #PB_Entity_SphereBody)
  HideEntity(bal1.l, 1)
  HideEntity(bal2.l, 1) 
  cube1.l = CreateCube(#PB_Any, 1)
  Compound = CreateEntity(#PB_Any, MeshID(cube1.l), MaterialID(mred))
  HideEntity(Compound, 1)
  AddElement( prg\tanks()\tank() )
  prg\tanks()\tank()\entity = Compound
  prg\tanks()\tank()\name  = "Compound"
  prg\tanks()\body = Compound
  ; Camera node
  prg\tanks()\node =  CreateEntity(#PB_Any, MeshID(sphere), MaterialID(mred))
  ScaleEntity(prg\tanks()\node , 1.6, 1.1, 1.6)
  MoveEntity(prg\tanks()\node , 0, 5, 0)
  HideEntity(prg\tanks()\node, 1)
  AddSubEntity(Compound, prg\tanks()\node,  #PB_Entity_BoxBody)
  ; add subentitys
  AddSubEntity(Compound, tbody,  #PB_Entity_BoxBody)
  AddSubEntity(Compound, ent, #PB_Entity_CapsuleBody)
  AddSubEntity(Compound, ent1, #PB_Entity_CapsuleBody)
  AddSubEntity(Compound, ent2,  #PB_Entity_BoxBody)
  AddSubEntity(Compound, ent3,  #PB_Entity_BoxBody)
  AddSubEntity(Compound, ent4.l, #PB_Entity_CapsuleBody)
  AddSubEntity(Compound, ent5.l, #PB_Entity_CapsuleBody)
  AddSubEntity(Compound, ent6.l, #PB_Entity_SphereBody)
  AddSubEntity(Compound, torus,  #PB_Entity_ConvexHullBody)
  MoveEntity(Compound, 0, -4, 0 , #PB_Local)
  FetchOrientation(EntityID(torus), #PB_Relative)
  SetOrientation(EntityID(torus), 0.128, 0, 0, GetW()) ; x op 0.6
  FetchOrientation(EntityID(ent6), #PB_Relative)
  SetOrientation(EntityID(ent6), 0.0, 1, 0, GetW()) ; y op 0.9
  prg\tanks()\ingame = ingame
  If ingame=1
    ; tank body
    box.l = CreateCube(#PB_Any, 10)
    boxe.l = CreateEntity(#PB_Any, MeshID(box.l), #PB_Material_None) 
    ScaleEntity(boxe.l, 0.8, 0.5, 0.6)
    CreateEntityBody(boxe.l, #PB_Entity_BoxBody, 1, -1, -1) ;0, 0) ;) 
    prg\tanks()\pbody = boxe
    ; body .. 
    HideEntity(prg\tanks()\pbody, 1)
    SetEntityCollisionFilter(prg\tanks()\pbody, 0, 0) 
    SelectElement(prg\weapons(), 0)
    AddElement(prg\tanks()\weapon())
    CopyStructure(prg\weapons(), prg\tanks()\weapon(), weapons) 
    SelectElement(prg\weapons(), 1)
    AddElement(prg\tanks()\weapon())
    CopyStructure(prg\weapons(), prg\tanks()\weapon(), weapons) 
    prg\tanks()\weapon()\amount = 3000 ;3
    prg\tanks()\activeweapon = 1 ; big shot
    prg\tanks()\speed = 40 
    SelectElement(prg\shields(), 0)
    AddElement(prg\tanks()\shield())
    CopyStructure(prg\shields(), prg\tanks()\shield(), shields) 
    prg\tanks()\shield()\mesh = CopyMesh(prg\shields()\mesh, #PB_Any)
    prg\tanks()\shield()\matmesh = CopyMaterial(prg\shields()\matmesh, #PB_Any)
    prg\tanks()\shield()\mattexture = CopyTexture(prg\shields()\mattexture, #PB_Any)
    prg\tanks()\shield()\maxpower= prg\tanks()\shield()\power
    SelectElement(prg\shields(), 1)
    AddElement(prg\tanks()\shield())
    CopyStructure(prg\shields(), prg\tanks()\shield(), shields) 
    prg\tanks()\shield()\mesh = CopyMesh(prg\shields()\mesh, #PB_Any)
    prg\tanks()\shield()\matmesh = CopyMaterial(prg\shields()\matmesh, #PB_Any)
    prg\tanks()\shield()\mattexture = CopyTexture(prg\shields()\mattexture, #PB_Any)
    prg\tanks()\shield()\maxpower= prg\tanks()\shield()\power
    prg\tanks()\activeshield = 0
  EndIf
EndProcedure

Procedure initweaponsandshields()
  Protected txt.l, mat.l, roundmesh.l, i.l, vertexNumbers.l, mesh.l, ArrSizeInd.l, Dim shield.PB_MeshVertex(0), Dim MeshDataInd.PB_MeshFace(0)
  NewList prg\weapons()
  AddElement(prg\weapons())
  prg\weapons()\name = "..."
  prg\weapons()\amount = -1
  prg\weapons()\impact = 1  
  prg\weapons()\type   = 0  
  prg\weapons()\scale  = 2 
  AddElement(prg\weapons())
  prg\weapons()\name = "..."
  prg\weapons()\amount = 1 
  prg\weapons()\impact = 10 
  prg\weapons()\type   = 0  
  prg\weapons()\scale  = 4 
  txt.l = CreateTexture(#PB_Any, 255, 255) 
  StartDrawing(TextureOutput(txt.l))
    DrawingMode(#PB_2DDrawing_AllChannels) 
    Box(0,0,255,255, RGBA(255,255,255,200))
  StopDrawing()
  mat.l = CreateMaterial(#PB_Any, TextureID(txt.l))
  MaterialBlendingMode(mat, #PB_Material_AlphaBlend) 
  roundmesh.l = CreateSphere(#PB_Any, 1)
  GetMeshData(roundmesh, 0, shield(), #PB_Mesh_Vertex|#PB_Mesh_UVCoordinate |#PB_Mesh_Normal|#PB_Mesh_Color|#PB_Mesh_Tangent,0,MeshVertexCount(roundmesh,0)-1)
  For i = 0 To ArraySize(shield())
    If shield(i)\y>-0.4;.99;<=0.3
      ClearStructure(shield(i), PB_MeshVertex)
      shield(i)\y = -1
    EndIf
  Next
  GetMeshData(roundmesh,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(roundmesh, 0)-1)
  vertexNumbers = MeshVertexCount(roundmesh)
  mesh = CreateMesh(#PB_Any , #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
  For i=0 To vertexNumbers
    MeshVertexPosition(0,0,0)
    MeshVertexTextureCoordinate(0,0)
  Next
  ArrSizeInd = ArraySize(MeshDataInd())
  For i=0 To ArrSizeInd Step 3
    If  i>ArrSizeInd/2 
      MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index,MeshDataInd(i+2)\Index)
    EndIf 
  Next
  FinishMesh(#True)
  SetMeshData(mesh, 0, shield(), #PB_Mesh_Vertex|#PB_Mesh_UVCoordinate |#PB_Mesh_Normal|#PB_Mesh_Color|#PB_Mesh_Tangent, 0, MeshVertexCount(mesh)-1)
  SetMeshData(mesh,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(mesh, 0)-1)
  UpdateMeshBoundingBox(mesh)
  NewList prg\shields()
  AddElement(prg\shields())
  prg\shields()\name   = "..."
  prg\shields()\amount = 10 
  prg\shields()\power  = 50
  prg\shields()\type   = 0 
  prg\shields()\mesh   = CopyMesh(roundmesh, #PB_Any)
  prg\shields()\mattexture = CopyTexture(txt.l, #PB_Any)
  prg\shields()\matmesh= CreateMaterial(#PB_Any, TextureID(prg\shields()\mattexture)) 
  AddElement(prg\shields())
  prg\shields()\name   = "..."
  prg\shields()\amount = 10 
  prg\shields()\power  = 50
  prg\shields()\type   = 2
  prg\shields()\mesh   = CopyMesh(mesh, #PB_Any)
  prg\shields()\mattexture = CopyTexture(txt.l, #PB_Any)
  prg\shields()\matmesh= CreateMaterial(#PB_Any, TextureID(prg\shields()\mattexture)) 
EndProcedure

Procedure hit(impact.f = 0)
  Protected bulletimpact.f, bullettype.f, scale.f, shieldpower.f, maxpower.f, shieldtype.f, deel.f, restpower.f, li.l
  SelectElement(prg\tanks()\shield(), prg\tanks()\activeshield)
  If impact = 0 
    bulletimpact.f = prg\Bullet()\bulletinfo\impact
    bullettype.f   = prg\Bullet()\bulletinfo\type
    scale.f        = prg\Bullet()\bulletinfo\scale   
  EndIf
  shieldpower.f  = prg\tanks()\shield()\power
  maxpower.f     = prg\tanks()\shield()\maxpower
  shieldtype.f   = prg\tanks()\shield()\type
  deel.f         = 200/maxpower 
  restpower.f = 0
  li.l = ListIndex(prg\tanks())
  If shieldpower>0 
    If shieldtype.f = 0
      If impact.f>0
        shieldpower = shieldpower - (impact.f)
      Else
        shieldpower = shieldpower - (bulletimpact * scale)
      EndIf
      If shieldpower<=0 
        If debugging = 1
          Debug "SHIELD DOWN!"
        EndIf
        restpower.f = -shieldpower
        FreeEntityBody(prg\tanks()\schild)
        FreeEntity(prg\tanks()\schild)
        shieldpower = 0
      EndIf
      prg\tanks()\shield()\power = shieldpower
      StartDrawing(TextureOutput(prg\tanks()\shield()\mattexture))
        DrawingMode(#PB_2DDrawing_AllChannels) 
        Box(0,0,255,255, RGBA(255,255,255,Int((deel * shieldpower))))
      StopDrawing()
      SetEntityMaterial(prg\tanks()\schild, MaterialID(prg\tanks()\shield()\matmesh))
    EndIf 
  EndIf
  ; terug naar huidige tank .. 
  SelectElement(prg\tanks(), li)  
EndProcedure

Procedure checkshield(plaatsen.l, tank.l, renew.l=0)
  Protected sel.l, deel.f
  SelectElement(prg\tanks(), tank)

  sel.l = 0 ;GetGadgetState3D(prg\lv3d1.l)
  If sel>-1
    ;SetGadgetText3D(prg\slv3d1.l, GetGadgetItemText3D(prg\lv3d1.l, sel))
    If plaatsen=1
      prg\tanks()\activeshield = sel
      SelectElement(prg\tanks()\shield(), prg\tanks()\activeshield)
      If IsEntity(prg\tanks()\schild)
        FreeEntityBody(prg\tanks()\schild)
        FreeEntity(prg\tanks()\schild)
      EndIf
      If prg\tanks()\shield()\type=0
        deel.f = 200/prg\tanks()\shield()\maxpower 
        StartDrawing(TextureOutput(prg\tanks()\shield()\mattexture))
          DrawingMode(#PB_2DDrawing_AllChannels) 
          Box(0,0,255,255, RGBA(255,255,255,Int((deel * prg\tanks()\shield()\power))))
        StopDrawing()
        If IsMaterial(prg\tanks()\shield()\matmesh): FreeMaterial(prg\tanks()\shield()\matmesh): EndIf
        prg\tanks()\shield()\matmesh = CreateMaterial(#PB_Any, TextureID(prg\tanks()\shield()\mattexture))
        MaterialBlendingMode(prg\tanks()\shield()\matmesh, #PB_Material_AlphaBlend) 
      Else
        StartDrawing(TextureOutput(prg\tanks()\shield()\mattexture))
          DrawingMode(#PB_2DDrawing_AllChannels) 
          Box(0,0,255,255, RGBA(255,255,255,200))
        StopDrawing()
        If IsMaterial(prg\tanks()\shield()\matmesh): FreeMaterial(prg\tanks()\shield()\matmesh): EndIf
        prg\tanks()\shield()\matmesh = CreateMaterial(#PB_Any, TextureID(prg\tanks()\shield()\mattexture))
        MaterialBlendingMode(prg\tanks()\shield()\matmesh, #PB_Material_AlphaBlend) 
      EndIf
      prg\tanks()\schild = CreateEntity(#PB_Any, MeshID(prg\tanks()\shield()\mesh), MaterialID(prg\tanks()\shield()\matmesh),  0,  0, 0) ; bovenste
      ScaleEntity(prg\tanks()\schild, 10, 10, 10) 
      RotateEntity(prg\tanks()\schild, 0, 180, 180)
      CreateEntityBody(prg\tanks()\schild, #PB_Entity_StaticBody, 1, 0, 0) ;#PB_Entity_SphereBody, 100) ;l#PB_Entity_StaticBody, 1)
      SetEntityCollisionFilter(prg\tanks()\schild, 0, 0) ;prg\tanks()\colgroup , #COL_Wapen);SetEntityCollisionFilter(prg\tanks()\schild, prg\tanks()\colgroup , #COL_Wapen)
    EndIf
  EndIf
EndProcedure

Procedure loop()
  Protected sp1.d=1, px.f, py.f, pz.f, time.l, MouseX.l, MouseY.l, kpu.l, kpd.l, kbl.l, kbr.l, keyx.f, keyz.f, tmx.d, kmy.d, hit.l, gocheck.l, raak.l, check.l, tijd.l, pos.Vector3, Bullet.l, col.l, tel.l
  SelectElement(prg\tanks(), 0)
  px.f = EntityX(prg\tanks()\body, #PB_Absolute)
  py.f = EntityY(prg\tanks()\body, #PB_Absolute)
  pz.f = EntityZ(prg\tanks()\body, #PB_Absolute)
  SelectElement(prg\tanks(), 1)
  MoveEntity(prg\tanks()\body, px+70, py, pz, #PB_Absolute)
  SelectElement(prg\tanks(), 2)
  MoveEntity(prg\tanks()\body, px-70, py, pz, #PB_Absolute)
  SelectElement(prg\tanks(), 3)
  MoveEntity(prg\tanks()\body, px, py, pz+70, #PB_Absolute)
  time.l = ElapsedMilliseconds()
  ; loop ..
  Repeat
    prg\event = WindowEvent()
    ; check mouse
    ExamineMouse()
    If MouseButton(#PB_MouseButton_Left)
      MouseX = -MouseDeltaX() *  0.1
      MouseY = -MouseDeltaY() *  0.1
    EndIf
    prg\camdist = prg\camdist - MouseWheel()*10
    If prg\camdist < 0 
      prg\camdist  = 0
    EndIf
    If prg\camdist > prg\dx 
      prg\camdist  = prg\dx
    EndIf
    ; check keyboard    
    ExamineKeyboard()
    kpu.l = KeyboardPushed(#PB_Key_Up)
    kpd.l = KeyboardPushed(#PB_Key_Down)
    kbl.l = KeyboardPushed(#PB_Key_Left)
    kbr.l = KeyboardPushed(#PB_Key_Right)
    keyx.f=(-Bool(kpu)+Bool(kpd))*0.4
    keyz.f=(-Bool(kbl)+Bool(kbr))*0.4    
    If KeyboardPushed(#PB_Key_W)
      tmx.d = sp1.d
    EndIf
    If KeyboardReleased(#PB_Key_W)
      tmx.d = 0
    EndIf
    If KeyboardPushed(#PB_Key_S)
      tmx.d = -sp1.d
    EndIf  
    If KeyboardReleased(#PB_Key_S)
      tmx.d = 0
    EndIf
    If KeyboardPushed(#PB_Key_A)
      kmy.d = sp1.d
    EndIf
    If KeyboardReleased(#PB_Key_A)
      kmy.d = 0
    EndIf
    If KeyboardPushed(#PB_Key_D)
      kmy.d = -sp1.d
    EndIf
    If KeyboardReleased(#PB_Key_D)
      kmy.d = 0
    EndIf
    If KeyboardPushed(#PB_Key_Z)
      SelectElement(prg\tanks(), prg\beurt)
      RotateEntity(prg\tanks()\body,0,1, 0, #PB_Relative)
    EndIf
    If KeyboardPushed(#PB_Key_C)
      SelectElement(prg\tanks(), prg\beurt)
      RotateEntity(prg\tanks()\body,0,-1, 0, #PB_Relative)
    EndIf
    ; shoot..
    If ListSize(prg\bullet()) = 0
      SelectElement(prg\tanks(), prg\beurt)
      AddShoot(prg\tanks()\sloop,prg\tanks()\eloop)
    EndIf
    ; position tanks
    ResetList(prg\tanks())
    While NextElement(prg\tanks())
      If prg\tanks()\ingame=1
        If ListIndex(prg\tanks()) = prg\beurt And EntityX(prg\tanks()\body,#PB_Absolute)+(prg\dx/2)-KeyX>=prg\minx And EntityX(prg\tanks()\body,#PB_Absolute)+(prg\dx/2)-KeyX<=prg\maxx And EntityZ(prg\tanks()\body,#PB_Absolute)+(prg\dz/2)+KeyZ>=prg\minz And EntityZ(prg\tanks()\body,#PB_Absolute)+(prg\dz/2)+KeyZ<=prg\maxz
          RotateEntity(prg\tanks()\node,0,EntityYaw(prg\tanks()\koepel)-90, 0, #PB_Absolute)         
          MoveEntity(prg\tanks()\body,-KeyX, 0, keyz) 
          prg\tentity = prg\tanks()\node 
          prg\camheight = EntityY(prg\tanks()\body)+60
          prg\camposprocent = 0.05
          prg\camrotprocent = 1
          prg\camangle = -90
        EndIf
        MoveEntity(prg\tanks()\body,EntityX(prg\tanks()\body),0,EntityZ(prg\tanks()\body),#PB_Absolute)
        If IsEntity(prg\tanks()\schild): MoveEntity(prg\tanks()\schild,EntityX(prg\tanks()\body),EntityY(prg\tanks()\body)+4,EntityZ(prg\tanks()\body),#PB_Absolute): EndIf
        If IsEntity(prg\tanks()\pbody): MoveEntity(prg\tanks()\pbody,EntityX(prg\tanks()\body),EntityY(prg\tanks()\body)+4,EntityZ(prg\tanks()\body),#PB_Absolute): EndIf
      EndIf
    Wend
    ; rotate stuff
    ResetList(prg\tanks())
    While NextElement(prg\tanks())
      If ListIndex(prg\tanks()) = prg\beurt And prg\tanks()\ingame = 1 ;And roteren = 0
        Yaw(EntityID(prg\tanks()\koepel), kmy.d, #PB_World)
        Roll(EntityID(prg\tanks()\loop), tmx.d, #PB_Local)
        FetchOrientation(EntityID(prg\tanks()\loop), #PB_Relative)
        If GetX()>0.7
          SetOrientation(EntityID(prg\tanks()\loop), 0.7, 0, 0, GetW())
        EndIf
        If GetX()<0 
          SetOrientation(EntityID(prg\tanks()\loop), 0, 0, 0, GetW())
        EndIf
      EndIf
    Wend    
    ; Any bullets flying?
    If ListSize(prg\bullet())>0
      ResetList(prg\bullet())
      While NextElement(prg\bullet())
        hit = 0
        If ElapsedMilliseconds()-prg\bullet()\tickcount>20000 ; 20 secs
          If IsEntity(prg\bullet()\bullet)
            FreeEntityBody(prg\bullet()\bullet)
            FreeEntity(prg\bullet()\bullet)
          EndIf
          If IsEffect(prg\bullet()\ribbon)
            FreeEffect(prg\bullet()\ribbon)
          EndIf
          DeleteElement(prg\bullet())
        Else
          ResetList(prg\tanks())
          While NextElement(prg\tanks())
            gocheck = 0
            If (ListIndex(prg\tanks()) <> prg\beurt)
              gocheck = 1
            Else
              If ElapsedMilliseconds()-prg\bullet()\tickcount>1000 ; start checking bullet hit after a sec for this tank
                gocheck = 1
              EndIf
            EndIf
            If gocheck = 1
              raak = 0
              If IsEntity(prg\tanks()\schild) And IsEntity(prg\bullet()\bullet)
                SelectElement(prg\tanks()\shield(), prg\tanks()\activeshield)
                If prg\tanks()\shield()\power>0 And IsEntity(prg\bullet()\bullet) And IsEntity(prg\tanks()\schild)
                  check =  EntityCollide(prg\bullet()\bullet, prg\tanks()\schild)
                  If check>0
                    If debugging = 1
                      Debug "SHIELD: " + Str(check) + ", tank: "  + Str(ListIndex(prg\tanks()))
                    EndIf
                    raak = 1
                  EndIf
                EndIf
              EndIf
              If raak=0 And IsEntity(prg\bullet()\bullet)
                check = EntityCollide(prg\bullet()\bullet, prg\tanks()\pbody)
                If check>0
                  If debugging = 1 
                    Debug "Body: " + Str(check) + ", tank: "  + Str(ListIndex(prg\tanks()))
                  EndIf
                  raak = 1
                EndIf
              EndIf
              If raak = 1
                tijd.l =  ElapsedMilliseconds()-prg\bullet()\tickcount
                Debug "Hit in " + Str(tijd.l) + " milliseconds.." 
                If tijd<100
                  Debug "IT JUST HAPPEND!!!!"
                  Debug " ----------------- "
                  Debug "Total time: " + Str(ElapsedMilliseconds()-time) + " milliseconds.."
                  End
                EndIf
                ; explosion
                pos\x = EntityX(prg\bullet()\bullet)
                pos\y = EntityY(prg\bullet()\bullet)
                pos\z = EntityZ(prg\bullet()\bullet)
                AddElement(prg\explosion())
                Bullet = CopyEntity(prg\bulletentity, #PB_Any)
                ScaleEntity(Bullet, 1.3, 1.3, 1.3)
                MoveEntity(Bullet, pos\x, pos\y, pos\z, #PB_Absolute) 
                prg\explosion()\bullet = Bullet
                prg\explosion()\tickcount = ElapsedMilliseconds()
                prg\explosion()\x = Int(EntityX(prg\explosion()\bullet)+prg\dx/2) 
                prg\explosion()\y = Int(EntityZ(prg\explosion()\bullet)+prg\dz/2)                
                CopyStructure(prg\Bullet()\bulletinfo, prg\explosion()\bulletinfo, weapons)
                hit()
                hit = 1
              EndIf
            EndIf
          Wend
          If hit=1
            If IsEntity(prg\bullet()\bullet)
              FreeEntityBody(prg\bullet()\bullet)
              FreeEntity(prg\bullet()\bullet)
            EndIf
            If IsEffect(prg\bullet()\ribbon)
              FreeEffect(prg\bullet()\ribbon)
            EndIf
            DeleteElement(prg\bullet())
          EndIf
          If hit = 0
            If IsEntity(prg\bullet()\bullet) And ElapsedMilliseconds()-prg\bullet()\tickcount>=1000
              prg\tentity = prg\bullet()\bullet 
              prg\camdist = 10 + ( (ElapsedMilliseconds() - prg\Bullet()\tickcount) / 100) 
            EndIf
            If IsEntity(prg\bullet()\bullet) And ElapsedMilliseconds()-prg\bullet()\tickcount<1000
              MoveCamera(0, EntityX(prg\bullet()\bulletinfo\ownerbody), EntityY(prg\bullet()\bulletinfo\ownerbody) + 60, EntityZ(prg\bullet()\bulletinfo\ownerbody), #PB_Absolute)
              CameraLookAt(0, EntityX(prg\bullet()\bullet), EntityY(prg\bullet()\bullet), EntityZ(prg\bullet()\bullet))
            EndIf
          EndIf
        EndIf
      Wend
    EndIf
    ; any explosions?
    If ListSize(prg\explosion())>0
      ResetList(prg\explosion())
      While NextElement(prg\explosion())
        If ElapsedMilliseconds()-prg\explosion()\tickcount>1000 ; explosie voorbij .. 
          If IsEntity(prg\explosion()\bullet)
            FreeEntity(prg\explosion()\bullet)  
          EndIf
          DeleteElement(prg\explosion())
          prg\camdist = prg\camdisto
        Else ; stille exploding?
          If IsEntity(prg\explosion()\bullet)
            col = Random(15)+10
            SetMaterialColor(prg\myellow, #PB_Material_AmbientColor, RGB(col+230,col+230,col+230))
            tel = (Random(2)+1)* prg\explosion()\bulletinfo\scale
            ScaleEntity(prg\explosion()\bullet, tel+3, tel+3, tel+3, #PB_Absolute)
            prg\tentity = EntityID(prg\explosion()\bullet) ; explosie volgen met camera
          EndIf
        EndIf
      Wend
    EndIf
    ; CameraFollow
    If IsEntity(prg\tentity)
      CameraFollow(0, EntityID(prg\tentity), prg\camangle, prg\camheight, prg\camdist, prg\camrotprocent, prg\camposprocent, #True)
    EndIf
    ; Render
    RenderWorld()
    FlipBuffers()
  Until prg\event=#PB_Event_CloseWindow  Or KeyboardPushed(#PB_Key_Escape)
  
EndProcedure

Procedure main()
  Protected i.l, bz.l, bx.l, chx.l, chz.l, y.l, bb.l, x.l, x1.l, y1.l, seed.l, txt.l, tmat.l, img.l, wi.l, he.l, mx.l
  ExamineDesktops()
  prg\sw = DesktopWidth(0)/2
  prg\sh = DesktopHeight(0)/2
  InitKeyboard():InitMouse():InitEngine3D(#PB_Engine3D_DebugLog):InitSprite()
  EnableWorldCollisions(0) 
  #Max = 2147483647
  seed = Random(#Max)
  Debug seed
  RandomSeed(seed)
  OpenWindow(0, 0, 0, prg\sw, prg\sh, "", #PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(0), 0, 0, prg\sw, prg\sh) 
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures/", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts"         , #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/GUI", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/fonts", #PB_3DArchive_FileSystem)
  Parse3DScripts()
  AmbientColor($111111*8)
  CreateLight(0, $111111*8, 40000, 10000, 10000)
  CreateCamera(0, 0, 0, 100, 100)
  CameraBackColor(0,$ff8888):CameraRange(0,0.1,800)
  Fog($ff8888,100,0,800)
  MoveCamera(0, 0, 1000, 0, #PB_Absolute)
  prg\tblack.l = CreateTexture(#PB_Any, 255, 255, "Black")
  prg\mblack.l = CreateMaterial(#PB_Any, TextureID(prg\tblack))
  prg\tyellow.l = CreateTexture(#PB_Any, 255, 255, "Yellow")
  StartDrawing(TextureOutput(prg\tyellow)): Box(0,0,255,255,RGB(255,255,0)): StopDrawing()
  prg\myellow.l = CreateMaterial(#PB_Any, TextureID(prg\tyellow))
  MaterialBlendingMode(prg\myellow.l, #PB_Material_Add)
  prg\tred.l = CreateTexture(#PB_Any, 255, 255, "Red")
  StartDrawing(TextureOutput(prg\tred)): Box(0,0,255,255,RGB(255,0,0)): StopDrawing()
  prg\mred.l = CreateMaterial(#PB_Any, TextureID(prg\tred))
  initweaponsandshields()
  maaktank(prg\mred.l,    0, 0, 1, RGBA(255,0,0,200))
  maaktank(prg\mred.l,  0, 0,  1, RGBA(0, 255, 0,200))
  maaktank(prg\mred.l, 0, 0,  1, RGBA(255,255,0,200))
  maaktank(prg\mred.l,   0, 0,  1, RGBA(0, 0, 255,200))
  prg\bulletentity = CreateEntity(#PB_Any, MeshID(CreateSphere(#PB_Any, 1)), MaterialID(prg\myellow)) 
  RotateEntity(prg\bulletentity, 0, 0, 0, #PB_Absolute)
  HideEntity(prg\bulletentity, 1)
  If debugging.l=1
    Debug "prg\bulletentity: " + Str(prg\bulletentity)
  EndIf
  prg\ribbonmaterial = GetScriptMaterial(#PB_Any, "Examples/LightRibbonTrail")
  prg\plane = CreatePlane(#PB_Any, 1024, 1024, 10, 10, 1, 1)
  prg\eplane.l = CreateEntity(#PB_Any,MeshID(prg\plane.l), MaterialID(prg\mblack)) 
  If debugging.l=1
    Debug "prg\eplane.l: " + Str(prg\eplane.l)
  EndIf
  CreateEntityBody(prg\eplane.l, #PB_Entity_StaticBody, 0, -1, -1) 
  prg\selectedcamera.l = 0
  prg\camdist.f = 260
  prg\camposprocent = 0.05
  prg\camrotprocent = 1
  prg\camheight = 60
  prg\beurt.l = 0
  SelectElement(prg\tanks(), 0)
  prg\tanks()\activeweapon = 1
  checkshield(1, 1, 1)
  checkshield(1, 2, 1)
  checkshield(1, 3, 1)
  checkshield(1, 0, 1)
  SelectElement(prg\tanks(), prg\beurt)
  loop()
EndProcedure

main()
User avatar
DK_PETER
Addict
Addict
Posts: 869
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: ApplyEntityImpulse

Post by DK_PETER »

It looks like it is a Bullet creation problem with #pb_any. (It may happen at random)
Try skipping the #pb_any and assign a value from 1 and increase the value for each bullet.
It should work.

Example:

Code: Select all

BulletID + 1
Ret = CopyEntity(prg\bulletentity, BulletID) 
Ofc. you have to change your lines accordingly to reflect the BulletID var instead, though.
“Tell me and I forget. Teach me and I remember. Involve me and I learn.”
— Benjamin Franklin
Current configurations: Windows 10, Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Post Reply