Skip to content

vehicle_script

Gombaris edited this page Oct 21, 2024 · 4 revisions

vehicle_script

vehicle_script is used to define physics parameters and behavior for ground vehicles.

When the vehicle is created for the first time, the engine invokes the init_chassis event in the script. This is expected to define the vehicle construction, adding wheels and returning vehicle parameters. The init_vehicle event is invoked for each new instance of the vehicle (including the first one), and it can be used to define per-instance parameters. Fps camera position should be defined here.

For each frame, update_frame event is called. Inputs and model animation is supposed to be handled here. In current version engine provides three input values - gas pedal state, brake pedal state, and steering value. These values are provided in normalized ranges, and the script is expected to produce the physical values for engine force, braking force and steering angle for each wheel.

The vehicle interface defines the following methods and events:

Methods

const ot::objdef_params& get_objdef_params() const;
Parameter Description Returns Description
return: objdef parameters Get the objdef parameters associated with the vehicle
bool resolve_path(ot::path_rel_to ref, const coid::token& path, ifc_out coid::charstr& dst) const;
Parameter Description Returns Description
ref - The reference path
path - The relative path
dst - The resolved path
return: false on argument error Resolve relative path
iref<ot::geomob> get_geomob(int id = 0) const;
Parameter Description Returns Description
id - Object ID return: geomob object Retrieve geomob
bool get_objdef_info(ifc_out ot::pkginfo::obj& info) const;
Parameter Description Returns Description
info - Object definition information return: objdef information Retrieve objdef information
coid::token get_objdef_param(const coid::token& key, const coid::token& group = coid::token()) const;
Parameter Description Returns Description
key - Key of the parameter
group - Group of the parameter
return: parameter value Retrieve parameter from objdef
uint get_collision_group(ifc_out uint* mask) const;
Parameter Description Returns Description
mask - Optional mask value return: collision group Get the collision group
void set_collision_group(uint group, uint mask);
Parameter Description Returns Description
group - Collision group ID
mask - Collision mask
Set the collision group
void fade(const coid::token& text) const;
Parameter Description Returns Description
text - Message to post to the fading log Posts a message to the fading log
ot::ECameraMode get_camera_mode() const;
Parameter Description Returns Description
return: camera mode Get the camera mode
uint64 get_custom_data_value() const;
Parameter Description Returns Description
return: custom data value Get the custom data value
float3 heading_pitch_roll() const;
Parameter Description Returns Description
return: heading/pitch/roll angles Get heading/pitch/roll angles of the object in radians
void extra_force(const float3& mpos, const float3& force, bool worldspace = false);
Parameter Description Returns Description
mpos - Position
force - Force vector
worldspace - Indicates if the force is in world space or local space
Add extra force to be applied to the object
void extra_impulse(const float3& mpos, const float3& impulse, bool worldspace = false);
Parameter Description Returns Description
mpos - Position
impulse - Impulse vector
worldspace - Indicates if the impulse is in world space or local space
Apply extra force impulse (force * dt), one-time impulse, non-persistent
uint get_joint_id(const coid::token& name) const;
Parameter Description Returns Description
name - Name of the joint return: joint/bone ID Get the joint/bone id for given bone name
uint get_mesh_id(const coid::token& name, uint8 lod_group = 0xff, uint8 mat_group = 0xff) const;
Parameter Description Returns Description
name - Name of the mesh
lod_group - LOD group index
mat_group - Material group index
return: mesh ID Gets the mesh id
void fire(const float3& pos, const float3& dir, float speed, float caliber, const float3& color, uint joint = pkg::InvalidBoneId);
Parameter Description Returns Description
pos - Position
dir - Direction
speed - Speed
caliber - Caliber
color - Color
joint - Joint ID
Fire a colored plasma projectile
void explode_ground(const ot::ground_explosion& ge);
Parameter Description Returns Description
ge - Ground explosion parameters Explode ground at model position
float elevation_test(const float3& pos, float maxdist, float negdist = -FLT_MIN, uint joint = pkg::InvalidBoneId) const;
Parameter Description Returns Description
pos - Position
maxdist - Maximum distance
negdist - Negative distance
joint - Joint ID
return: height above terrrain or >= maxdist if no hit, <= negdist if under terrain Elevation test
float elevation_test_ext(const float3& pos, float maxdist, float negdist = -FLT_MIN, ifc_out ot::hitpoint* hp = 0, ot::hit_flags hpflags = {.hit = true, .normal = true}, uint joint = pkg::InvalidBoneId) const;
Parameter Description Returns Description
pos - Position
maxdist - Maximum distance
negdist - Negative distance
joint - Joint ID
info - Result information
return: height above terrain or >= maxdist if no hit, <= negdist if under terrain Elevation test with extended info
float ray_test(const float3& pos, const float3& dir, float maxdist, float negdist = -FLT_MIN, ifc_out ot::hitpoint* hp = 0, ot::hit_flags hpflags = {.hit = true, .normal = true}, uint joint = pkg::InvalidBoneId) const;
Parameter Description Returns Description
pos - Position
dir - Direction
maxdist - Maximum distance
negdist - Negative distance
hp - Hit point
hpflags - Hit flags
joint - Joint ID
return: intersection distance or >= maxdist if no hit, <= negdist if under terrain Test if ray from model intersects with terrain
iref<ot::object> object_test(const float3& pos, const float3& dir, float maxdist, ifc_out ot::hitpoint* hp = 0, ot::hit_flags hpflags = {.hit = true, .normal = true, .objects = true, .dynamic = true, .selftest = false}, uint joint = pkg::InvalidBoneId) const;
Parameter Description Returns Description
pos - Starting position
dir - Ray direction
maxdist - Maximum distance of the ray
hp - Pointer to hitpoint structure
hpflags - Hit flags
joint - Joint ID
return: object hit Test if ray from model hits an object
void attach_object(const iref<ot::object>& obj, uint joint_id, const float3& pos, const quat& rot, uint custom_id);
Parameter Description Returns Description
obj - Object to attach
joint_id - Joint ID to attach to
pos - Position of attachment
rot - Rotation of attachment
custom_id - Custom ID for the attachment
Attach object
iref<ot::object> detach_object(uint custom_id, bool activate);
Parameter Description Returns Description
custom_id - Custom ID of the object to detach
activate - Flag to activate the detached object
return: detached object, if found Detach specified object
iref<ot::object> get_attached_to() const;
Parameter Description Returns Description
return: the object this object is attached to or nullptr if this object is free Get object we are attached to
bool detach_ot_object(const iref<ot::object>& object, bool activate);
Parameter Description Returns Description
object - Object to detach
activate - Flag to activate the detached object
return: true if successful Detach specified object
void set_fps_camera_pos(const float3& pos, uint joint_id = UMAX32, ot::EJointRotationMode joint_rotation = ot::JointRotModeEnable);
Parameter Description Returns Description
pos - Position to set
joint_id - Joint ID for position adjustment
joint_rotation - Joint rotation mode
Set model space position for FPS camera
void set_fps_camera_rot(const quat& rot, ot::ERotationMode mouse_rotation);
Parameter Description Returns Description
rot - Rotation quaternion
mouse_rotation - Mouse rotation mode
Set model space rotation frame
void set_fps_camera_fov(float hfov, float vfov = 0);
Parameter Description Returns Description
hfov - Horizontal field of view
vfov - Vertical field of view
Set FOV to chassis FPS preset and also to the current camera if vehicle is entered and FPC camera is active
float3 get_fps_camera_pos() const;
Parameter Description Returns Description
return: camera position Get current FPS camera position
quat get_fps_camera_rot(bool base = false) const;
Parameter Description Returns Description
base - Indicates if the rotation is base or relative return: camera rotation Get current FPS camera rotation in model space
float2 get_fps_camera_fov() const;
Parameter Description Returns Description
return: camera FOV Get FPS camera preset FOV for this chassis
void set_fps_camera_ypr(float yaw, float pitch, float roll, ot::ERotationMode mouse_rotation);
Parameter Description Returns Description
yaw - Yaw angle
pitch - Pitch angle
roll - Roll angle
mouse_rotation - Mouse rotation mode
Set model space orientation for FPS camera
float3 get_fps_camera_ypr(bool base = false) const;
Parameter Description Returns Description
base - Indicates if the angles are base or relative return: yaw/pitch/roll angles Get current yaw/pitch/roll angles of the camera in model space
bool set_fps_camera_tracking_point(const double3& target, bool level_horizon);
Parameter Description Returns Description
target - Target position
level_horizon - Flag to level the horizon
Enable tracking of world point
bool set_fps_camera_tracking(bool level_horizon);
Parameter Description Returns Description
level_horizon - Flag to level the horizon Enable tracking of the current world point (in screen center)
bool set_fps_camera_tracking_off();
Parameter Description Returns Description
Disable tracking
iref<ot::sndgrp> sound();
Parameter Description Returns Description
return: ot::snd_group interface Get sound interface
int register_handler_ext(const coid::token& name, uint group = 0);
Parameter Description Returns Description
name - Action name
group - Action group
return: slot id or -1 on fail Register action handler for an existing knob
int register_event_ext(const coid::token& name, bool release_event, uint group = 0);
Parameter Description Returns Description
name - Action name
release_event - Flag for release event
group - Action group
return: slot id or -1 on fail Register event action handler
int register_axis_ext(const coid::token& name, const ot::ramp_params& ramp, uint group = 0);
Parameter Description Returns Description
name - Action name
ramp - Ramp parameters
group - Action group
return: slot id or -1 on fail Register axis action handler
int register_handler(const coid::token& name, ot::fn_event_action&& handler, int handler_id = 0, uint group = 0);
Parameter Description Returns Description
name - Action name
handler - Event handler
handler_id - Handler ID
group - Action group
return: slot id or -1 on fail Register input action for an existing knob
int register_handler_default(const coid::token& name, ot::fn_event_action&& handler, const ot::ramp_params& def_ramp, int handler_id = 0, const float* def_val = nullptr, uint group = 0);
Parameter Description Returns Description
name - Action name
handler - Event handler
def_ramp - Default ramp parameters
handler_id - Handler ID
def_val - Default value
group - Action group
return: slot id or -1 on fail Register input action for an existing knob with default ramp and value
int register_axis_handler(const coid::token& name, ot::fn_axis_action&& handler, const ot::ramp_params& ramp, int handler_id = 0, const float* def_val = nullptr, uint group = 0);
Parameter Description Returns Description
name - hierarchic action name
handler - optional handler for changed value, void fn(float val, uint code, uint channel, int handler_id)
handler_id - optinal extra data for the handler
def_val - optional default action value
ramp - value limiter parameters
group - activation group where the action is assigned (can be enabled/disabled together)
return: slot id or -1 on fail Register input action sending events on value change (full state button and axis)
int register_event_handler(const coid::token& name, ot::fn_event_action&& handler, int handler_id = 0, uint group = 0, uint extra_channels = 0);
Parameter Description Returns Description
name - hierarchic action name
handler - optional handler for changed value, void fn(int flags, uint code, uint channel, int handler_id)
handler_id - optinal extra data for the handler
def_val - optional default action value
channels - number of extra channels that are handled (multiple engines etc)
group - activation group where the action is assigned (can be enabled/disabled together)
return: slot id or -1 on fail Register input action sending events on button press (value > 0)
void action_group(uint group, bool activate);
Parameter Description Returns Description
group - group id
activate - activate/deactivate the group
Activate or deactivate given action group
void set_center_value(int action, float center);
Parameter Description Returns Description
action: action id
center: center value, will be clamped to minval..maxval range
Set the value to center to, default 0
void set_action_value(int action, float value, bool hold);
Parameter Description Returns Description
action - actionid
value - action value, will be clamped to minval..maxval range
hold - true if the value should be held (disabled centering)
Set action value
void set_instant_action_value(int action, float value, bool notify);
Parameter Description Returns Description
action: action id
value: action value, will be clamped to minval..maxval range
notify: invoke/don't invoke the handler
Set action value
uint add_spot_light(const float3& offset, const float3& dir, const ot::light_params& lp, const coid::token& joint = coid::token());
Parameter Description Returns Description
offset: model-space offset relative to the bone or model pivot
dir: light direction
lp: light parameters
joint: joint name to attach the light to (optional)
uint: light emitter id Define circular spotlight source
uint add_point_light(const float3& offset, const ot::light_params& lp, const coid::token& joint = coid::token());
Parameter Description Returns Description
offset: model-space offset relative to the bone or model pivot
lp: light parameters
joint: joint name to attach the light to (optional)
uint: light emitter id Define point light source
void light(uint id, bool on);
Parameter Description Returns Description
id: light id
on: turn light on (true) or off (false)
Turn light on/off
void light_mask(uint mask, bool on, uint offset = 0);
Parameter Description Returns Description
mask: light bit mask
on: turn lights on (true) or off (false)
offset: offset value for the bit mask (optional)
Turn light on/off using bit mask
void light_toggle(uint id);
Parameter Description Returns Description
id: light id Toggle light
void light_toggle_mask(uint mask, uint offset = 0);
Parameter Description Returns Description
mask: light bit mask
offset: offset value for the bit mask (optional)
Toggle lights by bit mask
void light_color(uint id, const float4& color, float range = 0);
Parameter Description Returns Description
id: light id
color: light color
range: light intensity range (optional)
Set light color/intensity
void lights_off(bool instant = false);
Parameter Description Returns Description
instant: turn lights off instantly or gradually (optional) All lights off
void solar_time(ifc_out double& time, ifc_out float& sun_coef) const;
Parameter Description Returns Description
time: [out] solar time at vehicle location, in milliseconds
sun_coef: sun position relative to horizon: 0 sun at horizon, 1 sun at zenith, -1 sun at anti-zenith
Return current solar time and cosine of sun-zenith angle
int add_display(float width, float height, const coid::token& mesh_name);
Parameter Description Returns Description
width: width of the display
height: height of the display
mesh_name: name of the display's mesh in geomob
int: id of the display Add new display
iref<ot::canvas> get_display_canvas(uint display_id);
Parameter Description Returns Description
display_id: id of the display iref<ot::canvas>: canvas reference Get display canvas
coid::dynarray<coid::charstr> get_display_names();
Parameter Description Returns Description
coid::dynarray<coid::charstr>: list of display names Get display names
uint add_smoke_emitter(const coid::token& joint, const coid::token& texture, const ot::particle_emitter_params& params);
Parameter Description Returns Description
joint: name of the joint emitter will be attached to
texture: relative path of the texture to use as particle billboard (must be in RGBA8 dds format)
params: emitter parameters
uint: smoke emitter id Add smoke emitter
uint add_dust_emitter(const coid::token& joint, const coid::token& texture);
Parameter Description Returns Description
joint: name of the joint
texture: relative path of the texture
uint: dust emitter id Add dust emitter
void set_emitter_color(uint emitter, ot::color color);
Parameter Description Returns Description
emitter: emitter id
color: particle color
Set overall emitter alpha
void pause_emitter(uint emitter, bool pause);
Parameter Description Returns Description
emitter - Emitter ID
pause - Pause flag
Pause emitter
uint load_sound(const coid::token& filename);
Parameter Description Returns Description
filename - Name of the sound file return: sound id Load sound
uint add_sound_emitter(const coid::token& joint, int type = 0, float ref_distance = 0);
Parameter Description Returns Description
joint - Joint token
type - Type of the sound emitter
ref_distance - Reference distance for sound attenuation
return: sound emitter id Attach sound emitter to a model joint, by name
uint add_sound_emitter_id(uint joint_id, int type = 0, float ref_distance = 0);
Parameter Description Returns Description
joint_id - Joint ID
type - Type of the sound emitter
ref_distance - Reference distance for sound attenuation
Attach sound emitter to a model joint, by ID
void set_interior_sound_attenuation(float att);
Parameter Description Returns Description
att - Attenuation value for sounds coming from outside Set attenuation for sounds coming from outside
void fetch_controls(coid::dynarray32<int32>& buf, bool append);
Parameter Description Returns Description
buf - Buffer for storing fetched controls
append - Flag to append to existing buffer
Fetch controls
void apply_controls(const int32* cmd, uint ncmds);
Parameter Description Returns Description
cmd - Array of control commands
ncmds - Number of commands
Apply controls
void open(int openid, ot::kbd_modifier modifiers);
Parameter Description Returns Description
openid - ID of the open door command
modifiers - Keyboard modifiers
Insert an open door command
coref<ot::component::animator> get_animator();
Parameter Description Returns Description
returns: animator component Get animator component
int add_wheel(const coid::token& wheel_pivot, const ot::wheel& tp);
Parameter Description Returns Description
wheel_pivot - Pivot token for the wheel
tp - Wheel parameters
return: wheel id Add wheel with vertical suspension
int add_wheel_swing(const coid::token& axle_pivot, const coid::token& wheel_pivot, const ot::wheel& tp);
Parameter Description Returns Description
axle_pivot - Pivot token for the axle
wheel_pivot - Pivot token for the wheel
tp - Wheel parameters
return: wheel id Add wheel on half-axle swing suspension
int add_track(const coid::token& linkurl, uint nlinks, float x_pos, const coid::dynarray<uint>& wheels, const coid::token& joint = coid::token());
Parameter Description Returns Description
linkurl - URL for the track
nlinks - Number of links
x_pos - Position along x-axis
wheels - Array of wheel IDs
joint - Joint token
return: track id Define tracks
void steer(int wheel, float angle);
Parameter Description Returns Description
wheel - Wheel ID
angle - Steering angle in radians
Steer wheel by setting angle
void wheel_force(int wheel, float engine);
Parameter Description Returns Description
wheel - Wheel ID
engine - Propelling force
Apply propelling force on wheel(s)
void wheel_brake(int wheel, float brake);
Parameter Description Returns Description
wheel - Wheel ID
brake - Brake force
Apply brake force on wheel(s)
void wheel(int wheel, ifc_out ot::wheel_data& wd);
Parameter Description Returns Description
wheel - Wheel index
wd - Output variable for wheel data
Get run-time wheel data
bool on_ground() const;
Parameter Description Returns Description
return: true if at least one wheel has ground contact Return true if at least one wheel has ground contact
float speed() const;
Parameter Description Returns Description
return: current ground speed [m/s] Get current ground speed [m/s]
void velocity(bool model_space, ifc_out float3& linear, ifc_out float3& angular) const;
Parameter Description Returns Description
model_space - Flag indicating whether the velocity should be in model space or world space
linear - Output variable for linear velocity
angular - Output variable for angular velocity
Current vector speeds in model or world coordinate system [m/s]
float max_tire_speed();
Parameter Description Returns Description
return: contact speed of the fastest turning tire Contact speed of the fastest turning tire
float max_rpm();
Parameter Description Returns Description
return: max RPM of any wheel Max RPM of any wheel
void animate_wheels();
Parameter Description Returns Description
Articulate wheel joints according to the current state
void show_tracks(bool show, int track_id = -1);
Parameter Description Returns Description
show - Flag indicating whether to show or hide the tracks
track_id - ID of the track to show/hide
Show or hide the tracks
uint differential_lock(uint flags, bool toggle);
Parameter Description Returns Description
flags - Flags indicating the state of the differential lock
toggle - Flag to toggle the differential lock state
return: new state Set differential lock state
void set_mass(float m, const float3* inertia = 0);
Parameter Description Returns Description
m - Current vehicle mass
inertia - Optional inertia tensor
Set current vehicle mass
float get_mass() const;
Parameter Description Returns Description
return: current mass value Get current vehicle mass
float get_inv_mass(ifc_out float3x3* inv_inertia = 0) const;
Parameter Description Returns Description
inv_inertia - optional, out - inverse local inertia tensor of the body return: reciprocal mass (1.0/mass) Get the reciprocal mass (1.0/mass)
float3 com_offset() const;
Parameter Description Returns Description
return: current offset from model pivot to center of mass Get current offset from model pivot to center of mass
void set_com_offset(const float3& offset);
Parameter Description Returns Description
offset - Offset to set as the center of mass Set center of mass offset
float articulated_angle(int part) const;
Parameter Description Returns Description
part - Part index return: articulated_angle Get articulated angle
bool in_water() const;
Parameter Description Returns Description
return: true if vehicle is at least partially alleviated by water Get information, if vehicle is at least partially alleviated by water
float water_density() const;
Parameter Description Returns Description
return: local water density kg/m^3 Get local water density kg/m^3
float water_line() const;
Parameter Description Returns Description
return: water line height above bottom bound for current mass Get water line height above bottom bound for current mass
void set_hover(float hover);
Parameter Description Returns Description
hover - Hover height to set Set hover height
float get_wheel_param(int wheel, const coid::token& name) const;
Parameter Description Returns Description
wheel - Wheel index
name - Name of the parameter to get value for
return: wheel parameter value Get wheel parameter value
void set_wheel_param(int wheel, const coid::token& name, float value);
Parameter Description Returns Description
wheel - Wheel index
name - Name of the parameter to set value for
value - Value to set for the parameter
Set wheel parameter value
float get_wheel_radius(int wheel, bool original);
Parameter Description Returns Description
wheel - Wheel index
original - Flag indicating whether to get the original radius or the current (scaled) radius
return: tire radius Get tire radius
void set_wheel_radius(int wheel, float rad);
Parameter Description Returns Description
wheel - Wheel index
rad - Radius to set for the wheel
Set wheel radius
void set_axle_params(int wheel, const float2& zcs, float minz, float len);
Parameter Description Returns Description
wheel - Wheel index
zcs - Coordinates of the center of the wheel (zcs.x: static, zcs.y: dynamic)
minz - Minimum compression length
len - Axle length
Set axle parameters
void get_axle_params(int wheel, ifc_out float2& zcs, ifc_out float& minz, ifc_out float& len) const;
Parameter Description Returns Description
wheel - Wheel index
zcs - Output variable for the coordinates of the center of the wheel (zcs.x: static, zcs.y: dynamic)
minz - Output variable for the minimum compression length
len - Output variable for the axle length
Get axle parameters

