Change Entity Physics from Static to Box?

Everything related to 3D programming
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Change Entity Physics from Static to Box?

Post by Samuel »

I'm trying to create a box that at first has a Static body. Then right before I create an Impulse I want to change it to
a Box body. It seems like I might be missing something. That or maybe this can't be done.

Code: Select all

;*****Press ESCAPE to Exit*****
;*****Press and release SPACE BAR For Changing of Body and Impulse*****

#CameraSpeed = 0.05
#CameraViewSpeed = 1
Define.f KeyX, KeyY, MouseX, MouseY
ExamineDesktops()

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

DeskWid=DesktopWidth(0)
DeskHei=DesktopHeight(0)
    
  OpenWindow(0, 0, 0, DeskWid, DeskHei, "Test", #PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(0), 0, 0, DeskWid, DeskHei, 0, 0, 0)
  
EnableWorldPhysics(#True)
EnableWorldCollisions(#True)

  
    CreateTexture(0,32,32)
     StartDrawing(TextureOutput(0))
         Box(0, 0, 32, 32 ,RGB(150,0,0))
     StopDrawing()
     CreateMaterial(0,TextureID(0))
     
    
    CreatePlane(0, 100, 100, 100, 100, 1, 1)
    CreateEntity(0, MeshID(0), MaterialID(0),0,0,0)
    EntityPhysicBody(0, #PB_Entity_StaticBody, 0)
    
    CreateCube(0,1)
    CreateEntity(1,MeshID(0), MaterialID(0),0,0.21,0)
    ScaleEntity(1,0.5,0.2,0.2,#PB_Absolute)

    EntityPhysicBody(1, #PB_Entity_StaticBody, 0)
    ;EntityPhysicBody(1, #PB_Entity_BoxBody, 1)
    
    
    WorldDebug(#PB_World_DebugBody)

    ; Camera 
    CreateCamera(0, 0, 0, 100, 100)
    CameraRange(0, 0.08, 5000)
    CameraBackColor(0, RGB(50,50,50))
    MoveCamera(0,4.5, 1.2, 0,#PB_Absolute)
    CameraLookAt(0, 0, 1.2, 0)
    

    CreateLight(0, RGB(230,230,230), 1, 5, 4)
    SetLightColor(0, #PB_Light_DiffuseColor, RGB(255,255,255))
    AmbientColor(RGB(50,50,50))
    WorldShadows(#PB_Shadow_Additive, 600, RGB(100,100,100))


    
    Repeat
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraViewSpeed * 0.05
      EndIf
    
      
      If ExamineKeyboard()      
        If KeyboardReleased(#PB_Key_Space)
            EntityPhysicBody(1, #PB_Entity_BoxBody, 1)
            ApplyEntityImpulse(1, 0, 4, 4, 0, 0, 0)
        EndIf
        
        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
          MoveY = 0
        EndIf
        
     EndIf


      MoveCamera  (0, KeyX, 0, KeyY)
      RotateCamera(0,  MouseY, MouseX, 0, #PB_Relative)

      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape)
    
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
End
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Change Entity Physics from Static to Box?

Post by applePi »

Hi Samuel
in the modified code at first inside main loop when you press Z the cube will not move because it has physics property static, but when you press space you free the entity cube then recreate it again and assign to it the box physics property and now you can move it with Z since it has now Box property.

Code: Select all

;*****Press ESCAPE to Exit*****
;*****Press and release SPACE BAR For Changing of Body and Impulse*****

#CameraSpeed = 0.05
#CameraViewSpeed = 1
Define.f KeyX, KeyY, MouseX, MouseY
ExamineDesktops()

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

DeskWid=DesktopWidth(0)
DeskHei=DesktopHeight(0)
    
  OpenWindow(0, 0, 0, DeskWid, DeskHei, "Test", #PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(0), 0, 0, DeskWid, DeskHei, 0, 0, 0)
  
EnableWorldPhysics(#True)
EnableWorldCollisions(#True)

  
    CreateTexture(0,32,32)
     StartDrawing(TextureOutput(0))
         Box(0, 0, 32, 32 ,RGB(150,0,0))
     StopDrawing()
     CreateMaterial(0,TextureID(0))
     
    
    CreatePlane(0, 100, 100, 100, 100, 1, 1)
    CreateEntity(0, MeshID(0), MaterialID(0),0,0,0)
    EntityPhysicBody(0, #PB_Entity_StaticBody, 0)
    
    CreateCube(1,1)
    CreateEntity(1,MeshID(1), MaterialID(0),0,1,0)
    ScaleEntity(1,0.5,0.2,0.2,#PB_Absolute)

    ;EntityPhysicBody(1, #PB_Entity_BoxBody, 1)
    EntityPhysicBody(1, #PB_Entity_StaticBody, 1)
    
    
    ;WorldDebug(#PB_World_DebugBody)

    ; Camera 
    CreateCamera(0, 0, 0, 100, 100)
    CameraRange(0, 0.08, 5000)
    CameraBackColor(0, RGB(50,50,50))
    MoveCamera(0,4.5, 1.2, 0,#PB_Absolute)
    CameraLookAt(0, 0, 1.2, 0)
    

    CreateLight(0, RGB(230,230,230), 1, 5, 4)
    SetLightColor(0, #PB_Light_DiffuseColor, RGB(255,255,255))
    AmbientColor(RGB(50,50,50))
    ;WorldShadows(#PB_Shadow_Additive, 600, RGB(100,100,100))


    
    Repeat
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraViewSpeed * 0.05
      EndIf
    
      
      If ExamineKeyboard()      
        If KeyboardReleased(#PB_Key_Space)
          FreeEntity(1)
          CreateEntity(1,MeshID(1), MaterialID(0),0,1,0)
          ScaleEntity(1,0.5,0.2,0.2,#PB_Absolute)
          EntityPhysicBody(1, #PB_Entity_BoxBody, 1)
          ;ApplyEntityImpulse(1, -4, 1, 2, 0, 0, 0)
          ;flag = 1
          
        EndIf
        If KeyboardReleased(#PB_Key_Z)
          ApplyEntityImpulse(1, -4, 1, 2, 0, 0, 0)
        EndIf  
        
        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
          MoveY = 0
        EndIf
        
     EndIf


      MoveCamera  (0, KeyX, 0, KeyY)
      RotateCamera(0,  MouseY, MouseX, 0, #PB_Relative)

      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape)
    
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
End
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: Change Entity Physics from Static to Box?

Post by Samuel »

applePi wrote:Hi Samuel
in the modified code at first inside main loop when you press Z the cube will not move because it has physics property static, but when you press space you free the entity cube then recreate it again and assign to it the box physics property and now you can move it with Z since it has now Box property.
Hello, applePi
That's a good work around, but I was hoping there would be a way to simply swap the physics types. I have over 500 hundred entities to change at the same time. I was hoping that wouldn't be necessary to recreate all of them, but if it's the only way then I guess I'll have to.

I'm trying to create a huge cube made out of smaller static cubes. I need a few spheres to hit this cube, but then just bounce off. After that I'll switch the small cubes from static to a box body. Then I'll send a few more spheres, but this time they'll break the giant cube and send the small cubes flying.
I'd post this code, but its not done yet so maybe later I can.

I'll do some testing on the speed for recreating hundreds of entities at once. Most of mine are cubes. So, if the speed is good then this won't be a big deal.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Change Entity Physics from Static to Box?

Post by applePi »

Hi Samuel
consider also the possible usage of DisableEntityBody(entity, state), i don't know if it can be used in your project . here you create the cube as physics box, but disable its physics. when you press space or Z you enable its physics again and can apply force to it
PS: you may find Kelebrindae example usefull in your project http://www.purebasic.fr/english/viewtop ... r&start=15 , press space to move the hammer which will push a ball to a construction of cubes

Code: Select all

;*****Press ESCAPE to Exit*****
;*****Press and release SPACE BAR For Changing of Body and Impulse*****

#CameraSpeed = 0.05
#CameraViewSpeed = 1
Define.f KeyX, KeyY, MouseX, MouseY
ExamineDesktops()

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

DeskWid=DesktopWidth(0)
DeskHei=DesktopHeight(0)
    
  OpenWindow(0, 0, 0, DeskWid, DeskHei, "Test", #PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(0), 0, 0, DeskWid, DeskHei, 0, 0, 0)
  
EnableWorldPhysics(#True)
EnableWorldCollisions(#True)

  
    CreateTexture(0,32,32)
     StartDrawing(TextureOutput(0))
         Box(0, 0, 32, 32 ,RGB(150,0,0))
     StopDrawing()
     CreateMaterial(0,TextureID(0))
     
    
    CreatePlane(0, 100, 100, 100, 100, 1, 1)
    CreateEntity(0, MeshID(0), MaterialID(0),0,0,0)
    EntityPhysicBody(0, #PB_Entity_StaticBody, 0)
    
    CreateCube(1,1)
    CreateEntity(1,MeshID(1), MaterialID(0),0,1,0)
    ScaleEntity(1,0.5,0.2,0.2,#PB_Absolute)

    EntityPhysicBody(1, #PB_Entity_BoxBody, 1)
    DisableEntityBody(1,1)
   
    ; Camera 
    CreateCamera(0, 0, 0, 100, 100)
    CameraRange(0, 0.08, 5000)
    CameraBackColor(0, RGB(50,50,50))
    MoveCamera(0,4.5, 1.2, 0,#PB_Absolute)
    CameraLookAt(0, 0, 1.2, 0)
    

    CreateLight(0, RGB(230,230,230), 1, 5, 4)
    SetLightColor(0, #PB_Light_DiffuseColor, RGB(255,255,255))
    AmbientColor(RGB(50,50,50))
    
    Repeat
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraViewSpeed * 0.05
      EndIf
    
      
      If ExamineKeyboard()      
        If KeyboardReleased(#PB_Key_Space)
          DisableEntityBody(1, 0)
                    
        EndIf
        If KeyboardReleased(#PB_Key_Z)
          DisableEntityBody(1, 0)

          ApplyEntityImpulse(1, -4, 1, 2, 0, 0, 0)
        EndIf  
        
        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
          MoveY = 0
        EndIf
        
     EndIf


      MoveCamera  (0, KeyX, 0, KeyY)
      RotateCamera(0,  MouseY, MouseX, 0, #PB_Relative)

      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape)
    
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
End
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: Change Entity Physics from Static to Box?

Post by Samuel »

Not a bad idea, but I believe DisableEntityBody(entity, state) causes the entity to lose all physics. I need my entity to still have static physics so other entities
can come in contact with it.
I think I might have to go with your original idea, but I haven't had the time to test it with many entities at once. So, I'm not positive yet.
Also, thanks for the link to your other post I'll take a look through it later.
Olby
Enthusiast
Enthusiast
Posts: 461
Joined: Mon Jan 12, 2009 10:33 am
Contact:

Re: Change Entity Physics from Static to Box?

Post by Olby »

Why would you want to make them static and then change to dynamic in real time anyway? It's quite a performance hit to swap the physics bodies in your main loop unless these are predefined and inactive (but PB does not support that yet). I would just set them all up as dynamic entities. Bullet will automatically make inactive objects go in to "sleep" mode ie. physics calculations will not be performed on the entities thus saving precious CPU cycles.
Intel Core i7 Quad 2.3 Ghz, 8GB RAM, GeForce GT 630M 2GB, Windows 10 (x64)
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: Change Entity Physics from Static to Box?

Post by Samuel »

Hello, Olby

Normally, I would just set them as dynamic as you suggest. In this case though it is important for me to be able to swap them. Fortunately applePi's original suggestion of
just recreating the meshes is working fine. I tested it with 200 cubes and it was instant with no visible lag. I could see it being a problem for bigger more complicated meshes and for slower computers. For now I think my problem is solved.
Thanks for the help guys.
bmon
User
User
Posts: 54
Joined: Sat May 24, 2008 8:51 pm
Location: U.S.

Re: Change Entity Physics from Static to Box?

Post by bmon »

Hi Samuel ...
Check out the EntityLinearFactor() and EntityAngularFactor() commands. This is what I did in my game Drop-it Pock-it to allow the balls to remain stationary even though something collided with them ... then just set these values to (#entity,1,1,1) to be able to move or roll in all directions again.
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: Change Entity Physics from Static to Box?

Post by Samuel »

Thank you bmon!
That was exactly what I needed. I gave it a quick test and everything worked perfectly.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Change Entity Physics from Static to Box?

Post by applePi »

thank you bmon very much for the suggestion , i have just now tried it with the Kelebrindae example mentioned above and the wall are very solid using
EntityLinearFactor(entity,0,0,0) ;for every cube
EntityAngularFactor(entity, 0, 0, 0) ;for every cube
but after pressing Z now
EntityLinearFactor(entity,1,1,1) ;for every cube
EntityAngularFactor(entity, 1, 1, 1) ;for every cube
good from the developers to add those functions, i am trying to see other possible usages for it in mechanics such as earthquakes simulations.

Code: Select all

;originally posted by Kelebrindae
; Window size
#SCREENWIDTH = 800
#SCREENHEIGHT = 500
Dim a(20)
;- initialization
InitEngine3D()
InitSprite()
InitKeyboard()

;- Window
OpenWindow(0, 0, 0, #SCREENWIDTH, #SCREENHEIGHT, "press space to move the hammer, press Z before space to free the cubes restrictions", #PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_MinimizeGadget)
OpenWindowedScreen(WindowID(0), 0, 0, #SCREENWIDTH,#SCREENHEIGHT, 0, 0, 0,#PB_Screen_SmartSynchronization)

;-Texture
Add3DArchive(".",#PB_3DArchive_FileSystem)
CreateImage(0,128, 128)
StartDrawing(ImageOutput(0))
  Box(0, 0, 128, 128, $FFFFFF)
StopDrawing()
SaveImage(0,"temp.bmp")
FreeImage(0)
LoadTexture(0,"temp.bmp")
DeleteFile("temp.bmp")

;-Material
CreateMaterial(1,TextureID(0))
;MaterialAmbientColor(1,#PB_Material_AmbientColors)
SetMaterialColor(1, #PB_Material_AmbientColor, RGB(100,100,100))
CreateMaterial(2,TextureID(0))
;MaterialAmbientColor(2,$FFFF00)
SetMaterialColor(2, #PB_Material_AmbientColor, $FFFF00)
CreateMaterial(3,TextureID(0))
;MaterialAmbientColor(3,$0077FF)
SetMaterialColor(3, #PB_Material_AmbientColor, $0077FF)
CreateMaterial(4,TextureID(0))
;MaterialAmbientColor(4,$FF00FF)
SetMaterialColor(4, #PB_Material_AmbientColor, $FF00FF)
CreateMaterial(5,TextureID(0))
;MaterialAmbientColor(5,$0055BB)
SetMaterialColor(5, #PB_Material_AmbientColor, $FF00FF)

;- Entities
CreatePlane(1,30,30,20,20,1,1)
sol  = CreateEntity(#PB_Any, MeshID(1), MaterialID(1))
EntityPhysicBody(sol, #PB_Entity_StaticBody)   


; Ball and tee
CreateCube(2, 1.0)
socle = CreateEntity(#PB_Any,MeshID(2), MaterialID(1))
ScaleEntity(socle,0.5,1,0.5)
MoveEntity(socle,2.6,0.5,0)
EntityPhysicBody(socle, #PB_Entity_StaticBody)

CreateSphere(3,0.5)
ball = CreateEntity(#PB_Any, MeshID(3), MaterialID(3))
MoveEntity(ball,2.6,1.5,0)
EntityPhysicBody(ball, #PB_Entity_SphereBody,1,4,1)

; Targets
For i = 1 To 4
  temp = CreateEntity(#PB_Any, MeshID(2), MaterialID(4))
  r+1
  a(r) = temp ; store entities numbers
  MoveEntity(temp,-10,0.5,(i*1.1)-2.8)
  EntityPhysicBody(temp, #PB_Entity_BoxBody, 0.25)
  EntityLinearFactor(temp,0,0,0)
  EntityAngularFactor(temp, 0, 0, 0)

Next i
For i = 1 To 3
  temp = CreateEntity(#PB_Any, MeshID(2), MaterialID(4))
  r+1
  a(r) = temp
  MoveEntity(temp,-10,1.5,(i*1.1)-2.3)
  EntityPhysicBody(temp, #PB_Entity_BoxBody, 0.25)
  EntityLinearFactor(temp,0,0,0)
  EntityAngularFactor(temp, 0, 0, 0)
Next i
For i = 1 To 2
  temp = CreateEntity(#PB_Any, MeshID(2), MaterialID(4))
  r+1
  a(r) = temp
  MoveEntity(temp,-10,2.5,(i*1.1)-1.8)
  EntityPhysicBody(temp, #PB_Entity_BoxBody, 0.25)
  EntityLinearFactor(temp,0,0,0)
  EntityAngularFactor(temp, 0, 0, 0)
Next i

; Mobile
Global hammerX.f = 5,hammerY.f = 8.5,hammerZ.f
CreateSphere(4,0.4)
axe = CreateEntity(#PB_Any, MeshID(4), MaterialID(2))
MoveEntity(axe,hammerX,hammerY,hammerZ)
EntityPhysicBody(axe, #PB_Entity_StaticBody)

shaft = CreateEntity(#PB_Any, MeshID(2), MaterialID(5))
ScaleEntity(shaft,0.3,6,0.3)
MoveEntity(shaft,hammerX,hammerY - 3.5,hammerZ)
EntityPhysicBody(shaft, #PB_Entity_BoxBody,0.1)

hammerhead = CreateEntity(#PB_Any,MeshID(2), MaterialID(2))
ScaleEntity(hammerhead,2,1,1)
MoveEntity(hammerhead,hammerX,hammerY - 7,hammerZ)
EntityPhysicBody(hammerhead,#PB_Entity_BoxBody,20)

PointJoint(0,EntityID(shaft),0,-3.1 ,0, EntityID(hammerhead),0,0.5,0) ; Attach the hammerhead to the shaft
; Attach the hammer to axis.
HingeJoint(1,EntityID(axe),0,0,0, #False,#False,#True, EntityID(shaft),0,3.5,0, #False,#False,#True)
HingeJoint(2,EntityID(axe),0,0,0, #False,#False,#True, EntityID(hammerhead),0,7,0, #False,#False,#True)


;- Camera
CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0,-2,8,18)
CameraLookAt(0,-2,5,0)

;-Light
AmbientColor($666666)
CreateLight(0,RGB(160,160,255),200,400,200)
WorldShadows(#PB_Shadow_Additive)

KeyboardMode(#PB_Keyboard_International)

;- Main loop
Repeat
  Delay(1)
  While WindowEvent() : Wend

  ;- F1, F2, F3 : Change view
  If ExamineKeyboard()
    If KeyboardReleased(#PB_Key_Z) ; to free cubes from restrictions
      For i=1 To 9
     EntityLinearFactor(a(i),1,1,1)
     EntityAngularFactor(a(i), 1, 1, 1) 
     Next
   EndIf
   
    If KeyboardReleased(#PB_Key_F1)
      MoveCamera(0,-18,2,0)
      CameraLookAt(0,0,3,0)
    EndIf
    If KeyboardReleased(#PB_Key_F2)
      MoveCamera(0,0,12,20)
      CameraLookAt(0,0,5,0)
    EndIf
    If KeyboardReleased(#PB_Key_F3)
      MoveCamera(0,1,5,10)
      CameraLookAt(0,-3,3,0)
    EndIf
   
    ;- [Space] : Throw hammer
    If KeyboardPushed(#PB_Key_Space)
      ApplyEntityImpulse(hammerhead,  5, 0, 0)
    EndIf
    ;- R : Reset ball's position (doesn't work)
    If KeyboardReleased(#PB_Key_R)
      FreeEntity(ball)
    EndIf
   
    ;- Return : Display FPS
    If KeyboardReleased(#PB_Key_Return)
      MessageRequester("Infos","FPS = " + Str(Engine3DFrameRate(#PB_Engine3D_Average)))
    EndIf

  EndIf

  ; Render
  RenderWorld()
  FlipBuffers()

  If Not IsEntity(ball)
      ball = CreateEntity(#PB_Any, MeshID(3), MaterialID(3))
      MoveEntity(ball,2.6,1.5,0)
      EntityPhysicBody(ball, #PB_Entity_SphereBody,1,4,1)
  EndIf

Until KeyboardPushed(#PB_Key_Escape)

End
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Change Entity Physics from Static to Box?

Post by applePi »

by accident i have found that now in PB 5.31 it is possible to change physics bodies on the fly
example: look this 1771 cubes pyramid, it has static physics body ie the force will not affect it..
in the following code press Space key to change 500 cubes to box bodies and apply force over it
while they are flying press X key to return the flying cubes to static bodies again (they are frozen in place)

Code: Select all

#CameraSpeed = 0.5
Define.f KeyX, KeyY, MouseX, MouseY

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

ExamineDesktops()

OpenWindow(0,0,0,DesktopWidth(0), DesktopHeight(0),"...  Space to throw pyramid cubes.... 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/Packs/desert.zip", #PB_3DArchive_Zip)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()

 ;-------------  Materials  -----------------------------------------
  CreateMaterial(2, LoadTexture(2, "MRAMOR6X6.jpg"))
  CreateMaterial(1, LoadTexture(1, "wood.jpg"))
  CreateMaterial(3, LoadTexture(3, "ground_diffuse.png"))
  CreateMaterial(0, LoadTexture(0, "Geebee2.bmp"))
  CreateMaterial(4, LoadTexture(4, "nskinrd.jpg"))
  CreateMaterial(5, LoadTexture(5, "snow_1024.jpg"))

  
  ;MaterialShadingMode(1, #PB_Material_Wireframe     )
 ;--------------  Camera  -----------------------------------------
  #camera = 0
  CreateCamera(#Camera, 0, 0, 100, 100)
  MoveCamera(#camera, 0, 15, -35, #PB_Absolute)
  CameraLookAt(0, 0, 2, 0)
  
;- -------------  Light  ---------------------------------------------
  CreateLight(0, $FFFFFF, 1560, 900, 500)
  AmbientColor(RGB(255,255,255))
;- -------------  World  -------------------------------------------
  ;WorldGravity(-9)
  SkyBox("desert07.jpg")
  ;----------------- the big wheel-----------------------------------------
CreatePlane(5000, 30, 30, 5, 5, 2, 2)
CreateEntity(5000, MeshID(5000), MaterialID(5), 0,-0.5,0.0)
EntityPhysicBody(5000, #PB_Entity_StaticBody, 1, 0.1, 1)

CreateCube(0, 1)
min = -10: max = 10
For y = 0 To 10
For z = min To max
  For x = min To max
    i+1
    CreateEntity(i, MeshID(0), MaterialID(mat))
    MoveEntity(i, x*1.1, y, z*1.1, #PB_Absolute)
    EntityPhysicBody(i, #PB_Entity_StaticBody, 1)
  Next x
Next z
min+1 : max=Abs(min)
mat+1: If mat>4:mat=0:EndIf
Next y
;Debug i
  
Repeat
  WindowEvent()
  
  If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
      EndIf
  ShowCursor_(0)
    
  ExamineKeyboard()
  
        ; Use arrow keys and mouse to rotate camera and fly in/out
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed/2
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed/2
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed/2
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed/2
        Else
          KeyY = 0
        EndIf
        
        If KeyboardReleased(#PB_Key_Space)
          For n=i To i-500 Step -1
            EntityPhysicBody(n, #PB_Entity_BoxBody, 1)
            ApplyEntityImpulse(n, Random(10,5),Random(10,5),5)
          Next
        EndIf
        
        If KeyboardReleased(#PB_Key_X)
          For n=i To i-500 Step -1
            EntityPhysicBody(n, #PB_Entity_StaticBody, 1)
            
          Next
        EndIf
        


  If KeyboardReleased(#PB_Key_C) ; check camera position
    Debug CameraX(0): Debug CameraY(0): Debug CameraZ(0)
  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
User avatar
Bananenfreak
Enthusiast
Enthusiast
Posts: 519
Joined: Mon Apr 15, 2013 12:22 pm

Re: Change Entity Physics from Static to Box?

Post by Bananenfreak »

Thank you applePi, good to know.
So it will be much easier for me to handle all the entities in my game.
Last time I checked it is long time ago.
I am not at home, I will check it at soon as possible.
Image
box_80
Enthusiast
Enthusiast
Posts: 115
Joined: Mon Sep 03, 2012 8:52 pm

Re: Change Entity Physics from Static to Box?

Post by box_80 »

Thanks for sharing this accident applePi. This will help out a lot.
Post Reply