Page 1 of 1

Rusty plane example

Posted: Wed May 27, 2015 5:05 pm
by DK_PETER
Images provided here: removed


Code: Select all

;Simulate a rusty plane in transit
;By DK_PETER

InitEngine3D()
InitSprite()
InitKeyboard()

UseJPEGImageDecoder()
UsePNGImageDecoder()

Structure _par
  id.i
  ma.i
  tx.i
EndStructure

Structure _mesh
  id.i
  ms.i
  ma.i
  tx.i
EndStructure

Structure _plane
  body._mesh
  wing._mesh[2]
  tail._mesh
  engine._mesh[2]
  nose._mesh
EndStructure

Global Dim Clouds._mesh(5), Dim Engines._par(2)
Global CloudNode.i, PlaneNode.i, Ground._mesh, pl._plane, rust.i

OpenWindow(0, 0, 0, 1024, 768, "plane", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, 1024, 768, #False, 0, 0)

Add3DArchive("texture", #PB_3DArchive_FileSystem)

rust = LoadImage(#PB_Any, "texture\rust.jpg")


CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0, 0, 40, 76)
CameraDirection(0, 0, -0.4, -1)
CreateLight(0, $A5FFF7, 500, 2000, 400, #PB_Light_Directional)

CreateCamera(1, 75, 75, 25, 25)
CameraDirection(1, -1, 0, 0)
MoveCamera(1, 200, 26, 7, 0)


For x = 0 To 4
  With clouds(x)
    \tx = LoadTexture(#PB_Any, "c.jpg")
    \ma = CreateMaterial(#PB_Any, TextureID(\tx))
    MaterialBlendingMode(\ma, #PB_Material_Color)
    AddMaterialLayer(\ma, TextureID(\tx), #PB_Material_Color)
    AddMaterialLayer(\ma, TextureID(\tx), #PB_Material_Add)
    ScrollMaterial(\ma, -0.2, 0, #PB_Material_Animated, 0)
    ScrollMaterial(\ma, -0.4, 0, #PB_Material_Animated, 1)
    ScrollMaterial(\ma, -0.2, 0, #PB_Material_Animated, 2)
    \ms = CreatePlane(#PB_Any, 100, 100, 1, 1, 10, 10)
    \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, -200 + (x * 200) , 0)
    ScaleEntity(\id, 100, 100, 100)
  EndWith
Next x

With Ground
  \tx = LoadTexture(#PB_Any, "a.jpg")
  \ma = CreateMaterial(#PB_Any, TextureID(\tx))
  ScaleMaterial(\ma, 0.5, 0.5, 0)
  ScrollMaterial(\ma, -1.015, 0, #PB_Material_Animated)
  \ms = CreatePlane(#PB_Any, 100, 100, 1, 1, 30, 30)
  \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, -800, 0)
  ScaleEntity(\id, 300, 1, 300)
EndWith

With Engines(0)
  \tx = CreateTexture(#PB_Any, 10, 10)
  StartDrawing(TextureOutput(\tx))
  DrawingMode(#PB_2DDrawing_Gradient)
  BackColor($00F1E3) : FrontColor($0030FD)
  CircularGradient(5, 5, 3)
  Circle(5, 5, 3)
  StopDrawing()
  \ma = CreateMaterial(#PB_Any, TextureID(\tx))
  MaterialBlendingMode(\ma, #PB_Material_Add)
  \id = CreateParticleEmitter(#PB_Any, 0.5, 0.5, 0.5, #PB_Particle_Box, 3, 1.5 , -10)
  ParticleMaterial(\id, MaterialID(\ma))
  ParticleEmissionRate(\id, 100)
  ParticleTimeToLive(\id, 0.1, 0.1)
  ParticleVelocity(\id, 0.2, 0.4)
  ParticleEmitterDirection(\id, 1, 0 , 0)
  ParticleSize(\id, 4 , 1 )
EndWith

With Engines(1)
  \tx = CreateTexture(#PB_Any, 10, 10)
  StartDrawing(TextureOutput(\tx))
  DrawingMode(#PB_2DDrawing_Gradient)
  BackColor($00F1E3) : FrontColor($0030FD)
  CircularGradient(5, 5, 3)
  Circle(5, 5, 3)
  StopDrawing()
  \ma = CreateMaterial(#PB_Any, TextureID(\tx))
  MaterialBlendingMode(\ma, #PB_Material_Add)
  \id = CreateParticleEmitter(#PB_Any, 0.5, 0.5, 0.5, #PB_Particle_Box, 3, 1.5 , 10)
  ParticleMaterial(\id, MaterialID(\ma))
  ParticleEmissionRate(\id, 100)
  ParticleTimeToLive(\id, 0.1, 0.1)
  ParticleVelocity(\id, 0.2, 0.4)
  ParticleEmitterDirection(\id, 1, 0 , 0)
  ParticleSize(\id, 4 , 1 )
EndWith

With pl\body
  \tx = LoadTexture(#PB_Any, "rust.jpg")
  \ma = CreateMaterial(#PB_Any, TextureID(\tx))
  \ms = CreateSphere(#PB_Any, 50)
  \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 3, 0)
  ScaleEntity(\id, 0.4, 0.05, 0.07)
EndWith

With pl\nose
  \tx = CreateTexture(#PB_Any, 40, 40)
  StartDrawing(TextureOutput(\tx))
  Box(0, 0, 40, 10, $E68B00)
  DrawImage(ImageID(rust), 0, 10)
  StopDrawing()
  \ma = CreateMaterial(#PB_Any, TextureID(\tx))
  \ms = CreateSphere(#PB_Any, 50, 10, 10)
  \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), -15, 3, 0)
  ScaleEntity(\id, 0.1, 0.04, 0.04)
EndWith

With pl\wing[0] 
  \tx = LoadTexture(#PB_Any, "rust.jpg") 
  \ma = CreateMaterial(#PB_Any, TextureID(\tx))
  \ms = CreateCube(#PB_Any, 50)
  \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), -1, 2.3, -10)
  ScaleEntity(\id, 0.5, 0.01, 0.07)
  RotateEntity(\id, 0, 55, 0)
EndWith

With pl\wing[1]
  \tx = LoadTexture(#PB_Any, "rust.jpg")
  \ma = CreateMaterial(#PB_Any, TextureID(\tx))
  \ms = CreateCube(#PB_Any, 50)
  \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), -1, 2.3, 10)
  ScaleEntity(\id, 0.5, 0.01, 0.07)
  RotateEntity(\id, 0, -55, 0)
EndWith

With pl\engine[0]
  \tx = LoadTexture(#PB_Any, "rust.jpg") 
  \ma = CreateMaterial(#PB_Any, TextureID(\tx))
  \ms = CreateSphere(#PB_Any, 30, 10, 10)
  \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), -0.7, 1.5, 10)
  ScaleEntity(\id, 0.09, 0.02, 0.03)
EndWith

With pl\engine[1]
  \tx = LoadTexture(#PB_Any, "rust.jpg")
  \ma = CreateMaterial(#PB_Any, TextureID(\tx))
  \ms = CreateSphere(#PB_Any, 30, 10, 10)
  \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), -0.7, 1.5, -10)
  ScaleEntity(\id, 0.09, 0.02, 0.03)
EndWith

With pl\tail
  \tx = LoadTexture(#PB_Any, "rust.jpg") 
  \ma = CreateMaterial(#PB_Any, TextureID(\tx))
  \ms = CreateCylinder(#PB_Any, 3, 5, 2, 1, 6)
  \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 16, 7, 0)
  ScaleEntity(\id, 1, 1.7, 0.2)
EndWith

;Assemble the plane
PlaneNode = CreateNode(#PB_Any, 0, 0, 0)
AttachNodeObject(PlaneNode, EntityID(pl\body\id))
AttachNodeObject(PlaneNode, EntityID(pl\nose\id))
AttachNodeObject(PlaneNode, EntityID(pl\wing[0]\id))
AttachNodeObject(PlaneNode, EntityID(pl\wing[1]\id))
AttachNodeObject(PlaneNode, EntityID(pl\engine[0]\id))
AttachNodeObject(PlaneNode, EntityID(pl\engine[1]\id))
AttachNodeObject(PlaneNode, EntityID(pl\tail\id))
AttachNodeObject(PlaneNode, ParticleEmitterID(Engines(0)\id))
AttachNodeObject(PlaneNode, ParticleEmitterID(Engines(1)\id))

MoveNode(PlaneNode, 0, 2,0)

Repeat
 
  Repeat
    ev = WindowEvent()
    If ev = #PB_Event_CloseWindow : Quit = 1 : EndIf
  Until  ev = 0

  RenderWorld()
 
  ExamineKeyboard()
  
  If KeyboardPushed(#PB_Key_Down) And NodeRoll(PlaneNode) > -20
    RotateNode(PlaneNode,0, 0, -0.4, #PB_Relative)
  EndIf
  If KeyboardPushed(#PB_Key_Up) And NodeRoll(PlaneNode) < 20
    RotateNode(PlaneNode,0, 0, 0.4, #PB_Relative)
  EndIf
  If KeyboardPushed(#PB_Key_Left) And NodePitch(PlaneNode) > -56
    RotateNode(PlaneNode,-0.4, 0, 0, #PB_Relative)
  EndIf
  If KeyboardPushed(#PB_Key_Right) And NodePitch(PlaneNode) < 56
    RotateNode(PlaneNode,0.4, 0, 0, #PB_Relative)
  EndIf
  
  FlipBuffers()
 
Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1

Re: Rusty plane example

Posted: Wed May 27, 2015 5:28 pm
by IdeasVacuum
Hi DK_PETER

Do you have rust.jpg for us to download? How does an aircraft become rusty, they are all made from aluminium alloys and carbon fibre........ :mrgreen:

Re: Rusty plane example

Posted: Wed May 27, 2015 8:26 pm
by DK_PETER
Hey IdeasVacuum

Only shitty alloys were available, when this plane was made. :D

see first post for images...

Re: Rusty plane example

Posted: Wed May 27, 2015 8:28 pm
by IdeasVacuum
That is a beautiful piece of rust though 8)

Re: Rusty plane example

Posted: Wed May 27, 2015 9:34 pm
by DK_PETER
IdeasVacuum wrote:That is a beautiful piece of rust though 8)
Yes it is :wink:

Took the code to the next level.

Code: Select all

;Simulate a rusty plane
;By DK_PETER
;Uses the images provided here: http://www.purebasic.fr/english/viewtopic.php?f=36&t=62287
;Second try. Check small cam and use arrow keys.

InitEngine3D()
InitSprite()
InitKeyboard()

UseJPEGImageDecoder()
UsePNGImageDecoder()

Structure _par
  id.i
  ma.i
  tx.i
EndStructure

Structure _mesh
  id.i
  ms.i
  ma.i
  tx.i
EndStructure

Structure _plane
  body._mesh
  wing._mesh[2]
  tail._mesh
  engine._mesh[2]
  nose._mesh
EndStructure

Global Dim Clouds._mesh(5), Dim Engines._par(2)
Global CloudNode.i, PlaneNode.i, Ground._mesh, pl._plane, rust.i, CurrentY.f

OpenWindow(0, 0, 0, 1024, 768, "plane", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, 1024, 768, #False, 0, 0)

Add3DArchive("texture", #PB_3DArchive_FileSystem)

rust = LoadImage(#PB_Any, "texture\rust.jpg")


CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0, 0, 40, 76)
CameraDirection(0, 0, -0.4, -1)
CreateLight(0, $A5FFF7, 500, 2000, 400, #PB_Light_Directional)

CreateCamera(1, 75, 75, 25, 25)
CameraDirection(1, -1, 0, 0)
MoveCamera(1, 200, 26, 7, 0)


For x = 0 To 4
  With clouds(x)
    \tx = LoadTexture(#PB_Any, "c.jpg")
    \ma = CreateMaterial(#PB_Any, TextureID(\tx))
    MaterialBlendingMode(\ma, #PB_Material_Color)
    AddMaterialLayer(\ma, TextureID(\tx), #PB_Material_Color)
    AddMaterialLayer(\ma, TextureID(\tx), #PB_Material_Add)
    ScrollMaterial(\ma, -0.4, -0.02, #PB_Material_Animated, 0)
    ScrollMaterial(\ma, -0.5, 0.1, #PB_Material_Animated, 1)
    ScrollMaterial(\ma, -0.2, 0.04, #PB_Material_Animated, 2)
    MaterialCullingMode(\ma, #PB_Material_NoCulling)
    \ms = CreatePlane(#PB_Any, 100, 100, 1, 1, 10, 10)
    \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, -200 + (x * 200) , 0)
    ScaleEntity(\id, 100, 100, 100)
  EndWith
Next x

With Ground
  \tx = LoadTexture(#PB_Any, "a.jpg")
  \ma = CreateMaterial(#PB_Any, TextureID(\tx))
  ScaleMaterial(\ma, 0.5, 0.5, 0)
  ScrollMaterial(\ma, -1.015, 0, #PB_Material_Animated)
  \ms = CreatePlane(#PB_Any, 100, 100, 1, 1, 30, 30)
  \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, -800, 0)
  ScaleEntity(\id, 300, 1, 300)
EndWith

With Engines(0)
  \tx = CreateTexture(#PB_Any, 10, 10)
  StartDrawing(TextureOutput(\tx))
  DrawingMode(#PB_2DDrawing_Gradient)
  BackColor($00F1E3) : FrontColor($0030FD)
  CircularGradient(5, 5, 3)
  Circle(5, 5, 3)
  StopDrawing()
  \ma = CreateMaterial(#PB_Any, TextureID(\tx))
  MaterialBlendingMode(\ma, #PB_Material_Add)
  \id = CreateParticleEmitter(#PB_Any, 0.5, 0.5, 0.5, #PB_Particle_Box, 3, 1.5 , -10)
  ParticleMaterial(\id, MaterialID(\ma))
  ParticleEmissionRate(\id, 100)
  ParticleTimeToLive(\id, 0.1, 0.1)
  ParticleVelocity(\id, 0.2, 0.4)
  ParticleEmitterDirection(\id, 1, 0 , 0)
  ParticleSize(\id, 4 , 1 )
EndWith

With Engines(1)
  \tx = CreateTexture(#PB_Any, 10, 10)
  StartDrawing(TextureOutput(\tx))
  DrawingMode(#PB_2DDrawing_Gradient)
  BackColor($00F1E3) : FrontColor($0030FD)
  CircularGradient(5, 5, 3)
  Circle(5, 5, 3)
  StopDrawing()
  \ma = CreateMaterial(#PB_Any, TextureID(\tx))
  MaterialBlendingMode(\ma, #PB_Material_Add)
  \id = CreateParticleEmitter(#PB_Any, 0.5, 0.5, 0.5, #PB_Particle_Box, 3, 1.5 , 10)
  ParticleMaterial(\id, MaterialID(\ma))
  ParticleEmissionRate(\id, 100)
  ParticleTimeToLive(\id, 0.1, 0.1)
  ParticleVelocity(\id, 0.2, 0.4)
  ParticleEmitterDirection(\id, 1, 0 , 0)
  ParticleSize(\id, 4 , 1 )
EndWith

With pl\body
  \tx = LoadTexture(#PB_Any, "rust.jpg")
  \ma = CreateMaterial(#PB_Any, TextureID(\tx))
  \ms = CreateSphere(#PB_Any, 50)
  \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 3, 0)
  ScaleEntity(\id, 0.4, 0.05, 0.07)
EndWith

With pl\nose
  \tx = CreateTexture(#PB_Any, 40, 40)
  StartDrawing(TextureOutput(\tx))
  Box(0, 0, 40, 10, $E68B00)
  DrawImage(ImageID(rust), 0, 10)
  StopDrawing()
  \ma = CreateMaterial(#PB_Any, TextureID(\tx))
  \ms = CreateSphere(#PB_Any, 50, 10, 10)
  \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), -15, 3, 0)
  ScaleEntity(\id, 0.1, 0.04, 0.04)
EndWith

With pl\wing[0] 
  \tx = LoadTexture(#PB_Any, "rust.jpg") 
  \ma = CreateMaterial(#PB_Any, TextureID(\tx))
  \ms = CreateCube(#PB_Any, 50)
  \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), -1, 2.3, -10)
  ScaleEntity(\id, 0.5, 0.01, 0.07)
  RotateEntity(\id, 0, 55, 0)
EndWith

With pl\wing[1]
  \tx = LoadTexture(#PB_Any, "rust.jpg")
  \ma = CreateMaterial(#PB_Any, TextureID(\tx))
  \ms = CreateCube(#PB_Any, 50)
  \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), -1, 2.3, 10)
  ScaleEntity(\id, 0.5, 0.01, 0.07)
  RotateEntity(\id, 0, -55, 0)
EndWith

With pl\engine[0]
  \tx = LoadTexture(#PB_Any, "rust.jpg") 
  \ma = CreateMaterial(#PB_Any, TextureID(\tx))
  \ms = CreateSphere(#PB_Any, 30, 10, 10)
  \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), -0.7, 1.5, 10)
  ScaleEntity(\id, 0.09, 0.02, 0.03)
EndWith

With pl\engine[1]
  \tx = LoadTexture(#PB_Any, "rust.jpg")
  \ma = CreateMaterial(#PB_Any, TextureID(\tx))
  \ms = CreateSphere(#PB_Any, 30, 10, 10)
  \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), -0.7, 1.5, -10)
  ScaleEntity(\id, 0.09, 0.02, 0.03)
EndWith

With pl\tail
  \tx = LoadTexture(#PB_Any, "rust.jpg") 
  \ma = CreateMaterial(#PB_Any, TextureID(\tx))
  \ms = CreateCylinder(#PB_Any, 3, 5, 2, 1, 6)
  \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 16, 7, 0)
  ScaleEntity(\id, 1, 1.7, 0.2)
EndWith

;Assemble the plane
PlaneNode = CreateNode(#PB_Any, 0, 0, 0)
AttachNodeObject(PlaneNode, EntityID(pl\body\id))
AttachNodeObject(PlaneNode, EntityID(pl\nose\id))
AttachNodeObject(PlaneNode, EntityID(pl\wing[0]\id))
AttachNodeObject(PlaneNode, EntityID(pl\wing[1]\id))
AttachNodeObject(PlaneNode, EntityID(pl\engine[0]\id))
AttachNodeObject(PlaneNode, EntityID(pl\engine[1]\id))
AttachNodeObject(PlaneNode, EntityID(pl\tail\id))
AttachNodeObject(PlaneNode, ParticleEmitterID(Engines(0)\id))
AttachNodeObject(PlaneNode, ParticleEmitterID(Engines(1)\id))
AttachNodeObject(PlaneNode, CameraID(1))
MoveNode(PlaneNode, 0, 2, 0)

CloudNode = CreateNode(#PB_Any, 0, 0, 0)
AttachNodeObject(CloudNode, EntityID(Ground\id))

For p = 0 To 4
  AttachNodeObject(CloudNode, EntityID(Clouds(p)\id))
Next p


Repeat
 
  Repeat
    ev = WindowEvent()
    If ev = #PB_Event_CloseWindow : Quit = 1 : EndIf
  Until  ev = 0

  RenderWorld()
 
  ExamineKeyboard()
  
  If KeyboardPushed(#PB_Key_Down) And NodeRoll(PlaneNode) > -20
    RotateNode(PlaneNode,0, 0, -0.4, #PB_Relative)
  EndIf
  If KeyboardPushed(#PB_Key_Up) And NodeRoll(PlaneNode) < 20
    RotateNode(PlaneNode,0, 0, 0.4, #PB_Relative)
  EndIf
  If KeyboardPushed(#PB_Key_Left) And NodePitch(PlaneNode) > -56
    RotateNode(PlaneNode,-0.4, 0, 0, #PB_Relative)
  EndIf
  If KeyboardPushed(#PB_Key_Right) And NodePitch(PlaneNode) < 56
    RotateNode(PlaneNode,0.4, 0, 0, #PB_Relative)
  EndIf
  
  If NodeRoll(PlaneNode) < -1  ;Nose down
    CurrentY - 0.01
  ElseIf NodeRoll(PlaneNode) > 1  ;Nose up
    CurrentY + 0.01
  Else
     CurrentY = 0
  EndIf

  PointPick(1, ScreenWidth()/2, ScreenHeight()/2)
  ParticleEmitterDirection(Engines(0)\id, PickX(), PickY(), PickZ())
  ParticleEmitterDirection(Engines(1)\id, PickX(), PickY(), PickZ())

  MoveNode(CloudNode, 0, CurrentY, 0, #PB_Relative)
  
  FlipBuffers()
 
Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1

Re: Rusty plane example

Posted: Thu May 28, 2015 2:25 pm
by Kwai chang caine
i crash my plane with pleasure :mrgreen:
Very nice... , thanks for sharing 8)

Re: Rusty plane example

Posted: Thu May 28, 2015 3:13 pm
by heartbone
These are excellent 3D examples DK_PETER.

So many think about writing flight simulators, and it will be good to be able to show them some PB code to accomplish it.

Thanks for sharing this.

Re: Rusty plane example

Posted: Thu May 28, 2015 4:37 pm
by Num3
Nice,
You just forgot the sound effects...

https://www.youtube.com/watch?v=DlnOcZK892E

:mrgreen: