Page 1 of 1

Terrain + camera rotate question

Posted: Tue Mar 06, 2007 9:19 pm
by DeXtr0
Hello everybody,

I've recently started by using PureBasic so i'm still experimenting with it and until now, I like very much.
However, I got some issues to bypass and one of them is related to terrain & camera rotation.

This is what I have coded until now:

Code: Select all

;
; ------------------------------------------------------------
;
;   DeXtr0's Terrain Test
;
;    based on examples purebasic
; ------------------------------------------------------------
UsePNGImageDecoder()

TerrainHeight = 30
TerrainZPos = -10
YPos = 0
XRotateSpeed = 3
YRotateSpeed = 0

If InitEngine3D()

  InitSprite()
  InitSprite3D() 
  
  Add3DArchive("Data\"          , #PB_3DArchive_FileSystem)
  
  OpenScreen(800,600,32,"DeXtr0") 

  CatchSprite(10, ?Pic_Logo, 0) 
  
  ClearScreen(255,255,255)   

  CreateMaterial  (0, LoadTexture(0, "dex_terrain_texture.jpg"))
  AddMaterialLayer(0, LoadTexture(1, "Terrain_Detail.jpg"), 1)
  
  CreateTerrain("terrain.png", MaterialID(0), 4, 0.6, 4, 4)

  ;------------------------------------------------
  ; Create camera , width = 300, heigh = 300
  ;------------------------------------------------         
  CreateCamera(0, 0, 0, 100, 100)

  ;------------------------------------------------
  ; Set initial camera position
  ;------------------------------------------------         
  ;CameraLocate(0, 128, 25, 128)
  CameraLocate(0, 128, 30, 400)
  MoveCamera  (0, 0, TerrainHeight, TerrainZPos)

  Repeat

    ;------------------------------------------------
    ; Rotate camera
    ;------------------------------------------------           
    RotateCamera(0, XRotateSpeed, YPos, YRotateSpeed)
    
    RenderWorld()
    
    
    ;------------------------------------------------
    ; Show logo 
    ;------------------------------------------------           
    DisplayTransparentSprite(10,90,-50)
    
    FlipBuffers()
    
    If GetAsyncKeyState_(#VK_ESCAPE) 
      Break 
    EndIf 
       
  ForEver
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf


DataSection 

Pic_Logo:  IncludeBinary "Data/dextro24gc.png"
It might be very easy for most of you all but for me it's still hard since I'm not experienced :?

If you should run this code the terrain is rotation and the logo is shown above, which is correct.
However the rotation effect gives me the feeling that I'm in the center of the rotation (hard to explain?).
The effect I'm trying to code is that I see the terrain rotating but further away in the depth, so I don't want to be in the center of the rotation but the center of the rotation should be further away into the screen (I think Z-axis but i'm not sure :oops: )

Is anybody able to help me a hand with this one ?

The whole source, incl. pictures can be downloaded here:
http://www.glftpdsitemanager.com/downlo ... ources.zip

All help is appreciated...

Regards,
DeXtr0

Posted: Tue Mar 13, 2007 1:06 pm
by DeXtr0
Hey All,

is there really nobody that can help me out with this one ?

All help is welcome..

Thanks in advance,
DeXtr0

Posted: Tue Mar 13, 2007 1:10 pm
by Fluid Byte
* ACK *

Posted: Tue Mar 13, 2007 1:58 pm
by Kaeru Gaman
sorry... I read this thread last week but forgot it later.

the problem is that you'll have to calculate the vectors by yourself if you want to rotate excentered.

you need the point where the rotation should center.
then you calculate the vector to the cam, then you calculate the rotation of the vector,
then you position the cam on the new position and direction.

there is no native command to do such.

Posted: Tue Mar 13, 2007 7:45 pm
by DeXtr0
Hey Kearu,

thank you for the tip. Now I can go on with reading and looking up some examples..

Thanks a lot...

Regards,
DeXtr0

Posted: Tue Mar 13, 2007 8:53 pm
by Kale
This is a trick for doing things simply (and natively!):

Code: Select all


Enumeration
	#TEXTURE_GLOBAL
	#TEXTURE_DETAIL
	#MATERIAL_TERRAIN
	#CAMERA_ONE
EndEnumeration

;Set the width, height and bit depth of the screen
;Abbreviated variables are used here due to page width constraints :(
Global ScrW.l = 1280
Global ScrH.l = 1024
Global ScrD.l = 32

;Other global variables
Global Quit.b = #False

;Simple error checking procedure
Procedure HandleError(Result.l, Text.s)
	If Result = 0
		MessageRequester("Error", Text, #PB_MessageRequester_Ok)
		End
	EndIf
EndProcedure

;Initialize environment
HandleError(InitEngine3D(), "InitEngine3D() command failed.")
HandleError(InitSprite(), "InitSprite() command failed.")
HandleError(OpenScreen(ScrW, ScrH, ScrD, ""), "Could not open screen.")
HandleError(InitKeyboard(), "InitKeyboard() command failed.")

;Set 3D Archive
Add3DArchive("Data\", #PB_3DArchive_FileSystem)

;Create Terrain
HandleError(LoadTexture(#TEXTURE_GLOBAL, "Global.png"), "Can't load texture")
HandleError(LoadTexture(#TEXTURE_DETAIL, "Detail.png"), "Can't load texture")
CreateMaterial(#MATERIAL_TERRAIN, TextureID(#TEXTURE_GLOBAL))
AddMaterialLayer(#MATERIAL_TERRAIN,TextureID(#TEXTURE_DETAIL),#PB_Material_Add)
CreateTerrain("Terrain.png", MaterialID(#MATERIAL_TERRAIN), 1, 2, 1)

;Create Viewport and 3D Camera
CreateCamera(#CAMERA_ONE, 0, 0, 100, 100)

;rotate the camera down a little.
RotateCamera(#CAMERA_ONE, 0, -20, 0)

;Main loop
Repeat

	;move the camera to the centre of the rotation
	CameraLocate(#CAMERA_ONE, 545, 100, 280)

	;rotate the camera
	RotateCamera(#CAMERA_ONE, 0.5, 0, 0)

	;move the camera back a bit, so it's not still in the center
	MoveCamera(#CAMERA_ONE, 0, 0, 300)

	RenderWorld()
	FlipBuffers()

	ExamineKeyboard()
	If KeyboardReleased(#PB_Key_Escape)
		Quit = #True
	EndIf

Until Quit = #True
End
basically you move the camera to the center, then rotate it, then move it away. Do this once per frame and there you have a rotating cam. :)

Really though things like this are best suited to trigonometry (sin, cos), click the banner in my sig and download the code examples. Look at number 221a.

Posted: Tue Mar 13, 2007 9:15 pm
by DeXtr0
Hey Kale,

Thanks a lot for your clear answer & example.
However I use PB 3.94.

Could it be that the source 221 does not run in 3.94 ? I get a lot of compilation errors..

All help is welcome..

Thanks 1000x
DeXtr0

Posted: Tue Mar 13, 2007 10:32 pm
by Kaeru Gaman
I strongly recommend updating on 4.02


@Kale

interesting workaround.

three transformations (move, rotate, move)
instead of two transformations and a complex calculation.

maybe you method is even more performant than my approach?

Posted: Tue Mar 13, 2007 11:09 pm
by Kale
DeXtr0 wrote:Could it be that the source 221 does not run in 3.94 ? I get a lot of compilation errors..
All code on that page is for PureBasic v4. I would recommend you download the new version. :)

Posted: Tue May 22, 2007 12:55 pm
by Razon
I can advice you the CameraLookAt command, and move the camera position around this point with Sin, Cos