When moving, the speed can change unpredictably. 3d FPS

Everything related to 3D programming
siesit
User
User
Posts: 14
Joined: Fri Aug 21, 2009 8:40 am
Location: rus
Contact:

When moving, the speed can change unpredictably. 3d FPS

Post by siesit »

I'm trying to do something but I get unstable results.

In this example, the NPC's speed changes dramatically depending on where the camera is pointing.
When moving, the speed can change unpredictably.

How to solve this correctly?

Code: Select all

;- CONST

Global scale=20*10

;Camera 
Global.i camR     = 1000          ;Camera range

Global GUI, room_ok

#PlayerSpeed = 60
#CameraSpeed = 0.8
#window = 2
#kam_0 = 1

#plane = 0
#planent = 0
#cam_type = 2  ;main -2,  - 1
#Light=3

init=0

Global ScreenWidth, ScreenHeight

Structure s_Key
      Up.i
      Down.i
      Left.i
      Right.i
      Jump.i
EndStructure

Structure s_Entity
      EntityBody.i
      BodyOffsetY.f
      elapsedTime.f
      rot_x.f
      rot_y.f
      rot_z.f
      
      Fov.f
      Key.s_Key
      MainNode.i
      CameraNode.i
      HandNode.i
      ForwardNode.i
      StrafeNode.i
      LampNode.i
      hidelamp.i
      HideMenu.i
EndStructure
Global main_Entity.s_Entity

Macro GetNodePosition(Position, Node)
  Position\x = NodeX(Node)
  Position\y = NodeY(Node)
  Position\z = NodeZ(Node)
EndMacro

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

;-Declare
Declare HandleEntity(*Entity.s_Entity)
Declare OnGround(*Entity.s_Entity)

Define Robot.s_Entity
Global prefix$ = "main"
Global Scroll_stor, Scroll_inv

;;;;;;;;;;;;;;;;

Global global_x.l = 24;256-200
Global global_y.l = 12;32-10
Global global_z.l = 24;256-200


Global flash_on = 0

Declare g_start(type)

Structure game
      state_base_lamp.l
      wrds_id.l
      cur_save_ver.l
      item_up.l
      inv.l
      room_id.l
      inventory.s
      th_light.l
      sg.l
      flashlight.l
EndStructure

Global game.game

;- INIT
InitEngine3D();#PB_Engine3D_EnableCG)
InitSprite()
InitKeyboard()
InitMouse()


