Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / Tutorial: Programming with Newton

Author
Message
CuCuMBeR
21
Years of Service
User Offline
Joined: 11th Jan 2003
Location: Turkey
Posted: 15th Jun 2005 06:46 Edited at: 16th Jun 2005 06:28
Hello there,
Well i will be away for a loooong time because i need to do my military service, meanwhile i felt like i should leave these series of tutorials about Newton and DBPro programming so maybe it will help some people here.This is not very advanced, so those experienced guys should not expect much, but not very newbie stuff either and surely can find something for yourself too and do not hesitate correcting mistakes.
Okay here we go:
What you need:
1-DBPro 5.8
2-Newton wrapper 1.32 installed
(will add more when we need them )
Important Stuff:
1- Dark basic reads lines just like you read a book; line by line so writing the commands have to follow an order.
2- Always use variables with prefix like (pthLevels, mdlEnemy...etc)
3- working with include files and Sub routines makes your life easier and makes your code re-usable
(will add more when i realise them )

NEWTON ENVIRONMENT CREATION

1.PART:
1- Create a blank project with the name you want in your projects folder.And start typing the following code, type it yourself instead copy/paste so you can get used to some commands.

with this code we define some variables to be GLOBAL incase we may need them later on.
These variables are path variables(prefix: pth)
pthRoot = current folder we are working on, and will be the folder wherever the user installs or copies your program with the help of the command GET DIR$()
pthBin = will be a folder we will create in your project folder to keep included files, if we include some
pthMdl = models folder

I believe i dont need to explain what the above do.

2- Now we keep adding some more variables we will need further on
(prefix: scr) is shortly "screen"
simply we set screenfps, screen fov, screen width..etc variables here.


Now your code should look somethin like this:


3- And then we shall set the screen commands with the variables above:


And your code should look like this:


2.PART:
1- Now is a good time to setup Newton in our program, and we better make a new include file in our "Bin" Folder, to keep things less complicated and more re-usable.
Now lets first create a folder named "Bin" in our project folder and Lets add a new source file called "Newton.dba" in this new folder.
So we are ready to work in our new file now, this file will contain some re-usable subroutines for any future newton projects.
Open the Newton.dba and start entering the following code:
2- First we make 3 subroutines as follows


4.2- And we start with the prepareNewton routine
(Prefix n = newton)


NEwton has support for different materials,(read newton help files for further explanation)
We will use these materials later in our program, but we will do the creation here at the very beginning just so i can finish this tutorial faster.
Edit the prepareNewton routine as follows:


Okay we are finished with the creation part.

3.PART:
1-We have done with the newton creation and the subroutine, so we can now add this to our main.dba file


2- But hey, i think we will need these material variables later on, so lets make them global too


3- Now lets add our loop to the main file


4.PART:
1- Newton has debugging, so lets set it up first and we can use it later on.Go back to Newton.dba
go to debugNewton subroutine, thats where we will add code now
and change it as follows:

Now when we will press F12 key on your keyboard, we will debug newton, dont ask how now, ill explain it later.

2- Now lets go back to main.dba and add this new subroutine to the veeery end of the loop


3- While we are there, lets add the final newton subroutine to the exit of the loop:


we added an empty routine to the end, so lets go to Newton.dba and fill it, open Newton.dba in the included files and edit destroyNewton routine as follows:

This will make sure we first destroy the newton environment and then exit the program when user presses ESC Key

Now you have set up the newton environment perfectly, and can compile the program to see a black blank screen later on ill explain object creation
To be continued..

NEWTON OBJECTS CREATION

1.PART:

Newton objects are actually collision data for your visual DBPRo objects and there are many of them such as, empty, box, sphere, cylinders, capsules, cones, convexhull, compound and tree collision.

but before we go into these, we need some functions for our actual program to make things easier, i wont explain these functions, and i didnt make them but i found them somewhere in this forum and credits goes to the author.

