Hello. I tried stripping down the DBPro Newton Physics Wrapper FPS demo to only include the movement code (and initialisation). The demo doesn't work for me when compiled anyway (after the player moves a certain distance, he goes the same distance in the opposite direction, basically sliding around uncontrollably). I thought maybe I could figure out what was wrong after doing this, but I couldn't even compile it (crashed every time I tried). Any ideas on what I've done wrong? EDIT- I found out it has something to do with the environment loading part, but I'm still not sure what. EDIT2- Just noticed I accidently left in the old movement code, but that wasn't the source of the problem. EDIT3- Nevermind on that second problem, I noticed that I had omitted the "NeverCalled()" function (which I still don't really understand).
sync on:sync rate 120
hide mouse
global Player = 0
EnvironmentObj=1
global Player_Mass# = 12.0
global Player_Speed# = 10.0
global Player_Yangle# = 0.0
global Player_Xangle# = 0.0
NDB_NewtonCreate
NDB_SetVector 0.0, -50.0, 0.0
NDB_SetStandardGravity
`make environment
load object "room.x", EnvironmentObj
Col = NDB_NewtonCreateTreeCollision ( EnvironmentObj )
Room = NDB_NewtonCreateBody ( Col )
NDB_BodySetDBProData Room, EnvironmentObj
NDB_NewtonBodySetDestructorCallback Room
NDB_NewtonReleaseCollision Col
`make player
Sphere = NDB_NewtonCreateSphere( 2.5, 6.0, 2.5 )
Player = NDB_NewtonCreateBody( Sphere )
NDB_BuildMatrix 0.0, 0.0, 0.0, 0.0, -1.0, -25.0
NDB_NewtonBodySetMatrix Player
NDB_SetVector 6.0, 8.0, 6.0
NDB_CalculateMIBoxSolid Player_Mass#
NDB_NewtonBodySetMassMatrix Player, Player_Mass#
NDB_NewtonBodySetAutoFreeze Player, 0
NDB_BodySetGravity Player, 1
NDB_SetVector 0.0, 1.0, 0.0
UpVector = NDB_NewtonConstraintCreateUpVector( Player )
Default = NDB_NewtonMaterialGetDefaultGroupID()
PlayerID = NDB_NewtonMaterialCreateGroupID()
NDB_NewtonMaterialSetDefaultFriction Default, PlayerID, 0.01, 0.01
NDB_NewtonMaterialSetDefaultElasticity Default, PlayerID, 0.01
NDB_NewtonBodySetMaterialGroupID Player, PlayerID
`etc
time# = NDB_GetElapsedTimeInSec()
time# = NDB_GetElapsedTimeInSec()
T=timer()
MillisecondsDelay=10
do
Elapsed=timer()-T
TimeLeft=(SecondsDelay-Elapsed)+Incrementer
if TimeLeft<=0 then Pulse=1 : Incrementer=Incrementer+MillisecondsDelay
time# = NDB_GetElapsedTimeInSec()
NDB_NewtonUpdate time#
if upkey()=1 then move object PlayerObjNum, 0.2
if downkey()=1 then move object PlayerObjNum, -0.2
if leftkey()=1 then move object left PlayerObjNum, 0.1
if rightkey()=1 then move object right PlayerObjNum, 0.1
inc Player_Xangle#, mousemovey() * 0.25
inc Player_Yangle#, mousemovex() * 0.25
position mouse 320, 240
if camera angle x()<-90 then xrotate camera -90
if camera angle x()>90 then xrotate camera 90
MoveX# = 0.0
MoveZ# = 0.0
if keystate(17) or keystate(31)
`player is pressing "w" or "s" key, we need to move forward or backward!
`find direction vector my moving the camera...
`i use some trig to find the direction the player wants to move.
dx# = sin(Player_Yangle#)
dz# = cos(Player_Yangle#)
`then I add this to the move vector, making it positive for forward, negative for backward.
inc MoveX#, dx# * (keystate(17)-keystate(31)) :`makes a positive vector with "w", negative with "s"
inc MoveZ#, dz# * (keystate(17)-keystate(31))
endif
if keystate(30) or keystate(32)
`same thing here, but this is for strafing, so I add 90 degrees to the current angle!
dx# = sin(Player_Yangle#+90.0)
dz# = cos(Player_Yangle#+90.0)
inc MoveX#, dx# * (keystate(32)-keystate(30)) :`makes a positive vector with "d", negative with "a"
inc MoveZ#, dz# * (keystate(32)-keystate(30))
endif
length# = sqrt( (MoveX#^2)+(MoveZ#^2) )
MoveX# = MoveX# / length#
MoveZ# = MoveZ# / length#
NDB_NewtonBodyGetVelocity Player
CurrentVel_X# = NDB_GetVector_X()
CurrentVel_Y# = NDB_GetVector_Y()
CurrentVel_Z# = NDB_GetVector_Z()
GoalVel_X# = MoveX# * (Player_Speed# * ((shiftkey()+1)*2) )
GoalVel_Z# = MoveZ# * (Player_Speed# * ((shiftkey()+1)*2) )
AccelX# = 0.3 * ((GoalVel_X# - CurrentVel_X#) / time#)
AccelZ# = 0.3 * ((GoalVel_Z# - CurrentVel_Z#) / time#)
if AccelX# > 200.0 then AccelX# = 200.0
if AccelX# < -200.0 then AccelX# = -200.0
if AccelZ# > 200.0 then AccelZ# = 200.0
if AccelZ# < -200.0 then AccelZ# = -200.0
NDB_BodyGetPosition Player
`set temp vector 2 to force direction
NDB_SetVector 2, AccelX#, 0.0, AccelZ#
NDB_BodyAddForceGlobal Player
sync
loop
Also, I get screen flicker and a low (no greater than 30) FPS on anything compiled using Newton. I am using what I assume is the latest version of the Newton wrapper (from walaber.com) and the latest version of DBPro.
Thirdly, why bother using Newton at all (aside from the presumably greater speed)? I notice that just the movement code in the FPS example is about 60 lines, not including comments. I could easily code some simple, convincing FPS movement physics in much less than that. Why is it that you can't simply apply a certain force in a certain direction, then let Newton do the rest?
Thanks for any input.