I've been working on making a little demo that attempts to hack some believable tank movement out of DarkPhysics. At first, I was relying on syncrate and vsync to throttle the physics so it maintains a steady rate of flow. But I figured I'd try to throw together a timer for updating the physics so that the game can run at much higher FPS without the entire thing speeding up.
I have it working, but the tank seems to jitter when it moves now. Here's my code, and attached is the code and media.
SET DISPLAY MODE 1024, 768, 32, 0
sync on
sync rate 0
backdrop on
autocam off
set camera range 0.5, 30000
`set camera aspect 0.625
phy start
phy set continuous cd 1
`set up some globals to be used later
global maxmotor as float : maxmotor = 400
global camang as float
global camdist as float : camdist = 7.5
global mforce as float
global lasttimer as dword : lasttimer = timer()
global curtimer as dword
global phyupdatetime as dword : phyupdatetime = 10 `minimum time before next physics update in miliseconds
` movement
g_fSpeed# = 0.05
g_fTurn# = 0.3
` set the directory to media
`set dir "media"
` load base and detail texture
load image "texture.bmp", 101
load image "detail.tga", 102
make object terrain 1 ` create the terrain object
set terrain heightmap 1, "map.bmp" ` set the heightmap
set terrain scale 1, 12, 0.6, 12 ` set the scale
set terrain split 1, 8 ` split value by 16 * 16
set terrain tiling 1, 1 ` detail map tiling
set terrain light 1, 1, -0.25, 0, 1, 1, 0.78, 0.5 ` light - xdir, ydir, zdir, red, green, blue, intensity
set terrain texture 1, 101, 102 ` base and detail texture
build terrain 1 ` finally build the terrain
phy make rigid body static terrain 1
`Load tank
load object "H-Tank.x", 2
position camera 960,17,972
a# = get terrain ground height( 1, camera position x( ), camera position z( ) )
` now position the camera slightly above the terrain
position camera camera position x( ), a# + 3, camera position z()
position object 2, camera position x(), camera position y(), camera position z()
`correct the rotation of the tank so that it's facing forward.
rotate limb 2, 1, 0, -90, 0
`Be aware the vehicle width and length orientation
`this needs tweaking but i've no idea how to determine the proper values
width# = 1.5
height# = 0.8
length# = 4.4
wheelX# = 2.2
wheelY# = 0.0
wheelZ# = 3.0
radius# = 0.2
wheelHeight# = 0.1
`Create vehicle physics body
phy create vehicle 2
phy add vehicle body 2, length#, height#, width#, 0.0, height#, 0.0
phy add vehicle body 2, width#, height#, width#, 0.0, height#*2, 0.0
phy add vehicle wheel 2, 13, wheelZ#, wheelY#, wheelX#, radius#, wheelHeight#, 0, 1
phy add vehicle wheel 2, 11, wheelZ#, wheelY#, -wheelX#, radius#, wheelHeight#, 0, 1
phy add vehicle wheel 2, 7, -wheelZ#, wheelY#, -wheelX#, radius#, wheelHeight#, 1, 1
phy add vehicle wheel 2, 12, -wheelZ#, wheelY#, wheelX#, radius#, wheelHeight#, 1, 1
phy set vehicle max motor 2, maxmotor
steeringDelta# = 0.1 : phy set vehicle steering delta 2, steeringDelta#
phy set vehicle max steering angle 2, 0.4
`phy set vehicle auto 2, 0
phy set vehicle wheel multiplier 2,0.01
phy set vehicle suspension spring 2, 7500
phy set vehicle static friction 2, 1000
phy set vehicle dynamic friction 2, 1000
phy build vehicle 2
make object box 3, 0.1, 0.1, 0.1
hide object 3
global prevforce as float
global prevforce2 as float
global wheelmult as float
`Some sounds
load sound "idle.wav",1 : loop sound 1
load sound "bump.wav",2
hide mouse
color backdrop rgb(175,200,255)
fog on
fog color 175, 200, 255
fog distance 1000
global camdist:camdist = 10
global vel as float
do
rem Follow buggy
inc camang, mousemovex() * .05
inc camdist, mousemovez() * .005
rotcam(camang, camdist, 2)
information()
center text screen width()/2,30,"ARROWKEYS TO DRIVE, SPACE TO BRAKE, NUMPAD TO MOVE TURRET"
`Update physics and screen
curtimer = timer()
if (curtimer - lasttimer) >= phyupdatetime
driveCar()
update terrain
phy update
lasttimer = curtimer
endif
` final screen update
fastsync
loop
function drivecar()
`
rem movement
key = 0 `: force#=0.0
if upkey ( ) = 1
if mforce < maxmotor
mforce = mforce + 2
endif
key = 1
endif
if downkey ( ) = 1
if mforce > -maxmotor
mforce = mforce - 2
endif
key = 1
endif
if spacekey ( ) = 1
phy set rigid body linear damping 2, 5
else
phy set rigid body linear damping 2, 1
endif`and boost=0 then boost=50 : forcenoise#=2000.0
if leftkey() = 1 then Phy_RotateCharacterController(2, -66)
if rightkey() = 1 then Phy_RotateCharacterController(2, 66)
if keystate(75) `numpad 4, turn turret left
rotate limb 2, 3, 0, limb angle y(2, 3) - .25, 0
endif
if keystate(77) `numpad 6, turn turret right
rotate limb 2, 3, 0, limb angle y(2, 3) + .25, 0
endif
if keystate(76) `numpad 5, lower barrel
rotate limb 2, 4, limb angle x(2, 4) - .25, 0, 0
else `don't let both keys be used to make it move faster
if keystate(80) `numpad 2, lower barrel
rotate limb 2, 4, limb angle x(2, 4) - .25, 0, 0
endif
endif
if keystate(72) `numpad 8, raise barrel
rotate limb 2, 4, limb angle x(2, 4) + .25, 0, 0
endif
if key = 0
mforce = 0
endif
phy set vehicle motor force 2, mforce
set sound speed 1,18000+(vel*1000)
endfunction
function information()
` show some information
` start printing at top of screen
velx as float
vely as float
velz as float
fwdrev as string
fwdrev = "none"
velx = phy get rigid body linear velocity x (2)
vely = phy get rigid body linear velocity y (2)
velz = phy get rigid body linear velocity z (2)
vel = sqrt((velx * velx) + (vely * vely) + (velz * velz))
set cursor 0, 0
` show frame rate
print "fps = " + str$ ( screen fps ( ) )
print ""
` current camera position
print "Motor Force = " + str$( phy get vehicle motor force(2) )
print "Velocity = " + str$(vel)
print "Fwd/Rev = " + fwdrev
print ""
print "x = " + str$ ( object position x (2) )
print "y = " + str$ ( object position y (2) )
print "z = " + str$ ( object position z (2) )
print ""
` finally the polygon count
print "polygon count = " + str$ ( statistic ( 1 ) )
print ""
print "scancode = " + str$(scancode())
endfunction
Function rotcam(ang#,radius#,obj)
x#=object position x(obj)
y#=object position y(obj)
z#=object position z(obj)
position camera x#+(sin(ang#)*radius#),y#+(radius#/2.0),z#+(cos(ang#)*radius#)
point camera x#,y# + 5,z#
Endfunction
`Function Phy_MoveCharacterController(ControllerID, Speed as float)
``Move the object forward (along the z axis)
`phy add rigid body local force ControllerID, 0, 0, Speed, 4
`EndFunction
Function Phy_RotateCharacterController(ControllerID, YAngle as float)
`Rotate the controller along its y axis
Phy add rigid body local torque ControllerID, 0, YAngle, 0, 3
EndFunction
`Function Phy_MoveCharacterControllerLeft(ControllerID, Speed as float)
``Apply a negitive force along the x axis
`phy add rigid body local force ControllerID, -Speed, 0, 0, 4
`EndFunction
`Function Phy_MoveCharacterControllerRight(ControllerID, Speed as float)
``Apply a positive force along the x axis
`phy add rigid body local force ControllerID, Speed, 0, 0, 4
`EndFunction
`locks a camera to object, with offsets
function lockCameraToObject(camera as dword,lockTo as dword,xOff as float,yOff as float,zOff as float)
sx as float : sy as float : sz as float
cx as float : cy as float : cz as float
xPos as float : yPos as float : zPos as float
sx = sin(object angle x(lockTo)) : cx = cos(object angle x(lockTo))
sy = sin(object angle y(lockTo)) : cy = cos(object angle y(lockTo))
sz = sin(object angle z(lockTo)) : cz = cos(object angle z(lockTo))
xPos = object position x(lockTo) + xOff*(cy*cz) + yOff*(sx*sy*cz-cx*sz) + zOff*(cx*sy*cz+sx*sz)
yPos = object position y(lockTo) + xOff*(cy*sz) + yOff*(cx*cz+sx*sy*sz) + zOff*(cx*sy*sz-sx*cz)
zPos = object position z(lockTo) + xOff*(-1*sy) + yOff*(sx*cy) + zOff*(cx*cy)
position camera camera,xPos,yPos,zPos
set camera to object orientation camera,lockTo
endfunction
(EDIT)
I fixed it... It was the camera that was jittering. Had to move it into the update as well.
Anyone have any clue what things are safe to have outside of the timed update?
Nothing I say is intended to be rude. My autism means that I do not know what is rude and what isn't rude. I apologize if I seem rude. It is not my intention.