Page 1 of 1

WorldCollision and CameraFollow example

Posted: Wed Jul 17, 2013 6:39 am
by Comtois
Need 5.20 beta 6.
ExamineWorldCollisions(Mode)
Mode = #False -> only collision
Mode = #True -> collision and contacts

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - WorldCollision
;
;    (c) 2013 - Fantaisie Software
;
; ------------------------------------------------------------
;

IncludeFile "Screen3DRequester.pb"

#CameraSpeed = 2

#Sphere = 0
#Mesh = 1
#Line = #Mesh + 1
#Ground = 0
#Entity = 1
#Camera = 0

Define.f KeyX, KeyY, MouseX, MouseY, Speed1, Speed2, TimeSinceLastFrame, d = 0.4

If InitEngine3D(3)
  
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    Add3DArchive("Data/Textures", #PB_3DArchive_FileSystem)
    Add3DArchive("Data/Packs/skybox.zip", #PB_3DArchive_Zip)
    Parse3DScripts()
    
    WorldShadows(#PB_Shadow_Modulative, 100)
    
    CreateMaterial(0, LoadTexture(0, "Dirt.jpg"))
    CreateMaterial(1, LoadTexture(1, "DosCarte.png"))
    CreateMaterial(2, LoadTexture(2, "Wood.jpg"))
    
    CreateSphere(#Sphere, 0.06)
    CreateCube(#Mesh, 0.1)
    
    ;-Ground
    CreateEntity(#Ground,MeshID(#Mesh),MaterialID(0))
    ScaleEntity(#Ground, 150, 1, 150)
    EntityRenderMode(#Ground, 0)
    EntityPhysicBody(#Ground, #PB_Entity_BoxBody, 0, 0, 0.1)
    
    CreateEntity(#Entity, MeshID(#Sphere), MaterialID(1), 0, 0.2, 0)
    
    EntityPhysicBody(#Entity, #PB_Entity_SphereBody, 0.5, 0, 0.0)
    
    For i = 0 To 100
      If Random(100)>50
        Ent = CreateEntity(#PB_Any, MeshID(#Mesh), MaterialID(2), 7.5-Random(15), 0.5, 7.5-Random(15))
        EntityPhysicBody(Ent, #PB_Entity_BoxBody, 0.1, 0, 0.3)
      Else
        Ent = CreateEntity(#PB_Any, MeshID(#Mesh), MaterialID(0), 7.5-Random(15), 0.1, 7.5-Random(15))
        EntityPhysicBody(Ent, #PB_Entity_BoxBody, 0, 0, 0.3)
      EndIf
    Next
    
    SkyBox("stevecube.jpg")
    
    CreateCamera(#Camera, 0, 0, 100, 100)
    CameraRange(#Camera, 0.01, 50)
    
    CameraFollow(#Camera, EntityID(#Entity), 180, EntityY(#Entity) + 0.3, 1, 1, 1)
    
    CreateLight(0, RGB(155, 155, 155), 330, 500, 330)
    AmbientColor(RGB(100, 100, 100))
    
    Repeat
      Screen3DEvents()
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.4
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.5
      EndIf
      
      Speed1 * 0.9
      Speed2 * 0.9
      If ExamineKeyboard()
        
        If KeyboardPushed(#PB_Key_Left)
          Speed2 = Speed2 + 0.1 * ((0.02 * TimeSinceLastFrame) - Speed2) 
        ElseIf KeyboardPushed(#PB_Key_Right)
          Speed2 = Speed2 + 0.1 * ((-0.02 * TimeSinceLastFrame) - Speed2) 
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          Speed1 = Speed1 + 0.05 * ((0.03 * TimeSinceLastFrame) - Speed1) 
        ElseIf KeyboardPushed(#PB_Key_Down)
          Speed1 = Speed1 + 0.05 * ((-0.03 * TimeSinceLastFrame) - Speed1) 
        EndIf   
        
      EndIf
      
      RotateEntity(#Entity, 0, MouseX * TimeSinceLastFrame/100, 0, #PB_Relative)
      MoveEntity(#Entity, Speed2, 0, Speed1, #PB_Local)
      
      CameraFollow(#Camera, EntityID(#Entity), 180, EntityY(#Entity) + 0.2, 0.8, 0.08, 0.1)
      
      If ExamineWorldCollisions(#True) ; get also contacts
        While NextWorldCollision()
          If FirstWorldCollisionEntity() <> #Ground And SecondWorldCollisionEntity() <> #Ground
            WorldCollisionContact()
            x.f = GetX()
            y.f = GetY()
            z.f = GetZ()
            WorldCollisionNormal()
            xn.f = x + GetX() * d
            yn.f = y + GetY() * d
            zn.f = z + GetZ() * d
            
            CreateLine3D(Total + #Line, x, y, z, $FF00, xn ,yn, zn, $FF00) 
            total + 1            
            
            If total > 200
              
              For i = 0 To Total-1
                FreeMesh(i + #Line)
              Next
              total = 0
            EndIf  
            
          EndIf
        Wend
        
      EndIf
      
      TimeSinceLastFrame = RenderWorld(50)
      Screen3DStats()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End

Re: WorldCollision and CameraFollow example

Posted: Wed Jul 17, 2013 6:52 am
by aonyn
Thanks Comtois,

I had requested such a sample from Fred in the beta announcement post.

This is exactly what I needed to start working the new collision commands into my project (was hoping for such features).

Thank you for the example.

Regards,
David

Re: WorldCollision and CameraFollow example

Posted: Wed Jul 17, 2013 1:09 pm
by Olby
Comtois wrote:Need 5.20 beta 6.
ExamineWorldCollisions(Mode)
Mode = #False -> only collision
Mode = #True -> collision and contacts

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - WorldCollision
;
;    (c) 2013 - Fantaisie Software
;
; ------------------------------------------------------------
;

IncludeFile "Screen3DRequester.pb"

#CameraSpeed = 2

#Sphere = 0
#Mesh = 1
#Line = #Mesh + 1
#Ground = 0
#Entity = 1
#Camera = 0

Define.f KeyX, KeyY, MouseX, MouseY, Speed1, Speed2, TimeSinceLastFrame, d = 0.4

If InitEngine3D(3)
  
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    Add3DArchive("Data/Textures", #PB_3DArchive_FileSystem)
    Add3DArchive("Data/Packs/skybox.zip", #PB_3DArchive_Zip)
    Parse3DScripts()
    
    WorldShadows(#PB_Shadow_Modulative, 100)
    
    CreateMaterial(0, LoadTexture(0, "Dirt.jpg"))
    CreateMaterial(1, LoadTexture(1, "DosCarte.png"))
    CreateMaterial(2, LoadTexture(2, "Wood.jpg"))
    
    CreateSphere(#Sphere, 0.06)
    CreateCube(#Mesh, 0.1)
    
    ;-Ground
    CreateEntity(#Ground,MeshID(#Mesh),MaterialID(0))
    ScaleEntity(#Ground, 150, 1, 150)
    EntityRenderMode(#Ground, 0)
    EntityPhysicBody(#Ground, #PB_Entity_BoxBody, 0, 0, 0.1)
    
    CreateEntity(#Entity, MeshID(#Sphere), MaterialID(1), 0, 0.2, 0)
    
    EntityPhysicBody(#Entity, #PB_Entity_SphereBody, 0.5, 0, 0.0)
    
    For i = 0 To 100
      If Random(100)>50
        Ent = CreateEntity(#PB_Any, MeshID(#Mesh), MaterialID(2), 7.5-Random(15), 0.5, 7.5-Random(15))
        EntityPhysicBody(Ent, #PB_Entity_BoxBody, 0.1, 0, 0.3)
      Else
        Ent = CreateEntity(#PB_Any, MeshID(#Mesh), MaterialID(0), 7.5-Random(15), 0.1, 7.5-Random(15))
        EntityPhysicBody(Ent, #PB_Entity_BoxBody, 0, 0, 0.3)
      EndIf
    Next
    
    SkyBox("stevecube.jpg")
    
    CreateCamera(#Camera, 0, 0, 100, 100)
    CameraRange(#Camera, 0.01, 50)
    
    CameraFollow(#Camera, EntityID(#Entity), 180, EntityY(#Entity) + 0.3, 1, 1, 1)
    
    CreateLight(0, RGB(155, 155, 155), 330, 500, 330)
    AmbientColor(RGB(100, 100, 100))
    
    Repeat
      Screen3DEvents()
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.4
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.5
      EndIf
      
      Speed1 * 0.9
      Speed2 * 0.9
      If ExamineKeyboard()
        
        If KeyboardPushed(#PB_Key_Left)
          Speed2 = Speed2 + 0.1 * ((0.02 * TimeSinceLastFrame) - Speed2) 
        ElseIf KeyboardPushed(#PB_Key_Right)
          Speed2 = Speed2 + 0.1 * ((-0.02 * TimeSinceLastFrame) - Speed2) 
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          Speed1 = Speed1 + 0.05 * ((0.03 * TimeSinceLastFrame) - Speed1) 
        ElseIf KeyboardPushed(#PB_Key_Down)
          Speed1 = Speed1 + 0.05 * ((-0.03 * TimeSinceLastFrame) - Speed1) 
        EndIf   
        
      EndIf
      
      RotateEntity(#Entity, 0, MouseX * TimeSinceLastFrame/100, 0, #PB_Relative)
      MoveEntity(#Entity, Speed2, 0, Speed1, #PB_Local)
      
      CameraFollow(#Camera, EntityID(#Entity), 180, EntityY(#Entity) + 0.2, 0.8, 0.08, 0.1)
      
      If ExamineWorldCollisions(#True) ; get also contacts
        While NextWorldCollision()
          If FirstWorldCollisionEntity() <> #Ground And SecondWorldCollisionEntity() <> #Ground
            WorldCollisionContact()
            x.f = GetX()
            y.f = GetY()
            z.f = GetZ()
            WorldCollisionNormal()
            xn.f = x + GetX() * d
            yn.f = y + GetY() * d
            zn.f = z + GetZ() * d
            
            CreateLine3D(Total + #Line, x, y, z, $FF00, xn ,yn, zn, $FF00) 
            total + 1            
            
            If total > 200
              
              For i = 0 To Total-1
                FreeMesh(i + #Line)
              Next
              total = 0
            EndIf  
            
          EndIf
        Wend
        
      EndIf
      
      TimeSinceLastFrame = RenderWorld(50)
      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 have a question about the WorldCollision example. There are some entities which always (after each ExamineWorldCollisions call) report collisions even if the objects seem to be stationary. I was expecting that once an initial collision has occurred the objects "stick" (stop jittering) and physics body goes off into sleep mode. In the example above we have a "collision leak" and if I where to attach an impact sound sample to each collision event it would play the sound all the time. How would we go about this and prevent such situations?

Re: WorldCollision and CameraFollow example

Posted: Thu Jul 18, 2013 11:36 am
by Comtois
I dont know why these entities are still being tested by bullet, even if they are sleeping (not active !).

It work fine if i change line 64

Code: Select all

EntityPhysicBody(Ent, #PB_Entity_BoxBody, 0, 0, 0.3)
by this one

Code: Select all

EntityPhysicBody(Ent, #PB_Entity_StaticBody, 0, 0, 0.3)

Re: WorldCollision and CameraFollow example

Posted: Thu Jul 18, 2013 6:01 pm
by Comtois
we will add #PB_Entity_IsActive for GetEntityAttribute() so you can stop a sound if the entity isn't active.

Re: WorldCollision and CameraFollow example

Posted: Thu Jul 18, 2013 9:34 pm
by Olby
Comtois wrote:we will add #PB_Entity_IsActive for GetEntityAttribute() so you can stop a sound if the entity isn't active.
That sounds like a plan. Thanks!

Re: WorldCollision and CameraFollow example

Posted: Fri Jul 19, 2013 7:26 am
by Comtois
Olby wrote:I have a question about the WorldCollision example. There are some entities which always (after each ExamineWorldCollisions call) report collisions even if the objects seem to be stationary. I was expecting that once an initial collision has occurred the objects "stick" (stop jittering) and physics body goes off into sleep mode. In the example above we have a "collision leak" and if I where to attach an impact sound sample to each collision event it would play the sound all the time. How would we go about this and prevent such situations?
Fixed