|
Post by Spline on Feb 20, 2005 17:01:28 GMT 1
Hi Has anyone used Newton DBPro Wrapper for a 3rd Person game. i would be greatfull if anyone could help me maybe a small demo will a box as the Character colliding with a stack off boxes thanx in advance ???im just trying to get my head around this great plugin
|
|
|
Post by kjelle69 on Feb 20, 2005 18:23:14 GMT 1
Yeaah Sure !!! Here you have a sourcecode example which I made exclusively for you today. Just copy and paste it into the editor ! Make sure you have newton.dll in the same directory as the exe file. You dont need any included media ! Just compile and run !
|
|
|
Post by kjelle69 on Feb 20, 2005 20:27:18 GMT 1
Rem Project: FPSBox Rem Created: 2005-02-20 17:14:47 Rem Kjelle69 Rem ***** Main Source File *****
sync on sync rate 60 autocam off
Rem Setup Light set ambient light 100
Rem This command must be called before calling any other newton commands ! NDB_Newtoncreate
Rem Here we set the minimum framerate which newton will allow without having to make Rem substep calculation.
set camera range 1,1000
Rem Maximum number of boxes = maxy*maxx maxy=10 maxx=10
Rem Mass and size of each bbox boxmass#=1 boxsize#=1.8
Rem Mass and size of the FPS Object fpsmass#=3 fpssize#=2
Rem The force which we will use to control the fps object later on Rem We set it to 20 (Newton) Force#=20
Rem Gravity (-9.8) NDB_SetVector 0.0, -9.8, 0.0 NDB_SetStandardGravity
Rem Array to keep track of all boxes, Each bbox(x) will become a newton rigid body. dim bbox(maxy+maxx*maxy)
Rem Create Boxes, both DBPro Object and Newton Object for y = 0 to maxy for x=1 to maxx obj=x+(y*maxx)
Rem DBPro object make object box obj,boxsize#,boxsize#,boxsize# color object obj,RGB(100,100,x*2.5) set object ambience obj, 80
Rem Newton Collision object same size as DBPro Object col=NDB_NewtonCreateBox(boxsize#,boxsize#,boxsize#)
Rem Here bbox becomes a newton collision object bbox(obj) = NDB_NewtonCreateBody(col)
Rem Connect Newton and DBPro object This means that the callback system will Rem Automaticall update position of the graphical shape (The box) NDB_BodySetDBProData bbox(obj), obj
Rem Set Mass of NewtonObject NDB_NewtonBodySetMassMatrix bbox(obj),boxmass#
Rem Position Objects Rem First three Floats in the Buildmatrix represents the Global rotation Rem Next three Floats in the Buildmatrix represents the Global position NDB_BuildMatrix 0.0, 0.0, 0.0, x*2.0, y*2, 0.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 bbox(obj)
Rem Release collision to free up memory, Newton object stays active ! NDB_NewtonReleaseCollision col
Rem Make sure the Gravity in the Newton World affects each bbox NDB_BodySetGravity bbox(obj),1 next x next y
Rem Make World obj=2000
Rem Create a box which will act as floor make object box obj,1000,1,1000 color object obj,RGB(200,200,2.5) set object ambience obj, 80
Rem Make a Treecollision object of the box, treecollisions is Static objects. Rem They can be moved with Body Set matrix command, but there is no automatic callback to Rem reposition the DBPro object automatically REm External forces have no impact on them either. col=NDB_NewtonCreateTreeCollision(obj) Room = NDB_NewtonCreateBody(col) NDB_BodySetDBProData Room, obj
Rem This line positions the object Rem First three Floats in the Buildmatrix represents the Global rotation Rem Next three Floats in the Buildmatrix represents the Global position NDB_BuildMatrix 0.0, 0.0, 0.0, 0.0,-5.0,0.0 NDB_NewtonBodySetMatrix Room
Rem Release collision to free up memory, Newton object stays active ! NDB_NewtonReleaseCollision col
Rem Remeber that this is a Treecollision object, which means that DBPro does not Rem automatically updates the position , we have to do it ourselves ! Position object NDB_GetDBPro(Room),0,-5,0
Rem Make FPS Character obj=2001
Rem A bit boring, This is not Duke Nukem, but just a box, however with some Imagination Rem the box can become whatever you want it to be ! make object box obj,fpssize#,fpssize#,fpssize# color object obj,RGB(100,200,2.5) set object ambience obj, 80
Rem Lets put some shading for nice looks, take away this command to increase performance set shadow shading on obj
Rem Create a Newtoncollision in the shape of a box col=NDB_NewtonCreatebox(fpssize#,fpssize#,fpssize#)
Rem Here fps becomes a newton collision object fps = NDB_NewtonCreateBody(col)
Rem Connect Newton and DBPro object This means that the callback system will Rem Automaticall update position of the graphical shape (The box) NDB_BodySetDBProData fps, obj
Rem Set Mass of NewtonObject NDB_NewtonBodySetMassMatrix fps,fpsmass#
Rem Gravity affected = Yes If you put a 0 instead it will not be affected of gravity. NDB_BodySetGravity fps,1
Rem The Autofreeze command with a 0 makes sure that Newton always have the object 'Active' Rem The default value is Autofreeze on which means that Newton does not update the object Rem if it comes to rest. We want to move the fps object with the addforce command so we Rem Dont want it to freeze NDB_NewtonBodySetAutoFreeze fps,0
Rem Position Objects Rem First three Floats in the Buildmatrix represents the Global rotation Rem Next three Floats in the Buildmatrix represents the Global position NDB_BuildMatrix 0.0, 0.0, 0.0, 10.0,0.0,-25.0 NDB_NewtonBodySetMatrix fps
Rem Free up some memory NDB_NewtonReleaseCollision col
Rem Generate Materials Rem Without materials you cant change the friction elasticity and softness of Rem different Rigid bodies in your system.
Rem First we creates some identification variables to store the materials in Rem Defaultmat (Which is the default material always present in Newton Rem BoxMat which becomes the material of each bbox Rem FpsMat which is to be the material of the fps object DefaultMat = NDB_NewtonMaterialGetDefaultGroupID() BoxMat = NDB_NewtonMaterialCreateGroupID() FpsMat = NDB_NewtonMaterialCreateGroupID()
Rem Here we set the Static and the Dynamic friction between the Defaultmaterial Rem And the bbox material. Rem The Floor in our case will have the defaultmaterial because we dont specify Rem any material for that object. = Gets the defaultgroupid automatically. NDB_NewtonMaterialSetDefaultFriction DefaultMat, BoxMat, 0.5, 0.5
Rem Here the elasticity between the materials is specified NDB_NewtonMAterialSetDefaultElasticity DefaultMat, BoxMat, 0.5
Rem Here the softness between the materials is specified NDB_NewtonMaterialSetDefaultSoftness DefaultMat, BoxMat, .5
Rem Here we specify how the boxes will interact with eachother ! NDB_NewtonMaterialSetDefaultFriction BoxMat, BoxMat, 0.5,0.5
Rem Here the elasticity between the boxes is specified NDB_NewtonMAterialSetDefaultElasticity BoxMat, BoxMat, 0.3
Rem Here the softness between the boxes is specified NDB_NewtonMaterialSetDefaultSoftness BoxMat, BoxMat, .5
Rem Here we specify the friction between the floor and the fps object NDB_NewtonMaterialSetDefaultFriction DefaultMat, FpsMat, .2,.2
Rem Here the elasticity between the floor and the fps object NDB_NewtonMAterialSetDefaultElasticity DefaultMat, FpsMat, 0.5
Rem Here the softness between the floor and the fps object NDB_NewtonMaterialSetDefaultSoftness DefaultMat, FpsMat, .5
Rem Now we have to make sure each rigid body gets the proper material assignment Rem First we iterate through each bbox for y = 0 to maxy for x=1 to maxx obj=x+(y*maxx) NDB_NewtonBodySetMaterialGroupID bbox(obj), boxMat next x next y
Rem Than we set the material FpsMat to the fps object ! NDB_NewtonBodySetMaterialGroupID fps, FpsMat
Rem Now we Specify which objects will affect eachother with the stated material parameters Rem Floor against bbox NDB_NewtonMaterialSetCollisionCallback DefaultMat, BoxMat Rem Floor against fps object NDB_NewtonMaterialSetCollisionCallback DefaultMat, FpsMat Rem Boxes against boxes NDB_NewtonMaterialSetCollisionCallback BoxMat, BoxMat Rem Fps objct against boxes , thats all ! NDB_NewtonMaterialSetCollisionCallback FpsMat, BoxMat
Rem Main loop do Rem Calculate the time since the last call to update newton objects ! time# = NDB_GetElapsedTimeInSec() NDB_NewtonUpdate time#
Rem Just print out how many times we have called newton since the program started. text 10,10," Cycles "+str$(Looptimes)
Rem Increment Looptimes with 1 inc Looptimes
Rem Position the DBPro camera something like a 3rd person view option in a fps game :-) position camera Object Position x (NDB_GetDBPro(fps)),Object Position y (NDB_GetDBPro(fps))+5,Object Position z (NDB_GetDBPro(fps)) rotate camera object angle x (NDB_GetDBPro(fps)),object angle y (NDB_GetDBPro(fps)),object angle z (NDB_GetDBPro(fps)) Rem Back Off Camera a bit move camera -20
Rem Here we set the forces on the fps object (Movement) Rem The first vector 1 is in this case (NDB_BodyAddForceLocal) saying Rem WHERE on the fps object the force will be set. Rem I have made a slight offset in the direction of the front of the object Rem Which I will use to make the object turn. (I am making sure that Rem the forces will be pushing it on the 'nose') Rem (0.3 units in the Z direction, object front direction) NDB_SetVector 1,0,0,0.3
Rem The second vector is telling NDB_BodyAddForceLocal how BIG the force will be Rem Here we just reset it to 0 if no movement is made. NDB_SetVector 2,0,0,0
Rem In this input sequence we are setting the second vector to the size of Rem the force. if upkey() then NDB_SetVector 2,0,0,force# if downkey() then NDB_SetVector 2,0,0,-force# if leftkey() then NDB_SetVector 2,-force#,0,0 if rightkey() then NDB_SetVector 2,force#,0,0
Rem This commands read the contents of the Vector one and two and Rem Adds the force with the position (Point in the objects Local space) (Vector 1) Rem and the force vector (Vector 2 also in the objects local space ) NDB_BodyAddForceLocal fps
Rem this is a useful debugging command which means that if you push the Ctrl key Rem The world will be drawn as NEWTON sees it, regardless of how darkbasic is Rem drawing the objects if controlkey() then NDB_DebugDrawNewtonLines
Rem DBPro sync command sync loop
Rem This function block is needed when using treecollisions as we use DBPro memblocks to Rem make some calculations function NeverCalled() if memblock exist(1) then delete memblock 1 endfunction
|
|
|
Post by spline on Feb 21, 2005 15:26:23 GMT 1
thanks kgelle thats great your a wizz anychance off makeing the ftp have more of a rotate action than i side way movement, and a jump action would be great and collistion up and down staires
great work keeping up the plugin
|
|
|
Post by kjelle69 on Feb 21, 2005 16:50:50 GMT 1
The sideways movement: Try to change the vector which puts the offset in the positive Z-direction to a higher value than 0.3 This section: [glow=red,2,300]Rem Here we set the forces on the fps object (Movement) Rem The first vector 1 is in this case (NDB_BodyAddForceLocal) saying Rem WHERE on the fps object the force will be set. Rem I have made a slight offset in the direction of the front of the object Rem Which I will use to make the object turn. (I am making sure that Rem the forces will be pushing it on the 'nose') Rem (0.3 units in the Z direction, object front direction) NDB_SetVector 1,0,0,0.3 [/glow] The jumping ! Well try to do it yourself ! Hint: [glow=red,2,300]If user press space then Vector 1 = 0,0,0 Vector 2 = 0,10,0[/glow] (This will make the cube get a force push from below in the positive Y direction (Upwards)
|
|
|
Post by spline on Feb 21, 2005 21:27:46 GMT 1
thanx alot i will try that. this is a great adition to DP Pro im hoping that i can get my head around it soon, iv been checkind out your other example that you have done on this forum, your doing a great job. its alway handy knowing some one who knows what they are doing ;D i got my ftp box jumping,;D only one little prob when i jump in to the stack of boxes, whahay my head spinning im fealing sick:o, once the ftp box hits the stack off boxes it starts spinning is there any way of locking the rotation so it dosn't spin. keep up the great work and thanks again
|
|
|
Post by kjelle69 on Feb 24, 2005 15:06:13 GMT 1
Now this is really a piece of cake with the fantastic Newton System:; Use this code: Rem A vector pointing in the positive Y direction Rem (Straight upwards) NDB_SetVector 1, 0.0, 1.0, 0.0 Rem Create a Joint attached to your fps object: Joint = NDB_NewtonConstraintCreateUpVector( fps ) This is all code needed to solve that! I tried the code, added this lines just before the main do loop ! Try it ! Tell me what you think !
|
|
|
Post by spline on Feb 24, 2005 19:31:09 GMT 1
that great thank Kjelle ;D
|
|