This code shows how to move a perfectly stable object (same resultat as
Move Object), but the player as an infinite force, so he can push all the other bodies (even with a huge mass).
+ Code Snippet`standard DBPro startup code...
sync on
sync rate 60
autocam off
set ambient light 70
set point light 0, 50, 100, -50
set light range 0, 5000
randomize timer()
Type Character
angle As Float
body As Integer
object As Integer
speed As Float
EndType
Global player As Character
`Initialize Newton, and create the Newton World.
NDB_NewtonCreate
NDB_SetVector 0.0, -100.0, 0.0
NDB_SetStandardGravity
time# = NDB_GetElapsedTimeInSec()
time# = NDB_GetElapsedTimeInSec()
rem floor object
makeBox(0,-100,0, 1000, 10, 1000, 0)
rem a few random boxes
For b=0 to 20
s=10+rnd(20)
makeBox(rnd(200)-100,20+rnd(60),rnd(200)-100, s+rnd(4), s+rnd(4), s+rnd(4), 120+rnd(80))
Next b
rem player object
makeCharacter()
player.speed = 100
Position camera 0,40,-200
Point Camera 0,-60,0
do
`get the elapsed time since last frame and save into time# variable.
time# = NDB_GetElapsedTimeInSec()
NDB_NewtonUpdate time#
if controlkey()
obj = NDB_DebugMakeNewtonObject()
set object light obj, 0
else
obj = 0
endif
updateCharacter()
`update the screen
sync
if obj <> 0
delete object obj
endif
loop
` Destory the Newton world, clearing up any memory that was allocated.
NDB_NewtonDestroy
end
Function updateCharacter()
NDB_NewtonBodyGetVelocity player.body
If UpKey()
NDB_SetVector player.speed * sin(player.angle), NDB_GetVector_Y(), player.speed * cos(player.angle)
Else
NDB_SetVector 0, NDB_GetVector_Y(), 0
EndIf
NDB_NewtonBodySetVelocity player.body
player.angle = wrapvalue(player.angle + (RightKey()-Leftkey())*4)
rem object update
NDB_BodyGetPosition player.body
Position object player.object, NDB_GetVector_X(), NDB_GetVector_Y(), NDB_GetVector_Z()
rotate object player.object, 0, player.angle, 0
EndFunction
Function makeCharacter()
col = NDB_NewtonCreateBox( 40, 40, 40 )
body = NDB_NewtonCreateBody( col )
NDB_BuildMatrix 0.0, 0.0, 0.0, 0, 0, 0
NDB_NewtonBodySetMAtrix body
NDB_CalculateMIBoxSolid 100, 40, 40, 40
NDB_NewtonBodySetMassMatrix Body, 100
NDB_BodySetGravity Body, 1
NDB_NewtonReleaseCollision Col
NDB_NewtonBodySetAutoFreeze body, 0
NDB_SetVector 0,1,0
joint = NDB_NewtonConstraintCreateUpVector(body)
NDB_NewtonBodySetMaterialGroupID body, mplayer
NDB_NewtonBodySetLinearDamping body,0.0
obj = FreeObject()
Make object box obj, 40, 40, 40
position object obj, 0, 0, 0
color object obj, GetColor()
set object ambience obj, 50
player.body = body
player.object = obj
EndFunction character
Function makeBox(x#,y#,z#, sx#, sy#, sz#, mass#)
col = NDB_NewtonCreateBox( sx#, sy#, sz# )
body = NDB_NewtonCreateBody( col )
NDB_BuildMatrix 0.0, 0.0, 0.0, x#, y#, z#
NDB_NewtonBodySetMAtrix body
If mass# > 0
NDB_CalculateMIBoxSolid mass#, sx#, sy#, sz#
NDB_NewtonBodySetMassMatrix Body, mass#
NDB_BodySetGravity Body, 1
EndIf
NDB_NewtonReleaseCollision Col
obj = FreeObject()
Make object box obj, sx#, sy#, sz#
position object obj, x#, y#, z#
color object obj, GetColor()
set object ambience obj, 50
NDB_BodySetDBProData Body, obj
NDB_NewtonBodySetDestructorCallback Body
NDB_NewtonBodySetMaterialGroupID body, mfloor
EndFunction body
function FreeObject()
repeat
inc i
if object exist(i)=0 then found=1
until found
endfunction i
function GetColor()
repeat
r = rnd(1)*255
g = rnd(1)*255
b = rnd(1)*255
until r<>0 or g<>0 or b<> 0
color = rgb(r,g,b)
endfunction color
delete memblock 1
I think I'll use a function like the one in the fps exemple, it's not as stable, but rather good.
About bullet collisions on a character, I think it's not a good idea to rely only on a rigid body (especially if your character is animated). This is what I want to do : first, we have to check the collision with the character bounding box, then, if the bullet hits the box, we have to position some boxes/cylinders on each bone of the model, and finally restart the raycasting with the new boxes. This allows you to have an accurate and rather fast collision detection. Once done, it's pretty easy to have a rag doll using a force to simulate the bullet impact.