oh whoops. sorry, ummm, I've added a bit to the code. now it's a snippet with flying cars, like in star wars episode 2, except the flying cars are boxes
. There are 3 AI controlled flying cars that chase you like GTA cops, in the first few lines of code there is a variable named COP_AMOUNT, change that if you want more/less/no flying cars chasing you. Since the flying cars can only accelerate/slow down in 3 demensions, the AI was hard to program. All flying cars have a "stabilizer" which keeps the cars from rolling, and thus keeping the cars right side up(but doesn't control the pitch of the car, so you car pitch the car up, go upside down, the stabilizer will turn the car unright, and you can keep on flying) (The players car only tries to stay upright when you aren't pressing the roll buttons(the A and D keys). It would be really cool if there was an actual game made with a gta theme, but with flying cars, and you would have to free fall into a car to steel it
. Anyways, here's the code, the full source
Btw you'll need the latest version of DBP Newton installed and have textures "floor1.bmp" and "balltex.bmp" in the same dir as the dir you save the code to.
EDIT:
there must be a forum bug cuz it cut off the code again. I can't edit or delete the attached code, so here it is:
global ship_accz#=40.0
global ship_accx#=40.0
global ship_accy#=40.0
global ship_accxt#=40.0
global ship_accyt#=40.0
global ship_acczt#=40.0
global COP_AMOUNT=3
set display mode 800,600,32
sync on
sync rate 0
autocam off
hide mouse
set camera fov 90
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
Cam_Speed# = 1
set camera range 0.5,1000
make object cube 10000,0
TOP:
rem INITIALIZE PHYSICS WORLD!
NDB_NewtonCreate
rem Materials
FloorID = NDB_NewtonMaterialCreateGroupID()
ShipID = NDB_NewtonMaterialCreateGroupID()
NDB_NewtonMaterialSetDefaultFriction FloorID, ShipID, 0.4, 0.3
NDB_NewtonMaterialSetDefaultElasticity FloorID, ShipID, 0.01
NDB_NewtonMaterialSetDefaultSoftness FloorID, ShipID, 0.1
NDB_NewtonMaterialSetDefaultFriction ShipID, ShipID, 0.5, 0.6
NDB_NewtonMaterialSetDefaultElasticity ShipID, ShipID, 0.5
NDB_NewtonMaterialSetDefaultSoftness ShipID, ShipID, 0.5
rem floor:
load image "floor1.bmp", 1
n=0
inc n : make_box(n,300,10,300,0,-5,0,1,FloorID,0,1)
texture object n, 1
load image "balltex.bmp", 2
`texture object Ball1, Ball1
global enemy_amount=100
texture object n, 1
dim AI(enemy_amount) as AI_type
type AI_type
mode$ as string
target as ThreeD
target_type$ as string
body as dword
endtype
for n=1 to enemy_amount
AI(n).body=make_box(n+1,4,3,6,rnd(280)-140,1.5+rnd(80),rnd(280)-140,10,ShipID,1,0)
AI(n).mode$="NORMAL"
AI(n).target.x=rnd(280)-140
AI(n).target.y=1.5+rnd(80)
AI(n).target.z=rnd(280)-140
AI(n).target_Type$="LOCATION"
texture object n+1,2
NDB_SetVector 1, 0.4, 0.4, 0.4
NDB_NewtonBodySetAngularDamping AI(n).body
next n
for n=1 to COP_AMOUNT
AI(n).target_Type$="ENEMY":color object n+1,rgb(50,50,50)
next n
player=enemy_amount+2
global player_body
player_body=make_box(player,4,3,6,rnd(300)-150,1.5,rnd(300)-150,10,ShipID,1,0)
color object player,rgb(100,255,100)
n=player
inc n : make_box(n,300,100,10,0,50,145,1,FloorID,0,1)
texture object n, 1
inc n : make_box(n,300,100,10,0,50,-145,1,FloorID,0,1)
texture object n, 1
inc n : make_box(n,10,100,280,145,50,0,1,FloorID,0,1)
texture object n, 1
inc n : make_box(n,10,100,280,-145,50,0,1,FloorID,0,1)
texture object n, 1
rem set world size
NDB_SetVector 1, -150.0, -150.0, -150.0
NDB_SetVector 2, 150.0, 150.0, 150.0
NDB_NewtonSetWorldSize
global time#
repeat
time# = NDB_GetElapsedTimeInSec()
until time# < 0.1
GO = 0
rem gravity, changing the the x and z gravity will make wind
NDB_SetVector 0.0, -9.8, 0.0
NDB_SetStandardGravity
color backdrop rgb(0,0,0)
NDB_SetVector 1, 0.4, 0.4, 0.4
NDB_NewtonBodySetAngularDamping player_body
camfollowdist#=20
n=make vector3(1)
n=make vector3(2)
n=make vector3(3)
space#=3
do
time# = NDB_GetElapsedTimeInSec()
NDB_NewtonUpdate time#
text 10,10,"-------------------------------------------------------------"
text 10,20,"Newton Game Dynamics SDK DBPro Wrapper v"+str$(NDB_GetVersion())
text 10,40,"Thrusters W/S | Roll A/D | Turn J/L | Pitch I/K | T/F/G/H for vertical and sidways thrusters"
text 10,50,"Control camera with arrow keys"
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
`-------------------------
`CAMERA
`-------------------------
zmove=keystate(17)-keystate(31)
xmove=keystate(35)-keystate(33)
ymove=keystate(20)-keystate(34)
xturn=(keystate(23)-keystate(37))
yturn=keystate(38)-keystate(36)
zturn=keystate(30)-keystate(32)
`print scancode()
point camera object position x(player),object position y(player),object position z(player)
x1#=object position x(player)
y1#=object position y(player)
z1#=object position z(player)
rem get relative z axis
move object player,1
zx#=object position x(player)-x1#
zy#=object position y(player)-y1#
zz#=object position z(player)-z1#
move object player,-1
rem get relative y axis
move object up player,1
yx#=object position x(player)-x1#
yy#=object position y(player)-y1#
yz#=object position z(player)-z1#
move object down player,1
rem get relative x axis
move object right player,1
xx#=object position x(player)-x1#
xy#=object position y(player)-y1#
xz#=object position z(player)-z1#
move object left player,1
NDB_NewtonBodyGetVelocity player_body
x#=xmove*xx#*ship_accx#*time#+ymove*yx#*ship_accy#*time#+zmove*zx#*ship_accz#*time#
y#=xmove*xy#*ship_accx#*time#+ymove*yy#*ship_accy#*time#+zmove*zy#*ship_accz#*time#
z#=xmove*xz#*ship_accx#*time#+ymove*yz#*ship_accy#*time#+zmove*zz#*ship_accz#*time#
NDB_SetVector NDB_GetVector_X(1)+x#, NDB_GetVector_Y(1)+y#, NDB_GetVector_Z(1)+z#
NDB_NewtonBodySetVelocity player_body
xangle#=atanfull(zy#,SQRT(zx#^2+zz#^2))
yangle#=atanfull(zx#,zz#)
set vector3 1,zx#,zy#,zz#
set vector3 2,0,1,0
cross product vector3 3,2,1
normalize vector3 3,3
set vector3 1,xx#,xy#,xz#
angle#=dot product vector3(3,1)
`print "relative x:"
`print x vector3(1)
`print y vector3(1)
`print z vector3(1)
`print "world rel x:"
`print x vector3(3)
`print y vector3(3)
`print z vector3(3)
zangle#= acos(dot product vector3(3,1))
if xy#<0 then zangle#=0-zangle#
`print "x angle:",xangle#
`print "y angle:",yangle#
`print "z angle:",zangle#
NDB_NewtonBodyGetOmega player_body
REM Z axis stabilizer
zanglespeed#=NDB_GetVector_x(1)*zx#+NDB_GetVector_y(1)*zy#+NDB_GetVector_z(1)*zz#
turn_stop_dist#=1.0*abs(zanglespeed#)*zanglespeed#/(2.0*ship_acczt#*time#)
if zturn=0
if zangle#+turn_stop_dist#<0 then zturn=1
if zangle#+turn_stop_dist#>0 then zturn=-1
endif
REM X axis stabilizer (Remmed out
rem xanglespeed#=NDB_GetVector_x(1)*xx#+NDB_GetVector_y(1)*xy#+NDB_GetVector_z(1)*xz#
rem turn_stop_dist#=1.0*abs(xanglespeed#)*xanglespeed#/(2.0*ship_accxt#*time#)
`if xturn=0
` if xangle#-turn_stop_dist#>0 then xturn=1
` if xangle#-turn_stop_dist#<0 then xturn=-1
`endif
rem translate relative omega acceleration to world axises
x#=xx#*xturn*ship_accxt#*time#+yx#*yturn*ship_accyt#*time#+zx#*zturn*ship_acczt#*time#
y#=xy#*xturn*ship_accxt#*time#+yy#*yturn*ship_accyt#*time#+zy#*zturn*ship_acczt#*time#
z#=xz#*xturn*ship_accxt#*time#+yz#*yturn*ship_accyt#*time#+zz#*zturn*ship_acczt#*time#
NDB_SetVector NDB_GetVector_x(1)+x#, NDB_GetVector_Y(1)+y#,NDB_GetVector_z(1)+z#
NDB_NewtonBodySetOmega player_body
gosub AI
if leftkey()
yrotate camera wrapvalue(camera angle y()-90)
move camera Cam_Speed#
yrotate camera wrapvalue(camera angle y()+90)
endif
if rightkey()
yrotate camera wrapvalue(camera angle y()+90)
move camera Cam_Speed#
yrotate camera wrapvalue(camera angle y()-90)
endif
dec camfollowdist#,(upkey()-downkey())*0.1
if camera position y()-object position y(player)<4 then position camera camera position x(),object position y(player)+4,camera position z()
dist#=SQRT((camera position x()-object position x(player))^2+(camera position y()-object position y(player))^2+(camera position z()-object position z(player))^2)
move camera (dist#-camfollowdist#)/10.0
if spacekey()=1 then Newton_Explosion(100.0,object position x(player),object position y(player),object position z(player))
sync
loop
rem Destory the Physics world and all bodies in it.
NDB_NewtonDestroy
end
AI:
for n=1 to enemy_Amount
if AI(n).target_type$="ENEMY"
a=n
while a>7
dec a,7
endwhile
space#=0.5*SQRT((object position x(player)-object position x(n+1))^2+(object position y(player)-object position y(n+1))^2+(object position z(player)-object position z(n+1))^2)
if a=1
NDB_NewtonBodyGetVelocity player_body
AI(n).target.x=object position x(player)+NDB_GetVector_x(1)*0.5
AI(n).target.y=object position y(player)+NDB_GetVector_y(1)*0.5
AI(n).target.z=object position z(player)+NDB_GetVector_z(1)*0.5
endif
dec space#,(keystate(2)-keystate(3))*100
if a=2
move object player,space#
AI(n).target.x=object position x(player)
AI(n).target.y=object position y(player)
AI(n).target.z=object position z(player)
move object player,-space#
endif
if a=3
move object player,-space#
AI(n).target.x=object position x(player)
AI(n).target.y=object position y(player)
AI(n).target.z=object position z(player)
move object player,space#
endif
if a=4
move object left player,space#
AI(n).target.x=object position x(player)
AI(n).target.y=object position y(player)
AI(n).target.z=object position z(player)
move object right player,space#
endif
if a=5
move object right player,space#
AI(n).target.x=object position x(player)
AI(n).target.y=object position y(player)
AI(n).target.z=object position z(player)
move object left player,space#
endif
if a=6
move object up player,space#
AI(n).target.x=object position x(player)
AI(n).target.y=object position y(player)
AI(n).target.z=object position z(player)
move object down player,space#
endif
if a=7
move object down player,space#
AI(n).target.x=object position x(player)
AI(n).target.y=object position y(player)
AI(n).target.z=object position z(player)
move object up player,space#
endif
endif
obj=n+1
x1#=object position x(obj)
y1#=object position y(obj)
z1#=object position z(obj)
rem get relative z axis
move object obj,1
zx#=object position x(obj)-x1#
zy#=object position y(obj)-y1#
zz#=object position z(obj)-z1#
move object obj,-1
move object up obj,1
yx#=object position x(obj)-x1#
yy#=object position y(obj)-y1#
yz#=object position z(obj)-z1#
move object down obj,1
rem get relative x axis
move object right obj,1
xx#=object position x(obj)-x1#
xy#=object position y(obj)-y1#
xz#=object position z(obj)-z1#
move object left obj,1
xangle#=atanfull(zy#,SQRT(zx#^2+zz#^2))
yangle#=atanfull(zx#,zz#)
set vector3 1,zx#,zy#,zz#
set vector3 2,0,1,0
cross product vector3 3,2,1
normalize vector3 3,3
set vector3 1,xx#,xy#,xz#
zangle#= acos(dot product vector3(3,1))
if xy#<0 then zangle#=0-zangle#
target_angle#=wrapvalue(atanfull(AI(n).target.x-x1#,AI(n).target.z-z1#))
turn_angle#=wrapvalue(target_angle#-yangle#)
xtarget_angle#=wrapvalue(atanfull(0-(AI(n).target.y-y1#),SQRT((AI(n).target.x-x1#)^2+(AI(n).target.z-z1#)^2)))
`print "xtarget_angle#:",xtarget_angle#
xturn_angle#=wrapvalue(xtarget_angle#-xangle#)
if xturn_angle#>180 then dec xturn_Angle#,360
` print "xturn_angle#:",xturn_angle#
if turn_angle#>180 then dec turn_Angle#,360
NDB_NewtonBodyGetOmega AI(n).body
yanglespeed#=NDB_GetVector_x(1)*yx#+NDB_GetVector_y(1)*yy#+NDB_GetVector_z(1)*yz#
turn_stop_dist#=3.0*abs(yanglespeed#)*yanglespeed#/(2.0*ship_accyt#*time#)
yturn=0
xturn=0
zturn=0
if turn_Angle#+turn_stop_dist#<0 then yturn=1
if turn_Angle#+turn_stop_dist#>0 then yturn=-1
zanglespeed#=NDB_GetVector_x(1)*zx#+NDB_GetVector_y(1)*zy#+NDB_GetVector_z(1)*zz#
turn_stop_dist#=1.0*abs(zanglespeed#)*zanglespeed#/(2.0*ship_acczt#*time#)
if zangle#+turn_stop_dist#<0 then zturn=1
if zangle#+turn_stop_dist#>0 then zturn=-1
xanglespeed#=NDB_GetVector_x(1)*xx#+NDB_GetVector_y(1)*xy#+NDB_GetVector_z(1)*xz#
turn_stop_dist#=3.0*abs(xanglespeed#)*xanglespeed#/(2.0*ship_accxt#*time#)
if xturn_Angle#+turn_stop_dist#<0 then xturn=1
if xturn_Angle#+turn_stop_dist#>0 then xturn=-1
x#=xx#*xturn*ship_accxt#*time#+yx#*yturn*ship_accyt#*time#+zx#*zturn*ship_acczt#*time#
y#=xy#*xturn*ship_accxt#*time#+yy#*yturn*ship_accyt#*time#+zy#*zturn*ship_acczt#*time#
z#=xz#*xturn*ship_accxt#*time#+yz#*yturn*ship_accyt#*time#+zz#*zturn*ship_acczt#*time#
NDB_SetVector NDB_GetVector_x(1)+x#, NDB_GetVector_Y(1)+y#,NDB_GetVector_z(1)+z#
NDB_NewtonBodySetOmega AI(n).body
NDB_NewtonBodyGetVelocity AI(n).body
speed#=NDB_GetVector_X(1)*zx#+NDB_GetVector_Y(1)*zy#+NDB_GetVector_Z(1)*zz#
xspeed#=NDB_GetVector_X(1)*xx#+NDB_GetVector_Y(1)*xy#+NDB_GetVector_Z(1)*xz#
yspeed#=NDB_GetVector_X(1)*yx#+NDB_GetVector_Y(1)*yy#+NDB_GetVector_Z(1)*yz#
zmove=0
xmove=0
ymove=0
if xspeed#>ship_accx# then xmove=-1
if xspeed#<-ship_accx# then xmove=1
if yspeed#>ship_accy# then ymove=-1
if yspeed#<0 then ymove=1
`print speed#
min_stop_dist#=3.0*abs(speed#)*speed#/(2.0*ship_accz#)
dist#=(AI(n).target.x-X1#)*zx#+(AI(n).target.y-y1#)*zy#+(AI(n).target.z-z1#)*zz#
`print dist#
if AI(n).target_Type$="LOCATION"
if dist#-min_Stop_dist#<0 then zmove=1
if dist#-min_Stop_dist#>0 then zmove=-1
else
zmove=1
endif
if abs(turn_angle#)>90 then zmove=-zmove
x#=xmove*xx#*ship_accx#*time#+ymove*yx#*ship_accy#*time#+zmove*zx#*ship_accz#*time#
y#=xmove*xy#*ship_accx#*time#+ymove*yy#*ship_accy#*time#+zmove*zy#*ship_accz#*time#
z#=xmove*xz#*ship_accx#*time#+ymove*yz#*ship_accy#*time#+zmove*zz#*ship_accz#*time#
NDB_SetVector NDB_GetVector_X(1)+x#, NDB_GetVector_Y(1)+y#, NDB_GetVector_Z(1)+z#
NDB_NewtonBodySetVelocity AI(n).body
dist#=SQRT((object position x(obj)-AI(n).target.x)^2+(object position y(obj)-AI(n).target.y)^2+(object position z(obj)-AI(n).target.z)^2)
if dist#<35 and AI(n).target_Type$="LOCATION"
AI(n).target.x=rnd(280)-140
AI(n).target.y=1.5+rnd(80)
AI(n).target.z=rnd(280)-140
endif
position object 10000,AI(n).target.x,AI(n).target.y,AI(n).target.z
x1=object screen x(obj)
y1=object screen y(obj)
x2=object screen x(10000)
y2=object screen y(10000)
if object in screen(10000)=1:` and AI(n).target_Type$="ENEMY"
` line x1,y1,x2,y2
endif
next n
return
function make_box(obj_num,xs#,ys#,zs#,xp#,yp#,zp#,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_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
if move=1
NDB_NewtonBodySetTransformCallback 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
if move=1
NDB_NewtonBodySetTransformCallback body
NDB_NewtonBodySetForceAndTorqueCallback body
NDB_CalculateMISphereHollow mass#,radius#
NDB_NewtonBodySetMassMatrix Body, mass#
endif
obj_move(body)=move
endfunction body
function Newton_Explosion(force#,x#,y#,z#)
r=make vector3(1000)
for n=1 to 1000
if NDB_BodyExist(n)=1
if n<>player_body and obj_move(n)=1
NDB_NewtonBodyGetMatrix n
NDB_GetMatrix
x2# = NDB_GetVector_X(4)
y2# = NDB_GetVector_Y(4)
z2# = NDB_GetVector_Z(4)
` x2#=object position x(n)
` y2#=object position y(n)
` z2#=object position z(n)
dx#=x2#-x#
dy#=y2#-y#
dz#=z2#-z#
dist#=SQRT(dx#^2+dy#^2+dz#^2)
if dist#<force#
set vector3 1000,dx#,dy#,dz#
normalize vector3 1000,1000
NDB_NewtonBodyGetVelocity n
vx# = NDB_GetVector_X() : vy# = NDB_GetVector_Y() : vz# = NDB_GetVector_Z()
vx#=vx#+x vector3(1000)*force#/dist#^2
vy#=vy#+y vector3(1000)*force#/dist#^2
vz#=vz#+z vector3(1000)*force#/dist#^2
NDB_SetVector vx#, vy#, vz#
NDB_NewtonBodySetVelocity n
NDB_NewtonWorldUnfreezeBody n
endif
endif
endif
next n
r=delete vector3(1000)
endfunction
-----------------------------------
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.