the following code put the sphere from Sveinung code in the previous page to be used inside the example MP_Terrain.pb by Michael in MP3D Demos\Mesh folder. as a kind of a ufo or organic space entity to fly over the terrain at altitude of 500 over the terrain . I'm still searching for a suitable big terrain.
i have used another formula for deforming the sphere and a texture.
and it moves with keys, the right/Left keys makes the camera to turn around, my difficult task was to keep the sphere in front of the camera as if it is my car. the camera position begins from x=-714 z=-4308 so the angle from 0,0 to the camera will be 99.4, i want the sphere to be 10 units from the camera, so calculating the new coordinates of the sphere to be in front of the camera. look the picture

but i wonder when using MP_TurnCamera(camera, -0.32,0,0) for Left camera turning, i must use for the sphere an increment 0.76. ie a ratio 2.375 so the sphere will follow the camera exactly with a very small unbalance.
i can't see what is the reason, i was expecting to use 0.32 for both the camera turn and the sphere move. it may be a very simple geometry/algebra issue.
* the formula and the texture used are from 3Impact example.
to run the code save the program in the
MP3D Demos\Mesh folder and also save this texture to the same folder for the sphere texture.
Code: Select all
;original codes by Michael and Sveinung
MP_Graphics3D (640,480,0,1) ; Erstelle ein WindowsFenster #Window = 0
SetWindowTitle(0, "My first Terrain example, to Move use arrow keys up/down ,to Turn use arrow keys right/left")
Global amplitude.f = 0.10
Global frequency.f = 4.0
Global sp=mp_createsphere(40)
Global sp_o
camera=MP_CreateCamera() ; Kamera erstellen
MP_CameraSetRange(Camera, 0.1, 5000)
light=MP_CreateLight(2)
MP_LightSetColor (light, RGB(255,255,255))
MP_PositionEntity(light, 0,200,-5029)
MP_CreateSkyBox ("rhills","jpg",100)
Terrain = MP_LoadTerrain ("rhills_hmap.bmp", 64,64)
Texture = MP_LoadTexture("rhills_tmap.jpg")
MP_EntitySetTexture (Terrain,texture)
MP_EntitySetName(Terrain, "Terrain")
sp_o=MP_CopyEntity(sp)
MP_HideEntity(sp_o,1)
texture = MP_LoadTexture("marble.jpg")
MP_EntitySetTexture(sp, texture)
;MP_MaterialSpecularColor(texture,0,255,255,255,1)
MP_MaterialEmissiveColor (texture,0,255,255,255)
MP_MaterialAmbientColor (texture, 255, 128 , 255, 128) ;
MP_MaterialSpecularColor (texture, 255, 128 ,255, 128,40) ;
MP_VSync(1)
MP_Wireframe(0)
Global countvertices.l = mp_countvertices(sp)-1
Procedure ufo()
;warping a sphere
dtime.f=ElapsedMilliseconds()/800
offset.f
For i=0 To countvertices
;--------------------------------
vx.f=MP_VertexGetX(sp_o,i)
vy.f=MP_VertexGetY(sp_o,i)
vz.f=MP_VertexGetZ(sp_o,i)
;--------------------------------
offset = amplitude * Sin((dtime+MP_VertexGetX(sp_o,i))*frequency)
vx + offset
vy + offset
vz + offset
MP_VertexSetX(sp,i,vx)
MP_VertexSetY(sp,i,vy)
MP_VertexSetZ(sp,i,vz)
Next
EndProcedure
MP_PositionCamera(camera,-714,500,-4308)
;the beginning angle, press J to know the camera position at any time
angle.f = Degree(ATan(714/4308)+Radian(90)) ; 99.41 degrees
;Debug(StrF(angle))
;the sphere is 10 units away from the camera
;the sphere position differences in x, z coordinates from the camera x, z
xDelta.f = Cos(Radian(angle)) * 10 : zDelta.f = Sin(Radian(angle)) * 10
While Not MP_KeyDown(#PB_Key_Escape) And Not WindowEvent() = #PB_Event_CloseWindow; Esc abfrage oder Windows Schliessen
ufo() ; call the warped sphere
;Driving the sphere
If MP_KeyDown(#PB_Key_Up)
rotY+3 ;sphere rotation
MP_MoveCamera(camera,0,0,15)
ElseIf MP_KeyDown(#PB_Key_Down)
rotY-3 ;sphere rotation
MP_MoveCamera(camera,0,0,-15)
EndIf
If MP_KeyDown(#PB_Key_Right)
rotX+3
angle.f - 0.76
xDelta.f = Cos(Radian(angle)) * 10 : zDelta.f = Sin(Radian(angle)) * 10
rotX-3 ;sphere rotation
MP_TurnCamera(camera, 0.32,0,0)
ElseIf MP_KeyDown(#PB_Key_Left)
angle.f + 0.76
xDelta.f = Cos(Radian(angle)) * 10 : zDelta.f = Sin(Radian(angle)) * 10
rotX-3 ;sphere rotation
MP_TurnCamera(camera, -0.32,0,0)
EndIf
MP_RotateEntity(sp, rotX, rotY, 0 ) ;sphere rotation
MP_EntitySetNormals(sp)
;;pppppppppppppppppppppppppppppppppppppppppppppppppppppp
x.f = MP_CameraGetX (camera)
z.f = MP_CameraGetZ (camera)
yyyy.f = MP_TerrainGetY (Terrain,x,0,z) + 500
MP_PositionCamera(camera, x, yyyy , z)
MP_PositionEntity (sp,x+xDelta,MP_TerrainGetY(Terrain,x,0,z)+500,z+zDelta)
MP_PositionEntity(light, x-200,200,z)
MouseX = -(MP_MouseDeltaX()/10)
MouseY = MP_MouseDeltaY()/10
If MP_KeyHit(#PB_Key_J); to know the current camera position
Debug("x = "+StrF(x))
Debug("z = "+StrF(z))
Debug("Altitude = "+ StrF(yyyy))
Debug(" ")
EndIf
MP_TurnCamera(camera, MouseX, 0, MouseY);, #PB_Relative)
MP_RenderWorld() ; Erstelle die Welt
MP_Flip () ; Stelle Sie dar
Wend