WorldCollision and CameraFollow example

Everything related to 3D programming
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

WorldCollision and CameraFollow example

Post 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
Please correct my english
http://purebasic.developpez.com/
aonyn
User
User
Posts: 43
Joined: Tue May 05, 2009 5:20 am

Re: WorldCollision and CameraFollow example

Post 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
Olby
Enthusiast
Enthusiast
Posts: 461
Joined: Mon Jan 12, 2009 10:33 am
Contact:

Re: WorldCollision and CameraFollow example

Post 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?
Intel Core i7 Quad 2.3 Ghz, 8GB RAM, GeForce GT 630M 2GB, Windows 10 (x64)
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: WorldCollision and CameraFollow example

Post 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)
Please correct my english
http://purebasic.developpez.com/
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: WorldCollision and CameraFollow example

Post by Comtois »

we will add #PB_Entity_IsActive for GetEntityAttribute() so you can stop a sound if the entity isn't active.
Please correct my english
http://purebasic.developpez.com/
Olby
Enthusiast
Enthusiast
Posts: 461
Joined: Mon Jan 12, 2009 10:33 am
Contact:

Re: WorldCollision and CameraFollow example

Post 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!
Intel Core i7 Quad 2.3 Ghz, 8GB RAM, GeForce GT 630M 2GB, Windows 10 (x64)
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: WorldCollision and CameraFollow example

Post 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
Please correct my english
http://purebasic.developpez.com/
Post Reply