Events

virtual ot::chassis_params init_chassis( const coid::charstr& params )
Parameter Description Returns Description
params - Parameters for initializing the chassis return: chassis parameters Initialize chassis (shared across all vehicle instances of the same type)
virtual ot::chassis_params init_chassis_script( const coid::charstr& params )
Parameter Description Returns Description
params - Parameters for initializing the chassis return: chassis parameters Initialize chassis (shared across all vehicle instances of the same type)
virtual void update_actions_script( float dt, const coid::range<int32>& actbuf )
Parameter Description Returns Description
dt - Time step for the update
actbuf - Range of action buffers
Interface event for handling vehicle state before rendering (inputs, animating joints etc)
virtual void engine( bool start )
Parameter Description Returns Description
start - Flag indicating whether to start or stop the engine Start/stop engine
virtual float ext_param( const coid::token& name, const float* value )
Parameter Description Returns Description
name - Name of the external parameter to set/get
value - Value to set for the external parameter
return: new/existing param value Set/get externally provided parameter
virtual void init_vehicle( bool reload )
Parameter Description Returns Description
reload - Flag indicating whether to reload the vehicle Initialize vehicle
virtual void destroy_vehicle( bool reload )
Parameter Description Returns Description
reload - Flag indicating whether to reload the vehicle Vehicle is going to be destroyed or reloaded
virtual void update_frame( float dt, float engine, float brake, float steering, float parking )
Parameter Description Returns Description
dt - Time step for the update
engine - Engine status
brake - Brake status
steering - Steering angle
parking - Parking brake status
Update model instance for rendering each frame
virtual void render_displays( float dt )
Parameter Description Returns Description
dt - Time step for rendering Render displays
virtual void simulation_step( float dt )
Parameter Description Returns Description
dt - Time step for the simulation Update model instance 60 times per second (used for physics)
virtual void force_bind_script_events()
Parameter Description Returns Description
Force bind script events

See also geomob interface and Vehicle chassis parameters for further information.

You can see the usage of this interface in the following tutorials, based on your programming language preference

Test

Clone this wiki locally