Page 1 of 1

spring behaviour for balls over each other

Posted: Tue Dec 30, 2014 4:54 pm
by applePi
position balls over each other and restrict every ball horizontal movements to 0:
EntityLinearFactor(OneSphere, 0, 0.1, 0)
the same for the weights we will add later:
EntityLinearFactor(OneCube, 0, 1, 0)
now you have a spring, drop cubic weights (Z key) it will push the spheres into each other, release some weights (key V) and the spring will return to its position
but note this is static, we can't move it because x,z movement set to zero: EntityLinearFactor(OneSphere, 0, 0.1, 0)
Image

Code: Select all

Enumeration
 #plane
 #cube
 #sphere
EndEnumeration
Declare weights()
Global NewList Cubes()
Global meshesNum

Quit.b = #False
ExamineDesktops()
OpenWindow(3, 0, 0, DesktopWidth(0), DesktopHeight(0), "Z: Drop a weight,...V: push a weight.....Space ..to push the spring, ... Arrow keys and mouse to move/rotate Cam ", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

;Initialize environment
InitEngine3D()
InitSprite()
OpenWindowedScreen(WindowID(3), 0, 0, DesktopWidth(0), DesktopHeight(0), 0, 0, 0)
InitKeyboard()
InitMouse()
SetFrameRate(60)
Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)

CreateMaterial(1, LoadTexture(1, "clouds.jpg"))
CreatePlane(#plane, 20, 20, 1, 1, 1, 1)
CreateEntity (#plane, MeshID(#plane), MaterialID(1))
MoveEntity(#plane,0,-6,0)
EntityPhysicBody(#plane, #PB_Entity_StaticBody)


CreateLight(0,RGB(255,255,255),-100,40,30)
AmbientColor(RGB(100,100,100))

CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0, 7, 15, 20)
CameraLookAt(0, 2.0, 3, -1.5)

CreateMaterial(0, LoadTexture(0, "wood.jpg"))
MaterialCullingMode(0, #PB_Material_NoCulling)

MaterialShadingMode(0, #PB_Material_Wireframe)
DisableMaterialLighting(0, 1)
CreateMesh(1, #PB_Mesh_TriangleList, #PB_Mesh_Static )
SetMeshMaterial(1, MaterialID(0))

CreateMaterial(3, LoadTexture(3, "white.jpg"))
SetMaterialColor(3, #PB_Material_SelfIlluminationColor, RGB(255,0,0))
CreateCube(#cube,1)
Global r
y.f = -5.9
BaseBlock = CreateEntity(#PB_Any, MeshID(#cube), MaterialID(3))
ScaleEntity(BaseBlock, 3, 0.2, 3)
y = y + 0.2
MoveEntity(BaseBlock, 2.0, y, -1.5, #PB_Absolute)
SetMaterialColor(3, #PB_Material_SelfIlluminationColor, RGB(255,0,0))
EntityPhysicBody(BaseBlock, #PB_Entity_BoxBody, 100,1,1)

CreateSphere(#sphere, 1)    
y = y + 0.5
For i=0 To 5 ; constructing the 6 spheres (the spring components)
OneSphere = CreateEntity(#PB_Any, MeshID(#sphere), MaterialID(0))

MoveEntity(OneSphere, 2.0, y, -1.5, #PB_Absolute)
SetMaterialColor(3, #PB_Material_SelfIlluminationColor, RGB(255,0,0))
EntityPhysicBody(OneSphere, #PB_Entity_SphereBody, 1,1,10)
EntityLinearFactor(OneSphere, 0, 0.1, 0) ; important
y = y + 2
Next
;the Pan over the 6 spheres (spring components)
Pan = CreateEntity(#PB_Any, MeshID(#cube), MaterialID(3)) 
ScaleEntity(Pan, 2, 0.5, 2)
y = y + 0.2
MoveEntity(Pan, 2.0, y, -1.5, #PB_Absolute)
EntityPhysicBody(Pan, #PB_Entity_BoxBody, 100,1,10)
EntityLinearFactor(Pan, 0, 1, 0) ; important

;WorldDebug(#PB_World_DebugBody)
ExamineKeyboard()
Global.f keyX, ketY, MouseY, MouseX

CreateMaterial(4, LoadTexture(4, "wood.jpg"))
CreateMaterial(5, LoadTexture(5, "Geebee2.bmp"))
CreateMaterial(6, LoadTexture(6, "ValetCoeur.jpg"))
CreateMaterial(7, LoadTexture(7, "ground_diffuse.png"))

CreateCube(#cube, 1) ; the cube mesh for the adding weights later

Repeat
  Event = WindowEvent()
  

  If KeyboardPushed(#PB_Key_Space)
    
    ApplyEntityImpulse(OneSphere, 0,  -25,  0)
    
  EndIf
                
  If KeyboardReleased(#PB_Key_Z)
  weights() ; add Cubic weightes over the Pan

  ElseIf KeyboardReleased(#PB_Key_V)
    
    If ListSize(Cubes()) >= 1
    
    EntityLinearFactor(Cubes(), 1, 1, 1) 
       
    ApplyEntityImpulse(Cubes(), -1*420,  0,  0)
    DeleteElement(Cubes() ) 
    EndIf
    
  EndIf
  

  If ExamineMouse()
        MouseX = -MouseDeltaX()/10
        MouseY = -MouseDeltaY()/10
      EndIf
      
      
      If ExamineKeyboard()
                
        If KeyboardPushed(#PB_Key_Left)
          KeyX.f = -0.2
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX.f = 0.2
        Else
          KeyX.f = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY.f = -0.2
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY.f = 0.2
        Else
          KeyY.f = 0
        EndIf
        
      EndIf
      
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (0, KeyX.f, 0, KeyY.f)
  
  
    
  RenderWorld()
  FlipBuffers()
   
   ExamineKeyboard()
   If KeyboardPushed(#PB_Key_Escape)
      Quit = #True
    EndIf
  Until Quit = #True Or Event = #PB_Event_CloseWindow
  
Procedure weights() ; add cubes over the spring Pan
                      
OneCube = CreateEntity(#PB_Any, MeshID(#cube), MaterialID(Random(7,4)), 2, 12,-1.5)

AddElement(Cubes()) : Cubes() = OneCube
EntityPhysicBody(OneCube, #PB_Entity_BoxBody, 50, 0.1, 1)
EntityLinearFactor(OneCube, 0, 1, 0) ; prevent the cubes from horizontal moving
            
SetMaterialColor(3, #PB_Material_SelfIlluminationColor, RGB(255,0,0))

            
EndProcedure