3d camera scrolling around an object and turret pointing code by Philip22nd Feb 2004 7:49
|
---|
Summary The attached code came out of the following thread: http://darkbasicpro.thegamecreators.com/?m=forum_view&t=25644&b=7 Its code produced by Final Epsilon and myself. Basical Description Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com Remstart ****************************************** Turret rotation code by Philip and Final Epsilon ****************************************** Remend sync on sync rate 60 Rem PJY - lets just adjust this code for screen resolution Rem PJY - otherwise my 1024 x 768 makes the mouse camera scroll go mental screen_width = screen width() screen_height = screen height() Rem PJY - make a cube to act as the turret make object cube 1, 50 position object 1, 0, 0, 0 color object 1, rgb(200, 200, 200) set object light 1, 1 set object ambient 1, 0 Rem PJY - make a little sphere to act as the turret's aiming device make object sphere 2, 10, 10, 10 position object 2, 0, 30, 30 color object 2, rgb(200, 150, 100) set object light 2, 1 set object ambient 2, 0 Rem PJY - end insert sphere. Rem PJY - make a red box to act as the world's Y axis make object box 3, 5, 400, 5 position object 3, 0, 0, 0 Rem PJY - colour Y axis red color object 1, rgb(212,061,026) set object light 3, 1 set object ambient 3, 1 Rem PJY - make a blue box to act as the world's X axis make object box 4, 400, 5, 5 position object 4, 0, 0, 0 Rem PJY - colour X axis blue color object 4, rgb(000,000,255) set object light 4, 1 set object ambient 4, 1 Rem PJY - make a green box to act as the world's Z axis make object box 5, 5, 5, 400 position object 5, 0, 0, 0 Rem PJY - colour Z axis green color object 5, rgb(000,255,000) set object light 5, 1 set object ambient 5, 0 Rem PJY - make all the text red ink rgb(212, 061, 026), rgb(212, 061, 026) Rem PJY - initialise camera movement code x# = 0 y# = 0 z# = -400 position camera x#, y#, z# point camera 0, 0, 0, 0 bearing# = 0 azimuth# = 0 distance# = 300 mousedistancez# = 1000 Rem PJY - make a randomized matrix to represent "ground" make matrix 1, 1000, 1000, 50, 50 position matrix 1, -500, 0, -500 randomize matrix 1, 20 update matrix 1 Rem PJY - make some vectors - they are always handy! one = 1 two = 2 three = 3 four = 4 five = 5 six = 6 null = make vector3(one) null = make vector3(two) null = make vector3(three) null = make vector3(four) null = make vector3(five) null = make vector3(six) do Rem PJY - reset camera scroll variables mouse_x# = 0 mouse_y# = 0 Rem PJY - Camera control (and scrolling) code if mousey() <= 50 then mouse_y# = -2 if mousex() <= 50 then mouse_x# = -2 if mousey() >= (screen_height - 50) then mouse_y# = 2 if mousex() >= (screen_width - 50) then mouse_x# = 2 bearing# = wrapvalue(bearing# - mouse_x#) azimuth# = wrapvalue(azimuth# - mouse_y#) x# = distance# * sin(azimuth#) * cos(bearing#) z# = distance# * sin(azimuth#) * sin(bearing#) y# = distance# * cos(azimuth#) position camera x#, y#, z# point camera 0, 0, 0, 0 Rem PJY - display some useful variables set cursor 0, 0 print "Up key to make target object move away from camera" print "Down key to make target object move towards the camera" print "Click to place object at new world X Y Z coords" print "Blue is X axis, Red (ok, grey) is Y axis, Green is Z axis" print "" print "Camera bearing: ";bearing# print "Camera azimuth: ";azimuth# print "Camera distance: ";distance# print "Mouse X: ";mousex() print "Mouse Y: ";mousey() Rem PJY - code to pick the aiming object and move it pick_test = pick object(mousex(), mousey(), 2, 2) set vector3 one, object position x(2), object position y(2), object position z(2) set vector3 two, camera position x(0), camera position y(0), camera position z(0) subtract vector3 three, one, two length# = length vector3(three) print "Obj 2 x: ";object position x(2);" y: ";object position y(2);" z: ";object position z(2) print "Camera x: ";camera position x(0);" y: ";camera position y(0);" z: ";camera position z(0) print "Camera to Target Obj vector x: ";x vector3(three);" y: ";y vector3(three);" z: ";z vector3(three) Rem PJY - if upkey is pressed move the aiming object away from the camera if upkey() = 1 AND length# > 10 AND length# < 800 temp_x# = x vector3(three) * 1.1 temp_y# = y vector3(three) * 1.1 temp_z# = z vector3(three) * 1.1 set vector3 five, temp_x#, temp_y#, temp_z# add vector3 four, two, five endif Rem PJY - if downkey is pressed move the aiming object towards the camera if downkey() = 1 AND length# > 50 AND length# <= 1000 temp_x# = x vector3(three) * 0.9 temp_y# = y vector3(three) * 0.9 temp_z# = z vector3(three) * 0.9 set vector3 five, temp_x#, temp_y#, temp_z# add vector3 four, two, five endif Rem PJY - if mouseclick then place aiming object in 3d world if mouseclick() = 1 pick screen mousex(), mousey(), length# pick_x# = get pick vector x() pick_y# = get pick vector y() pick_z# = get pick vector z() set vector3 five, pick_x#, pick_y#, pick_z# add vector3 four, two, five endif Rem PJY - position aiming object at the new coordinates position object 2, x vector3(four), y vector3(four), z vector3(four) Rem PJY - calculate new orientation of cube Rem PJY - first get the azimuth angle set vector3 six, object position x(1), object position y(1), object position z(1) subtract vector3 three, six, one normalize vector3 three, three set vector3 two, 0, 1, 0 angle_y# = acos(dot product vector3(three, two)) print "Angle Y: ";angle_y# Rem PJY - secondly get the rotation angle temp_x1# = object position x(1) temp_x2# = object position x(2) temp_z1# = object position z(1) temp_z2# = object position z(2) temp_x# = temp_x1# - temp_x2# temp_z# = temp_z1# - temp_z2# ang1# = atanfull(temp_x#, temp_z#) if ang1# < 0 then ang1# = 360.0 + ang1# Rem PJY - finally rotate the cube to make it point at the aiming object yrotate object 1, ang1# xrotate object 1, angle_y# print "Angle on X/Z plane: ";ang1# sync loop |