Vehicle Physics

On January 21, 2012, in Tutorials, by Admin

This tutorial was written with the intent that the reader has a basic understanding
of modeling and logic blocks within blender.


The information in this tutorial is from my experiance after going through the
code from the Demo that was released and ‘reverse engineering’ it. Taking values
from different section of the code, altering it until I understood how it was being
used.

This may not be the best way or the most accurate, but I’ve learned enough to get
the vehicles to work and have fun with them.

To start out with, you need to create the basics – A vehicle (car) body and 4 (or more)
wheels.

In my example, my car is facing the front view (num 1). Pressing ’7′ for an above view
shows my car facing the bottom of my screen.

The object center for the car body will represent the ‘center of gravity’ for the vehicle.
Also, the wheels will be placed on the vehicle based on the car object’s center.

With your car selected, press ‘CTRL-A’ while in object mode to apply the current scale
and rotation to your object. Apparently, with blender, when you rotate or scale something
in Blender, it’s not truly ‘set in stone’ until this is done. Rotating and object while
editing it and then pressing ‘P’ to start the game engine, you may see the object
revert back to it’s previous rotation unless this step is done.

The object center on my car for this tutorial is set at about the floor level and in the middle (left to right)
of the body. You can see part of dotted circle in this image that shows the objects
center.


The car body will need to be an ‘actor’ so it will abide by the laws of gravity in
the game. Set the ‘Bounds’ to ‘convex hull polytope’ so collisions will be based on
the cars actual shape and not just a simple ‘box’ or ‘sphere’ shape.

There are 4 variables that need to be set for the car.

  • init
  • steer
  • cid
  • force

The init value is set at zero initially and basically just tells the python script
that this is the first time through the loop. Once the vehicle is created, the
script will skip the initialization part of the script and only go through the
part that deals with engine force and steering.

The steer value represents how much the wheels are turned. When we alter this value,
the wheels will turn either left or right.

The cid value, as far as I can tell, is only used as an id number for this vehicle.
I believe with mulitble vehicles, they would require different cid numbers.

The force value represents the amount for force that will cause the wheels to turn.
Changing this number will cause the wheels to turn faster, slower or even backwards.

The script for this demo will be available here to download.
Load this script inside of the text editor in blender. The name of the file (buggy.py)
will need to match the name of the script we type in the following step.


Select your car. In my example, the name of my car object is ‘car’. Switch
to ‘Logic’ (F4). Add an always sensor and a Python Controller. Connect the
two with a line and type in the name of your vehicle script here. For this
example, mine matches the name of the script that I loaded in the text editor
(buggy.py).

Pressing ‘P’ now, will start the game engine. The script will run through the
setup process first, change the init value and then continue to loop through
the script assigning steering and force values. We don’t have our wheels set up yet,
so the script can’t find them.

 


Setting up the wheels:

Select each wheel one at a time. Give each wheel a visibility actuator. Each
actuator must be labled accordingly because the vehicle script looks for this
lable when creating the vehicle. In this image, I have the right rear wheel selected.
The actuator for this wheel must be labled wheelRR so the vehicle script will find it
correctly. Hold ‘SHFT’ and select your car object so both the car and the wheel are
selected. Draw a connecting line between the wheel’s visibility actuator and the
python script controller. Once this is done, the vehicle script will be able to
locate your wheel and attache it to the car.

Complete this process for each of the remaining 3 wheels. Lable them, wheelRF,
wheelLR and wheelLF (Right Front, Left Rear and Left Front).


 

Vehicle Physics

The first part of the script starts the Game Logic and Physics Constraints. It
gets the current controller and owner, which in this case is our ‘car’ object.


The next part of the script checks the init value. If this value is 0, then
the script goes through the process of setting up the vehicle physics and
attaching the wheels.

This section also shows the values for the front and rear wheel axel height. Again,
this is based off of the object center for your ‘car’. In my car, the object center
is very close to the height where I want my wheels attached, so this value is small.
It represent the distance away from your car’s center.

