Game Development Community

Adding rolling start to vehicles

by Christopher Olson · in Torque Game Engine · 03/13/2006 (9:53 am) · 3 replies

I recently puchased TGE and I've been playing around with some of the example programs to get a feel for scripting. Today I decided to take starter.racing and add a "rolling start" to it -- that is, I wanted the car to be moving forward at a constant speed at the beginning of the race.

After looking through the files, I thought that the best place to add this would be in startRace() in starter.racing/server/scripts.game.cs. I started out by adding the following line to the end of the for loop:

%cl.car.setVelocity("50 50 0");

I really wasn't sure how large of a number to put so I just put 50. The car didn't move at all. I played around with some different values but the car refused to move after the countdown.

Then I found a thread mentioning that someone else was having problems with setVelocity and vehicles. That person said they were able to fix it by using applyImpulse instead. So I tried:

%cl.car.applyimpulse(%cl.car.getWorldBoxCenter(),"50 50 0");

Still no luck.

I put these lines into createCar as well, and that at least produced some motion. However, it actually sent the car flying end over end through the air! I'm guessing that the problem here is that I need to correct the vector somehow.

But even if I got it to work in startCar() I still want to be able to have the car take off right after the 3 2 1 countdown, not before.

Anyone have any suggestions? Thanks in advance.

#1
03/13/2006 (10:26 am)
When i've used applyImpulse() on player objects,
i needed values much larger than 50. more like 5,000.

but vehicles may be different.

in addition to the magnitude of the vector,
there's also size and direction,
and also the first parameter, which i believe is the point at which you're applying it.

that first parameter might be in player coordinates,
in which case you'd want something more like
%cl.car.getObjectBoxCenter(), which doesn't exist, but you could make it easy enough.
(actually it seems to me you'd want %cl.car.getCenterOfMass() ?)

try using simpler numbers to understand what the function is Doing,
then make it do what you want.

eg

applyImpulse("0 0 0", "1 0 0");
#2
03/13/2006 (10:28 am)
OK, I'll answer parts of my own question. :)

I continued to research after posting and determined that I was wrong when I said I couldn't get it to work in startGame(). It turns out I was just using bad vectors, so after adjusting them I was able to recreate the same end-over-end car thrusting as I did in createCar().

I found a post that included some code to normalize the vectors and so forth. Using that code adapted to starter.racing, I added these lines to startGame():

%vel = %cl.car.getVelocity();
%vec = vectorDot(%vel, vectorNormalize(%vel));
%cl.car.applyImpulse(%cl.car.position, Vectorscale(VectorNormalize(%cl.car.getEyeVector()), %cl.car.getDataBlock().mass * 30));

Now the car properly moves forward at the start of the race.

My next challenge is to figure out how to have the vehicle maintain the velocity instead of slowing down. Any ideas? Maybe something in the car/tire datablocks?
#3
03/13/2006 (10:38 am)
Looks good.
just a quick note - neither %vec nor %vel are used in your applyImpulse(), so you don't need to calculate them.

another note about dot product & vectors-

vectorDot(%anyVector, vectorNormalize(%anyVector)) = vectorLength(%anyVector).

to make the vehicle never slow down,
i'd look for friction or drag or something like that.