flattening a sphere to make a circular flat plane
Posted: Tue Apr 28, 2015 2:04 pm
to continue this thread: http://purebasic.fr/english/viewtopic.p ... 2&start=60 so the project can be seen.
to flatten a sphere we copy only its vertices x,z coordinates
xa.f=MeshData(c)\x
zb.f=MeshData(c)\z
set the MeshData(c)\y to some formula not what is necessary for making the sphere so we get the 2D surface with the waves represented by MeshData(c)\y
if setting MeshData(c)\y=0 then we get perfect disk, press 'V' to save disk02.mesh .
uncomment line 125
TransformMesh(0, 0, 0, 0, 1, 0, 1, 0, 0, 0)
;SaveMesh(0,"disk01.mesh")
to save a flat disk before entering the repeat loop. check it with the Bananenfreak model viewer attached below for its convenience (not perfect but usable: may needs some fine tunning)
i have used the DK_PETER suggestion for the waves formula suitable to trap the balls and cubes:
MeshData(c)\y=yy+Cos(dtime + (Sqr(Pow(0.7*xa,2)+Pow(0.4*zb,2))))*50
but primarily giving the object a static geometry so the ball can go between its bumps. and if you want uncomment line EntityPhysicBody(0, #PB_Entity_ConvexHullBody , 1, 1, 1) so it will fall down and can be raised up if we press 'space' for make the bridge lift it
there are still several questions to answer and investigate.
Edit: i forgot to say that since this example use the Bridge.pb demo from the PureBasic\Example\3D for its convenience you need only to look at this subject related code from line 113: ;the flatten sphere + procedure UpdateMatrix. very short and simple code.
to show the wireframe press 'W', for solidframe press 'S'
Bananenfreak model viewer (easy to use for viewing the .mesh files but needs fine tunning, used without his permission)
to flatten a sphere we copy only its vertices x,z coordinates
xa.f=MeshData(c)\x
zb.f=MeshData(c)\z
set the MeshData(c)\y to some formula not what is necessary for making the sphere so we get the 2D surface with the waves represented by MeshData(c)\y
if setting MeshData(c)\y=0 then we get perfect disk, press 'V' to save disk02.mesh .
uncomment line 125
TransformMesh(0, 0, 0, 0, 1, 0, 1, 0, 0, 0)
;SaveMesh(0,"disk01.mesh")
to save a flat disk before entering the repeat loop. check it with the Bananenfreak model viewer attached below for its convenience (not perfect but usable: may needs some fine tunning)
i have used the DK_PETER suggestion for the waves formula suitable to trap the balls and cubes:
MeshData(c)\y=yy+Cos(dtime + (Sqr(Pow(0.7*xa,2)+Pow(0.4*zb,2))))*50
but primarily giving the object a static geometry so the ball can go between its bumps. and if you want uncomment line EntityPhysicBody(0, #PB_Entity_ConvexHullBody , 1, 1, 1) so it will fall down and can be raised up if we press 'space' for make the bridge lift it
there are still several questions to answer and investigate.
Edit: i forgot to say that since this example use the Bridge.pb demo from the PureBasic\Example\3D for its convenience you need only to look at this subject related code from line 113: ;the flatten sphere + procedure UpdateMatrix. very short and simple code.
to show the wireframe press 'W', for solidframe press 'S'
Code: Select all
;
; ------------------------------------------------------------
;
; PureBasic - PointJoint (Bridge)
;
; (c) Fantaisie Software
;
; ------------------------------------------------------------
;
IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"
#CameraSpeed = 1
#NbPlanks = 30
#NbX=30
#NbZ=30
Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)
Global flag=0
Declare UpdateMatrix()
Define.f KeyX, KeyY, MouseX, MouseY
Dim Plank(#NbPlanks)
If InitEngine3D()
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts",#PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/desert.zip", #PB_3DArchive_Zip)
Parse3DScripts()
InitSprite()
InitKeyboard()
InitMouse()
If Screen3DRequester()
WorldShadows(#PB_Shadow_Modulative, -1, RGB(127, 127, 127))
;Materials
;
CreateMaterial(0, LoadTexture(0, "Wood.jpg"))
GetScriptMaterial(1, "SphereMap/SphereMappedRustySteel")
CreateMaterial(2, LoadTexture(2, "Dirt.jpg"))
GetScriptMaterial(3, "Scene/GroundBlend")
; Ground
;
CreatePlane(3, 500, 500, 5, 5, 5, 5)
CreateEntity(3,MeshID(3),MaterialID(3), 0, -50, 0)
EntityRenderMode(3, 0)
EntityPhysicBody(3, #PB_Entity_BoxBody, 0, 1, 1)
; Bridge
CreateCube(1, 1.0)
For i = 1 To #NbPlanks
Plank(i)=CreateEntity(#PB_Any, MeshID(1), MaterialID(0))
ScaleEntity(Plank(i), 2.8, 0.8, 20)
MoveEntity(Plank(i), i * 3, 0, 0, #PB_Absolute)
EntityPhysicBody(Plank(i), #PB_Entity_BoxBody, 1.0)
Next i
Pas.f = 1.5
PointJoint(#PB_Any, EntityID(Plank(1)), -Pas, 0, -5)
For i= 1 To #NbPlanks-2
Joint=PointJoint(#PB_Any, EntityID(Plank(i+1)), -Pas, 0, -5, EntityID(Plank(i)), Pas, 0, -5)
Next i
PointJoint(#PB_Any, EntityID(Plank(#NbPlanks)), Pas, 0, -5)
PointJoint(#PB_Any, EntityID(Plank(#NbPlanks-1)), Pas, 0, -5, EntityID(Plank(#NbPlanks)), -Pas, 0, -5)
PointJoint(#PB_Any, EntityID(Plank(1)), -Pas, 0, 5)
For i= 1 To #NbPlanks-2
Joint=PointJoint(#PB_Any, EntityID(Plank(i+1)), -Pas, 0, 5, EntityID(Plank(i)), Pas, 0, 5)
Next i
PointJoint(#PB_Any, EntityID(Plank(#NbPlanks)), Pas, 0, 5)
toto=PointJoint(#PB_Any, EntityID(Plank(#NbPlanks-1)), Pas, 0, 5, EntityID(Plank(#NbPlanks)), -Pas, 0, 5)
; Objects
;
CreateSphere(2, 3, 30, 30)
C = Plank(1)
For i = 1 To #NbPlanks/2
Perso = CreateEntity(#PB_Any, MeshID(2), MaterialID(1), EntityX(C) +i * 5, EntityY(C)+ i * 4, EntityZ(C))
EntityPhysicBody(Perso, #PB_Entity_SphereBody, 1.0, 0.3, 0.5)
Next i
For i = 1 To #NbPlanks/2
Perso = CreateEntity(#PB_Any, MeshID(1), MaterialID(2), EntityX(C) +i * 5, EntityY(C)+ i * 6, EntityZ(C))
ScaleEntity(Perso, 3, 3, 3)
EntityPhysicBody(Perso, #PB_Entity_BoxBody, 1.0)
Next i
; Camera
;
CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0, -20, 60, -140, #PB_Absolute)
CameraLookAt(0, EntityX(C) + 45, EntityY(C) + 10, EntityZ(C))
; Skybox
;
SkyBox("desert07.jpg")
; Light
;
CreateLight(0, RGB(255, 255, 255), 100, 800, -500)
AmbientColor(RGB(255, 255, 255))
Plank = 1
;the flatten sphere
CreateMaterial(4, LoadTexture(4, "MRAMOR6X6.jpg"))
;MaterialShadingMode(4, #PB_Material_Wireframe)
CreateSphere(0,20,30,30)
CreateEntity(0, MeshID(0), MaterialID(4), 70,10,0)
ScaleEntity(0, 3, 0.1, 3)
;EntityPhysicBody(0, #PB_Entity_ConvexHullBody , 1, 1, 1)
EntityPhysicBody(0, #PB_Entity_StaticBody , 1, 1, 1)
;TransformMesh(#Mesh, x, y, z, ScaleX, ScaleY, ScaleZ, RotateX, RotateY, RotateZ [, SubMesh])
TransformMesh(0, 0, 0, 0, 1, 0, 1, 0, 0, 0)
;SaveMesh(0,"disk01.mesh") ; will save a flattened sphere (disk)
GetMeshData(0,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(0, 0)-1)
;GetMeshData(0,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(0, 0)-1)
Repeat
Screen3DEvents()
If ExamineMouse()
MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
EndIf
If ExamineKeyboard()
If KeyboardReleased(#PB_Key_W)
MaterialShadingMode(4, #PB_Material_Wireframe)
ElseIf KeyboardReleased(#PB_Key_S)
MaterialShadingMode(4, #PB_Material_Solid)
EndIf
If KeyboardReleased(#PB_Key_V)
SaveMesh(0, "disk02.mesh")
EndIf
If KeyboardPushed(#PB_Key_Space)
ApplyEntityImpulse(Plank(#NbPlanks/2), 0, 9, 0)
EndIf
If KeyboardReleased(#PB_Key_Return)
If Plank <= #NbPlanks
DisableEntityBody(Plank(Plank), 0)
FreeEntityJoints(Plank(Plank))
Plank + 1
EndIf
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
EndIf
EndIf
MoveCamera (0, KeyX, 0, KeyY)
RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
RotateEntity(0, 0,0.2,0, #PB_Relative)
; Waves
UpdateMatrix()
RenderWorld()
Screen3DStats()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
EndIf
Else
MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
End
Procedure UpdateMatrix()
dtime.f=ElapsedMilliseconds()/600 ; reduce 600 to get speedier animation
For b=0 To #Nbz
For a=0 To #NbX
xa.f=MeshData(c)\x
zb.f=MeshData(c)\z
;yy.f= MeshData(c)\y
;new y vertex coordinates
;MeshData(c)\y=yy+Cos(dtime + (Sqr(Pow(0.5*xa,2)+Pow(0.5*zb,2))))*20
MeshData(c)\y=yy+Cos(dtime + (Sqr(Pow(0.7*xa,2)+Pow(0.4*zb,2))))*50
;MeshData(c)\y=0 ; uncomment to flatten the sphere completely
c + 1 ; vertex number
Next a
Next b
SetMeshData(0,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_Normal, 0, MeshVertexCount(0, 0)-1)
;EntityPhysicBody(0, #PB_Entity_ConvexHullBody , 1, 1, 1)
EntityPhysicBody(0, #PB_Entity_StaticBody , 1, 1, 1)
EndProcedureCode: Select all
EnableExplicit
Define.f KeyX, KeyY, MouseX, MouseY
Define nx.f, nz.f, Boost.f = 1, Yaw.f, Pitch.f
Define.i Quit, boden, mesh
Define.s path
Define meshsize.f
#kam_0 = 0
#window = 0
#plane = 0
#planent = 1
If InitEngine3D()
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
;Parse3DScripts()
InitSprite()
InitKeyboard()
InitMouse()
ExamineDesktops()
OpenWindow(#window, 0, 0, DesktopWidth(0), DesktopHeight(0), "3D-Template", #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(#window), 10, 10, DesktopWidth(0), DesktopHeight(0), 0, 10, 10, #PB_Screen_SmartSynchronization)
Parse3DScripts()
WorldShadows(#PB_Shadow_TextureAdditive, 200, RGB(255 * 0.2, 255 * 0.2, 255 * 0.2), 4096)
CreateLight(33,RGB(255,255,255),-100,40,30)
AmbientColor(RGB(255,255,255))
;AmbientColor(RGB(255 * 0.2, 255 * 0.2, 255 * 0.2))
;- Ground
;
CreateMaterial(0, LoadTexture(0, "snow_1024.jpg"))
CreatePlane(#plane, 500, 500, 40, 40, 15, 15)
CreateEntity(#plane,MeshID(#plane),MaterialID(0))
EntityRenderMode(#plane, 0)
;-Camera
CreateCamera(#kam_0, 0, 0, 100, 100)
MoveCamera(#kam_0, 0, 25, -10, #PB_Absolute)
CameraLookAt(#kam_0, 0, 0, 20)
CameraRange (#kam_0, 2, 5000)
CameraFOV (#kam_0, 90)
;CameraBackColor(#kam_0, RGB(0, 0, 0))
path = OpenFileRequester("Choose file to load", "", "OGRE-Mesh (*.mesh)|*.mesh", 0, 0)
If Not Add3DArchive(GetPathPart(path), #PB_3DArchive_FileSystem)
MessageRequester("Error", "Path can“t be solved!" + Chr(10) + GetPathPart(path))
EndIf
CreateMaterial(1, LoadTexture(1, "wood.jpg"))
MaterialCullingMode(1, #PB_Material_NoCulling)
MaterialShadingMode(1, #PB_Material_Wireframe)
mesh = LoadMesh(#PB_Any, GetFilePart(path))
meshsize.f = MeshRadius(mesh)
;Debug "MeshRadius = " + Str(meshsize)
CreateEntity(23,MeshID(mesh), MaterialID(1),0,15,0)
ScaleEntity(23,4/meshsize,4/meshsize,4/meshsize)
If Not mesh
MessageRequester("Error", "This file isn't a mesh!" + Chr(10) + GetFilePart(path))
EndIf
Repeat
If ExamineMouse()
Yaw = -MouseDeltaX() * 0.05
Pitch = -MouseDeltaY() * 0.05
EndIf
If ExamineKeyboard()
If KeyboardPushed(#PB_Key_Up)
MoveCamera(0, 0, 0, -0.3 * Boost)
ElseIf KeyboardPushed(#PB_Key_Down)
MoveCamera(0, 0, 0, 0.3 * Boost)
EndIf
If KeyboardPushed(#PB_Key_Left)
MoveCamera(0, -0.3 * Boost, 0, 0)
ElseIf KeyboardPushed(#PB_Key_Right)
MoveCamera(0, 0.3 * Boost, 0, 0)
EndIf
EndIf
RotateCamera(0, Pitch, Yaw, 0, #PB_Relative)
RotateEntity(23,0,1,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