the polygons of the origami shapes usually rotate around its edges, and in purebasic to be able to do that we need to center the polygon mesh about one of its edges, something like this:


, after that we create an Entity from that mesh and then only we can position this Entity anywhere and it will stay rotate correctly around that edge
we use TransformMesh to center the mesh on a specific edge. the best is to show the simplest example which you can make experiments. look instructions on the title bar
Code: Select all
#CameraSpeed = 0.2
Global Dim MeshData.PB_MeshVertex(0)
Global xx.f, yy.f
Global ArrSize
Define.f KeyX, KeyY, MouseX, MouseY
Enumeration
#Mesh
#Camera
EndEnumeration
If InitEngine3D()
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/Scripts",#PB_3DArchive_FileSystem)
Parse3DScripts()
InitSprite()
InitKeyboard()
InitMouse()
ExamineDesktops()
DesktopW = DesktopWidth(0)
DesktopH = DesktopHeight(0)
If OpenWindow(0, 0, 0, DesktopW, DesktopH, "QW....ASDF (for rotation around edges) , ....P toggle wire/solid Frame ... arrow keys/ mouse to move/ rotate Camera")
If OpenWindowedScreen(WindowID(0), 0, 0, DesktopW, DesktopH, 0, 0, 0)
Global monitor=CreateSprite(#PB_Any,120,60)
CreateCamera(#Camera, 0, 0, 100, 100)
MoveCamera(#Camera, 0, 10, 20, #PB_Absolute)
CameraLookAt(#Camera,0,0,0)
CameraBackColor(#Camera, RGB(250,250,250))
CreateLight(0, RGB(0, 0, 0), -20, 40, 20)
SetLightColor(0, #PB_Light_DiffuseColor, RGB(255,255,255))
AmbientColor(RGB(255, 255, 255))
CreateMaterial(5, LoadTexture(5, "wood.jpg"))
MaterialCullingMode(5, #PB_Material_NoCulling)
DisableMaterialLighting(5, 0)
CreateMaterial(6, LoadTexture(6, "Geebee2.bmp"))
MaterialCullingMode(6, #PB_Material_NoCulling)
;CreatePlane(#Mesh, TileSizeX, TileSizeZ, TileCountX, TileCountZ, TextureRepeatCountX, TextureRepeatCountZ)
CreatePlane(1, 10,5, 10,5,1,1)
TransformMesh(1, -5,0,2.5,1,1,1,0,0,0)
CreateEntity(1,MeshID(1), MaterialID(5))
CreatePlane(2, 10,5, 10,5,1,1)
TransformMesh(2, 5,0,2.5, 1,1,1, 0,0,0)
CreateEntity(2,MeshID(2), MaterialID(6))
CreateMaterial(0, LoadTexture(0, "White.jpg"))
DisableMaterialLighting(0, #True)
EndIf
EndIf
wireFrame = 1
Repeat
If ExamineMouse()
MouseX = -MouseDeltaX()/20
MouseY = -MouseDeltaY()/20
EndIf
If 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_P)
If wireFrame
MaterialShadingMode(5, #PB_Material_Wireframe)
MaterialShadingMode(6, #PB_Material_Wireframe)
wireFrame ! 1
Else
MaterialShadingMode(5, #PB_Material_Solid)
MaterialShadingMode(6, #PB_Material_Solid)
wireFrame ! 1
EndIf
EndIf
EndIf
If KeyboardPushed(#PB_Key_S)
RotateEntity(1, 0.5,0.0,0.0,#PB_Relative)
ElseIf KeyboardPushed(#PB_Key_A)
RotateEntity(1, -0.5,0.0,0.0,#PB_Relative)
EndIf
If KeyboardPushed(#PB_Key_F)
RotateEntity(1, 0,0.0,0.5,#PB_Relative)
ElseIf KeyboardPushed(#PB_Key_D)
RotateEntity(1, 0,0.0,-0.5,#PB_Relative)
EndIf
If KeyboardPushed(#PB_Key_Q)
RotateEntity(2, 0,0.0,0.5,#PB_Relative)
ElseIf KeyboardPushed(#PB_Key_W)
RotateEntity(2, 0,0.0,-0.5,#PB_Relative)
EndIf
RotateCamera(#Camera, MouseY, MouseX, 0, #PB_Relative)
MoveCamera(#Camera, KeyX, 0, KeyY)
;RotateEntity(#mesh, 0.0,0.1,0.0,#PB_Relative)
RenderWorld()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
Else
MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
End
i have obtained that precision values from experiment: you can make experiment yourself by doing this detailed procedure:
1-uncomment line 37 to position the camera over the scene
2-be sure #tempMesh = 5 in line 11 or the polygon you want to add or to correct
3-comment lines 101 + 102:
MoveEntity(5, 0.47, 0, 0.65, #PB_Absolute)
RotateEntity(5, 0,36,0, #PB_Relative)
when you run the code you get this:

4-note the polygon side on the 'X' coordinate and keep your eyes on it, you want to reposition it on the bottom-right of the pentagonal woody ground : use keys: IJKL to move the polygon, and ZX to rotate it, maneuver antil it fits excactly on its desired place, then press 'C' to get the x,y,z, EntityYaw , now copy the x,y,z values to MoveEntity(5, 0.47, 0, 0.65, #PB_Absolute) in line 101
and copy EntityYaw to Y in RotateEntity(5, 0,36,0, #PB_Relative) line 102 you will get very close values of the written values
i think this is the best approach to design complex origami (the positioning of parts experimentally else you need to read heavy and thick university Geometry Books)
Code: Select all
#CameraSpeed = 0.2
Define.f KeyX, KeyY, MouseX, MouseY
Enumeration
#sphere = 100
#Camera
EndEnumeration
#tempMesh = 5 ;
If InitEngine3D()
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/Scripts",#PB_3DArchive_FileSystem)
Parse3DScripts()
InitSprite()
InitKeyboard()
InitMouse()
ExamineDesktops()
DesktopW = DesktopWidth(0)
DesktopH = DesktopHeight(0)
If OpenWindow(0, 0, 0, DesktopW, DesktopH, "QWERT: fold , ASDFG: UnFold....P: Wire Frame ... Space: rotate/stop....arrow keys/ mouse to move/ rotate Camera")
If OpenWindowedScreen(WindowID(0), 0, 0, DesktopW, DesktopH, 0, 0, 0)
Global monitor=CreateSprite(#PB_Any,10,60)
CreateCamera(#Camera, 0, 0, 100, 100)
MoveCamera(#Camera, 0, 5, 5, #PB_Absolute)
;MoveCamera(#Camera, 0, 7, 0.01, #PB_Absolute)
CameraLookAt(#Camera,0,0,0)
CameraBackColor(#Camera, RGB(250,250,250))
CreateLight(0, RGB(0, 0, 0), -20, 40, 20)
SetLightColor(0, #PB_Light_DiffuseColor, RGB(255,255,255))
AmbientColor(RGB(255, 255, 255))
CreateMaterial(5, LoadTexture(5, "Wood.jpg"))
;MaterialCullingMode(5, #PB_Material_NoCulling)
RotateMaterial(5, 180, #PB_Material_Fixed)
DisableMaterialLighting(5, 0)
CreateMaterial(6, LoadTexture(6, "Geebee2.bmp"))
;MaterialCullingMode(6, #PB_Material_NoCulling)
CreateMaterial(7, LoadTexture(7, "RustyBarrel.png"))
;MaterialCullingMode(7, #PB_Material_NoCulling)
CreateMaterial(8, LoadTexture(8, "ground_diffuse.png"))
;MaterialCullingMode(8, #PB_Material_NoCulling)
CreateMaterial(9, LoadTexture(9, "Caisse.png"))
;MaterialCullingMode(9, #PB_Material_NoCulling)
CreateLine3D(500, 0, 0, 0, RGB(255,0,0), 20, 0, 0, RGB(255,0,0))
CreateSphere(0, 1, 5,10)
CreateEntity(0,MeshID(0), MaterialID(5))
TransformMesh(0, 0,0,0, 1,0,1, 0,0,0)
CreateSphere(1, 1, 5,10)
CreateEntity(1,MeshID(1), MaterialID(6))
TransformMesh(1, 0,0,0.8090299, 1,0,1, 0,0,0)
MoveEntity(1, -0.4799998,0,0.64999967, #PB_Absolute)
RotateEntity(1, 0,-36,0, #PB_Absolute)
AttachEntityObject(0,"",EntityID(1))
CreateSphere(2, 1, 5,10)
CreateEntity(2,MeshID(1), MaterialID(6))
TransformMesh(2, 0,0,0.8090299, 1,0,1, 0,0,0)
MoveEntity(2, -0.7599, 0,-0.24, #PB_Absolute)
RotateEntity(2, 0,-107.902,0, #PB_Absolute)
AttachEntityObject(0,"",EntityID(2))
CreateSphere(3, 1, 5,10)
CreateEntity(3,MeshID(1), MaterialID(6))
TransformMesh(3, 0,0,0.8090299, 1,0,1, 0,0,0)
MoveEntity(3, 0,0,-0.8, #PB_Absolute)
RotateEntity(3, 0,-180,0, #PB_Absolute)
AttachEntityObject(0,"",EntityID(3))
CreateSphere(4, 1, 5,10)
CreateEntity(4,MeshID(1), MaterialID(6))
TransformMesh(4, 0,0,0.8090299, 1,0,1, 0,0,0)
MoveEntity(4, 0.76,0,-0.24, #PB_Absolute)
RotateEntity(4, 0,108.5,0, #PB_Absolute)
AttachEntityObject(0,"",EntityID(4))
CreateSphere(5, 1, 5,10)
CreateEntity(5,MeshID(1), MaterialID(6))
TransformMesh(5, 0,0,0.8090299, 1,0,1, 0,0,0)
MoveEntity(5, 0.47, 0, 0.65, #PB_Absolute)
RotateEntity(5, 0,36,0, #PB_Relative)
AttachEntityObject(0,"",EntityID(5))
EndIf
EndIf
rot = 0:
wireFrame = 1
Repeat
If ExamineMouse()
MouseX = -MouseDeltaX()/20
MouseY = -MouseDeltaY()/20
EndIf
If 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_P)
If wireFrame
MaterialShadingMode(5, #PB_Material_Wireframe)
MaterialShadingMode(6, #PB_Material_Wireframe)
MaterialShadingMode(7, #PB_Material_Wireframe)
MaterialShadingMode(8, #PB_Material_Wireframe)
MaterialShadingMode(9, #PB_Material_Wireframe)
wireFrame ! 1
Else
MaterialShadingMode(5, #PB_Material_Solid)
MaterialShadingMode(6, #PB_Material_Solid)
MaterialShadingMode(7, #PB_Material_Solid)
MaterialShadingMode(8, #PB_Material_Solid)
MaterialShadingMode(9, #PB_Material_Solid)
wireFrame ! 1
EndIf
EndIf
EndIf
If KeyboardReleased(#PB_Key_Space)
rot ! 1
EndIf
;-------------------------------------------------------------
; folding and unfolding
If KeyboardPushed(#PB_Key_A)
RotateEntity(1, 1,0,0, #PB_Relative)
ElseIf KeyboardPushed(#PB_Key_Q)
RotateEntity(1, -1,0,0, #PB_Relative)
ElseIf KeyboardPushed(#PB_Key_W)
RotateEntity(2, -1,0,0, #PB_Relative)
ElseIf KeyboardPushed(#PB_Key_S)
RotateEntity(2, 1,0,0, #PB_Relative)
ElseIf KeyboardPushed(#PB_Key_E)
RotateEntity(3, -1,0,0, #PB_Relative)
ElseIf KeyboardPushed(#PB_Key_D)
RotateEntity(3, 1,0,0, #PB_Relative)
ElseIf KeyboardPushed(#PB_Key_R)
RotateEntity(4, -1,0,0, #PB_Relative)
ElseIf KeyboardPushed(#PB_Key_F)
RotateEntity(4, 1,0,0, #PB_Relative)
ElseIf KeyboardPushed(#PB_Key_T)
RotateEntity(5, -1,0,0, #PB_Relative)
ElseIf KeyboardPushed(#PB_Key_G)
RotateEntity(5, 1,0,0, #PB_Relative)
EndIf
;;;ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
;For positioning and rotating the new polygons correctly,
;after positioning press 'C' key to get the x,y,z and EntityYaw
;and put these values in MoveEntity, RotateEntity as an absolute value
If KeyboardPushed(#PB_Key_I)
MoveEntity(#tempMesh, 0,0,-0.01)
ElseIf KeyboardPushed(#PB_Key_K)
MoveEntity(#tempMesh, 0,0,0.01)
ElseIf KeyboardPushed(#PB_Key_L)
MoveEntity(#tempMesh, 0.01,0,0)
ElseIf KeyboardPushed(#PB_Key_J)
MoveEntity(#tempMesh, -0.01,0,0)
ElseIf KeyboardPushed(#PB_Key_X)
RotateEntity(#tempMesh, 0,0.1,0, #PB_Relative)
ElseIf KeyboardPushed(#PB_Key_Z)
RotateEntity(#tempMesh, 0,-0.1,0, #PB_Relative)
EndIf
If KeyboardReleased(#PB_Key_C) ; get the coordinates of the new added polygons
Debug EntityX(#tempMesh):Debug EntityY(#tempMesh):Debug EntityZ(#tempMesh)
Debug "==============================="
Debug "EntityYaw = " +StrF(EntityYaw(#tempMesh)):Debug EntityPitch(#tempMesh):Debug EntityRoll(#tempMesh)
EndIf
RotateCamera(#Camera, MouseY, MouseX, 0, #PB_Relative)
MoveCamera(#Camera, KeyX, 0, KeyY)
RotateEntity(0, 0,rot/5,0, #PB_Relative)
RenderWorld()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
Else
MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
End
Code: Select all
#CameraSpeed = 0.2
Define.f KeyX, KeyY, MouseX, MouseY
Enumeration
#sphere = 100
#Camera
EndEnumeration
If InitEngine3D()
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/Scripts",#PB_3DArchive_FileSystem)
Parse3DScripts()
InitSprite()
InitKeyboard()
InitMouse()
ExamineDesktops()
DesktopW = DesktopWidth(0)
DesktopH = DesktopHeight(0)
If OpenWindow(0, 0, 0, DesktopW, DesktopH, "QWERT: Unfold , ASDFG: Fold....P: Wire Frame ... Space: rotate/stop....arrow keys/ mouse to move/ rotate Camera")
If OpenWindowedScreen(WindowID(0), 0, 0, DesktopW, DesktopH, 0, 0, 0)
Global monitor=CreateSprite(#PB_Any,120,60)
CreateCamera(#Camera, 0, 0, 100, 100)
MoveCamera(#Camera, 0, 10, 20, #PB_Absolute)
CameraLookAt(#Camera,0,2,0)
CameraBackColor(#Camera, RGB(250,250,250))
CreateLight(0, RGB(0, 0, 0), -20, 40, 20)
SetLightColor(0, #PB_Light_DiffuseColor, RGB(255,255,255))
AmbientColor(RGB(255, 255, 255))
CreateMaterial(5, LoadTexture(5, "Wood.jpg"))
MaterialCullingMode(5, #PB_Material_NoCulling)
RotateMaterial(5, 180, #PB_Material_Fixed)
DisableMaterialLighting(5, 0)
CreateMaterial(6, LoadTexture(6, "Geebee2.bmp"))
MaterialCullingMode(6, #PB_Material_NoCulling)
CreateMaterial(7, LoadTexture(7, "RustyBarrel.png"))
MaterialCullingMode(7, #PB_Material_NoCulling)
CreateMaterial(8, LoadTexture(8, "ground_diffuse.png"))
MaterialCullingMode(8, #PB_Material_NoCulling)
CreateMaterial(9, LoadTexture(9, "Caisse.png"))
MaterialCullingMode(9, #PB_Material_NoCulling)
;CreatePlane(#Mesh, TileSizeX, TileSizeZ, TileCountX, TileCountZ, TextureRepeatCountX, TextureRepeatCountZ)
CreatePlane(1, 10,5, 10,5,1,1) ; Box Bottom
CreateEntity(1,MeshID(1), MaterialID(5))
CreatePlane(2, 5,5, 10,5,1,1) ; right side
TransformMesh(2, 2.5,0,0, 1,1,1, 0,0,0)
CreateEntity(2,MeshID(2), MaterialID(6))
MoveEntity(2, 5, 0, 0)
CreatePlane(3, 5,5, 10,5,1,1) ; Left side
TransformMesh(3, -2.5,0,0, 1,1,1, 0,0,0)
CreateEntity(3,MeshID(3), MaterialID(6))
MoveEntity(3, -5, 0, 0)
CreatePlane(4, 10,5, 10,5,1,1) ; back side
TransformMesh(4, 0,0,-2.5, 1,1,1, 0,0,0)
CreateEntity(4,MeshID(4), MaterialID(7))
MoveEntity(4, 0, 0, -2.5)
CreatePlane(5, 10,5, 10,5,1,1) ; top side
TransformMesh(5, 0,0,-2.5, 1,1,1, 0,0,0)
CreateEntity(5,MeshID(5), MaterialID(8))
MoveEntity(5, 0, 0, -5)
CreatePlane(6, 10,5, 10,10,1,1) ; front side
TransformMesh(6, 0,0,2.5, 1,1,1, 0,0,0)
CreateEntity(6,MeshID(6), MaterialID(9))
MoveEntity(6, 0, 0, 2.5)
EndIf
EndIf
AttachEntityObject(1,"",EntityID(2), 0,0,0, 0,0,0)
AttachEntityObject(1,"",EntityID(3), 0,0,0, 0,0,0)
AttachEntityObject(1,"",EntityID(4), 0,0,0, 0,0,0)
AttachEntityObject(1,"",EntityID(6), 0,0,0, 0,0,0)
AttachEntityObject(4,"",EntityID(5), 0,0,0, 0,0,0)
RotateEntity(2, 0,0.0,90,#PB_Relative)
RotateEntity(3, 0,0.0,-90,#PB_Relative)
RotateEntity(4, 90,0.0,0,#PB_Relative)
RotateEntity(5, 90,0.0,0,#PB_Relative)
RotateEntity(6, -90,0.0,0,#PB_Relative)
rot = 0:
wireFrame = 1
Repeat
If ExamineMouse()
MouseX = -MouseDeltaX()/20
MouseY = -MouseDeltaY()/20
EndIf
If 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_P)
If wireFrame
MaterialShadingMode(5, #PB_Material_Wireframe)
MaterialShadingMode(6, #PB_Material_Wireframe)
MaterialShadingMode(7, #PB_Material_Wireframe)
MaterialShadingMode(8, #PB_Material_Wireframe)
MaterialShadingMode(9, #PB_Material_Wireframe)
wireFrame ! 1
Else
MaterialShadingMode(5, #PB_Material_Solid)
MaterialShadingMode(6, #PB_Material_Solid)
MaterialShadingMode(7, #PB_Material_Solid)
MaterialShadingMode(8, #PB_Material_Solid)
MaterialShadingMode(9, #PB_Material_Solid)
wireFrame ! 1
EndIf
EndIf
EndIf
If KeyboardReleased(#PB_Key_Space)
rot ! 1
EndIf
If KeyboardPushed(#PB_Key_A) ; rotate right side
RotateEntity(2, 0,0.0,0.5,#PB_Relative)
ElseIf KeyboardPushed(#PB_Key_Q)
RotateEntity(2, 0,0.0,-0.5,#PB_Relative)
;;-------------------------------------------------
ElseIf KeyboardPushed(#PB_Key_W) ; rotate left side
RotateEntity(3, 0,0.0,0.5,#PB_Relative)
ElseIf KeyboardPushed(#PB_Key_S)
RotateEntity(3, 0,0.0,-0.5,#PB_Relative)
;;------------------------------------------------
ElseIf KeyboardPushed(#PB_Key_D) ; rotate north side
RotateEntity(4, 1,0.0,0,#PB_Relative)
ElseIf KeyboardPushed(#PB_Key_E)
RotateEntity(4, -1,0.0,0,#PB_Relative)
;----------------------------------------
ElseIf KeyboardPushed(#PB_Key_F) ; rotate top side
RotateEntity(5, 1,0.0,0,#PB_Relative)
ElseIf KeyboardPushed(#PB_Key_R)
RotateEntity(5, -1,0.0,0,#PB_Relative)
;----------------------------------------------
ElseIf KeyboardPushed(#PB_Key_T) ; rotate Front side
RotateEntity(6, 1,0.0,0,#PB_Relative)
ElseIf KeyboardPushed(#PB_Key_G)
RotateEntity(6, -1,0.0,0,#PB_Relative)
EndIf
;-------------------------------------------------------------
RotateCamera(#Camera, MouseY, MouseX, 0, #PB_Relative)
MoveCamera(#Camera, KeyX, 0, KeyY)
RotateEntity(1, 0.0,rot/10,0.0,#PB_Relative)
RenderWorld()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
Else
MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
End