If #window=1
      OpenWindow(#window, 0, 0, 1280, 1024, "FPS-Test", #PB_Window_ScreenCentered)
      OpenWindowedScreen(WindowID(#window), 10, 10, 1280-20, 1024-20, 1, 10, 10, #PB_Screen_SmartSynchronization)
EndIf

If #window=2
      OpenScreen(1280, 1024, 32, "test", #PB_Screen_SmartSynchronization)
      AntialiasingMode(#PB_AntialiasingMode_x6)

      SetFrameRate(60)
EndIf

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/Models", #PB_3DArchive_FileSystem)
;Add3DArchive("D:\~3D\m2", #PB_3DArchive_FileSystem)

Parse3DScripts()

Enumeration mesh 100 Step 10
      #mesh_baseblock
EndEnumeration

Global FPS, block$, index_cur.l
Global light2



;> LoadMesh

;Robot Body
LoadMesh(0, "robot.mesh")
TransformMesh(0, 0, 0, 0, 1, 1, 1, 0, -90, 0)

LoadMesh(1, "AKM.mesh")
LoadMesh(2, "ninja.mesh")
; TransformMesh(2, 0, 0, 0, 1, 1, 1, 0, 90, 0)

LoadTexture(0, "Dirt.jpg")

;- Material
CreateMaterial(1, LoadTexture(1,"viseur-jeux.png"))
MaterialBlendingMode(1, #PB_Material_AlphaBlend)

CreateMaterial(4, LoadTexture(4, "Dirt.jpg"))
CreateMaterial(5, LoadTexture(5, "t1.jpg"))
CreateMaterial(6, LoadTexture(6, "r2skin.jpg"))
CreateMaterial(7, LoadTexture(7, "RustySteel.jpg"))

;LoadMesh(#mesh_baseblock, "Barrel.mesh");
LoadMesh(#mesh_baseblock, "chassis.mesh");
TransformMesh(#mesh_baseblock, 0, 0, 0, 10, 10, 10, 0, 0, 0)

;CreateCube(#mesh_baseblock, 180)
;TransformMesh(#mesh_baseblock, -100, 10, -100,  1.1, 1.1, 0.1,  90, 0, 0)

BuildMeshShadowVolume(#mesh_baseblock)
NormalizeMesh(#mesh_baseblock) 

;<



Procedure Create_static(sg, mesh, Material, x, y, z, body=1, pick_mask=1, ent_grp=1, ent_mask=1, vis_mask=2);, scale=1, rot_x=0, rot_y=0, rot_z=0

ent = CreateEntity(#PB_Any, mesh, Material, x, y, z, pick_mask, vis_mask)
If ent
      ;EntityRenderMode(ent, #PB_Shadow_None)   
      EntityRenderMode(ent, #PB_Entity_CastShadow)  
      
      SetRenderQueue(EntityID(ent), 50 , 1000)
      
      If body
            CreateEntityBody(ent, #PB_Entity_StaticBody);, 0, 0.8, 1
            SetEntityCollisionFilter(ent, ent_grp, ent_mask)
      EndIf
      
      If vis_mask=2
            AddStaticGeometryEntity(sg, EntityID(ent), x, y, z, 1, 1, 1)
            HideEntity(ent, 1)
      EndIf
EndIf

ProcedureReturn ent
EndProcedure

Procedure StaticGeometry()
Define rf_x, rf_y, rf_z, r_x, r_y, r_z, f, ok, inn, x, z, y
Define.i ent, sg
Define rx, ry, rz, rrx, rry, rrz, inner_room, light_lamp, room$


sg = CreateStaticGeometry(#PB_Any, global_x*scale, global_y*scale, global_z*scale, 0)
Debug "Static " + sg
  
create_static(sg, MeshID(0), MaterialID(4), -1*scale, 0*scale, -1*scale)
create_static(sg, MeshID(0), MaterialID(4), (global_x+1)*scale, 0*scale, -1*scale)
create_static(sg, MeshID(0), MaterialID(4), -1*scale, 0*scale, (global_z+1)*scale)
create_static(sg, MeshID(0), MaterialID(4), (global_x+1)*scale, 0*scale, (global_z+1)*scale)


create_static(sg, MeshID(0), MaterialID(4), -1*scale, (global_y+1)*scale, -1*scale)
create_static(sg, MeshID(0), MaterialID(4), (global_x+1)*scale, (global_y+1)*scale, -1*scale)
create_static(sg, MeshID(0), MaterialID(4), -1*scale, (global_y+1)*scale, (global_z+1)*scale)
create_static(sg, MeshID(0), MaterialID(4), (global_x+1)*scale, (global_y+1)*scale, (global_z+1)*scale)


For u = 0 To 12
For i = 0 To 12
create_static(sg, MeshID(#mesh_baseblock), MaterialID(4), (-1+i)*scale, (0)*scale, (-1+u)*scale)
; create_static(sg, MeshID(#mesh_baseblock), MaterialID(4), (global_x+1+i)*scale, (global_y+1)*scale, -1*scale)
; create_static(sg, MeshID(#mesh_baseblock), MaterialID(4), (-1+i)*scale, (global_y+1)*scale, (global_z+1)*scale)
; create_static(sg, MeshID(#mesh_baseblock), MaterialID(4), (global_x+1+i)*scale, (global_y+1)*scale, (global_z+1)*scale)
cnt+1
Next
Next

For t = 0 To 60-40
For u = 0 To 12
For i = 0 To 12


create_static(sg, MeshID(#mesh_baseblock), MaterialID(4), (-22+i)*scale, (t)*scale/8, (-12+u)*scale)
; create_static(sg, MeshID(#mesh_baseblock), MaterialID(4), (global_x+1+i)*scale, (global_y+1)*scale, -1*scale)
; create_static(sg, MeshID(#mesh_baseblock), MaterialID(4), (-1+i)*scale, (global_y+1)*scale, (global_z+1)*scale)
; create_static(sg, MeshID(#mesh_baseblock), MaterialID(4), (global_x+1+i)*scale, (global_y+1)*scale, (global_z+1)*scale)
cnt+1
Next
Next
Next

Debug cnt
; ProcedureReturn sg

ProcedureReturn sg
EndProcedure

Procedure game_start()
Define.i ground, t, i

;- config
game\state_base_lamp=1
;game\state_base_lamp=3


game\sg=StaticGeometry()
Debug game\sg
BuildStaticGeometry(game\sg)
Debug "SG==="

      PosMain_x = 0*scale
      PosMain_y = 5*scale
      PosMain_z = 0*scale

     
      MoveEntity(main_Entity\EntityBody, PosMain_x, PosMain_y, PosMain_z, #PB_Absolute)
      SoundListenerLocate(PosMain_x, PosMain_y, PosMain_z)
      MoveNode(main_Entity\MainNode, PosMain_x, PosMain_y - main_Entity\BodyOffsetY, PosMain_z, #PB_Absolute)
      
      Debug main_Entity\EntityBody
      Debug EntityX(main_Entity\EntityBody)
      Debug EntityY(main_Entity\EntityBody)
      Debug EntityZ(main_Entity\EntityBody)
      
      main_Entity\rot_x = Rot_x
      main_Entity\rot_y = Rot_y
      main_Entity\rot_z = Rot_z

      RotateEntity(main_Entity\EntityBody, 0, main_Entity\Rot_y, 0, #PB_Absolute)
      RotateNode(main_Entity\CameraNode, main_Entity\Rot_x, 0, 0, #PB_Absolute)
      RotateNode(main_Entity\HandNode, main_Entity\Rot_x, 0, 0, #PB_Absolute)
      RotateNode(main_Entity\MainNode, 0, EntityYaw(main_Entity\EntityBody), 0)

EndProcedure

;- -----------------------------------------------------------------------

global_x.l = 12;256-200
global_y.l = 3;32-10
global_z.l = 12;256-200
flash_on = 1
prefix$ = "main_0"

EnableExplicit

;- START
Define.f KeyX, KeyY, MouseX, MouseY
Define nx.f, nz.f, Boost.f = 2.2, Yaw.f, Pitch.f, zaehler
Define.i ground, t, i
Define.b firster = #True

Define Mesh_plane, ent_plane, Mesh_robot, ent_robot, light1, Light.l

Mesh_plane=CreatePlane(#PB_Any, global_x*scale*2, global_z*scale*2, scale, scale, 20, 20)
ground = GetScriptMaterial(#PB_Any, "Scene/GroundBlend")
;TransformMesh(Mesh_plane, 256*10, -10, 256*10, 1, 1, 1, 0, 0, 0)
ent_plane=CreateEntity(#PB_Any, MeshID(Mesh_plane), MaterialID(ground), 0, -1.5, 0)
EntityRenderMode(ent_plane, 0)      
CreateEntityBody(ent_plane, #PB_Entity_StaticBody)

;- GAME

;WorldShadows(#PB_Shadow_Modulative, #PB_Default, RGB(205, 205, 205), 128)

AmbientColor(RGB(22, 14, 35))
Fog(RGB(126, 58, 1), 2048+5, 1*scale, 28*scale)

; WorldDebug(#PB_World_DebugBody)
; WorldDebug(#PB_World_DebugEntity)

EnableWorldCollisions(1) 
EnableWorldPhysics(1)
WorldGravity(-90.806)

AntialiasingMode(#PB_AntialiasingMode_x6)

light2 = CreateLight(#PB_Any ,RGB(255*0.4, 255*0.4,255*0.4), (1*scale), (1*scale), (1*scale), #PB_Light_Spot)
SetLightColor(light2, #PB_Light_SpecularColor, RGB(128, 50, 50))
LightAttenuation(light2, 8050, 0.9)
SpotLightRange(light2, 1, 60, 1)

Global lamp_y = -35
Global lamp_x = 15
Global lamp_z = 0
Global lamp_i = 0
Global lamp_o = 70
Global lamp_f.f = 1

Define onflore = 15

CreateEntity(0, MeshID(0), #PB_Material_None, 5*scale-100+30, 3*scale+50, 5*scale-100)
SetEntityAttribute(0,#PB_Entity_LinearSleeping,0)
SetEntityAttribute(0,#PB_Entity_AngularSleeping, 0.0)
ScaleEntity(0, 0.8, 0.8, 0.8)    
HideEntity(0, 1)

;- Gun
CreateEntity(1, MeshID(1), MaterialID(6))
ScaleEntity(1, 0.05, 0.05, 0.05)
RotateEntity(1, 90, -85, 0)

;- Body
CreateEntityBody(0, #PB_Entity_CapsuleBody, 60.00, 0.0001, 1)
; EntityAngularFactor(0, 0, 0, 0)

;-Billboard
CreateBillboardGroup(0, MaterialID(1), 0.05, 0.05, 0, 0, 13)
AddBillboard(0, 0, 0, 0)

With main_Entity
      \Fov = 70

      \rot_x = 0
      \rot_y = 0
      \rot_z = 0

      \EntityBody = 0
      \BodyOffsetY = 15
      If #cam_type = 2
            \Key\Down  = #PB_Key_S ;Down
            \Key\Left  = #PB_Key_A ;Left
            \Key\Right = #PB_Key_D;Right
            \Key\Up    = #PB_Key_W;Up
            \Key\Jump  = #PB_Key_Space
      ElseIf #cam_type = 1 
            \Key\Down  = #PB_Key_Down
            \Key\Left  = #PB_Key_Left
            \Key\Right = #PB_Key_Right
            \Key\Up    = #PB_Key_Up
            \Key\Jump  = #PB_Key_Space
      EndIf
      
      \MainNode   = CreateNode(#PB_Any) ; Entity position
      \ForwardNode= CreateNode(#PB_Any,  0,  0, -3) ; Direction normalized
      \StrafeNode = CreateNode(#PB_Any, -1,  0,  0) ; Direction normalized
      \LampNode = CreateNode(#PB_Any, 0,  50,  10) ; 

;       \LampNode = CreateNode(#PB_Any, 0,  50,  10) ; 
      \HandNode   = CreateNode(#PB_Any,  6, 43,  -8) ; Hand position

      \CameraNode = CreateNode(#PB_Any,  0, 50,  -10) ; Camera position 40

      \HideMenu = 1
      
      AttachNodeObject(\MainNode  , NodeID(\CameraNode))
      AttachNodeObject(\MainNode  , NodeID(\HandNode))
      AttachNodeObject(\MainNode  , NodeID(\ForwardNode))
      AttachNodeObject(\MainNode  , NodeID(\StrafeNode))
      AttachNodeObject(\MainNode  , NodeID(\LampNode))

      AttachNodeObject(\HandNode  , EntityID(1))

      AttachNodeObject(\CameraNode, BillboardGroupID(0))
      
      RotateNode(\HandNode, 5, 0, 0, #PB_Relative)
      MoveBillboardGroup(0, 0, 0, -15)
      MoveEntity(1, 0.1, -0.1, 0.01)

; RotateEntity(\EntityBody, 0, 90, 0, #PB_Absolute)

EndWith

  
;- Camera float
If #cam_type = 1  
      ;-Camera
      CreateCamera(#kam_0, 0, 0, 100, 100, 2)
      ; MoveCamera(#kam_0, -50, 50, -50, #PB_Absolute)
      MoveCamera(#kam_0, 0*scale, 5*scale, 5*scale, #PB_Absolute)
      
      
      CameraLookAt(#kam_0, 20*scale, 0, 20*scale)
      CameraBackColor(#kam_0, RGB(0 , 0, 0))
      CameraRange(#kam_0, 0, camR*1000)
EndIf

;- Camera main
If #cam_type = 2   
      CreateCamera(0, 0, 0, 100, 100)
      CameraRange(0, 0.08, 2000)
      ; RotateCamera(0, 0, 0, 0)
      
      AttachNodeObject(main_Entity\CameraNode, CameraID(0))
      CameraLookAt(0, NodeX(main_Entity\ForwardNode) * 100, NodeY(main_Entity\CameraNode), NodeZ(main_Entity\ForwardNode) * 100)
EndIf

Define set_empty
Define Input$, Event
Define.i Quit
Define close_gui, Gadget, click_els, els, move

;- GAME START
game_start()

Repeat
;     Debug "--------------------------------------------"
    Debug "Tris: " + Engine3DStatus(#PB_Engine3D_NbRenderedTriangles)
    Debug "Batches: " + Engine3DStatus(#PB_Engine3D_NbRenderedBatches)
    Debug "FPS: " + Engine3DStatus(#PB_Engine3D_CurrentFPS)
;     Debug "Min FPS: " + Engine3DStatus(#PB_Engine3D_MinimumFPS)
;     

      If #window=1
            Repeat
            Until WindowEvent() = 0
      EndIf

      If GUI=0
            HandleEntity(@main_Entity)
      EndIf

      Define.f TimeSinceLastFrame
      TimeSinceLastFrame = RenderWorld()
      main_Entity\elapsedTime = TimeSinceLastFrame



      FlipBuffers()

Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1

Procedure OnGround(*Entity.s_Entity)

Protected nx, nz, ny
Protected ent, ent1, ent2, ent3, ent4

nx=NodeX(*Entity\MainNode)
ny=NodeY(*Entity\MainNode)
nz=NodeZ(*Entity\MainNode)

; Debug nx


;Debug *Entity\BodyOffsetY
;   ProcedureReturn RayCollide(nx,  ny-*Entity\BodyOffsetY, nz, nx+10, ny-*Entity\BodyOffsetY-10,  nz)

ent=RayCollide(nx, ny-*Entity\BodyOffsetY, nz, nx, ny-*Entity\BodyOffsetY-15,  nz)
; CreateLine3D(#PB_Any, nx, ny-*Entity\BodyOffsetY, nz, $FFFF, nx, ny-*Entity\BodyOffsetY-15,  nz, $FFFF)
If ent=-1
      ent1=RayCollide(nx-2, ny-*Entity\BodyOffsetY, nz-2, nx+10, ny-*Entity\BodyOffsetY-10,  nz+10)
;       CreateLine3D(#PB_Any, nx-2, ny-*Entity\BodyOffsetY, nz-2, $FFFF, nx+10, ny-*Entity\BodyOffsetY-10,  nz+10, $FFFF)
      If ent1=-1
            ent2=RayCollide(nx-2, ny-*Entity\BodyOffsetY, nz+2, nx+10, ny-*Entity\BodyOffsetY-10,  nz-10)
;             CreateLine3D(#PB_Any, nx-2, ny-*Entity\BodyOffsetY, nz+2, $FFFF, nx+10, ny-*Entity\BodyOffsetY-10,  nz-10, $FFFF)
            If ent2=-1
                  ent3=RayCollide(nx+2, ny-*Entity\BodyOffsetY, nz-2, nx-10, ny-*Entity\BodyOffsetY-10,  nz+10)
;                   CreateLine3D(#PB_Any, nx+2, ny-*Entity\BodyOffsetY, nz-2, $FFFF, nx-10, ny-*Entity\BodyOffsetY-10,  nz+10, $FFFF)
                  If ent3=-1
                        ent4=RayCollide(nx+2, ny-*Entity\BodyOffsetY, nz+2, nx-10, ny-*Entity\BodyOffsetY-10,  nz-10)
;                         CreateLine3D(#PB_Any, nx+2, ny-*Entity\BodyOffsetY, nz+2, $FFFF, nx-10, ny-*Entity\BodyOffsetY-10,  nz-10, $FFFF)
                  EndIf
            EndIf
      EndIf
EndIf

If ent
ProcedureReturn ent
ElseIf ent1
ProcedureReturn ent1
ElseIf ent2
ProcedureReturn ent2
ElseIf ent3
ProcedureReturn ent3
ElseIf ent4
ProcedureReturn ent4
EndIf
EndProcedure

Procedure OnWall(*Entity.s_Entity, fx, fz)
Protected ent1, ent2, grp1, grp2, ent3, ent4, grp3, grp4, x, z, nx, nz, ny

If IsNode(*Entity\MainNode)

nx=NodeX(*Entity\MainNode)
ny=NodeY(*Entity\MainNode)
nz=NodeZ(*Entity\MainNode)
x=nx+10*fx
z=nz+10*fz

; CreateLine3D(#PB_Any, NodeX(*Entity\MainNode)+3, NodeY(*Entity\MainNode)+38, nz, $FFFF, NodeX(*Entity\MainNode)-3, NodeY(*Entity\MainNode)+38,  nz, $FFFF)
; CreateLine3D(#PB_Any, NodeX(*Entity\MainNode), NodeY(*Entity\MainNode)+38, nz+3, $FFFF, NodeX(*Entity\MainNode), NodeY(*Entity\MainNode)+38,  nz-3, $FFFF)

CreateLine3D(#PB_Any, nx, ny+38, nz, $FFFF, x, ny+38, z, $FFFF)

ent1=RayCollide(nx+3, ny+38, nz, nx-3, ny+38,  nz)
; Debug ent1
ent2=RayCollide(nx, ny+38, nz+3, nx, ny+38,  nz-3)
; Debug ent2
ent3=RayCollide(nx-3, ny+38, nz, nx+3, ny+38,  nz)
; Debug ent3
ent4=RayCollide(nx, ny+38, nz-3, nx, ny+38,  nz+3)
; Debug ent4

If ent1 And IsEntity(ent1)
grp1=GetEntityCollisionGroup(ent1)
EndIf
If ent2 And IsEntity(ent2)
grp2=GetEntityCollisionGroup(ent2)
EndIf
If ent3 And IsEntity(ent3)
grp3=GetEntityCollisionGroup(ent3)
EndIf
If ent4 And IsEntity(ent4)
grp4=GetEntityCollisionGroup(ent4)
EndIf
; Debug grp1
; Debug grp2
; Debug grp3
; Debug grp4


If grp1=5
ProcedureReturn ent1
ElseIf grp2=5
ProcedureReturn ent2
ElseIf grp3=5
ProcedureReturn ent3
ElseIf grp4=5
ProcedureReturn ent4
EndIf

EndIf
;  ProcedureReturn 
EndProcedure

Global jcnt=0

Procedure HandleEntity(*Entity.s_Entity)
Protected.Vector3 Forward, Strafe, PosMain, PosDir, PosStrafe, PosLamp
Protected.f Speed.f, Speed2.f
Protected x, y, bx, by, bz, Ground
Protected.f MouseX, MouseY
Static Jump.f, Speedjmp.f, MemJump.i, Rot.Vector3, Trans.Vector3, Clic

Protected room.Vector3, PosEnt.Vector3
Protected PlayerSpeed, PlayerSpeedS, OnWall
Protected nx, ny, nz, ent, stor

  
With *Entity
      GetNodePosition(PosMain, \MainNode)
      GetNodePosition(PosDir, \ForwardNode)
      GetNodePosition(PosStrafe, \StrafeNode)

      GetNodePosition(PosLamp, \LampNode)

      SubVector3(Forward, PosDir, PosMain)
      SubVector3(Strafe, PosStrafe, PosMain)
   
; Debug PosMain\x

      bx=Round(PosMain\x / scale, #PB_Round_Up)
      by=Round(PosMain\y / scale, #PB_Round_Up)-1
      bz=Round(PosMain\z / scale, #PB_Round_Up)

;*TimeSinceLastFrame*#PlayerSpeed

      ;\elapsedTime = 2.6

      Speed = 260 * 2.3 ;* \elapsedTime;#PlayerSpeed * \elapsedTime
      Speed2 = Speed * 0.8;back
      Speedjmp = Speed * 0.8
  
     
      If ExamineMouse()
            MouseX = -(MouseDeltaX()/3000) * \Fov * 5.8;\elapsedTime
            MouseY = -(MouseDeltaY()/3000) * \Fov * 5.8;\elapsedTime
            \rot_y + MouseX
            \rot_x + MouseY
            \rot_z = 0

; Debug NodePitch(\CameraNode)
; Debug NodeRoll(\CameraNode)

;             If NodePitch(\CameraNode) < 80 And MouseY > 0
;                   Rot\x + MouseY
;             ElseIf NodePitch(\CameraNode) > -80 And MouseY < 0
;                   Rot\x + MouseY
;             EndIf
      EndIf

      If ExamineKeyboard()

            Ground=OnGround(*Entity)

            If KeyboardPushed(#PB_Key_LeftShift)
                  Speed = Speed * 2.5
            EndIf
      
            If KeyboardPushed(\Key\Jump); And OnGround(*Entity)>-1 ;KeyboardReleased
                  If Ground>-1; Or OnWall(*Entity, Forward\x, Forward\z)   ; And MemJump=0    
                        Jump = Speedjmp ;* \elapsedTime
                        MemJump = 1
                  EndIf
            EndIf

            If Ground>-1 And MemJump = 0
                  Jump = 0
                  jcnt = 0
;Debug "jcnt = 0"
            Else
      
                  If MemJump
                        Jump + 140;Speedjmp
                        If Jump > 1160
                              MemJump = 0
                              Jump = 1160
                        EndIf
;Debug "Jump + "
                  Else
;Debug "Jump - "
                        If Jump > -350
                              Jump - 320; * \elapsedTime
                              If Jump < -350
                                    Jump - 350
                              EndIf
                        EndIf
                  EndIf
            EndIf
;Debug Jump

;                   If Jump > -800
;                         jcnt + 1
;                         Jump - (20 * jcnt); * \elapsedTime
;                   EndIf


            Trans\x = 0; * 0.80
            Trans\y = Jump
            Trans\z = 0;* 0.80




            If KeyboardPushed(\Key\Up)
                  Trans\x = Forward\x * Speed
                  Trans\z = Forward\z * Speed
            ElseIf KeyboardPushed(\Key\Down)
                  Trans\x = Forward\x * -Speed2
                  Trans\z = Forward\z * -Speed2
            EndIf
            
            If KeyboardPushed(\Key\Left)
                  Trans\x = Strafe\x * Speed
                  Trans\z = Strafe\z * Speed
            ElseIf KeyboardPushed(\Key\Right)
                  Trans\x = Strafe\x * -Speed
                  Trans\z = Strafe\z * -Speed
            EndIf

;            DisableEntityBody(\EntityBody, #False) ; Wake up entity (BugWare)

            EntityVelocity(\EntityBody,Trans\x,Trans\y,Trans\z)
            ;MoveEntity(\EntityBody, Trans\x, Trans\y, Trans\z)






            If \hidelamp=0
                  If game\flashlight Or flash_on
                        ;MoveLight(Light2, PosLamp\x+lamp_x, PosLamp\y+lamp_y, PosLamp\z+lamp_z, #PB_Absolute)
                        ; CreateLine3D(#PB_Any, PosLamp\x, PosLamp\y+40, PosLamp\z, $FFFF, PosLamp\x, PosLamp\y+41, PosLamp\z, $FFFF)
                        If #cam_type = 2   
      
                              LightAttenuation(light2, 8050, 1)
                              SpotLightRange(light2, 0, lamp_o, 1)
                              ; 
                              ; LightAttenuation(light2, 1550, 1)
                              ; SpotLightRange(light2, 1, 360, 1)
      
                              LightDirection(Light2, CameraDirectionX(0), CameraDirectionY(0), CameraDirectionZ(0))
                        EndIf
                  Else
                        \hidelamp=1
                        HideLight(Light2, \hidelamp)
                  EndIf
            EndIf



            If KeyboardReleased(#PB_Key_Back)
                  If \hidemenu=0
                        \hidemenu=1
                  Else
                        \hidemenu=0
                  EndIf
            EndIf

            If KeyboardReleased(#PB_Key_L)
                  If \hidelamp=0
                        \hidelamp=1
                  Else
                        \hidelamp=0
                  EndIf
                  HideLight(Light2, \hidelamp)
            EndIf



      EndIf
          
      If #cam_type = 2   
            CameraFOV(0, \Fov)
      EndIf      
      
      RotateEntity(\EntityBody, 0, \Rot_y, 0, #PB_Absolute)

      RotateNode(\CameraNode, \Rot_x, 0, 0, #PB_Absolute)
      RotateNode(\HandNode, \Rot_x, 0, 0, #PB_Absolute)
      
      SoundListenerLocate(EntityX(\EntityBody), EntityY(\EntityBody) - \BodyOffsetY, EntityZ(\EntityBody))

      MoveNode(\MainNode, EntityX(\EntityBody), EntityY(\EntityBody) - \BodyOffsetY, EntityZ(\EntityBody), #PB_Absolute)
      RotateNode(\MainNode, 0, EntityYaw(\EntityBody), 0)

      If game\flashlight Or flash_on
            MoveLight(Light2, PosLamp\x+0, PosLamp\y+0, PosLamp\z+0, #PB_Absolute)
      EndIf

      If \HideMenu

      EndIf

; NodeFixedYawAxis(\MainNode, #True, 0.2, 0, 0.2)
; Debug EntityYaw(\EntityBody)
; Debug EntityPitch(\EntityBody)
; Debug CameraYaw(\CameraNode)


EndWith
EndProcedure
site created by purebasic work-flow-Initiative
User avatar
minimy
Enthusiast
Enthusiast
Posts: 630
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: When moving, the speed can change unpredictably. 3d FPS

Post by minimy »

Helloo siesit i try to run your code but crash... and you need new screen 16/9 :wink:
I try with 6.02 and 6.21

I think you need use ElapsedMilliseconds() to control the time.
some thing like:

Code: Select all

If ElapsedMilliseconds() > time
  ;all my stuf...
  time= ElapsedMilliseconds() + 20 ;50 fps
EndIf
If translation=Error: reply="Sorry, Im Spanish": Endif
siesit
User
User
Posts: 14
Joined: Fri Aug 21, 2009 8:40 am
Location: rus
Contact:

Re: When moving, the speed can change unpredictably. 3d FPS

Post by siesit »

Hello/
I am using PureBasic 6.21 x86 (win 10)
and you need new screen 16/9
- This is not an option, but I have 2 monitors. 8)
ElapsedMilliseconds()
...
I am really at a dead end :(
site created by purebasic work-flow-Initiative
miso
Enthusiast
Enthusiast
Posts: 467
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: When moving, the speed can change unpredictably. 3d FPS

Post by miso »

I tested this. On this machine it crashed, as the screen doesnot support the resolution. I copied to another machine, there was no slowdown. ( As it was too strong to be affected ) Your map looks like some kind of stress test. I belive its not the tris, but the batches that makes an fps drop while you look toward a specific direction (containing entities). That change in the fps makes slowdowns and speedups. Vsync (smartsync) removes the tearing on screen, but waiting for syncronisation holds on your main loop. Removing it makes screen tearing come back, but you can have more control over the speed. (syncing movements to elapsed milliseconds.)
Post Reply