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.

DarkBASIC Discussion / Driving game help

Author
Message
Soroki
20
Years of Service
User Offline
Joined: 26th Jan 2006
Location: United States
Posted: 22nd Sep 2006 20:53
Hey, I am making a simple driving game on my own without my team's help, so I am asking this question here. I have made a small player control demo to demonstrate how the player can drive the car around. So far it isn't too hard. I have one small issue however. The cube I am using for a player object can turn 360 degrees while in place and I really don't want this. I only want the car to be able to turn so far, not all the way around while it isn't moving. I have tried and tried to make it to where the car can't turn all the way around, but I fail each time. I don't have enough time to continue doing this. Here is my code, if anyone could help me, that would be awesome.




Downfall:Arena by Terra Design Studios(TDS) is currently in development. Features full online or AI multiplayer maps, with 35 online maps and 15 AI maps. Demo release date will be somtime in October.
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 22nd Sep 2006 21:30
Hi Soroki,

Think of a car. If it's still, the wheels turn but the body doesn't move. So, only allow turning when there is a speed.




If you want more realism, you'll have to account for a turn radius. This isn't just a rotation around an axis; well, actually it is - but the focus wouldn't be the center of the car necessarily.

You'd need a distance variable as well to represent a rotation of a tire. If the cars wheels are turned 5 degrees, the car will travel a certain distance for one rotation of the wheel. Add up these rotations keeping the angle the same, and you've got a rough turn radius.

So instead of spinning in place, the car has to move forward a certain amount.

This can all be calculated out using polar coordinates, but I don't think it has to be that complicated.