Lets make a new include file in our "Bin" folder to hold such functions concerning the DBPRo environment.

1- Add a new file called "gameFunctions.dba" in your "Bin" Folder and open it for editing.

2-Paste the following function in it and save.


well, why dont we make a smiliar function for all available DBPRo stuff like images, sprites...etc
change the "gameFunctions.Dba" as follows:


now it will be very easy and automatic for any file creation.
Example usage:
obj = free_object()
load object "myObject.x", obj


2.PART:
Now, functions are very useful and very re-usable, they will help you write less code.
so we want to make whole newton object creation into functions which means we wont need to type same code for every visual object we create.

1- now lets create another include file in our "Bin" folder with the name "newtonFunctions.dba"
this file will hold functions for newton collision data creation and will be totally automated, which means we wont need to pay attention on newton as much as to our actual game.

2- lets edit this new file and create a function inside as follows:
now what we need for this function, is:
obj = object number that we want to work on
mass# = objects mass
Gravity = if the object will be effected with gravity or not
buoyancy = if the object will be effected with buoyancy or not
nMaterial = which collision material will the object use


As Dbpro reads the program line by line, at the beginning of the function lets get objects visual data:


as we know enough of the object, lets create the collision data for it now:
(i dont need to explain the commands i believe, but the order of the commands is important.)

what we need now is a math matrix to hold rotation and position data of this collision


now we can link the visual object to newton collision data


The object has been created, now all we have to do is define if it will get affected by gravity, buoyancy and set its material


as the function is ready to use now, we better get the id of the newton data for further use, just change the last line


here, our first newton function is ready to serve.


ADDING FREE MOTION CAMERA TO OUR PROGRAM

1.PART:
Again make a new include file in our "Bin" directory with the name "Camera.dba", and open to edit and just add the following camera routine for a free motion camera
(wasd keys and mouse)


after then, we shall call these routines in our main.dba


Well, enough for today, to summarise, we have newton set properly and a working camera.
we are ready to visualise the environment with newton now, but you need to give me couple of hours to rest

NEWTON OBJECTS CREATION 2

1.PART:
Now we are ready to create some objects, we will work on the main.dba file
our object creation is simple as this


why dont you just add this to your main.dba and see your new box object (it will look static for the moment but we will fix it in a minute)


your box wont react because we did not tell newton to start working yet.

so lets make it get effected:


a nice falling box, it will keep falling down until y is smaller than the newton world size we set before in the prepare newton part.
then both the newton data and visual object will be destroyed and cleaned out of the memory.

Now why dont you change the newton object to be effected by buoyancy and watch whats happening. and meanwhile alittle visualisation for the buoyancy plain might help you see it:


now, why dont you try messing with the mass# value and add some rotation on the z axis forexample and watch the results

Okay, this is it with the buoyancy, now i shall delete the buoyancy for the rest of the tutorial and show you something else.
But first lets add another function for the newton using the same style but this time lets create a sphere object.
open the "newtonFunctions.dba" and add a new function just like the following:

Now we can create sphere objects, and if you check the code you can see its very smiliar to the box object.

Lets go back to "main.dba" and clean up the water plain and add a solid ground plain with the help of newton box function:



its easy to add a sphere object here now:
(add it in the main.dba where we should create newton objects)


SOME MORE NEWTON FUNCTIONS BELOW

Following are couple more functions that you can include to your "newtonFunctions.dba"
(Convex hulls, tree collision and an innocent function to include memblock.dlls to our program that tree collision needs)


MORE NEWTON OBJECTS


Before we create more objects, you better read the newton manual for the above convexhull and treecollision datas, then you will probably understand what they are for. All i can say is, we will use tree collision for static objects like game levels.

oh, by the way our functions return values for the newton objects id's, so lets make them global in our main.dba too:


1.PART
First lets steal some code from Walaber's demos for interaction with newton objects (i wont explain this part, because its more maths rather than newton and you should try to understand it yourself.)

