Made this for fun, and hopefully a basic starting point for whoever wants to try Newton Physics but wants to set the position/rotation/velocity/whatever like normal objects.
Some functions that might be useful that are in the code snippet:
make_box(object number,x size,y size z size,mass,surface type, move flag)
makes a DBP object and a body with one command. IF the move flah is 1, the box can move and will if it collides with other objects and will be affected by gravity, else the object will remain still and only move with the next function.
set_object(object number,x position,y position,z position,x angle,y angle,z angle)
this function sets the object's and body's position and rotation.
set_velocity(object number, x velocity, y velocity, z velocity)
this function sets the linear velocity of the object.
set_omega(object number, x velocity, y velocity, z velocity)
this function sets the rotational velocity of the object.
add_velocity(object number, x velocity, y velocity, z velocity)
this functions increases the velocity of the object.
add_omega(object number, x velocity, y velocity, z velocity)
this functions increases the rotational velocity of the object.
set_relative_velocity(object number, x velocity, y velocity, z velocity)
this functions sets the velocity relative to the object, so a positive z velocity will make the object go forward no matter what direction it's facing.
set_relative_omega(object number, x velocity, y velocity, z velocity)
this functions sets the rotational velocity relative to the object, so a non-zero z velocity will make the object roll no matter what direction it's facing.
add_relative_velocity(object number, x velocity, y velocity, z velocity)
this functions increases the velocity relative to the object, so a positive z velocity will accelerate the object forward no matter what direction it's facing.
add_relative_omega(object number, x velocity, y velocity, z velocity)
this functions increases the rotational velocity relative to the object, so a positive y velocity will accelerate the object to the right no matter what direction it's facing.
Using these functions you don't have to worry about the bodies or what object is the visual representation of what body.
`screen stuff
sync on
sync rate 0
cls
hide mouse
`turn of automatic camera
autocam off
`make an array of all the objects(this must be as large as the amount of objects in the demo)
type object_type
body
move
endtype
dim obj(100) as object_type
`this starts newton
NDB_NewtonCreate
`create one material
global BOX_ID=NDB_NewtonMaterialCreateGroupID()
`make a floor
n=0
`1000,10,1000 is the x,y,z size of the floor, with the box id.
`the last number, 0, means the floor is stationary, or else it would fall
inc n:make_box(n,1000,10,1000,1,BOX_ID,0)
`this positions the floor at 500,-5,500 and rotates it at 0,0,0
`if you want change last three to 0,45,0 to see the floor rotated 45 degrees on the y axis
set_object(n,500,-5,500,0,0,0)
`this makes and positions 4 walls, one on each side:
`make wall 1
inc n:make_box(n,10,100,1000,1,BOX_ID,0)
`position wall 1
set_object(n,-5,50,500,0,0,0)
`make wall 2
inc n:make_box(n,10,100,1000,1,BOX_ID,0)
`position wall 2
set_object(n,1005,50,500,0,0,0)
`make wall 3
inc n:make_box(n,1000,100,10,1,BOX_ID,0)
`position wall 3
set_object(n,500,50,-5,0,0,0)
`make wall 4
inc n:make_box(n,1000,100,10,1,BOX_ID,0)
`position wall 4
set_object(n,500,50,1005,0,0,0)
`this is the box the player controls
inc n:make_box(n,10,5,15,1,BOX_ID,1)
`color it green
color object n,rgb(0,255,0)
`position it
set_object(n,400,6,500,0,0,0)
`this makes 94(100-1 floor-4 walls-1 player) boxes
repeat
`make the box
inc n:make_box(n,30,10,30,1,BOX_ID,1)
`position the box on the floor, with a y angle of rnd(359)
set_object(n,rnd(1000-200)+100,5,rnd(1000-200)+100,0,rnd(359),0)
`color it light blue
color object n,rgb(100,100,255)
until n=100
`position the camera
position camera 500,20,500
`make the time variable
global time#=NDB_GetElapsedTimeInSec()
time#=NDB_GetElapsedTimeInSec()
control$="CAR"
do
`this controls it like a rocket, press up to travel up in the direction of the rocket
`if the rocket is pointing down the rocket will go down when you press up, etc.
set cursor 0,0
print "(1) Car controls"
print "(2) Rocket controls"
if keystate(3)=1 then control$="ROCKET"
if keystate(2)=1 then control$="CAR"
if control$="ROCKET"
add_relative_velocity(6,0,(upkey()-downkey())*60*time#,0)
`w/a/s/d controls the angle acceleration, left/right arrow keys roll the rocket
add_relative_omega(6,(keystate(17)-keystate(31))*10*time#,(rightkey()-leftkey())*10*time#,(keystate(30)-keystate(32))*10*time#)
endif
if control$="CAR"
add_relative_velocity(6,0,0,(upkey()-downkey())*60*time#)
add_relative_omega(6,0,(rightkey()-leftkey())*10*time#,0)
endif
`update the newton stuff
time#=NDB_GetElapsedTimeInSec()
NDB_NewtonUpdate time#
`mouse look camera
gosub camera
sync
loop
camera:
rotate camera wrapvalue(camera angle x()+mousemovey()),wrapvalue(camera angle y()+mousemovex()),0
if mouseclick()<>0
inc move#,0.1
if mouseclick()=1 then move camera move# else move camera -move#
else
move#=0
endif
return
function make_box(obj_num,xs#,ys#,zs#,mass#,group_id,move)
`this makes a newton box
make object box obj_num, xs#,ys#,zs#
col = NDB_NewtonCreateBox(xs#,ys#,zs#)
body=NDB_NewtonCreateBody(col)
NDB_BodySetDBProData body, obj_num
NDB_SetVector 0,0,0
NDB_SetVectorToRadians
NDB_NewtonSetEulerAngle
NDB_SetVector 0,0,0, 1.0
NDB_SetMatrixPosRow
NDB_NewtonBodySetMatrix body
` NDB_NewtonBodySetDestructorCallback body
NDB_NewtonBodySetMaterialGroupID body, group_id
NDB_NewtonBodySetTransformCallback body
if move=1
NDB_NewtonBodySetForceAndTorqueCallback body
NDB_CalculateMIBoxSolid mass#,xs#,ys#,zs#
NDB_NewtonBodySetMassMatrix Body, mass#
NDB_NewtonBodySetAutoFreeze body, 0
else
NDB_NewtonBodySetAutoFreeze body, 1
endif
obj(obj_num).move=move
obj(obj_num).body=body
endfunction body
function set_object(objnum,xp#,yp#,zp#,xr#,yr#,zr#)
NDB_SetVector xr#,yr#,zr#
NDB_SetVectorToRadians
NDB_NewtonSetEulerAngle
NDB_SetVector xp#, yp#, zp#, 1.0
NDB_SetMatrixPosRow
NDB_NewtonBodySetMatrix obj(objnum).body
position object objnum,xp#,yp#,zp#
rotate object objnum,wrapvalue(xr#),wrapvalue(yr#),wrapvalue(zr#)
endfunction
function set_velocity(objnum,x#,y#,z#)
REM set the speed of a body
NDB_SetVector x#,y#,z#
NDB_NewtonBodySetVelocity obj(objnum).body
endfunction
function set_omega(objnum,x#,y#,z#)
REM set the omega(rotational velocity) of a body
NDB_SetVector x#,y#,z#
NDB_NewtonBodySetOmega obj(objnum).body
endfunction
function add_velocity(objnum,x#,y#,z#)
REM adds velocity to the body
`get speed of body
NDB_NewtonBodyGetVelocity obj(objnum).body
`add input velocities
NDB_SetVector NDB_GetVector_X()+x#, NDB_GetVector_Y()+y#, NDB_GetVector_Z()+z#
`set the speed of a body
NDB_NewtonBodySetVelocity obj(objnum).body
endfunction
function add_omega(objnum,x#,y#,z#)
REM set the speed of a body
NDB_NewtonBodyGetOmega obj(objnum).body
`add input velocities
NDB_SetVector NDB_GetVector_X()+x#, NDB_GetVector_Y()+y#, NDB_GetVector_Z()+z#
NDB_NewtonBodySetOmega obj(objnum).body
endfunction
function add_relative_velocity(objnum,x#,y#,z#)
NDB_NewtonBodyGetMatrix obj(objnum).body
NDB_GetMatrix
x_x# = NDB_GetVector_X(1)
x_y# = NDB_GetVector_Y(1)
x_z# = NDB_GetVector_Z(1)
y_x# = NDB_GetVector_X(2)
y_y# = NDB_GetVector_Y(2)
y_z# = NDB_GetVector_Z(2)
z_x# = NDB_GetVector_X(3)
z_y# = NDB_GetVector_Y(3)
z_z# = NDB_GetVector_Z(3)
ax#=x#*x_x#+y#*y_x#+z#*z_x#
ay#=x#*x_y#+y#*y_y#+z#*z_y#
az#=x#*x_z#+y#*y_z#+z#*z_z#
NDB_NewtonBodyGetVelocity obj(objnum).body
`add input velocities
NDB_SetVector NDB_GetVector_X()+ax#, NDB_GetVector_Y()+ay#, NDB_GetVector_Z()+az#
`set the speed of a body
NDB_NewtonBodySetVelocity obj(objnum).body
endfunction
function set_relative_velocity(objnum,x#,y#,z#)
NDB_NewtonBodyGetMatrix obj(objnum).body
NDB_GetMatrix
x_x# = NDB_GetVector_X(1)
x_y# = NDB_GetVector_Y(1)
x_z# = NDB_GetVector_Z(1)
y_x# = NDB_GetVector_X(2)
y_y# = NDB_GetVector_Y(2)
y_z# = NDB_GetVector_Z(2)
z_x# = NDB_GetVector_X(3)
z_y# = NDB_GetVector_Y(3)
z_z# = NDB_GetVector_Z(3)
ax#=x#*x_x#+y#*y_x#+z#*z_x#
ay#=x#*x_y#+y#*y_y#+z#*z_y#
az#=x#*x_z#+y#*y_z#+z#*z_z#
NDB_SetVector ax#, ay#, az#
`set the speed of a body
NDB_NewtonBodySetVelocity obj(objnum).body
endfunction
function add_relative_omega(objnum,x#,y#,z#)
NDB_NewtonBodyGetMatrix obj(objnum).body
NDB_GetMatrix
x_x# = NDB_GetVector_X(1)
x_y# = NDB_GetVector_Y(1)
x_z# = NDB_GetVector_Z(1)
y_x# = NDB_GetVector_X(2)
y_y# = NDB_GetVector_Y(2)
y_z# = NDB_GetVector_Z(2)
z_x# = NDB_GetVector_X(3)
z_y# = NDB_GetVector_Y(3)
z_z# = NDB_GetVector_Z(3)
ax#=x#*x_x#+y#*y_x#+z#*z_x#
ay#=x#*x_y#+y#*y_y#+z#*z_y#
az#=x#*x_z#+y#*y_z#+z#*z_z#
NDB_NewtonBodyGetOmega obj(objnum).body
`add input velocities
NDB_SetVector NDB_GetVector_X()+ax#, NDB_GetVector_Y()+ay#, NDB_GetVector_Z()+az#
`set the speed of a body
NDB_NewtonBodySetOmega obj(objnum).body
endfunction
function set_relative_omega(objnum,x#,y#,z#)
NDB_NewtonBodyGetMatrix obj(objnum).body
NDB_GetMatrix
x_x# = NDB_GetVector_X(1)
x_y# = NDB_GetVector_Y(1)
x_z# = NDB_GetVector_Z(1)
y_x# = NDB_GetVector_X(2)
y_y# = NDB_GetVector_Y(2)
y_z# = NDB_GetVector_Z(2)
z_x# = NDB_GetVector_X(3)
z_y# = NDB_GetVector_Y(3)
z_z# = NDB_GetVector_Z(3)
ax#=x#*x_x#+y#*y_x#+z#*z_x#
ay#=x#*x_y#+y#*y_y#+z#*z_y#
az#=x#*x_z#+y#*y_z#+z#*z_z#
NDB_SetVector ax#, ay#, az#
`set the speed of a body
NDB_NewtonBodySetOmega obj(objnum).body
endfunction
Controls arrow keys for turn left/right and forward/backward during car controls, during rocket controls arrow keys are same but up/down control the up/down acceleration, and WASD control the x and z axis rotational acceleration.
-----------------------------------
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.