Physics Car Tutorial

 carPhysics4

What You’ll learn

Below is a list of the topics this demo will address:

  • Physics Car: Add and configure the physics car component.
  • Car Controller: How to accelerate/deccelerate and turn the car.
  • Component cast: How to cast a target in schematic.
  • SubTypes: how to create a subtype and use it in VSL and schematic.
  • InnerGraph: how to create, connect and edit an inner graph.

Prerequisites

3DVIA Studio User Interface
You should be familiar with the key 3DVIA Studio interface elements, such as the views (Property view, Project view, editors) and basic drag and drop techniques.

Scripting Basics
We assume that you already have a grasp of basic scripting principles.

Project Structure

The default stage contains the following environment objects :

  • the car wich has 4 wheels as sub actors
  • a Physical World
  • the walls with a physics rigid object component set to fixed
  • a directional light
  • a camera wich follows the car and switch view using the “space” keyboard
  • a field environment wich will be the floor of our application

As a note we have resized the car and the field to be realistic meters-based sizes. So the car is 2m x 1.5m x 3m and the field is 40m x 40m

ProjectStructure

Physics car and Floor management

In this chapter we add the physics components to the car and the floor.

Download the Physics Car Tutorial Start stage here.

1. Drag and drop the Physical Rigid Object component from the Librairies, on the field object.

floorphysics1

2. On the vkPhysicsRigidObject component, change the Shape property from vkPhysicsBox to vkPhysicsHeightField. Expand the shape property and set the Half length and Half Width to 100. It is the topology precision we want to use for the floor.
Then set the Get Heightfield From Node property to the Field#vkEntity3D. This will compute our physic mesh to be used for the floor.
Then we set the Motion Type to Fixed, so that the floor doesn’t fall with gravity.

floorphysics2

3. Drag and drop the Physical Car component from the Librairies->Physics->Vehicles, on the car.

carPhysics1

4. On the vkPhysicsCar component, we set the shape to be vkPhysicsConvex. It will use the simplified 3D object Mesh to collide with the environment.

carPhysics2

We set Front/back left/right wheels by cliking the 3D objects in the 3D view.
The most important property is the car direction to inform the component how to orient the wheels. In our case the forward direction is along the Z axis.

carPhysics3

We also want more transmision power from the engine to the wheel, so we set this property to 30.

carPhysics3bis

the other settings are described here .
We leave the values by default because the car size is realistic.
You can hit play to test if the car is falling on the ground.

Turn, accelerate and decelerate

Now we have a physicalized environment this chapter explains how to drive the car.

5. Create a new behavior called “CarController” on the car.
Add 4 members to set the keyboard keys of acceleration, deceleration, turn right and left.
Add a member for the acceleration factor.

carPhysics5

6. Create a new VLS task named “CarControl”.
In the Target, we add the vkPhysicsCar component.

carPhysics6

In the Execute() function, we use the functions GearUp()/GearDown() and Accelerate() on the vkPhysicsCar class. Turn() function is used to turn the car.

carPhysics7

7. We need to set the behavior members values in the car property view. We choose an Acceleration Factor of 1.

carPhysics8

Run the experience, the car should be controlled using the chosen keys.

Restore car position

In many cases the car will somersault and the user may want to turn it over.
To do so we need to deactivate the physics car component, reset the wheels position, orientate the car and reactivate the physics.
At the start of the “CarControl” task we’ll store the initial wheel positions. A new task will be created in schematic to turn over the car.

8. create a subtype called “WheelLocalPosition”, of Class Type.

straightenUp1

create 3 members position, direction and up, of vkVec3 type.

straightenUp2

add 4 members in the CarController behavior, of our new subtype WheelLocalPosition type.

 straightenUp3

9. In the CarControl VSL task we get the initial wheel position values using the OnStart() function. But first we need to cast the 3D car component in the Target.

straightenUp4

To get the wheel 3D objects we use the Physics Car component, and then the vkPhysicsCar class targeted variable names “myPhyCar”. On this component we have set the members Front Left Wheel, Front Right Wheel, etc… which can be accessed in VSL. We need to cast these members to vkEntity3D class because they are vkComponent type. We use the ComponentCast() function.
Then we get the position, direction and up vectors in the car referential (local referential).

straightenUp5

10. create a Schematic task called “StraightenUp”.

straightenUp6

We want to pick the car to turn it, so we ignore the other 3D objects.
To do a picking test we use the building blocks combo “mouse waiter” + “Picking” + “switch on parameter”. Edit the variable settings of the “Mouse Waiter” to keep only the eButtonLeft button. Edit the variable settings of the switch on parameter to be vkNode3DPtr type.
We add a new Target of type vkNode3DPtr to refer to the car 3D object and connect the buildgin blocks as follow.

straightenUp7

11. After the picking test we deactivate the Physics Car component, reset the orientation of the car and reactivate the physics component.
We add a new Target of type vkPhysicsCarPtr to refer to this component and we use the building block “Set Activity”. We use “Set orientation in Space” and “Translate” building blocks to restore the car position.

straightenUp8

12. To reset the wheels position we use an Inner Graph. Right click in the schematic and choose “New Building Block”. In the dialog box choose Inner Graph and name it ResetWheels.

straightenUp9

Edit Inner graph and add the targets FrontLeftWheelPos, FrontRightWheelPos, BackLeftWheelPos, BackRightWheelPos from the CarController behavior; the wheels vkComponent from the Physics car; and the car 3D object which is a new Target of type vkNode3DPtr.

straightenUp10

straightenUp11

straightenUp11bis

For every wheel we’ll set position and orientation using the members we have set at the start of the VSL CarControl task.
As in VSL we need to cast the wheels from vkComponent to vkNode3D class. To do so we use the ComponentCast() function of the vkNode3D class. Control + Shift + Double clik on the schematic to call the ComponentCast() function. All the functions for every class are displayed. Select the one of the vkNode3D class.

straightenUp12

We can convert it in a parameter operator by right clicking on the building block and selecting “Toggle Evaluator”. We can then connect directly the wheels targets to the Component Cast parameter operator. For every wheel we use the building blocks “Set Position In Space” and “Set Orientation In Space”.

straightenUp13

Finally we finish the StraightenUp task by re activating our Physics Car component.

straightenUp14

Now you can play the experience and when the car turns over you can reset its position by cliking on it.

Your Final project should be similar to this one: Physics Car Tutorial Final Stage