1- Lets make a new include file called "newtonInteraction.dba" in our "Bin" Folder, just to keep things tidy.
And Paste the following in it:



2- Lets edit our "main.dba" to include these new sub routines
(meanwhile we will rem out some of ours to prevent camera routines to intersect)



Now you should be able to use your mouse2 button to interact with newton objects.

NEWTON JOINTS

1 - A Hanging Lamp using joints

here is a cool looking, untextured hanging lamp, you can watch it swing by any force applied to it

1.1- download the attached media and extract it in the "mdl" folder in your project (if it doesnt exist, create the folder)

1.2- add the following function to your "newtonFunctions.dba"


1.3- call this function in your "main.dba"



now your main.dba should look somethin like this:


Summary:
Okay this is it, now you know most of newton
if you already knew these, thats good what can i say..
But if you have learnt somethin, do not forget to give me credits and donations would also be cool
im just kidding, you dont even need to thank me at all...but ill appreciate it

id like to go on with this tutorial, but for now ill wait for your comments and wishes. But just remember ill be around for 15 more days and thats it.

CuCuMBeR

Attachments

Login to view attachments
Cian Rice
20
Years of Service
User Offline
Joined: 8th Jun 2004
Location:
Posted: 15th Jun 2005 08:57
Looks useful. I know I'll be using it.


CuCuMBeR
21
Years of Service
User Offline
Joined: 11th Jan 2003
Location: Turkey
Posted: 16th Jun 2005 06:33
For newton vehicle physics you can follow this link:
http://forum.thegamecreators.com/?m=forum_view&t=54135&b=6

And in the attachment, you can find the finished tutorial above incase you didnt read it at all:p

Attachments

Login to view attachments
CuCuMBeR
21
Years of Service
User Offline
Joined: 11th Jan 2003
Location: Turkey
Posted: 17th Jun 2005 00:34
uhmm..as theres nobody asking to continue with this, i see no reason to add more features with this and probably it was a waste of time.
Cellbloc Studios
20
Years of Service
User Offline
Joined: 15th Mar 2004
Location: Atlanta, GA
Posted: 17th Jun 2005 00:44
Me want more!

-This...is my boomstick!
Spline
20
Years of Service
User Offline
Joined: 30th Oct 2004
Location: London U.K
Posted: 17th Jun 2005 07:27
great work CuCuMBeR keep it up
please continue as newton is a great adition to darkbasic
Maleck
20
Years of Service
User Offline
Joined: 12th May 2004
Location: US
Posted: 17th Jun 2005 08:30
I am glad you put the time and effort into this. I know it will help get people started with Newton (me being one of them). Please keep up the good work.
Jess T
Retired Moderator
21
Years of Service
User Offline
Joined: 20th Sep 2003
Location: Over There... Kablam!
Posted: 18th Jun 2005 18:41
I've sent off the link to this thread to Rich...

Hopefully it'll go into the newsletter

Keep up the good work, as no matter how little or big the help, if as little as 5 people a month use it, it was worth while writting

Jess.

Team EOD :: All-Round Nice Guy
Want Better dbHelp Files?
Hawkeye
21
Years of Service
User Offline
Joined: 19th Sep 2003
Location: SC, USA
Posted: 22nd Jun 2005 00:38
Tadaaaa! It's in teh newsletter! Congrats

Nice tutorials, btw... I like that swinging lamp!


Benjamin says: fecking twat of a program [msn7]
Meh blog
CuCuMBeR
21
Years of Service
User Offline
Joined: 11th Jan 2003
Location: Turkey
Posted: 23rd Jun 2005 23:11
if you liked the swinging lamp, try this rope:
you will find its just as easy as the lamp if you will examine the code:
again attach the function in your newtonFunctions.dba and call it in your main.dba like this: nRope = NewtonRope(10, 140, 50)
Oddmind
20
Years of Service
User Offline
Joined: 20th Jun 2004
Location: Atlanta, Georgia
Posted: 24th Jun 2005 18:06 Edited at: 24th Jun 2005 18:17
er, when i followed the tutorial up until you said you could run it and get a black screen i did and it said it couldnt find the Newton dll, even though i copied from the "NewtonDLL_Seperate" file in the "Wrapper" file of the .rar that i downloaded from the website...it worked for the demos but i dont know whats wrong...please help

