Move an entity in the direction it is facing

Share your advanced PureBasic knowledge/code with the community.
Lykaestria
User
User
Posts: 76
Joined: Wed Sep 17, 2008 3:10 am
Location: New Zealand

Move an entity in the direction it is facing

Post by Lykaestria »

Thanks to LCD for a small snippet of code that changes radians into degrees, I can now calculate vectors to make an entity move relative to the direction it's facing rather than absolute coordinates. Unless and until we get a MoveEntity(#Entity, x, y, z, #PB_Relative) or similar to do the same we otherwise have to calculate vectors the long way :P

The following code is a modified version of SimpleCollisions from the ogre1.6 folder and should be run from same:

Code: Select all

#CameraSpeed=10
#EntitySpeed=30

#Camera=0

Enumeration
	#Robot
EndEnumeration

IncludeFile "Screen3DRequester.pb"

Define.f KeyX,KeyY,MouseX,MouseY

If InitEngine3D()

  Add3DArchive("Data\",#PB_3DArchive_FileSystem)
  Add3DArchive("Cube\",#PB_3DArchive_FileSystem)
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()

   	EnableWorldPhysics(#True)
   	EnableWorldCollisions(#True)
   	
    CreateMaterial(0,LoadTexture(0,"r2skin.jpg"))
    CreateMaterial(1,LoadTexture(1,"Background.png"))
    
    LoadMesh(0,"Cube.mesh")
    LoadMesh(1,"Robot.mesh")
    
    CreateCamera(#Camera,0,0,100,100)  ; Front camera
    CameraLocate(#Camera,0,200,300)
    CameraLookAt(#Camera,0,0,0)

		; The cube mesh is 100x100x100
    Scale=100
    
		; create the floor 
 		Floor=CreateEntity(#PB_Any,MeshID(0),MaterialID(1),0,-Scale,0)
 		ScaleEntity(Floor,11,1,11)
 		EntityPhysicBody(Floor,#PB_Entity_StaticBody)
    
    For x=-5 To 5 Step 2
    	For z=-5 To 5 Step 2
    		Block=CreateEntity(#PB_Any,MeshID(0),MaterialID(0),x*Scale,0,z*Scale)
    		EntityPhysicBody(Block,#PB_Entity_StaticBody)
    		SetEntityMass(Block,10)
    	Next
   	Next
   	
   	CreateEntity(#Robot,MeshID(1),MaterialID(0),0,-30,0)
   	EntityPhysicBody(#Robot,#PB_Entity_SphereBody,#PB_Entity_AbsoluteBodyMove)
   	SetEntityMass(#Robot,0.01)

    Repeat
      Screen3DEvents()
      
      If ExamineKeyboard()
      	
      	Active=0
      	
      	If KeyboardPushed(#PB_Key_PageUp)
          Speed=-#CameraSpeed 
        ElseIf KeyboardPushed(#PB_Key_PageDown)
          Speed=#CameraSpeed 
        Else
          Speed=0
        EndIf
        
        If KeyboardPushed(#PB_Key_Left)
          Turn_X+1
        ElseIf KeyboardPushed(#PB_Key_Right)
          Turn_X-1
        EndIf
        
        If KeyboardPushed(#PB_Key_X)
          Move_X=-#EntitySpeed
          Active=1
        ElseIf  KeyboardPushed(#PB_Key_C)
          Move_X=#EntitySpeed
          Active=1
        EndIf
                  
        If  KeyboardPushed(#PB_Key_A)
	      	Move_Y=#EntitySpeed
	      	Active=2
        ElseIf  KeyboardPushed(#PB_Key_Z)
	      	Move_Y=-#EntitySpeed
	      	Active=2
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
	      	Move_Z=#EntitySpeed
	      	Active=3
        ElseIf KeyboardPushed(#PB_Key_Down)
	      	Move_Z=-#EntitySpeed
	      	Active=3	
        EndIf
        
        If Turn_X>360
          Turn_X-360
        ElseIf Turn_X<0
          Turn_X+360
        EndIf
        
        Deg_X=Turn_X+90
        If Deg_X>360
          Deg_X-360
        ElseIf Deg_X<0
          Deg_X+360
        EndIf 
                            
        RotateEntity(#Robot,0,Turn_X,0)

        Select Active
          Case 1
            deg.f=(#PI/180)*Turn_X
      	    MoveEntity(#Robot,Sin(deg)*Move_X,0,Cos(deg)*Move_X)
          Case 2
      	    MoveEntity(#Robot,0,Move_Y,0)
          Case 3
            deg.f=(#PI/180)*Deg_X
      	    MoveEntity(#Robot,Sin(deg)*Move_Z,0,Cos(deg)*Move_Z)
      	  Default
      	    MoveEntity(#Robot,0,0,0)
      	EndSelect 
      EndIf  
      
      If ExamineMouse()
        MouseX=-(MouseDeltaX()/10)*#CameraSpeed/2
        MouseY=-(MouseDeltaY()/10)*#CameraSpeed/2
      EndIf
      
      RotateCamera(0,MouseY,MouseX,RollZ,#PB_Relative)
      MoveCamera(0,0,0,Speed)
          
      RenderWorld()
      Screen3DStats()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit=1
  EndIf
    
Else
  MessageRequester("Error","The 3D Engine can't be initialized",0)
EndIf
  
End
I hope this code is of use to someone :)