November 30, 2020, 01:42:19 AM

Author Topic: [bb] CameraMouseView by markcw [ 1+ years ago ]  (Read 533 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] CameraMouseView by markcw [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : CameraMouseView
Author : markcw
Posted : 1+ years ago

Description : I couldn't find any code here like this, so I thought I'd add this example. The drag code was the tricky bit, but it seems to be working nicely now.

This is the kind of camera control you get in Milkshape or UltimateUnwrap.

edit: i never liked the way this used a lot of globals, so i've updated it so it uses no globals and instead a bank to hold the "globals". i thought about types but decided this was better as it's simpler to transfer to a project this way.


Code :
Code: BlitzBasic
  1. ;CameraMouseView, on 10/10/06
  2.  
  3. Graphics3D 640,480,0,2
  4. SetBuffer BackBuffer()
  5.  
  6. camerapivot=CreatePivot() ;pivot
  7. camera=CreateCamera(camerapivot) ;camera on pivot
  8. camerabank=CreateBank(48) ;camera bank variables
  9.  
  10. light=CreateLight()
  11. RotateEntity light,90,0,0
  12.  
  13. cube=CreateCube()
  14.  
  15. While Not KeyHit(1)
  16.  UpdateWorld
  17.  RenderWorld
  18.  
  19.  CameraMouseView(camerapivot,camera,camerabank)
  20.  
  21.  Text 0,0,"Press LShift to Zoom, LCtrl to Drag"
  22.  
  23.  Flip
  24. Wend
  25.  
  26. Function CameraMouseView(camerapivot,camera,camerabank,speed#=0.05)
  27.  ;camerapivot,camera,camerabank=valid handles, speed#=speed of view
  28.  
  29.  Local movex,movey,camx#,camy#,camz#,pivx#,pivz#,angle#,xaxis#,zaxis#
  30.  
  31.  ;error messages for invalid handles
  32.  If camerapivot=0 Then RuntimeError "Pivot entity is zero!"
  33.  If camera=0 Then RuntimeError "Camera entity is zero!"
  34.  If camerabank=0 Then RuntimeError "Bank handle is zero!"
  35.  
  36.  ;set default values
  37.  If speed#<0.01 Then speed#=0.01 ;min speed
  38.  If PeekFloat(camerabank,36)=0 Then PokeFloat camerabank,36,-10 ;no camz
  39.  
  40.  ;update mouse variables
  41.  PokeInt camerabank,0,PeekInt(camerabank,8) ;lastx=msex
  42.  PokeInt camerabank,4,PeekInt(camerabank,16) ;lasty=msey
  43.  PokeInt camerabank,8,MouseX() ;msex
  44.  PokeInt camerabank,16,MouseY() ;msey
  45.  PokeInt camerabank,20,MouseX()-PeekInt(camerabank,0) ;movex=msex-lastx
  46.  PokeInt camerabank,24,MouseY()-PeekInt(camerabank,4) ;movey=msey-lasty
  47.  
  48.  ;fill in the position/rotation variables
  49.  movex=PeekInt(camerabank,20)
  50.  movey=PeekInt(camerabank,24)
  51.  camx#=PeekFloat(camerabank,28)
  52.  camy#=PeekFloat(camerabank,32)
  53.  camz#=PeekFloat(camerabank,36)
  54.  pivx#=PeekFloat(camerabank,40)
  55.  pivz#=PeekFloat(camerabank,44)
  56.  
  57.  If MouseDown(1) ;Left Mouse button
  58.   If KeyDown(29) ;Left Ctrl key, camera drag
  59.    angle#=camy# : xaxis#=speed# : zaxis#=speed# ;init xz axis
  60.    If angle#>90 And angle#<270 Then zaxis#=-zaxis# ;-z if 90..270
  61.    If angle#>180 Then angle#=360-angle# : xaxis#=-xaxis# ;-x if 180..360
  62.    If angle#>90 Then angle#=180-angle# ;reduce y angle to 90
  63.    angle#=angle#*0.011 ;y as fraction of 1, ie. 90->100
  64.    ;set mouse xy movements by y fraction and add to pivot xz positions
  65.    pivx#=pivx#-(movex*(1-angle#)*zaxis#)-(movey*angle#*xaxis#)
  66.    pivz#=pivz#-(movex*angle#*xaxis#)+(movey*(1-angle#)*zaxis#)
  67.   ElseIf KeyDown(42) ;Left Shift key, camera zoom
  68.    camz#=camz#-movey*speed#  ;-msey sets camera z mt
  69.    If camz#>-3 Then camz#=-3 ;limit zoom to 3mt
  70.   Else ;camera rotation
  71.    camx#=camx#+(movey*10*speed#) ;mousey sets camera x dg
  72.    If camx#>90 Then camx#=90   ;limit x to -90..90
  73.    If camx#<-90 Then camx#=-90
  74.    camy#=camy#-(movex*10*speed#) ;-mousex sets camera y dg
  75.    If camy#>359 Then camy#=0   ;limit y to 0..360
  76.    If camy#<0 Then camy#=359
  77.   EndIf
  78.  EndIf
  79.  
  80.  ;update position/rotation variables
  81.  PokeFloat camerabank,28,camx#
  82.  PokeFloat camerabank,32,camy#
  83.  PokeFloat camerabank,36,camz#
  84.  PokeFloat camerabank,40,pivx#
  85.  PokeFloat camerabank,44,pivz#
  86.  
  87.  ;move pivot, rotate and zoom camera
  88.  PositionEntity camerapivot,pivx#,0,pivz# ;drag
  89.  RotateEntity camerapivot,camx#,camy#,0 ;rotate
  90.  PositionEntity camera,0,0,camz# ;zoom
  91.  
  92. End Function


Comments :


markcw(Posted 1+ years ago)

 Here is the old code entry, just as a reminder.
Code: [Select]
;Rotate, Zoom and Drag Camera with Mouse, on 18/11/05, by muk

Graphics3D 640,480,16,2
SetBuffer BackBuffer()

Global camx#,camy#,camz#=-10,pivx#,pivz#,camspd#=0.02 ;camera
Global msex,msey,lastx,lasty,movex,movey ;mouse
Global pivot,camera,light ;entity

pivot=CreatePivot() ;pivot
camera=CreateCamera(pivot) ;camera with pivot parent

light=CreateLight() ;light
RotateEntity light,90,0,0

cube=CreateCube() ;cube

;Main Loop
While Not KeyDown(1)

 UpdateCamera()

RenderWorld

 Text 0,0,"Press LShift to Zoom"
 Text 0,16,"Press LCtrl to Drag"
 Text 0,48,"X dg:"+camx
 Text 0,64,"Y dg:"+camy
 Text 0,80,"Zoom:"+camz
 Text 0,96,"X mts:"+pivx
 Text 0,112,"Z mts:"+pivz

Flip
Wend
End

;Functions
Function UpdateCamera()

lastx=msex : lasty=msey ;get mouse movements
msex=MouseX() : msey=MouseY()
movex=msex-lastx : movey=msey-lasty

If MouseDown(1)
 If KeyDown(29) ;LCtrl key, camera drag
  yrot#=camy : xaxis#=camspd : zaxis#=camspd ;init xz axis
  If yrot>90 And yrot<270 Then zaxis=-zaxis     ;-zaxis if 90-270dg
  If yrot>180 Then yrot=360-yrot : xaxis=-xaxis ;-xaxis if 180-360dg
  If yrot>90 Then yrot=180-yrot ;reduce y rotation to 90dg
  yfr#=yrot*0.011 : ifr#=1-yfr  ;set y as fraction of 1 and get inverse
  mxpx#=movex*ifr*zaxis : mxpz#=movex*yfr*xaxis ;set mouse xy movements
  mypx#=movey*yfr*xaxis : mypz#=movey*ifr*zaxis ;by y fraction and axis
  pivx=pivx-mxpx-mypx : pivz=pivz-mxpz+mypz     ;add to pivot positions
 ElseIf KeyDown(42) ;LShift key, camera zoom
  camz=camz-movey*camspd  ;-mousey sets camera z position
  If camz>-3 Then camz=-3 ;limit z to 3-30mt
  If camz<-30 Then camz=-30
 Else ;Neither keys, camera rotation
  camx=camx+movey*10*camspd ;mousey sets camera x rotation
  If camx>90 Then camx=90   ;limit x to 0-90dg
  If camx<0 Then camx=0
  camy=camy-movex*10*camspd ;-mousex sets camera y rotation
  If camy>359 Then camy=0   ;wrap y to 0-359dg
  If camy<0 Then camy=359
 EndIf
EndIf

RotateEntity pivot,camx,camy,0   ;rotate camera
PositionEntity camera,0,0,camz   ;zoom camera
PositionEntity pivot,pivx,0,pivz ;drag camera

End Function


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal