Vehicle Tut

download Vehicle Tut

of 9

Transcript of Vehicle Tut

  • 8/3/2019 Vehicle Tut

    1/9

    Vehicle Physics - Basics

    Posted by: Admin on Saturday, December 08, 2007 -

    10:25 PM

    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 tutorialis from my experience 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 wasbeing used.

    This may not be the best wayor the most accurate, but I've

    learned enough to get the

    vehicles to work and have funwith 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 wheelswill 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 yourotate or scale something, it's not truly 'set in stone'

    http://www.blender3dclub.com/index.php?name=News&file=article&sid=42http://www.blender3dclub.com/index.phphttp://www.blender3dclub.com/index.php?name=News&catid=&topic=6http://www.blender3dclub.com/index.php?name=News&file=article&sid=42
  • 8/3/2019 Vehicle Tut

    2/9

    until this is done. Rotating an 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 asimple '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 firsttime 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 withmultiple vehicles, they would be assigned different cid numbers.

    The force value represents the amount for force thatwill 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 editorin 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 myexample, the name of my

    car object is 'car'. Switch to

    'Logic' (F4). Add an always

    sensor and a PythonController. 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 Iloaded in the text editor (buggy.py).

    http://www.blender3dclub.com/downloads/buggy.pyhttp://www.blender3dclub.com/downloads/buggy.py
  • 8/3/2019 Vehicle Tut

    3/9

    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 forcevalues. 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 labeled

    accordingly because the vehicle script looks for this label when creating the vehicle. In this image, Ihave the right rear wheel selected. The actuator for this wheel must be labeled wheelRRso 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 attach it to the car.

    Complete this process for each of the remaining 3 wheels. Label 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.

  • 8/3/2019 Vehicle Tut

    4/9

    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 vehiclephysics and attaching the wheels.

    This section also shows the values for the front and rear wheel axle height. Again, this is based off ofthe 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 vertical 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 be1.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' length of your shock absorbers. In my example, it'sset to the same radius of my wheel, 0.6. Think of it as the spring length between your wheel's center and

    your vehicle's center in the Z direction (up and down).

  • 8/3/2019 Vehicle Tut

    5/9

    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 upthe object's properties.

  • 8/3/2019 Vehicle Tut

    6/9

    In my example, we'relooking 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 roughly

    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).

  • 8/3/2019 Vehicle Tut

    7/9

    This section is basically the same as the last section. It sets the wheel X and Y locations and attaches therear 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 alterthe 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)

  • 8/3/2019 Vehicle Tut

    8/9

    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 rearwheels.

    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 wheelys and flipping over. Too much value on the front wheels and the car does a wheely

    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 wheely

    effect but at the same time it enables you to propel the car even when it's not touching the ground.

    Steering:

    To control 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 90degrees.

    In order to straighten out our wheels again, we need to get our steer value back to zero. We could makeit 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

  • 8/3/2019 Vehicle Tut

    9/9

    -.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

    mailto:[email protected]://www.blender3dclub.com/index.phpmailto:[email protected]