Page 1 of 1

Entity is still inactive

Posted: Fri Oct 11, 2013 6:42 am
by Bananenfreak
Heyho,

I´ve got a Problem. I want to create a game and used the way to move Entities from the "FPSFirstPerson.pb". So, it is functional without physics, but if I attach a physics Body to my Entity, my Entity will be inactive. So, no Problem, I thought. I could set my entity to "active" I thought. Wrong, we have no Attribute for that (SetEntityAttribute()).
I don´t know, why the character in the FPS-example is active all the time... And I find nothing in the code in this direction.
So, could anyone help me, why my Entity is inactive all the time? And is there a solution for this?

Greets,

Bananenfreak

Re: Entity is still inactive

Posted: Fri Oct 11, 2013 7:28 am
by Bananenfreak
I used

Code: Select all

SetEntityAttribute(\bodyEnt, #PB_Entity_LinearSleeping, 0)
    SetEntityAttribute(\bodyEnt, #PB_Entity_AngularSleeping, 0)
.
Now my entity is active all the time, but is rotating :shock:
I only want a character like in the FPS-example. Not a static guy and not a rotating guy....

Re: Entity is still inactive

Posted: Mon Oct 21, 2013 2:41 pm
by Bananenfreak
Here´s an example showing the Problem.
Is anyone able to solve this Problem, so the ninja walks really and not only the Animation is played?

Code: Select all

EnableExplicit

#MINFPS = 30

#PLAYER_SIZE = 1.8
#CAMERA_DIST = 8

#SCALE_WALK = 1.0
#SCALE_RUN  = 1.4
#SCALE_TURN = 0.1

Structure fVector3
  x.f
  y.f
  z.f
EndStructure

Structure structKeysettings
  forward.i
  backward.i
  turnLeft.i
  turnRight.i
  strafeLeft.i
  strafeRight.i
  run.i
EndStructure

Structure structPlayer
  init.b
  Keys.structKeysettings
  Rotate.fVector3
  Move.fVector3
  entity.i
  physic.i
  camera.i
  bbCross.i
  nodeMain.i
  nodeForward.i
  nodeStrafe.i
  nodeCamera.i
EndStructure

Structure structLevel
  sun.i
  floor.i
  grass.i
  Array houses.i(25)
EndStructure

Define.b            DoLoop = #True
Define.f            TimeSinceLastFrame
Define.structPlayer Player
Define.structLevel  Level

Declare Level_Create()
Declare Player_Create()
Declare Player_UpdateControls()
Declare Player_Update()
Declare CheckCollisions()

; init
InitEngine3D(#PB_Engine3D_DebugLog)
InitSprite()
InitKeyboard()
InitMouse()

OpenWindow(0,0,0,1024,768,"Simple 3rd Person",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, WindowWidth(0), WindowHeight(0), 0, 0, 0, #PB_Screen_WaitSynchronization)

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)
Parse3DScripts()

WorldShadows(#PB_Shadow_Additive)

EnableWorldPhysics(#True)
EnableWorldCollisions(#True)

Level_Create()
Player_Create()

WorldDebug(#PB_World_DebugBody)

Repeat
 
  While WindowEvent() : Wend
  
  Player_Update()
  
  TimeSinceLastFrame = RenderWorld(#MINFPS) / 60
  FlipBuffers()
  
  CheckCollisions()
 
Until DoLoop = #False

End

Macro SetPosition(Object, PositionX=0, PositionY=0, PositionZ=0)
  If IsNode(Object)
    MoveNode(Object, PositionX, PositionY, PositionZ, #PB_Absolute)
  ElseIf IsEntity(Object)
    MoveEntity(Object, PositionX, PositionY, PositionZ, #PB_Absolute)
  ElseIf IsCamera(Object)
    MoveCamera(Object, PositionX, PositionY, PositionZ, #PB_Absolute)
  EndIf
EndMacro

Macro GetPosition(Object, Position)
  If IsNode(Object)
    Position\x = NodeX(Object)
    Position\y = NodeY(Object)
    Position\z = NodeZ(Object)
  ElseIf IsEntity(Object)
    Position\x = EntityX(Object)
    Position\y = EntityY(Object)
    Position\z = EntityZ(Object)
  ElseIf IsCamera(Object)
    Position\x = CameraX(Object)
    Position\y = CameraY(Object)
    Position\z = CameraZ(Object)
  EndIf
EndMacro

Macro AddVec3(v1, v2, vResult)
  vResult\x = v1\x + v2\x
  vResult\y = v1\y + v2\y
  vResult\z = v1\z + v2\z
EndMacro

Macro SubVec3(v1, v2, vResult)
  vResult\x = v1\x - v2\x
  vResult\y = v1\y - v2\y
  vResult\z = v1\z - v2\z
EndMacro
  
Procedure Level_Create()
  
  Shared      Level
  Protected.l i
  Protected.i tempTexture, tempMaterial, tempMesh
  Protected.f radius, scale, height
  
  SkyDome("clouds.jpg", 60)
  
  ; floor
  tempTexture  = LoadTexture(#PB_Any, "grass.jpg")
  tempMaterial = CreateMaterial(#PB_Any, TextureID(tempTexture))
  tempMesh     = CreatePlane(#PB_Any, 512, 512, 1, 1, 128, 128)
  Level\floor  = CreateEntity(#PB_Any, MeshID(tempMesh), MaterialID(tempMaterial), 0, -#PLAYER_SIZE/2, 0)
  EntityPhysicBody(Level\floor, #PB_Entity_StaticBody)
  FreeMesh(tempMesh)
  FreeMaterial(tempMaterial)
  FreeTexture(tempTexture)
  
  ; grass
  tempTexture  = LoadTexture(#PB_Any, "grass1.png")
  tempMaterial = CreateMaterial(#PB_Any, TextureID(tempTexture))
  MaterialBlendingMode(tempMaterial, #PB_Material_AlphaBlend)
  Level\grass  = CreateBillboardGroup(#PB_Any, MaterialID(tempMaterial), 1, 1)
  For i = 0 To 10240
    AddBillboard(i, Level\grass, Random(512)-256, 0, Random(512)-256)
  Next
  FreeMaterial(tempMaterial)
  FreeTexture(tempTexture)

  ; houses
  tempMesh = LoadMesh(#PB_Any, "tudorhouse.mesh")
  radius   = MeshRadius(tempMesh)
  scale    = 6 * #PLAYER_SIZE / radius
  height   = scale * (radius / 2) + 1.75
  For i = 0 To 19
    Level\houses(i) = CreateEntity(#PB_Any, MeshID(tempMesh), #PB_Material_None, Random(384)-162, height, Random(384)-162)
    RotateEntity(Level\houses(i), 0, Random(90), 0, #PB_Absolute)
    ScaleEntity(Level\houses(i), scale, scale, scale, #PB_Absolute)
    EntityPhysicBody(Level\houses(i), #PB_Entity_StaticBody)
  Next
  FreeMesh(tempMesh)
  
  ; sun
  Level\sun = CreateLight(#PB_Any, RGB(255, 250, 205), -512, 512, 0, #PB_Light_Point)
  
  ; done !
  
EndProcedure

Procedure Player_Create()
  
  Shared      Player
  Protected.i tempTexture, tempMaterial, tempMesh
  Protected.f radius, scale
  
  tempTexture  = LoadTexture(#PB_Any, "viseur-jeux.png")
  tempMaterial = CreateMaterial(#PB_Any, TextureID(tempTexture))
  MaterialBlendingMode(tempMaterial, #PB_Material_AlphaBlend)
  
  tempMesh = LoadMesh(#PB_Any, "ninja.mesh")
  radius   = MeshRadius(tempMesh)
  scale    = #PLAYER_SIZE / radius
  
  With Player
    
    \entity = CreateEntity(#PB_Any, MeshID(tempMesh), #PB_Material_None, 0, 0, 0)
    ScaleEntity(\entity, scale, scale, scale, #PB_Absolute)
    EntityPhysicBody(\entity, #PB_Entity_CapsuleBody, 0.8, 0, 0)
    
    ;\physic = CreateEntity(#PB_Any, MeshID(tempMesh), #PB_Material_None, 0, 0, 0)
    ;ScaleEntity(\physic, scale, scale, scale, #PB_Absolute)
    ;HideEntity(\physic, #True)
    ;EntityPhysicBody(\physic, #PB_Entity_CapsuleBody, 0.8, 0, 0)
    
    \bbCross = CreateBillboardGroup(#PB_Any, MaterialID(tempMaterial), 1, 1, 0, 0, -10)
    AddBillboard(0, \bbCross, 0, 0, 0)
    
    \nodeMain    = CreateNode(#PB_Any, 0, #PLAYER_SIZE, 0)
    \nodeForward = CreateNode(#PB_Any, 0, 0, -1)
    \nodeStrafe  = CreateNode(#PB_Any, -1, 0, 0)
    \nodeCamera  = CreateNode(#PB_Any, 0, #CAMERA_DIST/4, #CAMERA_DIST)
    
    \camera = CreateCamera(#PB_Any, 0, 0, 100, 100)
    RotateCamera(\camera, -10, 0, 0, #PB_Absolute)
    
    AttachNodeObject(\nodeMain, NodeID(\nodeForward))
    AttachNodeObject(\nodeMain, NodeID(\nodeStrafe))
    AttachNodeObject(\nodeMain, NodeID(\nodeCamera))
    AttachNodeObject(\nodeMain, BillboardGroupID(\bbCross))
    AttachNodeObject(\nodeCamera, CameraID(\camera))
    
    \Keys\forward     = #PB_Key_W
    \Keys\backward    = #PB_Key_S
    \Keys\turnLeft    = #PB_Key_A
    \Keys\turnRight   = #PB_Key_D
    \Keys\strafeLeft  = #PB_Key_Q
    \Keys\strafeRight = #PB_Key_E
    \Keys\run         = #PB_Key_LeftShift
    
  EndWith
  
  FreeMesh(tempMesh)
  FreeMaterial(tempMaterial)
  FreeTexture(tempTexture)
  
EndProcedure

Procedure Player_UpdateControls()
  
  Shared             Player, DoLoop, TimeSinceLastFrame
  Protected.f        speed, speed2
  Protected.fVector3 mainPos, forwardPos, strafePos, forwardDir, strafeDir, forwardMove, strafeMove
  
  With Player
    
    GetPosition(\nodeMain, mainPos)
    GetPosition(\nodeForward, forwardPos)
    GetPosition(\nodeStrafe, strafePos)
    SubVec3(forwardPos, mainPos, forwardDir)
    SubVec3(strafePos, mainPos, strafeDir)
    
  EndWith
  
  If ExamineKeyboard()
    
    If KeyboardReleased(#PB_Key_Escape)
      DoLoop = #False
    EndIf
    
    With Player\Keys
      
      If KeyboardPushed(\run)
        speed = #SCALE_RUN * TimeSinceLastFrame
      Else
        speed = #SCALE_WALK * TimeSinceLastFrame
      EndIf
      speed2 = speed / 2
      
      If KeyboardPushed(\forward)
        forwardMove\x = forwardDir\x * speed
        forwardMove\z = forwardDir\z * speed
      ElseIf KeyboardPushed(\backward)
        forwardMove\x = forwardDir\x * -speed2
        forwardMove\z = forwardDir\z * -speed2
      Else
        forwardMove\x = 0
        forwardMove\z = 0
      EndIf
      
      If KeyboardPushed(\turnLeft)
        Player\Rotate\y = 1
      ElseIf KeyboardPushed(\turnRight)
        Player\Rotate\y = -1
      Else
        Player\Rotate\y = 0
      EndIf
      
      If KeyboardPushed(\strafeLeft)
        strafeMove\x = strafeDir\x * speed2
        strafeMove\z = strafeDir\z * speed2
      ElseIf KeyboardPushed(\strafeRight)
        strafeMove\x = strafeDir\x * -speed2
        strafeMove\z = strafeDir\z * -speed2
      Else
        strafeMove\x = 0
        strafeMove\z = 0
      EndIf
        
    EndWith
      
  EndIf
  
  If ExamineMouse()
    
    If Player\Rotate\y = 0
      Player\Rotate\x = -MouseDeltaY() * #SCALE_TURN
      Player\Rotate\y = -MouseDeltaX() * #SCALE_TURN
    EndIf
    
  EndIf
  
  AddVec3(forwardMove, strafeMove, Player\Move)
  
EndProcedure

Procedure Player_Update()
  
  Shared   Player
  Static.b walking
  
  Player_UpdateControls()
  
  With Player
    
    If \Move\x <> 0 Or \Move\z <> 0
      If Not walking
        StartEntityAnimation(\entity, "Walk")
        walking = #True
      EndIf
    Else
      If walking
        StopEntityAnimation(\entity, "Walk")
      EndIf
      walking = #False
    EndIf
    
    MoveEntity(\entity, \Move\x, 0, \Move\z, #PB_Relative)
    RotateEntity(\entity, 0, \Rotate\y, 0, #PB_Relative)
    ;MoveNode(\nodeMain, \Move\x, 0, \Move\z, #PB_Relative)
    ;RotateNode(\nodeMain, 0, \Rotate\y, 0, #PB_Relative)
    
    RotateNode(\nodeCamera, \Rotate\x, 0, 0, #PB_Relative)
    
    MoveNode(\nodeMain, EntityX(\entity), #PLAYER_SIZE, EntityZ(\entity), #PB_Absolute)
    RotateNode(\nodeMain, 0, EntityYaw(\entity), 0, #PB_Absolute) 
    ;MoveEntity(\entity, NodeX(\nodeMain), -#PLAYER_SIZE/2, NodeZ(\nodeMain), #PB_Absolute)
    ;RotateEntity(\entity, 0, NodeYaw(\nodeMain), 0, #PB_Absolute) 
    
    ;MoveEntity(\physic, NodeX(\nodeMain), -0.1, NodeZ(\nodeMain), #PB_Absolute)
    
  EndWith

EndProcedure

Procedure CheckCollisions()
  Shared      Level
  Protected.i first, second
  If ExamineWorldCollisions(#False)
    While NextWorldCollision()
      first  = FirstWorldCollisionEntity()
      second = SecondWorldCollisionEntity()
      If first <> Level\floor And second <> Level\floor
        Debug "Entity #"+Str(first)+" collide with Entity #"+Str(second)
      EndIf
    Wend
  EndIf
EndProcedure

Re: Entity is still inactive

Posted: Mon Oct 21, 2013 5:05 pm
by Comtois
try this

Code: Select all

   MoveEntity(\entity, \Move\x, 0, \Move\z)

Re: Entity is still inactive

Posted: Tue Oct 22, 2013 6:58 am
by Bananenfreak
I am online with my smartphone, so I can't test it. But why this should work? #Pb_Relative is the standart value and so there is no difference?!

EDIT:
I tested it. It works; So there is a Problem with MoveEntity() and the flag #PB_Relative. I´m not posting anything in our bugsection until Comtois confirmed this.