The main thing for ease of use would be to
1.put a limit on how much of an angle can be created using the left and right keys
2.make sure the car can't turn until it is actually moving
3.maybe make sure the car has moved some x and z distance before it starts rotating. You decide on the distance.
(some arbitrary distance = sqrt(((x2-x1)^2)+((z2-z1)^2))

Enjoy your day.
Soroki
20
Years of Service
User Offline
Joined: 26th Jan 2006
Location: United States
Posted: 22nd Sep 2006 21:44
Thanks for your post. I also thank you for your help and support. I really need to work harder on my driving game, because I won't accomplish anything if I continue not working. Lately FPSC has been my hobby, but I decided to get back into DB. Thanks again. I will have to study that code for future reference.

Downfall:Arena by Terra Design Studios(TDS) is currently in development. Features full online or AI multiplayer maps, with 35 online maps and 15 AI maps. Demo release date will be somtime in October.
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 22nd Sep 2006 22:35
you're welcome!

I just hope my advice is sound...

Enjoy your day.
Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 23rd Sep 2006 13:57 Edited at: 23rd Sep 2006 13:58
Latch,

I already found out how to turn around a certain distance, speed using mass, etc.

But there's always one point where I have to give up:
to slip with the car...

Do you have any info on that subject?

(Sorry to interupt your thread Soroki)

It's the programmer's life:
Have a problem, solve the problem, and have a new problem to solve.
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 24th Sep 2006 01:53 Edited at: 24th Sep 2006 02:02
@ Sven

So you're going to make me use the old coconut, huh? I wish I remembered physics a little better but I'll take a stab at your question.

I think it will help Soroki too. But, test out what I say because I'm definately no expert.

What I think you're talking about is centripetal and centrifugal forces. These act in opposite directions keeping the object moving in a circle along it's track without moving inward or outward. If you've already got your speed (it really should be velocity implying speed and direction), and your mass, you'll need two or three more things depending on how you want to handle it.

1. the radius of your circle or your curve
2. a slip-force variable that you choose - when exceeded, your car will slip sideways
3. and - depending on how you want to do the simulation - some kind of friction variable

For this example, we'll use a friction variable.

I created a function for the centripetal force which is directed toward the center of the circle:



The centrifugal force is directed AWAY from the center of the circle - compare this force to your slip force. But to keep the car going in a circle, they are equal.

This is where you could use the friction coefficient (variable). Let's say the car with a certain mass is turning at a certain speed. You could always treat the radius as 1, and the determinations of your force could be speed and mass.

Your program calculates the centrifugal force + your friction variable. If the total is greater than the slip-force, then you make the car slide.

Now I'm assuming you have a radius - This is important because the forces are less as the radius increases. So always treating the radius as 1 makes things nice and easy, but the slip would always be at the same speed.

But if you don't have a radius this is where things can get complicated. Calculating an unknown radius can be done, but you need other information to do it.

Note* If you are using atanfull() in your calculations, the angles are going to be clockwise and the calculations I'm gonna provide are counter-clockwise - the principals should be the same, you'd just have to switch your x and y in the atanfull() function (i.e. atanfull(y,x) ).

The first thing we need is to calculate how far one rotation of a wheel on the car is. You can make up a radius for this relative to the size of the 3d world. Let's say we use 7.



Our rotation length is ~44 units.

So we assume now that every time the car moves, it covers a distance of speed# * 44. This becomes important later. If the car wheels are turned, it's going to move in an arc at speed# * 44 at whatever angle. This is enough information to find out what the radius of the circle it would be traveling on is.



This function should calculate our radius. I had to convert degrees to radians inside the function to keep things consistant.

If the car is turning at 5 degrees and going at a speed of 1, the radius of our circle should be: 44/5*(pi()/180) = 505.75 This seems huge but it would be adjusted based on the scale of your world. And if the wheels were turned at 50 degrees (a tighter turn) then the radius would be 50.6.

Anyway, now that we have the radius of the circle the car is going around, we can apply it to the centripetal and centrifugal forces.

if our mass#=10
our speed#=1
our radius = ~506, What's our cetripetal, and centrifugal forces?



centripetal = ((10 * (1^2))/506)*-1 = -.02
centrifugal = .02

Now if we have our friction variable set to .5 for example, and our slip_force = 2,

We want to do a comparison to see if our car is slipping:
if centrifugal + friction >= slip_force
goto to the slide car routine

centrifugal + friction = .02 + .5 = .52
.52 < slip_force = our car is not sliding

Ok. Mass will be constant. But speed and radius can change. How fast do we have to go at the same angle for the car to slip?

mass=10
speed=10
radius =~506
friction = .5

(10*10^2)/506 = 1.976 1.976 + friction = 2.476 = our car is sliding

Looks like in this scenario, a speed of about 10 would cause the car to slide.

Now you just have find the velocity vector (speed and direction) of the car and the tangent to that times the difference between centripetal and [centrifugal + friction] forces, and calculate the position of where the car would end up.

Enjoy your day.
Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 24th Sep 2006 13:29
I understand your explanation.

But if the car comes from the ground (or centrifugal + friction >= slip_force), I have troubles calculation the new angles, new friction (a car doesn't slip forever), etc. based on the angle of the wheels. If a car slips, it will rarely slip in a straight line, and when the driver has enough car skills, you should be able to get your car back on the track during these slips.

Unless I missed a piece...

It's the programmer's life:
Have a problem, solve the problem, and have a new problem to solve.
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 25th Sep 2006 02:40 Edited at: 25th Sep 2006 02:41
I'm still trying to get my colliding spherical objects physics routine down and your asking me to use my brain on car physics?

Okay, when the car starts sliding, it has a direction perpendicular to the direction it's going. So far so good. The thing not to forget is that it also has forward motion. These two directions combined will send the car off at an angle which is equal to the sum of the two vectors. Keep that thought in the back of your mind.

Now, the friction will only change if the surface changes, assuming that the wheels are always rubber. Now if you want to get real complicated, you can start trying to account for traction which could be a function of wheel wear and the terrain the car is on. But I'm gonna skip that.

Anyway, the difference of ((centrifugal+friction)+centripetal) (remember the forces are opposite and I'm calculating centripetal as negative) is the little extra force that is going to be applied to the sideways slide of the car. I want to keep this simple so I'm gonna treat that extra bit of force as if it's momentum. That means force=momentum=velocity*mass which means velocity = force/mass. I'm treating this as the sideways speed. You already have your forward speed based on how you control the car.

Ok - so if a speed of 1 moves the car 44 units, the distance of the slide should be a percentage of 44. So slide distance = sideways_speed*44. This isn't "real" physics per se, but it should give us pretty good approximations realitive to each other.

Now we should have a distance and a speed for forward and perpendicular which gives us enough information to solve the resulting vector. Which would be the direction and speed we move the car towards. This happens within the loop so if you steer the car in another direction, it should affect everything that is going on. And if the steering over compensates, the forces should still apply in the same matter. The higher the friction value (sort of reverse logic) the sooner the car will slide.

Enjoy your day.
Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 25th Sep 2006 20:31
Quote: "I'm still trying to get my colliding spherical objects physics routine down and your asking me to use my brain on car physics?"


Well... Yes

Anyway, I'm still full of questions. But I remember I printed the source code of car physics of a guy named "the masked coder".
It was 16 pages long, but I could give it a try... (the physics alone were 5 pages).
Let's see... Where did I put it.

It's the programmer's life:
Have a problem, solve the problem, and have a new problem to solve.
Soroki
20
Years of Service
User Offline
Joined: 26th Jan 2006
Location: United States
Posted: 25th Sep 2006 20:40
Ouch...my brain hurts. LOL.

Downfall:Arena by Terra Design Studios(TDS) is currently in development. Features full online or AI multiplayer maps, with 35 online maps and 15 AI maps. Demo release date will be somtime in October.
Sonic 91 Software
21
Years of Service
User Offline
Joined: 19th Mar 2005
Location: In a Cryptic Crossworld!
Posted: 10th Oct 2006 00:51
Quote: "Ouch...my brain hurts"

Mine too. But for a different reason...

"Fight the good fight of faith,
Lay hold on eternal life"
-1 Timothy 6:12
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 10th Oct 2006 02:55
This is the "car on a pendulum" version. A better way to calculate it would be using aerodynamics and lift - though the directions would have to be calculated similarly. Then we have to account for weight distribution and the actual angle of the wheels, and whether or not the car is being pushed by the rear wheels or pulled by the front wheels.

Then of course there is breaking... cluth pumping... down shifting...

Enjoy your day.

Login to post a reply

Server time is: 2026-07-07 07:34:42
Your offset time is: 2026-07-07 07:34:42