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.

Newcomers DBPro Corner / [Tutorial] Simple Vehicle Physics. DBPro only.

Author
Message
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 6th Jan 2006 03:55 Edited at: 6th Jan 2006 04:02
Hovering Vehicle Physics Tutorial

Level: Medium

This tutorial mainly focuses on the basics of physics coding. You may notice in most games you will have objects falling around the place, bouncing and everything, even with vehicles. But today we wont be learning this, this tutorial may be at a medium level, but it is mostly aimed at complete newcomers, which means, that you shouldn't start coding like this, this is just to give you a clue as to how it's done.

I have been working on some physics functions of my own recently, and I have learned enough while I was at it, so i thought I should share some of that knowledge.

I'm now going to stop blabbing and get to the get go, first of all, we will set up the program. So here's the first piece of code I will give you.



You may notice the parts marked with a open quote mark (`), these aren't commands, they completely blank out that line.
I made some fancy decoration above th setup, this isn't just so it looks cool, but also to keep my program tidy. I'll tell you one thing, keep your program tidy, because no matter how far you get, if your code isn't tidy, then you will feel the urge to restart.


Next, for some more seting up, we will use this code:


These are types. Two things you need to notice are: The name of the type, and what's within it. First of all, what's in it, can be called anything you want, I called them X, Y and Z, so I can use them as coordinates. (Floats mean numbers with decimals). The name of the type doesn't really have to be anything special, but I called it vector anyway. Basically, if I make a type, in this case, this one, I can make a variable as a type. I.e. Pos as vector. That means I can extend that variable to pos.x, pos.y and pos.z, which can be easier than always doing posx, posy, and posz.


Ok, now we have setup the basics. Now we will look at the player. Enter this code just after,




Again, I have made a fancy little thing at the top. Notice I have made most of the variables as vectors, they will have x, y and z attached onto the end of them. I have also made them all Global, which means they can be used in the whole program. This means I can put it in a function, and if I can do that, I can have AI Bots with physics too, but this would mean that I would have to use arrays. I may explain arrays later. Just ask me to if you want.

Next, I will have to configure the player. While I'm at it, please take note that I have a very big setup, The setup if a very large part of the code, and you can have hundreds of lines just setting up, but it's all very important, so try not to setup stuff all in seperate bits.



The mass is a very important part here, the higher the mass, the slower you will accelerate.


Now we have set up. Time to create some stuff.




The 3D box will represent the player, though it can be modeled in some very good programs out there, but since this is a tutorial it doesn't matter.

A matrix will make a grid that can be used as a ground, I am only using this so we can see ourselves moving.


Ok, now, this part is the main loop, as you may have guessed, it's a loop, it loops, it makes continuous loops, and it doesn't stop unless you tell it to, or you have a power-cut in your house. This is where the controls and everything that changes goes.




'Sync' has to go before the 'loop' command so the screen will refresh, otherwise you wouldn't see anything changes.

Ok, to start off the loop, we have to atleast set out the controls.




These variables here that I have made a called boolean variables. For example, if the up button is pressed, the up variale is 1, and if not, then it turns to 0. That's all a boolean variable is, 1, or 0. In some languages they can be called 'true' or 'false'.

Next thing we have to do is to change the forces that are applyed to the player when you press the controls.




Notice now we have put the types into use. Also, if the buttons are not pressed, then we have to change the forces back to 0, it wont do this for you.
Also, notice that when you press the left or right keys, the angular force is the speed devided 5. As you may know, if you are in your car and you're not moving, then you cannot turn, so if your speed (which we will calculate later) is 0, then you will not have any angular force. If 0 is devided by anything, then it will end up as 0, which means you will not turn. Also, the more you devide it by, the less you will turn.

Now, time to recap, here's out code so far.



Nothing will happen so far, but that doesn't mean we don't have progress.

Now we have to get down to the nitty-gritty of the program. The equations!!

You may find them complex, depending on you physics knowledge, so I will go through it all bit by bit.




The MDir variable is a variable which will calculate the direction of your movement. E.G. If you wanted them to react the the direction you are moving on, you could do these calculations, and draw a 3d line for 0,0,0 to mdir.x, mdir.y, mdir.z, the angle of that line would be the same your your angle. We do not have to have such complex equations for moving along the X and Z axis, but it doesn't make any difference doing it this way apart from the fact that you can make it for moving in the air, like an aeroplane, or a spaceship.


Now, we will work out some forces.


traction = Direction * Engine Force
drag = -Constant drag * Velocity * Speed

These equations are important to our movement, because as you may have noticed at the controls part, the force that is used to push us depends on these two forces.

Next, we will calculate our velocities, the velocity is just like a speed, instead in only one axis, i.e. if you are moving at 100mph along the z axis, then your x velocity will still be 0.



Acceleration = Force / Mass
Velocity = (Velocity + Acceleration) * Constant traction
Speed = Square root ((Velocity X*Velocity X) + (Velocity y*Velocity y) + (Velocity z*Velocity z))

Now, first, notice the force and mass affect the acceleration, this is why you speed up gradualy as force is applyed, and the higher the mass, the slower you speed up, it makes perfect sense.

The velocity equation isn't entirely true, but we will skip all the really complex stuff to get it. The constant traction is important to slowing the vehicle down over time, otherwise it'll just keep on going. It has to be less than 1, here's what I mean. let's say the velocity is at 10, and you multiply it by 0.98 all the time, this is what will happen to it:

10
9.8
9.604
9.41192
9.2236816


Notice how it keeps going down, success, we are slowing down.

The speed also has to be worked out, because it has been used. The speed it like velocity, instead affects all axis.

Now, the important parts. We have to affect out angles and coordinates from these.

To affect the coordinates, there are two equations you can use:
Position = Position + Velocity
Position = Position + (Direction * Speed)

We will use the first one. It will not move you forward all the time, your angles will only affects your movement if you press the up arrow key, otherwise you will continue in one direction no matter which way you turn. It's like the difference between the way a skateboard and a snowboard moves. The skateboard will only move forward. Sometimes you can make lateral forces to affect the positions, but we wont.
We are going to do this type of movement because it is a floating type of vehicle, and it gives us better movement. Though if you do want sliding, you can check out this site for car physics, it is much more complex and doesn't actually give you any code, but it gives you alot of equations. http://home.planet.nl/~monstrous/tutcar.html

Now the positions have been affected, all we have to do now is affect the angles.




We are pretty much finished. Only two things to do, first, we will update the player with the positions.


I changed the Pos.Y variable so you are always above the ground.

Now, to finish with, we will make a good little 3rd person camera to follow us around.




And here's the code in full for you to try out:



I hope you have learned the basics of physics programming, and agknowledge that no matter how much you hate school, you still have to use real equations to make something convincing.

Have fun, bye

x1b
19
Years of Service
User Offline
Joined: 19th Sep 2004
Location:
Posted: 6th Jan 2006 04:12 Edited at: 6th Jan 2006 04:22
Ok, that was just, plain, cool.

Thanks for putting that out,toaster. I was talking to LIT, not all that long ago about using vectors in my WIP to emulate the player gradually building speed from a walk to run as opposed to just taking off like a bullet, then gradually slowing down as opposed to just comming to a grinding hault.

Think i'll study this a little more intensely to properly understand it then work it into my WIP to meet that purpose.

- Do it, Do it Right, Do it right now..
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 6th Jan 2006 04:25
It isn't really vectors, it's a kind of simulation of my own, but it's good to get people to learn to use types too.

x1b
19
Years of Service
User Offline
Joined: 19th Sep 2004
Location:
Posted: 6th Jan 2006 04:38 Edited at: 6th Jan 2006 04:40
Quote: "It isn't really vectors"


Oh,I know. What i'm saying is,you've given me a new approach and something new to learn.

I frequently learn such things from, Lost In Thought.

Again,thank you.

- Do it, Do it Right, Do it right now..
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 6th Jan 2006 06:25
No problem.

I encourage you all to look at some functions that I have been making for things like this. You may find them pretty useful.

http://forum.thegamecreators.com/?m=forum_view&t=68472&b=8&p=0

Black Hawk
18
Years of Service
User Offline
Joined: 6th Jan 2006
Location:
Posted: 6th Jan 2006 10:59
THx alot Toaster i have just started but know alot about Physics so with this it shall be help agian thxs alot
x1bwork
18
Years of Service
User Offline
Joined: 9th Nov 2005
Location:
Posted: 6th Jan 2006 17:32
Toaster,could you write a tut prior to this? like an introduction to.

I get the gist of this and can learn a fair amount by mangling the code for awhile but can only learn so much.
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 6th Jan 2006 17:52
I don't know if I can go any further back, try to read the comments that i put, they will help. You really also need a good understanding of maths and stuff.

x1bwork
18
Years of Service
User Offline
Joined: 9th Nov 2005
Location:
Posted: 6th Jan 2006 18:47
you're right. helps if I read the comments out side of the code and not just slam my head into the code and go by its `comments.

Am I the only one infatuated with this tutorial? ...im such a geek *sob*
DarkBasic Pro Guy
19
Years of Service
User Offline
Joined: 4th Jun 2004
Location: Broomfield, Colorado
Posted: 7th Jan 2006 20:29
I notice your competition entry is like the best. Are you doing all this because it ties in with the current challenge? Nice tutorial btw Would really help me

Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 7th Jan 2006 22:45
Kinda, when i started the challenge I was having some difficulties with the physics, but slowly slowly I got more into it, so I decided to make some functions. So really, it was because of the challenge that I made the functions, and it was because of the functions that i won the challenge .

So yeah I thought I would share some of my knowledge with the rest of ya. I may even make a tutorial using my own functions just to teach people how to use them.

Login to post a reply

Server time is: 2024-03-29 14:27:09
Your offset time is: 2024-03-29 14:27:09