Utilisation d'ogre3D pour de la 2D

Généralités sur la programmation 3D
G-Rom
Messages : 3626
Inscription : dim. 10/janv./2010 5:29

Utilisation d'ogre3D pour de la 2D

Message par G-Rom »

Ce petit code montre comment utilisé Ogre3D pour faire de la 2D avec la physique et tout le tralala que propose PB.
Ce qui pleurent que PB à de la 2D Has-Been ( même si ils ont pas tout à fait tord ;) ) , non plus de raison de l'être.
La technique est simple on affiche des plans 2D tout bêtement.
pour la physique , PB permet de contraindre les objets sur certain axes ( en mouvement linéaire & en rotation )
j'utilise donc pour la physique du petit jeu , des cube de 1 unité de coté que j'ai contraint sur les axes , je vous laisse découvrir le code qui à la base
me servais pour le punch en cour , flèches & espace pour jouer :

sheet.png

Code : Tout sélectionner

IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"
Global Quit.b = #False 

Structure Tile
  CollideEntity.l
  TileEntity.l
  life.l
  autotile.b
  climb.b
EndStructure

Structure Destructible
  ID.l
  x.l:y.l
  explode.b
  explodeForce.f
  scrapType.l
EndStructure

NewList Destructible.Destructible()

Structure pt
  ent.l
  mat.l
  px.d
  py.d
  ox.d
  oy.d
  v.d
  a.d
  t.d
  ttl.l
EndStructure

Global NewList Particle.pt()


Global Dim Pixels.i(1920,1080)
Global Dim ground.Tile(512,128)

CamX.d:CamY.d = 128:CamZ.d = 100


