Hi all. I picked up DBPro about a month ago and have been lurking for just as long. I have a background with basic (nothing flashy), so I haven't had to fight a learning curve.
Anyway, I was looking around for camera snippets to put together a 3rd person, switch between selected players sort of thing. The things I found didn't quite do what I needed (though the 'match camera to player then move camera back' trick is ingenious), so I wrote some code to make it work for me (inspired by 300happy's snippet from the codebase [I think]).
My apologies that there are no remarks; I did try to make the variable names as clear as possible though.
sync on : backdrop on : color backdrop 000000
type position
x as float
y as float
z as float
endtype
type movement
old as position
cur as position
new as position
endtype
type sphere
location as position
full_radius as float
small_radius as float
height_angle as float
rotate_angle as float
move_rotate as float
move_height as float
move_radius as float
change_rotate as float
change_height as float
change_radius as float
focus as integer
endtype
camera as sphere
target as movement
target_distance as position
pickobj as integer
mouse_click_old as integer
mouse_click_new as integer
orb as integer
make matrix 1, 40,40,4,4
position matrix 1, -20,-1,-20
set matrix 1, 1,0,0,0,0,0,0
black = rgb(0,0,0)
white = rgb(255,255,255)
camera.full_radius = 30
camera.height_angle = 70
mouse_click_old=0
mouse_click_new=0
camera.focus = 1
for orb=1 to 10
make object sphere orb, rnd(2)+1
position object orb, rnd(40)-20,rnd(40)-20,rnd(40)-20
color object orb, rgb(rnd(220)+30,rnd(220)+30,rnd(220)+30)
next orb
target.cur.x=object position x(camera.focus)
target.cur.y=object position y(camera.focus)
target.cur.z=object position z(camera.focus)
target.old.x=object position x(camera.focus)
target.old.y=object position y(camera.focus)
target.old.z=object position z(camera.focus)
do
mouse_click_new=mouseclick()
if mouse_click_new<mouse_click_old then camera.focus=pick object(mousex(),mousey(),1,10)
mouse_click_old=mouse_click_new
if camera.focus>0
if inkey$()="w" then position object camera.focus,object position x(camera.focus),object position y(camera.focus),object position z(camera.focus)+0.05
if inkey$()="a" then position object camera.focus,object position x(camera.focus)-0.05,object position y(camera.focus),object position z(camera.focus)
if inkey$()="s" then position object camera.focus,object position x(camera.focus),object position y(camera.focus),object position z(camera.focus)-0.05
if inkey$()="d" then position object camera.focus,object position x(camera.focus)+0.05,object position y(camera.focus),object position z(camera.focus)
if inkey$()="1" then position object camera.focus,object position x(camera.focus),object position y(camera.focus)+0.05,object position z(camera.focus)
if inkey$()="2" then position object camera.focus,object position x(camera.focus),object position y(camera.focus)-0.05,object position z(camera.focus)
endif
if camera.focus>0
target.new.x = object position x(camera.focus)
target.new.y = object position y(camera.focus)
target.new.z = object position z(camera.focus)
target_distance.x = (target.cur.x - target.new.x)/200
target_distance.y = (target.cur.y - target.new.y)/200
target_distance.z = (target.cur.z - target.new.z)/200
endif
if target.cur.x <> target.new.x or target.cur.y <> target.new.y or target.cur.z <> target.new.z
target.old.x = target.cur.x
target.old.y = target.cur.y
target.old.z = target.cur.z
target.cur.x = target.old.x - target_distance.x
target.cur.y = target.old.y - target_distance.y
target.cur.z = target.old.z - target_distance.z
else
if target.old.x <> target.new.x
target.old.x = target.new.x
endif
if target.old.y <> target.new.y
target.old.y = target.new.y
endif
if target.old.z <> target.new.z
target.old.z = target.new.z
endif
endif
if target.old.x - target.new.x<1 or target.old.y - target.new.y<1 or target.old.z - target.new.z<1
target_distance.x = 0
target_distance.y = 0
target_distance.z = 0
endif
camera.move_rotate = leftkey() + (rightkey() * -1)
camera.rotate_angle = wrapvalue(camera.rotate_angle - (camera.move_rotate/3))
camera.move_radius = 0
if inkey$() = "+" then camera.move_radius = 0.1
if inkey$() = "-" then camera.move_radius = -0.1
camera.full_radius = camera.full_radius + (camera.move_radius/1.5)
if camera.full_radius > 100 then camera.full_radius = 100
if camera.full_radius < 3 then camera.full_radius = 3
camera.move_height=upkey() + (downkey()*-1)
camera.height_angle = camera.height_angle + (camera.move_height/5)
if camera.height_angle >= 170 then camera.height_angle = 170
if camera.height_angle < 10 then camera.height_angle = 10
camera.small_radius = sin(camera.height_angle) * camera.full_radius
camera.location.y = target.cur.y + cos(camera.height_angle) * camera.full_radius
camera.location.x = target.cur.x + cos(camera.rotate_angle) * (camera.small_radius)
camera.location.z = target.cur.z + sin(camera.rotate_angle) * (camera.small_radius)
position camera camera.location.x, camera.location.y, camera.location.z
point camera target.cur.x,target.cur.y,target.cur.z
ink white, black
text 10,20, "height angle = "+str$(int(camera.height_angle))
text 10,35, "rotation angle = "+str$(int(camera.rotate_angle))
text 10,50, "camera radius = "+str$(int(camera.full_radius))
text screen width()-120,20, "camera x = "+str$(int(camera.location.x))
text screen width()-120,35, "camera y = "+str$(int(camera.location.y))
text screen width()-120,50, "camera z = "+str$(int(camera.location.z))
text screen width()-130,screen height()-50, "Press X to eXit"
text screen width()-230,screen height()-35, "Move sphere with W,A,S,D,1,2"
text screen width()-320,screen height()-20, "Move camera with up,down,left,right,+,-"
text 10,screen height()-20,"Frames/Second = "+str$(screen fps())
if inkey$()="x" then end
if inkey$()="X" then end
sync
loop
I was thinking I would put this in the CodeBase or Snippets and would love to get feedback on how to clean/improve/optimize this stuff. Also, I'm pretty proud of my self, as this is my first DBPro project
Anyway, nice to meet you all.