You need the latest version of the Newton Physics wrapper and have the dll be in the same folder as you save this source too. no media required. The camera has an object and a body. the object follows the player, the camera is positioned at the object following the player and points at the player.
controls: WASD player movement (player is a box)
if the camera gets stuck, it goes up until it goes above the geometry.
rem acceleration variables
global accz#=300.0
global accyt#=1.0
rem screen stuff
set display mode 800,600,32
sync on
sync rate 0
autocam off
hide mouse
rem forget about this array, it's only called in functions
dim obj_move(1000)
`Three D Point in space, used for positions, velocities, etc (NOT USED BY NEWTON)
TYPE ThreeD
x as float
y as float
z as float
ENDTYPE
rem make textures
ink rgb(0,255,0),0
box 0,0,100,100
ink 0,0
box 2,2,98,98
sync
get image 1,0,0,100,100
rem and texture 2
cls
ink 0,0
box 0,0,100,100
ink rgb(200,200,200),0
box 1,1,9,9
sync
get image 2,0,0,10,10
rem set the camera range
set camera range 0.5,200
`make object cube 10000,0
TOP:
rem INITIALIZE PHYSICS WORLD!
global CURRENT_OBJECT
CURRENT_OBJECT=0
NDB_NewtonCreate
rem Materials
GroundID = NDB_NewtonMaterialCreateGroupID()
WallID = NDB_NewtonMaterialCreateGroupID()
CamID = NDB_NewtonMaterialCreateGroupID()
BOXID = NDB_NewtonMaterialCreateGroupID()
NDB_NewtonMaterialSetDefaultFriction CAMID, WallID, 0,0
NDB_NewtonMaterialSetDefaultElasticity CAMID, WallID, 0.001
NDB_NewtonMaterialSetDefaultSoftness CAMID, WallID, 0.1
NDB_NewtonMaterialSetDefaultFriction BOXID, GroundID, 0.7,0.4
NDB_NewtonMaterialSetDefaultElasticity BOXID, GroundID, 0.001
NDB_NewtonMaterialSetDefaultSoftness BOXID, GroundID, 0.1
rem world:
rem floor
inc CURRENT_OBJECT : make_box(CURRENT_OBJECT,100,10,100,50,-5,50,0,0,0,1,GroundID,0,1)
texture object CURRENT_OBJECT, 1
rem walls
inc CURRENT_OBJECT : make_box(CURRENT_OBJECT,100,50,10,50,15,105,0,0,0,10,WallID,0,1)
texture object CURRENT_OBJECT, 2
inc CURRENT_OBJECT : make_box(CURRENT_OBJECT,100,50,10,50,15,-5,0,0,0,10,WallID,0,1)
texture object CURRENT_OBJECT, 2
inc CURRENT_OBJECT : make_box(CURRENT_OBJECT,10,50,100,105,15,50,0,0,0,10,WallID,0,1)
texture object CURRENT_OBJECT, 2
inc CURRENT_OBJECT : make_box(CURRENT_OBJECT,10,50,100,-5,15,50,0,0,0,10,WallID,0,1)
texture object CURRENT_OBJECT, 2
rem boxes
for n=1 to 30
inc CURRENT_OBJECT : make_box(CURRENT_OBJECT,2+rnd(8),4+rnd(6),2+rnd(8),rnd(80)+10,3,rnd(80)+10,0,rnd(359),0,10000,WallID,1,1)
texture object CURRENT_OBJECT, 2
next n
rem player
inc CURRENT_OBJECT
player_body=make_box(CURRENT_OBJECT,3,2,4,50,1,50,0,0,0,2,BOXID,1,0)
player_num=CURRENT_OBJECT
texture object CURRENT_OBJECT, 2
rem camera
inc CURRENT_OBJECT
CAM_BODY=make_sphere(CURRENT_OBJECT,10,50,20,50,0.01,CamID,1,0)
CAM_OBJECT=CURRENT_OBJECT
hide object CAM_OBJECT
global time#
repeat
time# = NDB_GetElapsedTimeInSec()
until time# < 0.1
do
time# = NDB_GetElapsedTimeInSec()
NDB_NewtonUpdate time#
rem display
ink rgb(0,255,0),0
text 10,10,"-------------------------------------------------------------"
text 10,20,"Newton Game Dynamics SDK DBPro Wrapper v"+str$(NDB_GetVersion())
text 10,40,"Smart camera follow, camera avoids objects"
text 10,50,"Control box with W/A/S/D"
text 10,60,"Reset by using the r key."
text 10,70,"FPS "+str$(screen fps())
text 10,80,"-------------------------------------------------------------"
set cursor 0,100
if inkey$() = "r"
NDB_NewtonDestroy
goto TOP
endif
rem control the box
zmove=keystate(17)-keystate(31)
yturn=keystate(32)-keystate(30)
`print scancode()
point camera object position x(player_num),object position y(player_num),object position z(player_num)
x1#=object position x(player_num)
y1#=object position y(player_num)
z1#=object position z(player_num)
rem get relative z axis
move object player_num,1
zx#=object position x(player_num)-x1#
zy#=object position y(player_num)-y1#
zz#=object position z(player_num)-z1#
move object player_num,-1
rem move forward
NDB_NewtonBodyGetVelocity player_body
x#=zmove*zx#*accz#*time#
z#=zmove*zz#*accz#*time#
NDB_SetVector NDB_GetVector_x(1)*0.7+x#,0, NDB_GetVector_z(1)*0.7+z#
NDB_NewtonBodySetVelocity player_body
yangle#=atanfull(zx#,zz#)
rem turn
NDB_NewtonBodyGetOmega player_body
NDB_SetVector NDB_GetVector_x(1), NDB_GetVector_Y(1)*0.7+yturn*accyt#,NDB_GetVector_z(1)
NDB_NewtonBodySetOmega player_body
rem control the camera:
dist#=8
rem if the camera gets stuck, increase th height until it gets over the wall,
rem and hope there's no ceiling
height#=SQRT((object position x(CAM_OBJECT)-fx#)^2+(object position z(CAM_OBJECT)-fz#)^2)
oay#=yangle#
opx#=object position x(player_num)
opy#=object position y(player_num)
opz#=object position z(player_num)
fx#=opx#-dist#*sin(oay#)
fy#=opy#+height#
fz#=opz#-dist#*cos(oay#)
rem fx#,fy#,fz# is the place where the camera WANTS to be
curve#=3
cx#=camera position x()
cy#=camera position y()
cz#=camera position z()
rem slowly move the camera there
x#=(fx#-object position x(CAM_OBJECT))*curve#
y#=(fy#-object position y(CAM_OBJECT))*curve#
z#=(fz#-object position z(CAM_OBJECT))*curve#
NDB_SetVector x#, y#, z#
NDB_NewtonBodySetVelocity CAM_BODY
rem position the camera
position camera object position x(CAM_OBJECT),object position y(CAM_OBJECT),object position z(CAM_OBJECT)
point camera object position x(player_num),object position y(player_num),object position z(player_num)
sync
loop
rem Destory the Physics world and all bodies in it.
NDB_NewtonDestroy
end
function make_box(obj_num,xs#,ys#,zs#,xp#,yp#,zp#,xr#,yr#,zr#,mass#,group_id,move,freeze)
make object box obj_num, xs#,ys#,zs#
position object obj_num, xp#,yp#,zp#
col = NDB_NewtonCreateBox(xs#,ys#,zs#)
body=NDB_NewtonCreateBody(col)
NDB_BodySetDBProData body, obj_num
NDB_SetVector xr#,yr#,zr#
NDB_SetVectorToRadians
NDB_NewtonSetEulerAngle
NDB_SetVector xp#, yp#, zp#, 1.0
NDB_SetMatrixPosRow
NDB_NewtonBodySetMatrix body
NDB_NewtonBodySetAutoFreeze body, freeze
NDB_NewtonBodySetDestructorCallback body
NDB_NewtonBodySetMaterialGroupID body, group_id
NDB_NewtonBodySetTransformCallback body
if move=1
NDB_SetVector 0.0, 0.0, 0.0
NDB_NewtonBodySetVelocity body
NDB_NewtonBodySetForceAndTorqueCallback body
NDB_CalculateMIBoxSolid mass#,xs#,ys#,zs#
NDB_NewtonBodySetMassMatrix Body, mass#
endif
obj_move(body)=move
endfunction body
function make_sphere(obj_num,s#,xp#,yp#,zp#,mass#,group_id,move,freeze)
make object sphere obj_num, s#
position object obj_num, xp#,yp#,zp#
col = NDB_NewtonCreateSphere(s#*0.5)
body=NDB_NewtonCreateBody(col)
NDB_BodySetDBProData body, obj_num
NDB_SetMatrixIdentityMatrix
NDB_SetVector xp#, yp#, zp#, 1.0
NDB_SetMatrixPosRow
NDB_NewtonBodySetMatrix body
NDB_NewtonBodySetAutoFreeze body, freeze
NDB_NewtonBodySetDestructorCallback body
NDB_NewtonBodySetMaterialGroupID body, group_id
NDB_NewtonBodySetTransformCallback body
if move=1
NDB_SetVector 0.0, 0.0, 0.0
NDB_NewtonBodySetVelocity body
NDB_NewtonBodySetForceAndTorqueCallback body
NDB_CalculateMISphereHollow mass#,radius#
NDB_NewtonBodySetMassMatrix Body, mass#
endif
obj_move(body)=move
endfunction body
-----------------------------------
To delete the bug, delete the code.
Specs: Sony VAIO Laptop, Windows XP, P4 2.8Ghz, 512MB RAM, ATI Radeon 64MB video memory, DBP Upgrade 5.3.