If InitEngine3D():InitSprite():InitKeyboard():InitJoystick()
  
  If Screen3DRequester()
    
    Declare.i CreateTile(Size.d)
    Declare.d LinearInterpolate(y1.d,y2.d,mu.d)
    Declare CreateParticle(x.f,y.f,angle.f,mat.i,vx.d,vy.d)
    
    ; Create tile
    ;
    Add3DArchive("./",#PB_3DArchive_FileSystem)
    SHEET = LoadTexture(#PB_Any,"sheet.png")
    
    StartDrawing(TextureOutput(SHEET))
      For y = 0 To 1080-1
        For x = 0 To 1920-1
          DrawingMode(#PB_2DDrawing_AlphaChannel)
          Pixels(x,y) = Point(x,y)
        Next
      Next
    StopDrawing()
    
    index = 0
    
    For ty = 0 To 8
      For tx = 0 To 15
        CreateTexture(index,32,32)
        StartDrawing(TextureOutput(index))
          DrawingMode(#PB_2DDrawing_AlphaChannel):Box(0,0,32,32,RGBA(0,0,0,0))
          DrawingMode(#PB_2DDrawing_AlphaBlend)
          For y = 0 To 31
            For x = 0 To 31
              Plot(x,31-y,Pixels(x+(tx*32),y+(ty*32)))
            Next
          Next 
          
        StopDrawing()
        
        CreateMaterial(index,TextureID(index))
        MaterialBlendingMode(index,#PB_Material_AlphaBlend)
        MaterialFilteringMode(index, #PB_Material_None)
        
        index+1
      Next 
    Next
    

    
    BG_TEX = CreateTexture(#PB_Any,240,270)
    StartDrawing(TextureOutput(BG_TEX))
      DrawingMode(#PB_2DDrawing_AlphaChannel)
      Box(0,0,240,270,RGBA(0,0,0,0))
      DrawingMode(#PB_2DDrawing_AlphaBlend)
      For y = 0 To 270-1
        For x = 0 To 240-1
           Plot(x,269-y,Pixels(x+960,y))
        Next
      Next      
    StopDrawing()
    m = CreateMaterial(#PB_Any,TextureID(BG_TEX))
    MaterialBlendingMode(m,#PB_Material_AlphaBlend)
    MaterialFilteringMode(m, #PB_Material_None)
    
    BG_TEX = CreateTexture(#PB_Any,240,270)
    StartDrawing(TextureOutput(BG_TEX))
      DrawingMode(#PB_2DDrawing_AlphaChannel)
      Box(0,0,240,270,RGBA(0,0,0,0))
      DrawingMode(#PB_2DDrawing_AlphaBlend)
      For y = 0 To 270-1
        For x = 0 To 240-1
           Plot(x,269-y,Pixels(x+1200,y))
        Next
      Next      
    StopDrawing()
    mb = CreateMaterial(#PB_Any,TextureID(BG_TEX))
    MaterialBlendingMode(mb,#PB_Material_AlphaBlend)
    MaterialFilteringMode(mb, #PB_Material_None)
    
    
 
    
    For i = 0 To 20
    PlaneMesh = CreatePlane(#PB_Any,240,270,1,1,1,1)
    e = CreateEntity(#PB_Any,MeshID(PlaneMesh),MaterialID(mb),(-i*240)+Random(120),135-70+Random(140),-250)
    RotateEntity(e,90,0,0)
    
    e = CreateEntity(#PB_Any,MeshID(PlaneMesh),MaterialID(m),(-i*340)+Random(220),135-70+Random(140),-450)
    RotateEntity(e,90,0,0)
    Next 
    
    
    INVISIBLE_TEXTURE = CreateTexture(#PB_Any,64,64)
    StartDrawing(TextureOutput(INVISIBLE_TEXTURE))
      DrawingMode(#PB_2DDrawing_AlphaChannel)
      Box(0,0,64,64,RGBA(0,0,0,0))
;         DrawingMode(#PB_2DDrawing_AlphaBlend)
;        Box(0,0,64,64,RGBA(255,0,0,64))
    StopDrawing()
    
    INVISIBLE_MATERIAL = CreateMaterial(#PB_Any,TextureID(INVISIBLE_TEXTURE))
    MaterialBlendingMode(INVISIBLE_MATERIAL,#PB_Material_AlphaBlend)
    
    
    ; create tile mesh
    ;
    TILE_MESH = CreateTile(1)
    CUBE_MESH = CreateCube(#PB_Any,1)
    MCUBE_MESH = CreateCube(#PB_Any,0.5)
   
     For y = 0 To 127
       For x = 0 To 511
         value.i =  Pixels(x,(1080-127)+y)
        
         If value = $FFFFFFFF
           ground(x,y)\autotile         = #True 
           ground(x,y)\life             = -1
           ground(x,y)\TileEntity       = CreateEntity(#PB_Any,MeshID(TILE_MESH),MaterialID(0),x,128-y,0)
           ground(x,y)\CollideEntity    = CreateEntity(#PB_Any,MeshID(CUBE_MESH),MaterialID(INVISIBLE_MATERIAL),x,128-y,0)
           EntityPhysicBody(ground(x,y)\CollideEntity,#PB_Entity_BoxBody,0,0.1,0.5)
;            CreateEntityBody(ground(x,y)\CollideEntity,#PB_Entity_BoxBody,0,0.1,0.5)
         EndIf 
         
         If value = $FF002080
           ground(x,y)\life             = 8
           ground(x,y)\TileEntity       = CreateEntity(#PB_Any,MeshID(TILE_MESH),MaterialID(48))
           ground(x,y)\CollideEntity    = CreateEntity(#PB_Any,MeshID(CUBE_MESH),MaterialID(INVISIBLE_MATERIAL),x,128-y,0)
           AttachEntityObject(ground(x,y)\CollideEntity,"",EntityID(ground(x,y)\TileEntity))
           EntityPhysicBody(ground(x,y)\CollideEntity,#PB_Entity_BoxBody,0.8,0,0.5)
;            CreateEntityBody(ground(x,y)\CollideEntity,#PB_Entity_BoxBody,0.8,0,0.5)
           EntityAngularFactor(ground(x,y)\CollideEntity,0,0,1) 
           EntityLinearFactor(ground(x,y)\CollideEntity,1,1,0)
           
           AddElement(Destructible())
           Destructible()\ID = ground(x,y)\CollideEntity
           Destructible()\x = x:Destructible()\y = y
           Destructible()\scrapType = 1 ; wood
         EndIf 
         
         
         If value = $FF808080
           ground(x,y)\climb = #True 
           ground(x,y)\TileEntity       = CreateEntity(#PB_Any,MeshID(TILE_MESH),MaterialID(41+Random(6,0)),x,128-y,-0.1)
         EndIf 
         
         
       Next
     Next
     
     
     
     ;autotile
     
     For y = 0 To 127
      For x = 0 To 511
        
       ;level 
      If ground(x,y)\autotile
        tile_value.l = 0 
        If y-1 =>0  : If ground(x,y-1)\autotile = 1 : tile_value + 1 :  EndIf :  EndIf
        If y+1 <128 : If ground(x,y+1)\autotile = 1 : tile_value + 4 :  EndIf :  EndIf
        If x-1 =>0  : If ground(x-1,y)\autotile = 1 : tile_value + 8 :  EndIf :  EndIf
        If x+1 <512 : If ground(x+1,y)\autotile = 1 : tile_value + 2 :  EndIf :  EndIf
        SetEntityMaterial(ground(x,y)\TileEntity,MaterialID(tile_value))
      EndIf 
      Next
    Next
  
     
     
    
    TILE_MESH = CreateSphere(#PB_Any,0.49,32,32);CreateTile(0.8)
    
    D = CreateEntity(#PB_Any,MeshID(TILE_MESH),MaterialID(INVISIBLE_MATERIAL),0,128,0)
    EntityPhysicBody(D,#PB_Entity_SphereBody,1,0.1,0.5)
;     CreateEntityBody(D,#PB_Entity_SphereBody,1,0.1,0.5)
    EntityAngularFactor(D,0,0,0) 
    EntityLinearFactor(D,1,1,0)
    
    T = CreateTile(1)
    TM = CreateEntity(#PB_Any,MeshID(T),MaterialID(32))
    AttachEntityObject(D,"",EntityID(TM))
 
    WorldGravity(-9.81)
    
    
    Camera2D = CreateCamera(#PB_Any,0,0,100,100)
    CameraBackColor(Camera2D,RGB(255,128,64))
    CameraRange(Camera2D,0.01,1000)
    CameraFOV(Camera2D,40)
    
    AmbientColor(RGB(255,128,64))
;     CreateLight(0,$CACACA,100,100,100)
    
    deltaTime.f = 0
    While #True
      GameClock = ElapsedMilliseconds()
      
      Screen3DEvents()
      If Quit Or KeyboardPushed(#PB_Key_Escape)
         Break
      EndIf 
      
      
      If KeyboardPushed(#PB_Key_Right) And GetEntityAttribute(D,#PB_Entity_LinearVelocityX) < 4
        ApplyEntityForce(D,10,0,0)
      EndIf
      
      If KeyboardPushed(#PB_Key_Left) And GetEntityAttribute(D,#PB_Entity_LinearVelocityX) > -4
        ApplyEntityForce(D,-10,0,0)
      EndIf
      
      Tx = Int(EntityX(D)+0.5)
      Ty = (128)-Int(EntityY(D))
       
      If Tx<0 : Tx=0 : EndIf 
      If Ty<0 : Ty=0 : EndIf 
      If Tx>511 : Tx=511 : EndIf 
      If Ty>127 : Ty=127 : EndIf 
      
      
      If ground(Tx,Ty)\climb = 1 And kf = #False 
        If GetEntityAttribute(D,#PB_Entity_LinearVelocityY)<=1.8
          EntityVelocity(D, GetEntityAttribute(D,#PB_Entity_LinearVelocityX)/1.1 , 0, 0)
          EntityLinearFactor(D,1,0,0)
        EndIf 
      ElseIf ground(Tx,Ty)\climb = 0
        EntityLinearFactor(D,1,1,0)
      ElseIf ground(Tx,Ty)\climb = 1 And kf = #True 
        If GetEntityAttribute(D,#PB_Entity_LinearVelocityY)<=1.8
          EntityLinearFactor(D,1,1,0)
          ApplyEntityImpulse(D,0,7.5,0) 
          kf = #False 
        EndIf 
      EndIf 
      
      If KeyboardPushed(#PB_Key_Up) And ground(Tx,Ty)\climb
       MoveEntity(D,0,0.1*deltaTime,0)
      EndIf 
      
      If KeyboardPushed(#PB_Key_Down) And ground(Tx,Ty)\climb
        MoveEntity(D,0,-0.1*deltaTime,0)
      EndIf 
      
      
      If KeyboardPushed(#PB_Key_Space) And kf = #False And Abs(GetEntityAttribute(D,#PB_Entity_LinearVelocityY)) < 0.005
        kf = #True         
        ApplyEntityImpulse(D,0,7.5,0)        
      EndIf 

      
      If Not KeyboardPushed(#PB_Key_Space) And kf=#True
        kf=#False 
      EndIf 

      
      SetEntityAttribute(D,#PB_Entity_LinearSleeping,0)
      SetEntityAttribute(D,#PB_Entity_AngularSleeping,0)

      
      ExamineWorldCollisions(1)
      
      While NextWorldCollision()
        ForEach Destructible()
          If Destructible()\ID = FirstWorldCollisionEntity()
            WorldCollisionAppliedImpulse()
            
            ground( Destructible()\x,Destructible()\y)\life - GetY()
            
            If ground( Destructible()\x,Destructible()\y)\life < 0
              For i = 0 To 9
                vx.d=GetEntityAttribute(Destructible()\ID,#PB_Entity_LinearVelocityX)
                vy.d=GetEntityAttribute(Destructible()\ID,#PB_Entity_LinearVelocityY)
                CreateParticle(EntityX(Destructible()\ID),EntityY(Destructible()\ID),90,49+Random(2),vx,vy)
              Next 
              FreeEntity( ground( Destructible()\x,Destructible()\y)\CollideEntity)
               FreeEntity( ground( Destructible()\x,Destructible()\y)\TileEntity)
               DeleteElement(Destructible())
               Break 2
            EndIf 
             
          EndIf 
        Next
      Wend 
      
      ForEach Particle()
       
        If(Particle()\ttl<ElapsedMilliseconds())
           FreeEntity(Particle()\ent)
            DeleteElement(Particle(),1)
         EndIf 
        
      Next
      

      
      

      
      CamX = LinearInterpolate(CamX,EntityX(D),deltaTime/600)
      CamY = LinearInterpolate(CamY,EntityY(D),deltaTime/200)
       
      
      
      MoveCamera(Camera2D,CamX,CamY,10,#PB_Absolute)
      CameraLookAt(Camera2D,EntityX(D),EntityY(D),0)
      
      
      RenderWorld(deltaTime*2) ; Draw 3d stuff
                             ; Draw hud
      FlipBuffers()
      deltaTime = (ElapsedMilliseconds() - GameClock)
    Wend
      
    
    
    
    
    
    
  EndIf
  
Else
  MessageRequester("Error","Engine3D.dll error when loading.")
  End
EndIf

Procedure.i CreateTile(Size.d)
    TILE_MESH = CreateMesh(#PB_Any)
    
    MeshVertexPosition(-(Size/2), -(Size/2), 0)
    MeshVertexTextureCoordinate(0, 0)
    
    MeshVertexPosition((Size/2), -(Size/2), 0)
    MeshVertexTextureCoordinate(1, 0)
    
    MeshVertexPosition((Size/2), (Size/2), 0)
    MeshVertexTextureCoordinate(1, 1)
    
    MeshVertexPosition(-(Size/2), (Size/2), 0)
    MeshVertexTextureCoordinate(0, 1)
    
    MeshFace(0,1,2)
    MeshFace(3,0,2)
    
    FinishMesh(#True)
    NormalizeMesh(TILE_MESH) 
    UpdateMeshBoundingBox(TILE_MESH)
    ProcedureReturn TILE_MESH
EndProcedure

Procedure.d LinearInterpolate(y1.d,y2.d,mu.d)
   ProcedureReturn(y1*(1-mu)+y2*mu)
EndProcedure


Procedure CreateParticle(x.f,y.f,angle.f,mat.i,vx.d,vy.d)
  AddElement(Particle())
  
  Particle()\ox = (x-0.5)+((10-Random(10))/10)
  Particle()\oy = (y-0.5)+((10-Random(10))/10)
  Particle()\ttl = ElapsedMilliseconds()+3000
  Particle()\ent = CreateEntity(#PB_Any,MeshID(CreateTile(1)),MaterialID(mat),Particle()\ox,Particle()\oy,0.5)

    EntityAngularFactor(Particle()\ent,0,0,1) 
    EntityLinearFactor(Particle()\ent,1,1,0)
    EntityPhysicBody(Particle()\ent,#PB_Entity_BoxBody,4,1,0)
    ApplyEntityImpulse(Particle()\ent,(((-10+Random(20))/10)*3)+vx,(((-10+Random(20))/10)*10)+vy,0)
    SetEntityCollisionFilter(Particle()\ent, 1, $1001)

    
EndProcedure
Avatar de l’utilisateur
microdevweb
Messages : 1798
Inscription : mer. 29/juin/2011 14:11
Localisation : Belgique

Re: Utilisation d'ogre3D pour de la 2D

Message par microdevweb »

Merci G-Rom,

Vraiment très instructif.
Windows 10 64 bits PB: 5.70 ; 5.72 LST
Work at Centre Spatial de Liège
G-Rom
Messages : 3626
Inscription : dim. 10/janv./2010 5:29

Re: Utilisation d'ogre3D pour de la 2D

Message par G-Rom »

Merci, je ferais un article plus imagé après le punch.
Avatar de l’utilisateur
Cool Dji
Messages : 1126
Inscription : ven. 05/sept./2008 11:42
Localisation : Besançon
Contact :

Re: Utilisation d'ogre3D pour de la 2D

Message par Cool Dji »

Cool,

T'es à fond G-Rom, merci :D
Only PureBasic makes it possible
comtois
Messages : 5172
Inscription : mer. 21/janv./2004 17:48
Contact :

Re: Utilisation d'ogre3D pour de la 2D

Message par comtois »

Si tu es d'accord je pourrais l'ajouter aux exemples 3D de la doc. C'est un bel exemple de jeu de plateforme.
http://purebasic.developpez.com/
Je ne réponds à aucune question technique en PV, utilisez le forum, il est fait pour ça, et la réponse peut profiter à tous.
G-Rom
Messages : 3626
Inscription : dim. 10/janv./2010 5:29

Re: Utilisation d'ogre3D pour de la 2D

Message par G-Rom »

Laisse moi m'en occuper dans ce cas.
comtois
Messages : 5172
Inscription : mer. 21/janv./2004 17:48
Contact :

Re: Utilisation d'ogre3D pour de la 2D

Message par comtois »

ok
http://purebasic.developpez.com/
Je ne réponds à aucune question technique en PV, utilisez le forum, il est fait pour ça, et la réponse peut profiter à tous.
G-Rom
Messages : 3626
Inscription : dim. 10/janv./2010 5:29

Re: Utilisation d'ogre3D pour de la 2D

Message par G-Rom »

Tu veut un exemple court , ou un exemple plus imagé ?
comtois
Messages : 5172
Inscription : mer. 21/janv./2004 17:48
Contact :

Re: Utilisation d'ogre3D pour de la 2D

Message par comtois »

Faut que ça reste court, et pas gourmand en média, ton image fait 4 ko c'est bien.

Si tu peux améliorer l"animation du personnage, avec une image quand il monte , descend, marche à droite, marche à gauche, sans alourdir la taille de l'image, ça serait parfait.

Pas la peine de faire un jeu complet, ta démo donne déjà plein d'idées de ce qu'il est possible de faire.
http://purebasic.developpez.com/
Je ne réponds à aucune question technique en PV, utilisez le forum, il est fait pour ça, et la réponse peut profiter à tous.
comtois
Messages : 5172
Inscription : mer. 21/janv./2004 17:48
Contact :

Re: Utilisation d'ogre3D pour de la 2D

Message par comtois »

Il y a 390 lignes dans ton code, au besoin tu peux l'allonger un peu pour ajouter des effets supplémentaires comme le tir ou/et la gestion des points ou ce que tu veux, en te limitant à 700 lignes maxi.Je crois que c'est déjà pas mal pour une démo.
http://purebasic.developpez.com/
Je ne réponds à aucune question technique en PV, utilisez le forum, il est fait pour ça, et la réponse peut profiter à tous.
G-Rom
Messages : 3626
Inscription : dim. 10/janv./2010 5:29

Re: Utilisation d'ogre3D pour de la 2D

Message par G-Rom »

je te laisse géré comtois , j'ai plus le temps du coup.
je passe à autre chose. Merci.
comtois
Messages : 5172
Inscription : mer. 21/janv./2004 17:48
Contact :

Re: Utilisation d'ogre3D pour de la 2D

Message par comtois »

Version corrigée pour la 5.50. L'adresse de l'image nécessaire se trouve dans le premier post. Renommez l'image sheet.png.

Code : Tout sélectionner

IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"
Global Quit.b = #False

Structure Tile
  CollideEntity.l
  TileEntity.l
  life.l
  autotile.b
  climb.b
EndStructure

Structure Destructible
  ID.l
  x.l:y.l
  explode.b
  explodeForce.f
  scrapType.l
EndStructure

NewList Destructible.Destructible()

Structure pt
  ent.l
  mat.l
  px.d
  py.d
  ox.d
  oy.d
  v.d
  a.d
  t.d
  ttl.l
EndStructure

Global NewList Particle.pt()


Global Dim Pixels.i(1920,1080)
Global Dim ground.Tile(512,128)

CamX.d:CamY.d = 128:CamZ.d = 100


If InitEngine3D():InitSprite():InitKeyboard():InitJoystick()
  
  If Screen3DRequester()
    
    Declare.i CreateTile(Size.d)
    Declare.d LinearInterpolate(y1.d,y2.d,mu.d)
    Declare CreateParticle(x.f,y.f,angle.f,mat.i,vx.d,vy.d)
    
    ; Create tile
    ;
    Add3DArchive("./",#PB_3DArchive_FileSystem)
    SHEET = LoadTexture(#PB_Any,"sheet.png")
    
    StartDrawing(TextureOutput(SHEET))
    For y = 0 To 1080-1
      For x = 0 To 1920-1
        DrawingMode(#PB_2DDrawing_AlphaChannel)
        Pixels(x,y) = Point(x,y)
      Next
    Next
    StopDrawing()
    
    index = 0
    
    For ty = 0 To 8
      For tx = 0 To 15
        CreateTexture(index,32,32,"")
        StartDrawing(TextureOutput(index))
        DrawingMode(#PB_2DDrawing_AlphaChannel):Box(0,0,32,32,RGBA(0,0,0,0))
        DrawingMode(#PB_2DDrawing_AlphaBlend)
        For y = 0 To 31
          For x = 0 To 31
            Plot(x,31-y,Pixels(x+(tx*32),y+(ty*32)))
          Next
        Next
        
        StopDrawing()
        
        CreateMaterial(index,TextureID(index))
        MaterialBlendingMode(index,#PB_Material_AlphaBlend)
        MaterialFilteringMode(index, #PB_Material_None)
        
        index+1
      Next
    Next
    
    
    
    BG_TEX = CreateTexture(#PB_Any,240,270,"")
    StartDrawing(TextureOutput(BG_TEX))
    DrawingMode(#PB_2DDrawing_AlphaChannel)
    Box(0,0,240,270,RGBA(0,0,0,0))
    DrawingMode(#PB_2DDrawing_AlphaBlend)
    For y = 0 To 270-1
      For x = 0 To 240-1
        Plot(x,269-y,Pixels(x+960,y))
      Next
    Next     
    StopDrawing()
    m = CreateMaterial(#PB_Any,TextureID(BG_TEX))
    MaterialBlendingMode(m,#PB_Material_AlphaBlend)
    MaterialFilteringMode(m, #PB_Material_None)
    
    BG_TEX = CreateTexture(#PB_Any,240,270,"")
    StartDrawing(TextureOutput(BG_TEX))
    DrawingMode(#PB_2DDrawing_AlphaChannel)
    Box(0,0,240,270,RGBA(0,0,0,0))
    DrawingMode(#PB_2DDrawing_AlphaBlend)
    For y = 0 To 270-1
      For x = 0 To 240-1
        Plot(x,269-y,Pixels(x+1200,y))
      Next
    Next     
    StopDrawing()
    mb = CreateMaterial(#PB_Any,TextureID(BG_TEX))
    MaterialBlendingMode(mb,#PB_Material_AlphaBlend)
    MaterialFilteringMode(mb, #PB_Material_None)
    
    
    
    
    For i = 0 To 20
      PlaneMesh = CreatePlane(#PB_Any,240,270,1,1,1,1)
      e = CreateEntity(#PB_Any,MeshID(PlaneMesh),MaterialID(mb),(-i*240)+Random(120),135-70+Random(140),-250)
      RotateEntity(e,90,0,0)
      
      e = CreateEntity(#PB_Any,MeshID(PlaneMesh),MaterialID(m),(-i*340)+Random(220),135-70+Random(140),-450)
      RotateEntity(e,90,0,0)
    Next
    
    
    INVISIBLE_TEXTURE = CreateTexture(#PB_Any,64,64,"")
    StartDrawing(TextureOutput(INVISIBLE_TEXTURE))
    DrawingMode(#PB_2DDrawing_AlphaChannel)
    Box(0,0,64,64,RGBA(0,0,0,0))
    ;         DrawingMode(#PB_2DDrawing_AlphaBlend)
    ;        Box(0,0,64,64,RGBA(255,0,0,64))
    StopDrawing()
    
    INVISIBLE_MATERIAL = CreateMaterial(#PB_Any,TextureID(INVISIBLE_TEXTURE))
    MaterialBlendingMode(INVISIBLE_MATERIAL,#PB_Material_AlphaBlend)
    
    
    
    ; create tile mesh
    ;
    TILE_MESH = CreateTile(1)
    CUBE_MESH = CreateCube(#PB_Any,1)
    MCUBE_MESH = CreateCube(#PB_Any,0.5)
    
    For y = 0 To 127
      For x = 0 To 511
        value.i =  Pixels(x,(1080-127)+y)
        
        If value = $FFFFFFFF
          ground(x,y)\autotile         = #True
          ground(x,y)\life             = -1
          ground(x,y)\TileEntity       = CreateEntity(#PB_Any,MeshID(TILE_MESH),MaterialID(0),x,128-y,0)
          ground(x,y)\CollideEntity    = CreateEntity(#PB_Any,MeshID(CUBE_MESH),MaterialID(INVISIBLE_MATERIAL),x,128-y,0)
          CreateEntityBody(ground(x,y)\CollideEntity,#PB_Entity_BoxBody,0,0.1,0.5)
          
          ;            CreateEntityBody(ground(x,y)\CollideEntity,#PB_Entity_BoxBody,0,0.1,0.5)
        EndIf
        
        If value = $FF002080
          ground(x,y)\life             = 8
          ground(x,y)\TileEntity       = CreateEntity(#PB_Any,MeshID(TILE_MESH),MaterialID(48))
          ground(x,y)\CollideEntity    = CreateEntity(#PB_Any,MeshID(CUBE_MESH),MaterialID(INVISIBLE_MATERIAL),x,128-y,0)
          AttachEntityObject(ground(x,y)\CollideEntity,"",EntityID(ground(x,y)\TileEntity))
          CreateEntityBody(ground(x,y)\CollideEntity,#PB_Entity_BoxBody,0.8,0,0.5)
          ;            CreateEntityBody(ground(x,y)\CollideEntity,#PB_Entity_BoxBody,0.8,0,0.5)
          EntityAngularFactor(ground(x,y)\CollideEntity,0,0,1)
          EntityLinearFactor(ground(x,y)\CollideEntity,1,1,0)
          
          AddElement(Destructible())
          Destructible()\ID = ground(x,y)\CollideEntity
          Destructible()\x = x:Destructible()\y = y
          Destructible()\scrapType = 1 ; wood
        EndIf
        
        
        If value = $FF808080
          ground(x,y)\climb = #True
          ground(x,y)\TileEntity       = CreateEntity(#PB_Any,MeshID(TILE_MESH),MaterialID(41+Random(6,0)),x,128-y,-0.1)
        EndIf
        
        
      Next
    Next
    
    
    
    ;autotile
    
    For y = 0 To 127
      For x = 0 To 511
        
        ;level
        If ground(x,y)\autotile
          tile_value.l = 0
          If y-1 =>0  : If ground(x,y-1)\autotile = 1 : tile_value + 1 :  EndIf :  EndIf
          If y+1 <128 : If ground(x,y+1)\autotile = 1 : tile_value + 4 :  EndIf :  EndIf
          If x-1 =>0  : If ground(x-1,y)\autotile = 1 : tile_value + 8 :  EndIf :  EndIf
          If x+1 <512 : If ground(x+1,y)\autotile = 1 : tile_value + 2 :  EndIf :  EndIf
          SetEntityMaterial(ground(x,y)\TileEntity,MaterialID(tile_value))
        EndIf
      Next
    Next
    
    
    
    
    TILE_MESH = CreateSphere(#PB_Any,0.49,32,32);CreateTile(0.8)
    
    D = CreateEntity(#PB_Any,MeshID(TILE_MESH),MaterialID(INVISIBLE_MATERIAL),0,128,0)
    CreateEntityBody(D,#PB_Entity_SphereBody,1,0.1,0.5)
    ;     CreateEntityBody(D,#PB_Entity_SphereBody,1,0.1,0.5)
    EntityAngularFactor(D,0,0,0)
    EntityLinearFactor(D,1,1,0)
    
    T = CreateTile(1)
    TM = CreateEntity(#PB_Any,MeshID(T),MaterialID(32))
    AttachEntityObject(D,"",EntityID(TM))
    
    WorldGravity(-9.81)
    
    
    Camera2D = CreateCamera(#PB_Any,0,0,100,100)
    CameraBackColor(Camera2D,RGB(255,128,64))
    CameraRange(Camera2D,0.01,1000)
    CameraFOV(Camera2D,40)
    
    AmbientColor(RGB(255,128,64))
    ;     CreateLight(0,$CACACA,100,100,100)
    
    deltaTime.f = 0
    While #True
      GameClock = ElapsedMilliseconds()
      
      Screen3DEvents()
      If Quit Or KeyboardPushed(#PB_Key_Escape)
        Break
      EndIf
      
      
      If KeyboardPushed(#PB_Key_Right) And GetEntityAttribute(D,#PB_Entity_LinearVelocityX) < 4
        ApplyEntityForce(D,10,0,0)
      EndIf
      
      If KeyboardPushed(#PB_Key_Left) And GetEntityAttribute(D,#PB_Entity_LinearVelocityX) > -4
        ApplyEntityForce(D,-10,0,0)
      EndIf
      
      Tx = Int(EntityX(D)+0.5)
      Ty = (128)-Int(EntityY(D))
      
      If Tx<0 : Tx=0 : EndIf
      If Ty<0 : Ty=0 : EndIf
      If Tx>511 : Tx=511 : EndIf
      If Ty>127 : Ty=127 : EndIf
      
      
      If ground(Tx,Ty)\climb = 1 And kf = #False
        If GetEntityAttribute(D,#PB_Entity_LinearVelocityY)<=1.8
          EntityVelocity(D, GetEntityAttribute(D,#PB_Entity_LinearVelocityX)/1.1 , 0, 0)
          EntityLinearFactor(D,1,0,0)
        EndIf
      ElseIf ground(Tx,Ty)\climb = 0
        EntityLinearFactor(D,1,1,0)
      ElseIf ground(Tx,Ty)\climb = 1 And kf = #True
        If GetEntityAttribute(D,#PB_Entity_LinearVelocityY)<=1.8
          EntityLinearFactor(D,1,1,0)
          ApplyEntityImpulse(D,0,7.5,0)
          kf = #False
        EndIf
      EndIf
      
      If KeyboardPushed(#PB_Key_Up) And ground(Tx,Ty)\climb
        MoveEntity(D,0,0.1*deltaTime,0)
      EndIf
      
      If KeyboardPushed(#PB_Key_Down) And ground(Tx,Ty)\climb
        MoveEntity(D,0,-0.1*deltaTime,0)
      EndIf
      
      
      If KeyboardPushed(#PB_Key_Space) And kf = #False And Abs(GetEntityAttribute(D,#PB_Entity_LinearVelocityY)) < 0.005
        kf = #True         
        ApplyEntityImpulse(D,0,7.5,0)       
      EndIf
      
      
      If Not KeyboardPushed(#PB_Key_Space) And kf=#True
        kf=#False
      EndIf
      
      
      SetEntityAttribute(D,#PB_Entity_LinearSleeping,0)
      SetEntityAttribute(D,#PB_Entity_AngularSleeping,0)
      
      
      ExamineWorldCollisions(1)
      
      While NextWorldCollision()
        ForEach Destructible()
          If Destructible()\ID = FirstWorldCollisionEntity()
            WorldCollisionAppliedImpulse()
            
            ground( Destructible()\x,Destructible()\y)\life - GetY()
            
            If ground( Destructible()\x,Destructible()\y)\life < 0
              For i = 0 To 9
                vx.d=GetEntityAttribute(Destructible()\ID,#PB_Entity_LinearVelocityX)
                vy.d=GetEntityAttribute(Destructible()\ID,#PB_Entity_LinearVelocityY)
                CreateParticle(EntityX(Destructible()\ID),EntityY(Destructible()\ID),90,49+Random(2),vx,vy)
              Next
              FreeEntity( ground( Destructible()\x,Destructible()\y)\CollideEntity)
              FreeEntity( ground( Destructible()\x,Destructible()\y)\TileEntity)
              DeleteElement(Destructible())
              Break 2
            EndIf
            
          EndIf
        Next
      Wend
      
      ForEach Particle()
        
        If(Particle()\ttl<ElapsedMilliseconds())
          FreeEntity(Particle()\ent)
          DeleteElement(Particle(),1)
        EndIf
        
      Next
      
      CamX = LinearInterpolate(CamX,EntityX(D),deltaTime/600)
      CamY = LinearInterpolate(CamY,EntityY(D),deltaTime/200)
      
      
      MoveCamera(Camera2D,CamX,CamY,10,#PB_Absolute)
      CameraLookAt(Camera2D,EntityX(D),EntityY(D),0)
      
      
      RenderWorld(deltaTime*2) ; Draw 3d stuff
                               ; Draw hud
      FlipBuffers()
      deltaTime = (ElapsedMilliseconds() - GameClock)
    Wend
        
  EndIf
  
Else
  MessageRequester("Error","Engine3D.dll error when loading.")
  End
EndIf

Procedure.i CreateTile(Size.d)
  TILE_MESH = CreateMesh(#PB_Any)
  
  MeshVertexPosition(-(Size/2), -(Size/2), 0)
  MeshVertexTextureCoordinate(0, 0)
  
  MeshVertexPosition((Size/2), -(Size/2), 0)
  MeshVertexTextureCoordinate(1, 0)
  
  MeshVertexPosition((Size/2), (Size/2), 0)
  MeshVertexTextureCoordinate(1, 1)
  
  MeshVertexPosition(-(Size/2), (Size/2), 0)
  MeshVertexTextureCoordinate(0, 1)
  
  MeshFace(0,1,2)
  MeshFace(3,0,2)
  
  FinishMesh(#True)
  NormalizeMesh(TILE_MESH)
  UpdateMeshBoundingBox(TILE_MESH)
  ProcedureReturn TILE_MESH
EndProcedure

Procedure.d LinearInterpolate(y1.d,y2.d,mu.d)
  ProcedureReturn(y1*(1-mu)+y2*mu)
EndProcedure


Procedure CreateParticle(x.f,y.f,angle.f,mat.i,vx.d,vy.d)
  AddElement(Particle())
  
  Particle()\ox = (x-0.5)+((10-Random(10))/10)
  Particle()\oy = (y-0.5)+((10-Random(10))/10)
  Particle()\ttl = ElapsedMilliseconds()+3000
  Particle()\ent = CreateEntity(#PB_Any,MeshID(CreateTile(1)),MaterialID(mat),Particle()\ox,Particle()\oy,0.5)
  
  EntityAngularFactor(Particle()\ent,0,0,1)
  EntityLinearFactor(Particle()\ent,1,1,0)
  CreateEntityBody(Particle()\ent,#PB_Entity_BoxBody,4,1,0)
  ApplyEntityImpulse(Particle()\ent,(((-10+Random(20))/10)*3)+vx,(((-10+Random(20))/10)*10)+vy,0)
  SetEntityCollisionFilter(Particle()\ent, 1, $1001)
  
  
EndProcedure
http://purebasic.developpez.com/
Je ne réponds à aucune question technique en PV, utilisez le forum, il est fait pour ça, et la réponse peut profiter à tous.
Avatar de l’utilisateur
djes
Messages : 4252
Inscription : ven. 11/févr./2005 17:34
Localisation : Arras, France

Re: Utilisation d'ogre3D pour de la 2D

Message par djes »

Merci pour la mise à jour de ce code super impressionnant ! :D
Avatar de l’utilisateur
venom
Messages : 3071
Inscription : jeu. 29/juil./2004 16:33
Localisation : Klyntar
Contact :

Re: Utilisation d'ogre3D pour de la 2D

Message par venom »

J'ai une erreur chez moi, sa me dit que la texture n'est pas charger 8O Pourtant je met le chemin de l'image






@++
Windows 10 x64, PureBasic 5.73 x86 & x64
GPU : radeon HD6370M, CPU : p6200 2.13Ghz
Avatar de l’utilisateur
djes
Messages : 4252
Inscription : ven. 11/févr./2005 17:34
Localisation : Arras, France

Re: Utilisation d'ogre3D pour de la 2D

Message par djes »

Faut mettre la texture dans le même dossier que le fichier, et la renommer.
Répondre