EDIT:
ok i copied the DLL from youre example and pasted in the folder of my project...it still says

dbp_program: dbp_program.exe - Unable To Locate Component

"This application failed to start because Newton.dll was not found. Re-installing the application may fix the problem."


EDIT:
lol well im not as dumb/helpless as i was imlyin i fixed the problem by naming the project file and the source file the same thing///i dunno why that helped but it did..ayways thats that...
CuCuMBeR
21
Years of Service
User Offline
Joined: 11th Jan 2003
Location: Turkey
Posted: 24th Jun 2005 19:20
good to see you solved the problem yourself
Zero Blitzt
20
Years of Service
User Offline
Joined: 18th Jan 2004
Location: Different Stages
Posted: 29th Jun 2005 14:40
This definitely helped clear up so much. Thanks a bunch.


"And C++ has 2 version Microsoft visual C++ and Bordland C++."
DrReaper
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location: United States
Posted: 4th Jul 2005 05:57
Man you have to continue. This is really the only thing I can find on physics and dark basic pro. I have more questions now than before i finished the tutorial.
LordoFire
20
Years of Service
User Offline
Joined: 14th Feb 2004
Location: United States
Posted: 12th Aug 2005 09:02
I found the tutorial very usefull. Newton physics can seem quite complicated at first keep up the good work

When it comes to programing, I'm on fire!
Formerly known as flame master...Changed name because I do not flame
Oddmind
20
Years of Service
User Offline
Joined: 20th Jun 2004
Location: Atlanta, Georgia
Posted: 4th Oct 2005 06:13
late i know,
i just wanted to thank you so much for this. i know this took a big ass chunk of time, and with pending military duty im sure you had more things to worry about. This helped me understand newton so much more. i can hopefully add collsion using this dll in my current project. anyways i know alot more people read ad followed this tutorial than the ones who commented, they are probably so amazed and are experiementing with it and forgot to comment ^_^. anyways thanks again and hope to see more after your military service.

p.s. to those who get an error message about "cant find Newton.dll" you first have to follow installation instructions, then put newton.dll in the file with your exe. my problem was that my exe was in the temp folder so make sure you go over to the left on the "project manager" thing and specify where you want your exe to be made.

later everyone
Krimzon DestinE
19
Years of Service
User Offline
Joined: 18th Sep 2005
Location:
Posted: 8th Oct 2005 09:02
what is Newton anyway?
Lukas W
21
Years of Service
User Offline
Joined: 5th Sep 2003
Location: Sweden
Posted: 8th Oct 2005 10:57 Edited at: 8th Oct 2005 10:57
x1b
20
Years of Service
User Offline
Joined: 19th Sep 2004
Location:
Posted: 8th Oct 2005 11:46 Edited at: 10th Oct 2005 05:35
excellent work man! So, thats it? Thats all you're going to write about Newton?

- Do it, Do it Right, Do it right now..
CuCuMBeR
21
Years of Service
User Offline
Joined: 11th Jan 2003
Location: Turkey
Posted: 20th Oct 2005 00:52
hi again ppl, im back
its nice that i could help some of you here.
Gimme sometime and maybe i can add more features on this tut
cya
Stigity bob
19
Years of Service
User Offline
Joined: 10th Nov 2005
Location:
Posted: 22nd Nov 2005 12:00
Awesome tut CuCuMBeR very helpful indeed

Login to post a reply

Server time is: 2024-11-23 10:52:35
Your offset time is: 2024-11-23 10:52:35