Hi All
How do you move an object in Newton is it the same way as in DBPro, If its in the demos I can't see it.
just want the box I've made wiuth Newton to move backwards and frowards on the ground its sitting on.
here's the code only go if you have Newton dll installed.
Rem Project: pennywaterfalls
Rem Created: 5/13/2005 3:08:50 PM
Rem ***** Main Source File *****
`standard DBPro startup code...
sync on
sync rate 60
autocam off
set ambient light 100
set point light 0, 50, 100, -50
set light range 0, 5000
position camera 0,0,30,-200
randomize timer()
load image "floor.png",1
`Initialize Newton, and create the Newton World.
NDB_NewtonCreate
`Set the temp vector to our gravity constant (acceleration due to gravity)
`then set as our standard gravity force.
NDB_SetVector 0.0, -50.0, 0.0
NDB_SetStandardGravity
`make the ground for objects to fall on.
gosub MakeFloor
`gosub makeplatfrom
MakeBox(0,10,0,100,10,100,0,0,0,100)
` the first time you call this command, you may get a very large time#,
` so I call it twice at the start, to make sure we're getting a nice small time# value.
time# = NDB_GetElapsedTimeInSec()
time# = NDB_GetElapsedTimeInSec()
`this variable is used for pausing/unpausing the simulation.
GO = 1
` -----------------------------------
` MAIN LOOP
` -----------------------------------
do
`get the elapsed time since last frame and save into time# variable.
time# = NDB_GetElapsedTimeInSec()
if go = 1 then ndb_newtonupdate time#
`if the user presses the "c" key, make a random cylinder!
if lower$(inkey$()) = "c"
x# = 0
y# = 300
z# = 0
r# = 3
h# = 1
rx# = 360
ry# = 90
rz# = 360
mass# = 10.0
MakeCylinder(x#,y#,z#,r#,h#,rx#,ry#,rz#,mass#)
endif
if move#>-45 and done<>1
dec move# ,0.1
else
done=1
endif
if move#<=0 and done<>0
inc move# ,0.1
else
done=0
endif
position object plat,0,0,move#
sync
loop
makeplatfrom:
col= ndb_newtoncreatebox(100.0,10.0,100.0)
platfrom= ndb_newtoncreatebody(col)
obj = FreeObject()
load object "box.x", obj
position object obj, 0.0, -20.0, 0.0
scale object obj, (100.0*100.0), (10.0*100.0), (100.0*100.0)
color object obj, GetColor()
set object ambience obj, 50
scale object texture obj, 10.0, 10.0
texture object obj, 1
`this tells the wrapper that if I destroy the Floor rigid body,
`I also want the wrapper to delete the visual object as well.
NDB_NewtonBodySetDestructorCallback platfrom
`this command connects a visual object with a rigid body, making them a "pair"
NDB_BodySetDBProData platfrom, obj
platfromObj = obj
NDB_BodySetDBProData platfrom, obj
NDB_NewtonBodySetDestructorCallback platfrom
NDB_BodySetGravity platfrom, 1
return
`this subroutine makes the "floor" object. it is just like any other rigid body, except we never set a mass for it.
`Newton rigid bodies have a default mass of zero, so not setting a mass leaves the mass at the default value of zero.
`in Newton, objects with a mass of zero are immobile. this means that they will never move, no matter how hard you
`hit them with other objects. This is useful for making background levels, etc.
MakeFloor:
` GROUND OBJECT
` this makes a simple box with mass=0. this makes the object immobile, like a solid ground.
col = NDB_NewtonCreateBox( 1000.0, 10.0, 1000.0 )
Floor = NDB_NewtonCreateBody( col )
NDB_BuildMatrix 0.0, 0.0, 0.0, 0.0, -20.0, 0.0
NDB_NewtonBodySetMAtrix Floor
`now make a visual object to represent the floor onscreen... same size and position as
`the rigid body above.
obj = FreeObject()
load object "box.x", obj
position object obj, 0.0, -20.0, 0.0
scale object obj, (1000.0*100.0), (10.0*100.0), (1000.0*100.0)
scale object texture obj, 10.0, 10.0
texture object obj, 1
`this tells the wrapper that if I destroy the Floor rigid body,
`I also want the wrapper to delete the visual object as well.
NDB_NewtonBodySetDestructorCallback Floor
`this command connects a visual object with a rigid body, making them a "pair"
NDB_BodySetDBProData Floor, obj
FloorObj = obj
return
`this function is just like the others, but I make a cylinder, based on a radius and height value.
function MakeCylinder(x#,y#,z#,r#,h#,rx#,ry#,rz#,mass#)
Col = NDB_NewtonCreateCylinder( r#, h# )
Body = NDB_NewtonCreateBody(Col)
`Set initial position and rotation
NDB_BuildMatrix rx#, ry#, rz#, x#, y#, z#
NDB_NewtonBodySetMatrix Body
`calculate MI for a cylinder.
NDB_CalculateMICylinderSolid mass#, r#, h#
NDB_NewtonBodySetMassMatrix Body, mass#
NDB_NewtonReleaseCollision Col
obj = FreeObject()
load object "cylinder.x", obj
color object obj, GetColor()
set object ambience obj, 50
scale object obj, h#*100.0, r#*100.0, r#*100.0
position object obj, x#, y#, z#
rotate object obj, rx#, ry#, rz#
NDB_BodySetDBProData Body, obj
NDB_NewtonBodySetDestructorCallback Body
NDB_BodySetGravity Body, 1
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
`all of these functions are pretty similar, they just make different collision
`primitives. I'll explain the BOX in detail, and the others are all basically the same,
`except for the Convex Hulls, which I'll explain in detail as well.
function MakeBox(x#,y#,z#,sx#,sy#,sz#,rx#,ry#,rz#,mass#)
`the basic process when making a Newton Rigid Body is this:
`Step 1- make a "collision object". this defines the shape of the body you want to make.
` in this case, we're making a simple box object with size sx#, sy#, sz#.
` the newly created box collision is saved into the variable "Col", so we can reference
` it later.
Col = NDB_NewtonCreateBox(sx#, sy#, sz#)
`Step 2- Now we make the actual Rigid Body. when we do so, we must tell Newton the shape of
` the body, which we get from the collision data we just made. so, we pass the "Col"
` variable into this function, and it returns an index to the new rigid body into the
` variable "Body". we'll use "Body" to refer to this Rigid Body for the rest of the
` function.
Body = NDB_NewtonCreateBody(Col)
`Step 3- set the initial position and rotation of the rigid body. Newton does not use
` angles like DBPro, instead it uses transformation matrices to describe the position
` and rotation of a body. But the wrapper has a function to create these matrices for you
` based on position and angles, like you would use in DBpro. you set both the position
` and the rotation in a single command. this command "BuildMatrix" takes these values,
` and creates an internal matrix from them. then you tell Newton to use that matrix
` to set the orientation of the rigid body.
NDB_BuildMatrix rx#, ry#, rz#, x#, y#, z#
NDB_NewtonBodySetMatrix Body
`Step 4- Now we need to set the mass of the object. This should be a realistic value for
` best results. also, there is another property of rigid bodies called "moment of inertia"
` which basically describes how easily a body will spin when hit by other bodies. You can
` imagine a long thin rod. it rolls very easily, but spinning it like a baton takes more energy.
` that's an example of moment of inertia. Anyway the good news is that I have put in helper
` functions into the wrapper to calculate these values for you, based on physics forumulas.
` you just pass the size of your body, and the mass, and it fills the internal vector with the proper
` Moment of inertia values. then if you immediately call the SetMassMatrix command, it will use the
` values stored in the temp vector, and voila! you have a properly-behaving object!
NDB_CalculateMIBoxSolid mass#, sx#, sy#, sz#
NDB_NewtonBodySetMassMatrix Body, mass#
`Step 5- This just releases the collision object we created, telling Newton "we're done with it".
` If you wanted to make another body from this same collision data (shape), you wouldn't call
` this command.
NDB_NewtonReleaseCollision Col
`Okay, so we've now created a rigid body in the Newton World. We've set it's size, shape, mass, and position.
`Now we need to make a standard 3D object so we can visualize this rigid body in DBPro. This part should be
`familiar to you if you have used DBPro much. I simply load in a box.x primitive, and scale it so it's the
`same size as our rigid body. then I position and rotate it so it's matching the orientation of the rigid body.
obj = FreeObject()
load object "box.x", obj
scale object obj, sx#*100.0, sy#*100.0, sz#*100.0
position object obj, x#, y#, z#
rotate object obj, rx#, ry#, rz#
color = GetColor()
color object obj, color
set object ambience obj, 50
`Step6 - this is the last important part. Here we tell the wrapper what visual object we are using to represent
` our rigid body in DBPro. Then after this, the wrapper will automatically update the position and rotation
` of the DBPro object to represent the rigid body. this is the beauty of the Newton system.
` NOTE: in previous versions of the wrapper you also had to call a SetTransformCallback function. this command
` has been removed, because it didn't really need to be called by the user. so now, all you need
` to do is call the BodySetDBProData command, and Newton knows you want to let the wrapper control
` this object.
NDB_BodySetDBProData Body, obj
`finally this command makes the link even stronger between Rigid Body and DBPro object. not only will the wrapper
`position and rotate the object for you (as set above), calling this command will tell the wrapper to automatically
`delete the DBPro object when the rigid body is destroyed. so for example when you call NDB_NewtonDestroy to destroy
`the entire newton world, Newton goes through and destroys each rigid body one by one. when it destroys this body,
`it will also delete the DBpro object for you.
NDB_NewtonBodySetDestructorCallback Body
`finally, this sets the "gravity flag" for this body. basically if you set this to 1, Newton will apply the
`StandardGravity (that we set at the beginning of the program) to the body each time you call NewtonUpdate, making
`the body fall. if you set this to 0, the body will not be affected by gravity. you can change this at any time,
`turning gravity on and off for bodies in realtime.
` NOTE: in previous wrapper versions you also had to call a confusing command called "SetForceandTorqueCallback".
` that command is no longer necessary. just call the SetGravity command to turn gravity on or off.
NDB_BodySetGravity Body, 1
endfunction Body
Its the blavk box I wish to move.
<Edit>
Have got the object to move but now the Newton objects are not moved by my box object when get by them.
In Space No One can Hear You Scream! (When your comm Line is cut?)