This next section shows where the script attaches the front wheels. Depending on
the shape and size of your vehicle, you’ll need to adjust these values to get the
wheels where you want them.

The waX and waY value represents the wheel attachment location on the X and Y axis of
your car. Again, this will be based off of your vehicle object’s center. In my example,
my front left wheel will be 1.2 units away from my car’s center in the X direction and
2.2 units away from my car’s center in the Y direction.

suspensionRestLength represents the ‘imaginary’ lenght of your shock absorbers.
In my example, it’s set to the same radius of my wheel, 0.6. Think of it as the spring
lenght between your wheel’s center and your vehicle’s center in the Z direction (up and down).

This image shows my vehicle with the suspensionRestLength at 0.6.

This image shows my vehicle with the suspensionRestLength set at 3.6.

In order to see the Physics during the game, from the ‘Game’ menu, check the
‘Show Physics Visualization’.

wheelradius(front wheels) and Rwheelradius(rear wheels) values
represent the radius of your wheels. An easy way to determine this number is
to select your wheel while in ‘Object’ mode and press ‘N’ to bring up the
object’s properties.

In my example, we’re looking at the dimY and dimZ values. These represent the
diameter of our wheel and should be fairly close in value (Unless you’ve got some
crazy wheels going on that aren’t even round!). Mine are rougly 1.2 units in diameter
so I’ve set my wheel radius to 0.6.

With the Physics Visualization on and pressing ‘P’ to start the game engine,
you can see that the wheel radius is represented by a purple line extending out from the wheel attachment center.

Finally, in this section, the hasSteering value simply means that when we alter the
steering value, it will affect these wheels (currently the front wheels).

This section is basically the same as the last section. It sets the wheel X and
Y locations and attaches the rear wheels.


Controlling the Car:

To get our vehicle to move, we simply change the ‘force’ value. One way of doing
this, is to select your vehicle and create a keyboard sensor using the uparrow
button. With this sensor, we can use it to alter the force value. In my example,
it assigns 10 to the force value causing the wheels to rotate forward. The car
begins to move.

As the script continues to loop through, you can see here where it applies the
engine force.

vehicle.applyEngineForce(body.force * .5, 0)

This line applies the engine force to the first wheel (represented by the 0).
The next 3 lines applies the force amount to the remaining three wheels (1, 2, 3).
0 and 1 are the front wheels. 2 and 3 are the rear wheels.

I’ve had to work with these lines quite a bit in order to get the car to roll
correctly. As these lines show, the front wheels receive half of the total force
value (10 * .5). The 2 rear wheels receive the total force value times .15
(force * .15). Too much force from the rear wheels and most of the time my cars
ended up doing wheelies and flipping over. Too much value on the front wheels and the
car does a wheelie while in reverse (a force amount with a negative number).

A different method could be to apply a negative value to the car’s Force Y value.
This would basically be the same as pushing a toy car with your hand across the floor.
This technique eliminates the wheelie effect but at the same time it enables you
to propell the car even when it’s not touching the ground.


Steering:

To controll the vehicle’s steering, create a keyboard sensor. Starting with the
right arrow, use it to set the steer property to -1. -1 will rotate the front wheels
about 50 degrees. It looks like 1.5 equals about 90 degrees.

In order to straighten out our wheels again, we need to get our steer value back to
zero. We could make it so they just jump right back to zero when you let go of the
arrow key, but that may look a little quirky. Using the sensor and controller in
the above image, we can cause our wheels to straighten out gradually. This will
look more like the normal function of car. It adds .02 to the steer value when ever
it is between -.001 and -1 until the wheels are straight again.

This covers the basics that I’ve learned so far. With some trial and error, you can
create some terrific vehicles.

Have fun!

Scott

email

 

Comments are closed.



muzica noua
filme Online
filme online gratis
seriale online
filme noi
muzica noua
desene animate online
jocuri noi online
jocuri online
filme bune
filme online
filme online hd
filme online subtitrate
filme noi