animated 2D robots, and other flat models

Everything related to 3D programming
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

animated 2D robots, and other flat models

Post by applePi »

to convert a 3D model to 2D just scale down its x or z to 0, i prefer to use TransformMesh http://www.purebasic.com/documentation/ ... mmesh.html for this task
the robots here folow the tangent to a curve on a specific position like in this post http://purebasic.fr/english/viewtopic.php?f=36&t=61469
we can also use the spline http://purebasic.fr/english/viewtopic.php?f=12&t=62251 to determine custom routs for the robots to follow , look example spline.pb in PB examples.
the flat models used here is: robots, house, the ground
Image

Code: Select all

Declare DrawCurve()

#CameraSpeed = 0.4
Define.f KeyX, KeyY, MouseX, MouseY
;the range of the function under study is from xMin to xMax
Global xMin.f = -2*#PI
Global xMax.f =  2*#PI
Global stepx.f = (xMax - xMin) / 1000 ; the increment of x every loop

Enumeration
  #tex
  #light
  #plane = 10
  
EndEnumeration

ExamineDesktops()

InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()

OpenWindow(0,0,0,DesktopWidth(0), DesktopHeight(0),"2D models, .. press space key to start/stop Marching ----- mouse/arrow keys for the camera",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,DesktopWidth(0), DesktopHeight(0),1,0,0,#PB_Screen_WaitSynchronization)

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Models", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/fonts", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()

KeyboardMode(#PB_Keyboard_AllowSystemKeys)

CreateMaterial(0, LoadTexture(0, "White.jpg"))
DisableMaterialLighting(0, #True)

CreateMaterial(1, LoadTexture(1, "snow_1024.jpg"))
;MaterialCullingMode(1, #PB_Material_NoCulling)
;CreatePlane(#plane, 210, 210, 50, 50, 10, 10)
CreateSphere(#plane, 100)
TransformMesh(#plane, 0,0,0, 1,0,1, 0,0,0)
CreateEntity(#plane,MeshID(#plane),MaterialID(1), 0,-0.5,0)
UpdateMeshBoundingBox(#plane)

;WorldShadows(#PB_Shadow_Modulative)

CreateLight(0,RGB(245,245,205),0, 100, -100)

;Create Camera
CreateCamera(0,0,0,100,100)
MoveCamera(0, -34.41, 13.56, 23.49,#PB_Absolute)
CameraLookAt(0, -10, 5, 0)
;CameraBackColor(0, RGB(227,239,242))
CameraBackColor(0, RGB(148,197,158))


AmbientColor(RGB(255,255,255))

CreateLine3D(#PB_Any, -20, 0, 0, #Red, 20, 0, 0, RGB(255, 0, 0)) ; Red 
CreateLine3D(#PB_Any, 0,-20,0, #Green, 0, 20, 0, RGB(0, 255, 0)); Green 
CreateLine3D(#PB_Any, 0,0,-20, #Blue, 0, 0, 20, RGB(0, 0, 255)) ;Blue 
;-Mesh
DrawCurve() ; call the function drawing procedure
  
; Create flare
 GetScriptMaterial(0, "Scene/burst")
 
 CreateBillboardGroup(0, MaterialID(0), 16, 16)
 AddBillboard(0, 0, 10, -100) 
 
LoadMesh(4, "tudorhouse.mesh")
TransformMesh(4, 0,0,0, 1,1,0, 0,0,0)
UpdateMeshBoundingBox(4)
CreateEntity(4, MeshID(4), #PB_Material_None)
ScaleEntity(4, 0.015,0.015,0.015)
MoveEntity(4,0,9,-12, #PB_Absolute)

LoadMesh(5, "Barrel.mesh")
CreateEntity(5, MeshID(5), #PB_Material_None)
ScaleEntity(5, 0.4,0.4,0.4)
MoveEntity(5,-20,1,-6, #PB_Absolute)


;TransformMesh(#Mesh, x, y, z, ScaleX, ScaleY, ScaleZ, RotateX, RotateY, RotateZ [, SubMesh])
LoadMesh(1, "robot.mesh")
TransformMesh(1, 0,0,0, 0,1,1, 0,0,0)
UpdateMeshBoundingBox(1)
CreateEntity(1, MeshID(1), #PB_Material_None)
ScaleEntity(1, 0.06,0.06,0.06)
MoveEntity(1,-20,0,0)
;RotateEntity(1, 90,0,0)
StartEntityAnimation(1, "Walk")

CopyEntity(1, 2)
MoveEntity(2,-24,0,-3)
StartEntityAnimation(2, "Walk")


x.f = xMin
start = 0
Repeat
  WindowEvent()
  
  If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
      EndIf
  ShowCursor_(0)
    
; Use arrow keys and mouse to rotate and fly in/out
  ExamineKeyboard()
      
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed
        Else
          KeyY = 0
        EndIf
        
             
      If KeyboardReleased(#PB_Key_Space)   
        start ! 1
      EndIf
      
      If KeyboardReleased(#PB_Key_C) 
        Debug CameraX(0): Debug CameraY(0):Debug CameraZ(0)
      EndIf
      
      If start
          ; for the first robot
          x.f + stepx
          y.f = 0
          z.f= Sin(x)
          ;visit this site to find the derivative of any function: http://www.wolframalpha.com/examples/Derivatives.html
          slope.f= Cos(x) ; the derivative of sin(x) is cos(x)
          slope = Degree(ATan(slope))
          
          MoveEntity(1,x*3,y, z*3,   #PB_Absolute) ; we just amplify x,y by 3
          RotateEntity(1, 0,-slope+ro,0)
          
          ;for the second robot
          z.f= Sin(x-1)
          slope.f= Cos(x-1) ; the derivative of sin(x) is cos(x)
          slope = Degree(ATan(slope))
          MoveEntity(2,(x-1)*3,y, z*3,   #PB_Absolute) ; we just amplify x,y by 3
          RotateEntity(2, 0,-slope+ro,0)
          
          If x > xMax
            ro = 180
            x =  xMax
            stepx * -1
          EndIf  
          
          If x < xMin
            x =  xMin
            ro = 0
            stepx * -1
          EndIf  
              
      EndIf
      
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (0, KeyX, 0, KeyY)
      
  RenderWorld()
  
  FlipBuffers()

Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow

;function drawing
Procedure DrawCurve()
  Protected.f x, y
    
  CreateMesh(0, #PB_Mesh_PointList, #PB_Mesh_Static)
    
  x.f = xMin: z.f = 0
  
    While x <= 2*#PI
      
      z.f = Sin(x)
                      
      MeshVertexPosition(x*3, 0, z*3) 
      MeshVertexColor(RGB(0,0,255))
      
      x.f + stepx
    Wend
        
    FinishMesh(#True)
    CreateEntity(0, MeshID(0), MaterialID(0) ,0,0,0)
    
EndProcedure  
Last edited by applePi on Fri Jun 26, 2015 6:12 pm, edited 1 time in total.
box_80
Enthusiast
Enthusiast
Posts: 117
Joined: Mon Sep 03, 2012 8:52 pm

Re: animated 2D robots, and other flat models

Post by box_80 »

That is a cool trick and simple too. Might add this to my game if I ever really get started. :lol: Thanks for sharing.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: animated 2D robots, and other flat models

Post by IdeasVacuum »

.... it's 2.5D :wink:
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5524
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: animated 2D robots, and other flat models

Post by Kwai chang caine »

Cool !!!! :D
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: animated 2D robots, and other flat models

Post by applePi »

Thank you all, note that if we choose dimension zero (using TransformMesh) for x or z then we can't scale it up again later, but if we choose small number such as 0.0001 then we can do this, this is useful for games if you want your characters to loose or gain thickness according to some fantasia story.
you can hide treasures inside the thinned models such as purebasic 'Z'
also Transform Mesh the house without using scaleEntity will enable us to attach objects to it Locally in a convenient way, we attach 'Z' to the house and we attach Text3d to the 'Z'
in the above example i have mixed TransformMesh with scaleentity for the house so i get trouble and confused when i want to attach another object to it in a specific place. so preferably in these cases to use TransformMesh alone so you have an idea about the real size of the house mesh relative to your scene. remember the Entity is an instance of the mesh .
press Space several times to let the house gain thickness again, go inside to see the 'Z' inside

Code: Select all

#CameraSpeed = 0.4
Define.f KeyX, KeyY, MouseX, MouseY
;the range of the function under study is from xMin to xMax
Global xMin.f = -2*#PI
Global xMax.f =  2*#PI
Global stepx.f = (xMax - xMin) / 1000 ; the increment of x every loop

Enumeration
  #tex
  #light
  #plane = 10
  
EndEnumeration

ExamineDesktops()

InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()

OpenWindow(0,0,0,DesktopWidth(0), DesktopHeight(0),"2D models, .. press space key to inflate the house ----- mouse/arrow keys for the camera",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,DesktopWidth(0), DesktopHeight(0),1,0,0,#PB_Screen_WaitSynchronization)

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Models", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/fonts", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()

KeyboardMode(#PB_Keyboard_AllowSystemKeys)

CreateMaterial(0, LoadTexture(0, "White.jpg"))
DisableMaterialLighting(0, #True)

CreateMaterial(1, LoadTexture(1, "snow_1024.jpg"))
CreateMaterial(3, LoadTexture(3, "fw12b.jpg"))
MaterialCullingMode(3, #PB_Material_NoCulling)


CreatePlane(#plane, 210, 210, 50, 50, 10, 10)
;CreateSphere(#plane, 100)
;TransformMesh(#plane, 0,0,0, 1,0,1, 0,0,0)
CreateEntity(#plane,MeshID(#plane),MaterialID(1), 0,-0.5,0)
UpdateMeshBoundingBox(#plane)

CreateLight(0,RGB(245,245,205),0, 100, -100)
CreateLight(1,RGB(255,0,0),0, 9, -8)

;Create Camera
CreateCamera(0,0,0,100,100)
MoveCamera(0, -34.41, 13.56, 23.49,#PB_Absolute)
CameraLookAt(0, -10, 5, 0)
CameraBackColor(0, RGB(148,197,158))


AmbientColor(RGB(255,255,255))

CreateLine3D(#PB_Any, -20, 0, 0, #Red, 20, 0, 0, RGB(255, 0, 0)) ; Red 
CreateLine3D(#PB_Any, 0,-20,0, #Green, 0, 20, 0, RGB(0, 255, 0)); Green 
CreateLine3D(#PB_Any, 0,0,-20, #Blue, 0, 0, 20, RGB(0, 0, 255)) ;Blue 
  
; Create flare
 GetScriptMaterial(0, "Scene/burst")
 
 CreateBillboardGroup(0, MaterialID(0), 16, 16)
 AddBillboard(0, 0, 10, -100) 
 
LoadMesh(4, "tudorhouse.mesh")
TransformMesh(4, 0,0,0, 0.015,0.015,0.00001, 0,0,0)
UpdateMeshBoundingBox(4)
CreateEntity(4, MeshID(4), MaterialID(3))
MoveEntity(4,0,9,-10, #PB_Absolute)

LoadMesh(6, "PureBasic.mesh")
TransformMesh(6, 0,0,0, 0.1,0.1,0.001, 0,0,0)
UpdateMeshBoundingBox(6)
NormalizeMesh(6)
BuildMeshTangents(6)
GetScriptMaterial(6, "Color/Red")
CreateEntity(6, MeshID(6), MaterialID(6))
MoveEntity(6,0,-6,0, #PB_Absolute | #PB_Local )
;AttachEntityObject(#Entity, Bone$, ObjectID [, x, y, z, Pitch, Roll, Yaw])
AttachEntityObject(4, "", EntityID(6))

Repeat
  WindowEvent()
  
  If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
      EndIf
  ShowCursor_(0)
    
; Use arrow keys and mouse to rotate and fly in/out
  ExamineKeyboard()
      
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed
        Else
          KeyY = 0
        EndIf
        
             
If KeyboardPushed(#PB_Key_Space)   
ScaleEntity(4, 1, 1,  1.1)
        
c+1
If c=22
CreateText3D(7, "You Win Lotto")
Text3DColor(7, RGBA(150, 0, 250, 255))
Text3DAlignment(7, #PB_Text3D_HorizontallyCentered)
ScaleText3D(7, 1,1,1,#PB_Absolute)
;MoveText3D(7, 0,-6,0, #PB_Absolute | #PB_Local )
MoveText3D(7, 0,0,-2, #PB_Absolute | #PB_Local )
AttachEntityObject(6, "", Text3DID(7))
;;;;;;;;;;;
rot=1 ; to enable 'Z' symbol of purebasic to rotate
EndIf
        

EndIf
      
      
      RotateEntity(6, 0,rot,0 ,#PB_Relative)      
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (0, KeyX, 0, KeyY)
      
  RenderWorld()
  
  FlipBuffers()

Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow

box_80
Enthusiast
Enthusiast
Posts: 117
Joined: Mon Sep 03, 2012 8:52 pm

Re: animated 2D robots, and other flat models

Post by box_80 »

Inflate a house, I don't see that often. All this 2d stuff would work nice for a fantasy or a game about dreams. Nice to see something different then the ultra real-like graphics found in a lot of modern games.
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: animated 2D robots, and other flat models

Post by Keya »

Post Reply