Here is the full source for a simple four wheel drive vehicle which interact with true physics against other objects, (No Media Needed):
http://walaber.proboards23.com/index.cgi?board=newtdiscuss&action=display&num=1108726051&start=0
Before compiling you must have Newton DBPro wrapper installed, or at least the NDB.dll file in the Dark Basic SoftwareDark Basic Professionalplugins-user library
You must have the latest Newton.dll in the same directory as the .exe file.
Get the Newton DBPro wrapper with all needed dll's from
http://walaber.com
Rem Project: Simple Controllable Vehicle
Rem Created: 2005-02-21 22:24:21
Rem By Kjelle Mickelsson
Rem ***** Main Source File *****
sync on
autocam off
Rem Size of vehicle
sizeA=8
Rem Weight of Vehicle
Weight#=50
Rem Max Steeringangle
MaxSteeringAngle#=45
Dim wheels(4)
Rem Wheelmass
mass# = 3
Rem Wheelwidth
width# = 3
Rem Wheelradius
radius# = 6
Rem Shocks
Damping#=0.7
Rem Spring
Spring#=0.95
Rem Length of suspension
Length#=1.7
Rem Forward Torque
FTorque#=35000
Rem Reverse Torque
RTorque#=5000
Rem Maximum Brakeforce
MaxBrake#=7000
Rem this are values for linear and angular damping on the Vehicle Chassi
Rem This makes the vehicle behave more realistic
Lindamp#=0.2
Angdamp#=0.9
Rem Size of boxes in wall
boxsize#=6
Rem Weigth of boxes in Wall
boxmass#=1
Rem Max boxes in wall X direction
maxx= 8
Rem Max boxes in wall Y direction
maxy= 8
dim bbox(maxx*maxy+maxx)
NDB_NewtonCreate
NDB_SetVector 1,0,-98.0,0
NDB_SetStandardGravity
NDB_Newtonsetminimumframerate 80
Rem Make world
make object box 1,1500,4,1500
color object 1,RGB(25,250,10)
set object ambience 1, 80
col = NDB_NewtonCreateTreeCollision(1)
Room = NDB_NewtonCreateBody(col)
NDB_BodySetDBProData Room, 1
NDB_NewtonBodySetDestructorCallback Room
NDB_NewtonReleaseCollision col
NDB_BuildMatrix 0,0,0,0,-20,0
position object 1,0,-20,0
NDB_NewtonBodySetMatrix 1
Rem Make Vehicle body
Col=NDB_NewtonCreatebox(sizeA,sizeA,sizeA)
BoxA=Ndb_NewtonCreateBody(Col)
NDB_NewtonReleaseCollision Col
NDB_BodySetGravity BoxA,1
NDB_BuildMatrix 0,0,0,30,-4,-40
NDB_NewtonBodySetMatrix BoxA
make object box 2,sizeA,sizeA,sizeA
NDB_BodySetDBProData Boxa,2
NDB_CalculateMIBoxSolid Weight#,sizeA,sizeA,sizeA
NDB_NewtonBodySetMassMatrix BoxA,Weight#
NDB_NewtonBodySetLinearDamping BoxA,Lindamp#
NDB_SetVector 1,Angdamp#,Angdamp#,Angdamp#
NDB_NewtonBodySetAngularDamping BoxA
Rem Make wheels
for t = 0 to 1
make object box 3+t,radius#,radius#,Radius#/1.5
color object 3+t,rgb(0,20,35)
set object ambience 3+t,80
next t
for t = 2 to 3
make object box 3+t,radius#*1.5,radius#*1.5,Radius#/1.5
color object 3+t,rgb(0,20,35)
set object ambience 3+t,80
next t
NDB_SetVector 1,0,1,0
Body = NDB_NewtonConstraintCreateVehicle( BoxA )
NDB_BuildMatrix 0,0,0,-Sizea*1.8,-Sizea/2,-Sizea
NDB_SetVector 1,0,0,1
wheels(1) = NDB_NewtonVehicleAddTire( Body,mass#/2, width#, radius#/2, Damping#,Spring#,Length# )
NDB_VehicleTireSetDBProData Body, wheels(1), 3
NDB_BuildMatrix 0,0,0,-Sizea*1.8,-Sizea/2,Sizea
NDB_SetVector 1,0,0,1
Wheels(2) = NDB_NewtonVehicleAddTire( Body,mass#/2, width#, radius#/2, Damping#,Spring#,Length# )
NDB_VehicleTireSetDBProData Body, wheels(2), 4
NDB_BuildMatrix 0,0,0,Sizea/1.5,-Sizea/2,Sizea
NDB_SetVector 1,0,0,1
Wheels(3) = NDB_NewtonVehicleAddTire( Body,mass#, width#, radius#, Damping#,Spring#,Length# )
NDB_VehicleTireSetDBProData Body, Wheels(3), 5
NDB_BuildMatrix 0,0,0,Sizea/1.5,-Sizea/2,-Sizea
NDB_SetVector 1,0,0,1
Wheels(4) = NDB_NewtonVehicleAddTire( Body,mass#, width#, radius#, Damping#,Spring#,Length# )
NDB_VehicleTireSetDBProData Body, Wheels(4), 6
Rem set wheel max brakes
For t = 1 to 4
NDB_VehicleTireSetMaxBrake body, Wheels(t), Maxbrake#
next t
NDB_GetStandardGravity
NDB_NewtonVehicleBalanceTires Body, NDB_GetVector_Y()
NDB_NewtonBodySetAutoFreeze BoxA,0
set ambient light 60
Position Camera 0,30,-100
for t= 1 to 6
if object exist (t) then set shadow shading on t
next t
Rem Create Boxes, both DBPro Object and Newton Object
for y = 0 to maxy
for x=1 to maxx
obj=x+(y*maxx)+1000
objarr=x+(y*maxx)
Rem DBPro object
h#=RND(2)+1
make object box obj,boxsize#,boxsize#/h#,boxsize#
color object obj,RGB(100,100,x*2.5)
set object ambience obj, 80
Rem Newton Collision object same size as DBPro Object
col=NDB_NewtonCreateBox(boxsize#,boxsize#/h#,boxsize#)
Rem Here bbox becomes a newton collision object
bbox(objarr) = NDB_NewtonCreateBody(col)
Rem Connect Newton and DBPro object This means that the callback system will
Rem Automaticall update position of the graphical shape (The box)
NDB_BodySetDBProData bbox(objarr), obj
NDB_CalculateMIBoxSolid boxmass#,boxsize#,boxsize#,boxsize#
Rem Set Mass of NewtonObject
NDB_NewtonBodySetMassMatrix bbox(objarr),boxmass#
Rem Position Objects
Rem First three Floats in the Buildmatrix represents the Global rotation
Rem Next three Floats in the Buildmatrix represents the Global position
NDB_BuildMatrix 0.0, 0.0, 0.0, -50+x*boxsize#*1.3,10+y*boxsize#*2, 0.0
Rem This command reads the matrix created above and rotates and position the object
Rem bbox(obj) regarding to the content of the matrix
NDB_NewtonBodySetMatrix bbox(objarr)
Rem Release collision to free up memory, Newton object stays active !
NDB_NewtonReleaseCollision col
Rem Make sure the Gravity in the Newton World affects each bbox
NDB_BodySetGravity bbox(objarr),1
next x
next y
Rem Generate Materials
Rem Without materials you cant change the friction elasticity and softness of
Rem different Rigid bodies in your system.
Rem First we creates some identification variables to store the materials in
Rem Defaultmat (Which is the default material always present in Newton
Rem BoxMat which becomes the material of each bbox
Rem FpsMat which is to be the material of the fps object
DefaultMat = NDB_NewtonMaterialGetDefaultGroupID()
BoxMat = NDB_NewtonMaterialCreateGroupID()
CarMat = NDB_NewtonMaterialCreateGroupID()
Rem Here we set the Static and the Dynamic friction between the Defaultmaterial
Rem And the bbox material.
Rem The Floor in our case will have the defaultmaterial because we dont specify
Rem any material for that object. = Gets the defaultgroupid automatically.
NDB_NewtonMaterialSetDefaultFriction DefaultMat, BoxMat, .95, 0.85
Rem Here the elasticity between the materials is specified
NDB_NewtonMAterialSetDefaultElasticity DefaultMat, BoxMat, 0.5
Rem Here the softness between the materials is specified
NDB_NewtonMaterialSetDefaultSoftness DefaultMat, BoxMat, 0.5
Rem Here we specify how the boxes will interact with eachother !
NDB_NewtonMaterialSetDefaultFriction BoxMat, BoxMat, 0.5,0.5
Rem Here the elasticity between the boxes is specified
NDB_NewtonMAterialSetDefaultElasticity BoxMat, BoxMat, 0.2
Rem Here the softness between the boxes is specified
NDB_NewtonMaterialSetDefaultSoftness BoxMat, BoxMat, 0.9
Rem Here we specify the friction between the floor and the Car object
NDB_NewtonMaterialSetDefaultFriction DefaultMat, CarMat, 2.0,2.0
Rem Here the elasticity between the floor and the fps object
NDB_NewtonMAterialSetDefaultElasticity DefaultMat, CarMat, 0.2
Rem Here the softness between the floor and the fps object
NDB_NewtonMaterialSetDefaultSoftness DefaultMat, CarMat, .2
Rem Now we have to make sure each rigid body gets the proper material assignment
Rem First we iterate through each bbox
for y = 0 to maxy
for x=1 to maxx
obj=x+(y*maxx)
NDB_NewtonBodySetMaterialGroupID bbox(obj), boxMat
next x
next y
Rem Than we set the material FpsMat to the fps object !
NDB_NewtonBodySetMaterialGroupID Body, CarMat
Rem Now we Specify which objects will affect eachother with the stated material parameters
Rem Floor against bbox
NDB_NewtonMaterialSetCollisionCallback DefaultMat, BoxMat
Rem Floor against fps object
NDB_NewtonMaterialSetCollisionCallback DefaultMat, CarMat
Rem Boxes against boxes
NDB_NewtonMaterialSetCollisionCallback BoxMat, BoxMat
Rem Fps objct against boxes , thats all !
NDB_NewtonMaterialSetCollisionCallback CarMat, BoxMat
Speed=make vector3(SpeedVec)
repeat
Point Camera object position x(2),object position y(2),object position z(2)
Sec#=NDB_GetElapsedTimeInSec()
NDB_NewtonUpdate Sec#
Rem Next command is needed for the next version of the wrapper: 1.31+
Rem NDB_NewtonUpdateTires Body
text 10,10,"Time since last update"+str$(Sec#)
text 10,20,"Maximum Speed :"+str$(MaxSpeed#)
NDB_NewtonBodyGetVelocity BoxA
SET VECTOR3 SpeedVec, NDB_GetVector_X(),NDB_GetVector_Y(), NDB_GetVector_Z()
Speed#=Length vector3(SpeedVec)
if Speed#>MaxSpeed# then MaxSpeed#=Speed#
if inkey$()="r"
wait 200
NDB_BuildMatrix 0,0,0,30,-4,-40
NDB_NewtonBodySetMatrix BoxA
NDB_SetVector 0.0,0.0,0.0
NDB_NewtonBodySetVelocity BoxA
NDB_NewtonBodySetOmega BoxA
MaxSpeed#=0
NDB_NewtonVehicleReset Body
endif
NDB_NewtonBodyGetOmega BoxA
VecX#=NDB_GetVector_X (1)
VecY#=NDB_GetVector_Y (1)
VecZ#=NDB_GetVector_Z (1)
if VecX# >1 then VecX#=1
if VecY# >1 then VecY#=1
if VecZ# >1 then VecZ#=1
NDB_SetVector 1,VecX#,VecY#,VecZ#
NDB_NewtonBodySetOmega BoxA
Rem CONTROLS FOR MOVING THE VEHICLE
ACCEL = 0
Rem if the user pressed "W" key, set positive torque for the tires
if keystate(17)
ACCEL = 1
Torque# = FTorque#
endif
Rem if the user pressed "S" key, set negative torque for the tires.
if keystate(31)
ACCEL = 1
Torque# = -RTorque#
endif
Rem If no input then turn torque down to 0
Rem steering basically works the same way!
STEERING = 0
if keystate(32)
STEERING = 1
inc steerangle#, 0.7
if steerangle# > MaxSteeringAngle# then steerangle# = MaxSteeringAngle#
endif
if keystate(30)
STEERING = 1
dec steerangle#, 0.7
if steerangle# < -MaxSteeringAngle# then steerangle# = -MaxSteeringAngle#
endif
if STEERING = 0
steerangle# = steerangle# * 0.05
endif
Rem If the Shift key is pressed, we turn the brakes on!
if Shiftkey() then BRAKES = 1 else BRAKES = 0
Rem Loop through each wheel, and apply the torque, steering, and brakes!
for i=3 to 4
if ACCEL AND (120 > ABS(NDB_NewtonVehicleGetTireOmega(body, Wheels(i))))
NDB_VehicleTireSetTorque body, Wheels(i), torque#
else
if 5 < ABS(NDB_NewtonVehicleGetTireOmega(body, Wheels(i)))
NDB_VehicleTireSetTorque body, Wheels(i), -torque#*0.1
else
NDB_VehicleTireSetTorque body, Wheels(i), 0.0
endif
endif
if BRAKES then NDB_VehicleTireBrakesOn body, Wheels(i)
next i
for i = 1 to 2
NDB_VehicleTireSetSteeringAngle body, Wheels(i), steerangle#
if BRAKES then NDB_VehicleTireBrakesOn body, Wheels(i)
next i
Rem Press Ctrl key to show debug lines
if controlkey() then NDB_DebugDrawNewtonLines
Rem update the screen
sync
until escapekey()
NDB_NewtonDestroy
end
if memblock exist(1) then end
if mesh exist(1) then end