1. A Little Help
- Posted by Icy_Viking May 29, 2019
- 1251 views
Hello all,
As some of you know, I am currently wrapping the Newton Game Dynamics physics library. While I haven't wrapped all of it yet, I decided I'd make a small test program to make sure all is well. Anyways, the problem I'm having is I can't get the window to stay open after the physics init function is called. Any help would be appericated.
NOTE: The example program is based off the simple tutorial included in the Newton Dynamics pacakage.
//C example program #include <stdio.h> #include "Newton.h" int main (int argc, const char * argv[]) { // Print the library version. printf("Hello, this is Newton version %d\n", NewtonWorldGetVersion()); // Create the Newton world. NewtonWorld* const world = NewtonCreate(); // Step the (empty) world 60 times in increments of 1/60 second. const float timestep = 1.0f / 60; for(int i=0; i<60; i++) { NewtonUpdate(world, timestep); } // Clean up. NewtonDestroyAllBodies(world); NewtonDestroy(world); return 0; }
--Wrapper Code public constant xNewtonCreate = define_c_func(nd,"NewtonCreate",{},C_POINTER) public function NewtonCreate() return c_func(xNewtonCreate,{}) end function xNewtonUpdate = define_c_proc(nd,"NewtonUpdate",{C_POINTER,C_FLOAT}) public procedure NewtonUpdate(atom world,atom timeStep) c_proc(xNewtonUpdate,{world,timeStep}) end procedure public constant xNewtonDestroy = define_c_proc(nd,"NewtonDestroy",{C_POINTER}) public procedure NewtonDestroy(atom world) c_proc(xNewtonDestroy,{world}) end procedure constant xNewtonDestroyAllBodies = define_c_proc(nd,"NewtonDestroyAllBodies",{C_POINTER}) public procedure NewtonDestroyAllBodies(atom world) c_proc(xNewtonDestroyAllBodies,{world}) end procedure
--Example include std/machine.e include std/get.e include NGD.ew atom key position(1,1) puts(1,"Press ESC to close\n") atom ver = NewtonWorldGetVersion() --this works public atom world atom timeStep = 1.0 / 60 position(2,1) printf(1,"Newton Game Dynamics Version: %d",{ver}) --this works procedure Init() world = NewtonCreate() end procedure while 1 do Init() --as soon as this is called, the window closes rapidly key = get_key() if key = 27 then exit end if for i = 0 to 60 do NewtonUpdate(world,timeStep) end for NewtonDestroyAllBodies(world) NewtonDestroy(world) end while
2. Re: A Little Help
- Posted by euphoric (admin) May 29, 2019
- 1236 views
Looks like your end while is in the wrong place. (And the C example looks wrong...)
--Example include std/machine.e include std/get.e include NGD.ew atom key position(1,1) puts(1,"Press ESC to close\n") atom ver = NewtonWorldGetVersion() --this works public atom world atom timeStep = 1.0 / 60 position(2,1) printf(1,"Newton Game Dynamics Version: %d",{ver}) --this works procedure Init() world = NewtonCreate() end procedure Init() -- only need to call once (?) while 1 do key = get_key() if key = 27 then exit end if -- not sure if this should be inside or outside while loop -- you might want to refactor so you don't have a for loop inside your while -- you need a timeDelta or something else depending on the frame rate for i = 0 to 60 do NewtonUpdate(world,timeStep) end for end while -- should go here? NewtonDestroyAllBodies(world) NewtonDestroy(world)
3. Re: A Little Help
- Posted by Icy_Viking May 29, 2019
- 1240 views
Thanks for the tips. I've removed the for loop outside of the while loops. The window now stays open. Yeah I thought the C example was bad. I don't think it really works besides showing the physics library version number.
4. Re: A Little Help
- Posted by euphoric (admin) May 29, 2019
- 1240 views
Thanks for the tips. I've removed the for loop outside of the while loops. The window now stays open. Yeah I thought the C example was bad. I don't think it really works besides showing the physics library version number.
It's likely that NewtonUpdate() needs to be called each frame, so determine how many frames you're getting, and use that to determine your timeStep.
integer frames = 0 atom elapsedTime = time()+1 while 1 do key = get_key() if key = 27 then exit end if frames += 1 -- if it's been a second since last calc, re-calc if time() >= elapsedTime then timeStep = elapsedTime / frames frames = 0 elapsedTime = time()+1 end if NewtonUpdate(world,timeStep) end while
In lots of game engines I've played with, the "timeStep" is referred to as the "delta time." You might want to look at that, or see if Newton has some docs on it. You have to use that to get consistent game speed across multiple processors.
5. Re: A Little Help
- Posted by Icy_Viking May 29, 2019
- 1238 views
Thanks for the tips. I've removed the for loop outside of the while loops. The window now stays open. Yeah I thought the C example was bad. I don't think it really works besides showing the physics library version number.
It's likely that NewtonUpdate() needs to be called each frame, so determine how many frames you're getting, and use that to determine your timeStep.
integer frames = 0 atom elapsedTime = time()+1 while 1 do key = get_key() if key = 27 then exit end if frames += 1 -- if it's been a second since last calc, re-calc if time() >= elapsedTime then timeStep = elapsedTime / frames frames = 0 elapsedTime = time()+1 end if NewtonUpdate(world,timeStep) end while
In lots of game engines I've played with, the "timeStep" is referred to as the "delta time." You might want to look at that, or see if Newton has some docs on it. You have to use that to get consistent game speed across multiple processors.
Thanks for helping. I tried making an update method according to your code, but once again the window closes too fast. Note, that it only closed rapdily when the update method is called.
integer frames = 0 atom elapsedTime = time()+1 procedure Update() NewtonUpdate(world,timeStep) end procedure while 1 do key = get_key() if key = 27 then exit end if frames += 1 if time() >= elapsedTime then timeStep = elapsedTime / frames frames = 0 elapsedTime = time()+1 end if Update() end while NewtonDestroyAllBodies(world) NewtonDestroy(world)
7. Re: A Little Help
- Posted by Icy_Viking May 29, 2019
- 1222 views
What does the ex.err say?
That's the thing, there is no ex.err, lol. The window pops up, but vanishes quickly, before anything can be read. However, the window stays open if the NewtonUpdate() method is NOT called.