haha hi again dudes.. I am trying to fight against Newton engine.. It's really cool but bad as a giant black spider..
Take a look to this really simple code. If I press UP arrow key I give permission to a newton force to push my cube in X coord.
If I press DOWN arrow I remove the permission to that newton force.
All that using the ok_movement variable 1 or 0.
This is the main loop of my program:
ok_movement as integer
ok_movement = 0
DO
print ("ok_movement = ")
print (ok_movement)
rem IF I PUSH UP ARROW KEY SO I GIVE PERMISSION TO NEWTON FORCE TO rem PUSH MY CUBE IN X COORD
If upkey()= 1 then ok_movement = 1
if downkey()=1 then ok_movement = 0
IF ok_movement = 1
NDB_SetVector 100.0, 0.0, 0.0
NDB_NewtonBodySetVelocity cube1
ENDIF
`gets the amount ot time elapsed since the last call to this function, in seconds.
time# = NDB_GetElapsedTimeInSec()
`then the big command that updates the physics world. you tell it how much
`time has passed, and it updates that much.
NDB_NewtonUpdate time#
LOOP
You can notice the variable ok_movement is set to 0 before the start of main loop.
So, this code is NOT WORKING.. I can give and remove the permission (the ok_movement flag) to the Newton force but nothing appear and nothing moving...
Ok so now take a look at the following code:
ok_movement as integer
ok_movement = 0
DO
print ("ok_movement = ")
print (ok_movement)
rem IF I PUSH UP ARROW KEY SO I GIVE PERMISSION TO NEWTON FORCE TO rem PUSH MY CUBE IN X COORD
If upkey()= 1 then ok_movement = 1
if downkey()=1 then ok_movement = 0
IF ok_movement = 1
NDB_SetVector 100.0, 0.0, 0.0
NDB_NewtonBodySetVelocity cube1
ENDIF
`gets the amount ot time elapsed since the last call to this function, in seconds.
time# = NDB_GetElapsedTimeInSec()
`then the big command that updates the physics world. you tell it how much
`time has passed, and it updates that much.
NDB_NewtonUpdate time#
LOOP
yes, exactly, it's the same code of before but in this case the variable ok_movement is set to 1 before the main loop..
So when the main loop start the cube is receiving the Newton push until I press the DOWN arrow key.
So with DOWN key I stopped the cube and now (BEFORE THE CUBE STOPPED COMPLETLY), If I press UP key again so the cube back to move! (BUT ONLY IF I do not waited for it to stop completely!!)
IN FINISH:
If I start the main loop with the ok_movement variable set (before the main loop) to 0, never I will can use the Newton force to push my cube
But If I start the main loop with previously set the ok_variable to 1 so I get my cube moving and from now I can stop it and back to let it get pushed again.. (BUT ONLY IF NEVER IT STOPPED COMPLETLY....... IF NOT NEVER AGAIN I WILL CAN MOVE IT AGAIN....)
WHY THE HELL ALL THAT?? Sorry but I can reach it..
PS: here the complete code if you want to test it:
make matrix 1,1000,1000,10,10
load image "grass.jpg",1
position matrix 1,0,0,0
prepare matrix texture 1,1,1,1
rem SET MATRIX WIREFRAME ON 1
update matrix 1
`this first command initializes the Newton world, and must be called before
`any other Newton commands. to remove newton, use the NDB_NewtonDestroy command.
NDB_NewtonCreate
`We need to set the World limits, this is very important to do otherwise there
`could be some unpredictable results
NDB_SetVector 1, -500.0, -500.0, -500.0
NDB_SetVector 2, 1500.0, 500.0, 1500.0
NDB_NewtonSetWorldSize
`okay, we have a newto world now. let's make our cube!
`first we make the DBPro cube, like always.
make object cube 1, 100
make object cube 2, 30
`now we must make a cube just like this one, that will exist in the Newton world.
`this command makes a "collision object", which is how we describe the shape of an
`object to Newton. there are no "cubes" in newton, so we make a "box", with 100 for
`each dimension (length, width, height).
Col1 = NDB_NewtonCreateBox( 100.0, 100.0, 100.0 )
Col2 = NDB_NewtonCreateBox( 30.0, 30.0, 30.0 )
`okay, now our variable "Col" holds an index to this collision. we'll use this to create
`a "rigid body", which is an object in the Newton world (like our DBPro cube is an object in
`the visual world of dbpro).
Cube1 = NDB_NewtonCreateBody( Col1 )
Cube2 = NDB_NewtonCreateBody( Col2 )
`next, we need to set a mass for our cube, otherwise Newton will think that it's a static object.
`for now, don't worry too much about the other values, they deal with how much a body resists
`rotation around different axis... look at the docs for more info.
NDB_NewtonBodySetMassMatrix Cube1, 10.0, 1.0, 1.0, 1.0
NDB_NewtonBodySetMassMatrix Cube2, 10.0, 1.0, 1.0, 1.0
`now "Cube" holds an index to our new rigid body, with the shape of Col (which we described
`as a box 100x100x100 above).
`next, we will "connect" the Newton Rigid Body to the DBPro cube. this will let Newton
`position and rotate the DBPro object for us, based on the physics it calculates!
NDB_BodySetDBProData Cube1, 1
NDB_BodySetDBProData Cube2, 2
NDB_NewtonUpdate 0.1
`that assigned DBpro object# 1 to the Rigid Body indexed by "Cube".
`finally, we want the cube to spin, so we'll set an initial "Omega", which is just a name
`for "spin". this command is a good example of how you do things with newton... because many
`commands ask for a 3D vector (x,y,z), and sometimes commands actually RETURN a 3D vector, I had
`to come up with a simple way to get vectors in and out of Newton. as you know, a function cannot
`return more than 1 value in DBPro, so I came up with the "temp vector" system in my wrapper. basically,
`the wrapper has 4 internal "temp" vectors for temporarily storing data. so for example this Omega
`command wants you to tell it 3 values, the spin around each axis. rather than doing this:
` NDB_NewtonBodySetOmega Body, x#, y#, z#
`Instead, with the wrapper I require that you do this:
` NDB_SetVector x#, y#, z#
` NDB_NewtonBodySetOmega Cube
`
`This makes the system cleaner, I promise. anyway, you'll get used to it.
`okay so let's set an inital OMEGA for the cube body!
rem , X , Y , Z
rem NDB_SetVector 0.0 , 10.0 , 0.0
rem NDB_NewtonBodySetOmega Cube1
NDB_SetVector 10.0 , 0.0 , 0.0
NDB_NewtonBodySetOmega Cube2
`you'll notice that I only put in a value for the Y axis... so the cube should spin around
`the Y axis only, just like the simple DBPro demo.
cube1_DBpro = NDB_GetDBPro(cube1)
cube2_DBpro = NDB_GetDBPro(cube2)
NDB_BuildMatrix 0.0, 0.0, 0.0, 122.0, 55.0, 150.0
Rem This command reads the matrix created above and rotates and position the object
Rem bbox(obj) regarding to the content of the matrix
NDB_NewtonBodySetMatrix cube1
remstart
Position object cube1_DBpro,140,5,140
Position object cube2_DBpro,340,5,140
remend
position camera -260,3,-260
`okay, that's all of the setup we need, let's make our main loop!
ok_movement as integer
ok_movement = 1
do
print ("ok_movement = ")
print (ok_movement)
rem simple control of the player object
remstart
if upkey()=1 then move object cube1_DBpro,0.2
if downkey()=1 then move object cube1_DBpro,-0.2
if leftkey() = 1 then turn object left cube1_DBpro,0.1
if rightkey() = 1 then turn object right cube1_DBpro,0.1
remend
If upkey()= 1 then ok_movement = 1
if downkey()=1 then ok_movement = 0
IF ok_movement = 1
NDB_SetVector 100.0, 0.0, 0.0
NDB_NewtonBodySetVelocity cube1
ENDIF
rem mouse view
if mouseclick()=1 then cf#=1
if mouseclick()=2 then cf#=-1
cx#=wrapvalue(cx#+mousemovey()*0.5)
cy#=wrapvalue(cy#+mousemovex()*0.5)
if cx#<=290 and cx#>180 then cx#=290
if cx#>=70 and cx#<=180 then cx#=70
acx#=curveangle(cx#,acx#,2.1)
acy#=curveangle(cy#,acy#,2.1)
rotate camera acx#,acy#,0
acf#=curvevalue(cf#,acf#,20)
move camera acf#
cf#=0
`gets the amount ot time elapsed since the last call to this function, in seconds.
time# = NDB_GetElapsedTimeInSec()
`then the big command that updates the physics world. you tell it how much
`time has passed, and it updates that much.
NDB_NewtonUpdate time#
loop