OpenBF1942-Plus
Refractor 1, rebuilt from scratch
Loading...
Searching...
No Matches
physics.h
Go to the documentation of this file.
1// Physics (ADR 0004): Jolt owns rigid bodies, contacts and collision queries; the Refractor vehicle parts
2// (Spring wheels, Engine) are ours and act on Jolt bodies as forces. Spec: docs/formats/vehicles.md
3#pragma once
4#include <map>
5#include <memory>
6#include <utility>
7#include <set>
8#include <string>
9#include <vector>
10#include "con.h"
11#include "mat.h"
12#include "scene.h"
13#include "sm.h"
14#include "terrain.h"
15
16namespace obf {
17
18struct WheelDef {
19 float pos[3] = {}; // wheel centre at rest, vehicle space
20 float radius = 0.4f;
21 float strength = 25, damping = 5; // setStrength / setDamping
22 float steer_deg = 0, steer_rate = 0; // RotationalBundle on c_PIYaw above the spring: max angle, deg/s
23 bool driven = false; // under the Engine
24};
25
26// Wing: a flat surface pushing along its normal (local y). Control input deflects it (flap lift).
27struct WingDef {
28 float pos[3] = {}, normal[3] = {0, 1, 0}; // force point (position + setPositionOffset), vehicle space
29 float lift = 0, flap = 0, regulate = 0; // setWingLift, setFlapLift, setRegulateToLift (always 4.91 in retail)
30 int input = 0; // setInputToPitch: 0 none, 1 c_PIPitch, 2 c_PIRoll, 3 c_PIYaw
31 float sign = 1, rate = 4; // direction (sign of setAcceleration), full deflections per second
32 float range = 0; // degrees at full deflection (drawing)
33};
34
35// FloatingBundle: a buoyant column from pos down setHullHeight; lifts setFloatMaxLift x mass x submerged fraction.
36struct FloatDef {
37 float pos[3] = {};
38 float height = 1, lift = 0;
39};
40
41// A moving part, drawn through build_object's JointFn. Generic joints follow an input per axis (yaw, pitch, roll):
42// absolute inputs drive towards min/max, mouse-look inputs move at setMaxSpeed and stop at the limits.
43struct JointDef {
46 float mn[3] = {}, mx[3] = {}, speed[3] = {}, sign[3] = {1, 1, 1};
47 bool reset = false;
48 int wheel = -1, wing = -1; // wheels spin and ride their springs; wings show their deflection
49 bool propeller = false; // spins with the throttle
50 int seat = 0; // the seat whose controls move it (look inputs)
51};
52
53// The first FireArms with a projectile in a vehicle tree, and that projectile. Spec: docs/formats/weapons.md
54struct WeaponDef {
55 std::string name; // lower-case FireArms template: its drawn part is the muzzle
56 float velocity = 0, interval = 0.1f; // velocity (m/s); 1 / roundOfFire (rounds per second: Sherman 0.35, MG42 15)
57 float ttl = 10; // projectile timeToLive
58 int material = 0, material2 = -1; // projectile hit and explosion materials (damage table)
59 int mag_size = 0, mags = 0; // magSize, numOfMag (0: unlimited)
60 float reload = 0; // reloadTime (s)
61 bool fire_once = false; // fireOnce: one round per trigger pull
62 int ammo_type = 0; // ammoType: which supply depots refill it
63 float radius = 0; // projectile radius: explosion (material2 damage falls off to 0 at it)
64 std::string end_effect; // projectile endEffectTemplate (an EffectBundle: explosion sound, later visuals)
65 std::string projectile; // lower-case projectile template (its sound script plays in flight)
66 bool bounce = false; // dieAfterColl 0 (grenades, mines, explosive packs)
67 float near = 0; // explodeNearEnemyDistance (mines), 0: none
68 bool heals = false; // magType 1: a medic pack ("healing type"), fires no projectile
69 std::string muzzle; // its EffectBundle child (e_MuzzGun): shown at the muzzle on each shot
70 float muzzle_pos[3] = {}; // that child's setPosition on the weapon
71};
72
73// A crew position: the vehicle's own PlayerControlObject (seat 0, the driver) or one nested in it (gunners, passengers).
74struct SeatDef {
75 std::string name; // lower-case PlayerControlObject template
76 WeaponDef weapon; // its first FireArms; empty name: unarmed
77 float exit[3] = {-2, 0, 0}; // setSoldierExitLocation, vehicle space
78 float pos[3] = {}; // its SeatObject, vehicle space (at rest)
79 Mat4 at = Mat4::identity(); // the SeatObject's transform, vehicle space
80 int show = 0; // seatFlags: 0 hidden (inside), 1 sitting (full body), 2 half body, 3 standing
81 std::string lower, upper; // seatAnimationLowerBody / UpperBody, lower-case; empty: by `show`
82};
83
84struct VehicleDef {
85 float mass = 1000, drag = 0;
86 float torque = 0, differential = 1; // Engine setTorque / setDifferential
87 bool tracked = false; // c_ETTank: the engine turns the hull (setInputToYaw) instead of steering wheels
88 float turn_rate = 0; // deg/s at full steer, tracked only
89 bool plane = false; // c_ETPlane: the engine is thrust, wheels roll freely
90 bool ship = false; // has a c_ETShip engine: propeller thrust only in water
91 float thrust = 0; // aircraft and ships: m/s^2 at full throttle, summed over engines
92 std::vector<FloatDef> floats;
93 std::map<std::pair<std::string, int>, JointDef> joints; // (lower template name, occurrence) as JointFn
94 float exit[3] = {-2, 0, 0}; // setSoldierExitLocation, vehicle space
95 float hitpoints = 100;
96 int material = 0; // the PlayerControlObject's material: armour where no face is hit
97 std::vector<float> armor; // triangles (9 floats), vehicle space: the parts' projectile collision blocks
98 std::vector<int> armor_material; // per triangle
99 WeaponDef weapon; // empty name: unarmed
100 std::vector<SeatDef> seats; // [0] the driver
101 std::vector<WingDef> wings;
102 std::vector<WheelDef> wheels;
103 std::vector<float> hull; // xyz points, vehicle space
104 // End (in floats) of each part's points in hull: one convex hull per part, joined as a compound (a carrier's deck
105 // and island, a tank's hull and turret). Empty: one convex hull of all points.
106 std::vector<size_t> hull_split;
107};
108
109// FireArms / HandFireArms template and its projectile; empty name if it has none.
110WeaponDef weapon_def(const Con& con, const std::string& name);
111
112// Walks an object template tree (first LodObject child only). Throws if it has no hull geometry.
113VehicleDef vehicle_def(const Con& con, const Vfs& vfs, const std::string& name);
114
115class Physics {
116public:
117 // water_level: sea surface height (Terrain.con waterLevel); floats need it.
118 Physics(const Heightmap& hm, float world_size, float water_level = -1e9f);
120 // Static collision for placed .sm meshes (first collision block); returns bodies added.
121 // ladders: object templates in collision group c_CGLadders (lower case); their placed boxes become climbable.
122 size_t add_scene(const std::vector<DrawItem>& items, const Vfs& vfs, const std::set<std::string>& ladders = {});
123 void add_ladder(const float lo[3], const float hi[3]); // a climbable world box
124 size_t ladders() const;
125 void add_static(const std::string& key, const CollisionMesh& mesh, const Mat4& world);
126 int add_vehicle(const VehicleDef& def, const Mat4& world);
127 // Inputs in [-1, 1], named after Refractor's c_PI*: yaw +1 = right (cars steer, tanks turn, rudders); roll +1 = right
128 // wing down; pitch +1 = nose down (stick forward).
129 void drive(int vehicle, float throttle, float yaw, float pitch = 0, float roll = 0);
130 void set_velocity(int vehicle, float x, float y, float z);
131 void look(int vehicle, float x, float y, int seat = 0); // c_PIMouseLookX / Y in [-1, 1]: that seat's turrets and guns
132 // Local transform of a moving part for build_object (identity for parts that do not move).
133 Mat4 joint(int vehicle, const std::string& name, int occurrence) const;
134 void step(float dt); // fixed 60 Hz substeps
135 Mat4 transform(int vehicle) const;
136 float speed(int vehicle) const; // m/s
137
138 // Projectiles: ballistic points (gravity), tested against the world each step; never hit `ignore_vehicle`.
139 struct Hit {
140 int vehicle = -1; // or -1: terrain, water, static objects
141 int character = -1; // a soldier hit; height: above its feet (head, chest or limbs)
142 float height = 0;
143 int armor = -1; // material of the armour face hit (vehicles), else -1
144 int material = 0, material2 = -1;
145 int tag = 0; // as given to fire (the game uses the shooter's team)
146 float pos[3] = {};
147 float radius = 0; // explosion radius as given to fire; also reported when the shot's time runs out
148 int shot = -1; // the id fire returned
149 };
150 // Returns the shot's id. bounce (dieAfterColl 0): bounces off whatever it meets and comes to rest, until its
151 // time runs out or explode(); no hit on contact.
152 int fire(const float pos[3], const float vel[3], int material, int material2, float ttl, int ignore_vehicle = -1,
153 int ignore_character = -1, int tag = 0, float radius = 0, bool bounce = false);
154 bool shot_position(int id, float out[3]) const; // false once it is gone
155 void explode(int id); // ends it at the next step (a hit there if it has a radius)
156 std::vector<Hit> take_hits(); // since the last call
157 // Nothing between a and b except the given soldiers and vehicle (the looker and its target).
158 bool line_of_sight(const float a[3], const float b[3], int ignore_character, int target_character, int target_vehicle,
159 int ignore_vehicle = -1) const; // ignore_vehicle: the one the looker sits in
160 void projectiles(std::vector<float>& out) const; // position and velocity (6 floats) per live projectile
161 void projectile_ids(std::vector<int>& out) const; // fire's id per live projectile, in the same order
162
163 // Soldiers: a 1.8 m capsule (Jolt CharacterVirtual) standing at feet position (x, y, z). It walks on the terrain,
164 // static meshes and vehicles, steps up 0.4 m, falls, jumps, and swims at the surface.
165 int add_character(float x, float y, float z);
166 void walk(int character, float vx, float vz, bool jump); // wanted horizontal velocity, m/s
167 void place_character(int character, float x, float y, float z);
168 void set_character_active(int character, bool active); // inactive: not simulated (in a vehicle)
169 void kill_character(int character); // out of the world for good (shots pass)
170 void character_position(int character, float out[3]) const;
171 // 0 stand (1.8 m), 1 crouch (1.2 m), 2 prone (0.7 m capsule; shortcut: upright, not lying along the body).
172 // False, posture kept, when there is no room (standing up under a low roof).
173 bool set_posture(int character, int posture);
174 // Parachute: quadratic drag on the fall, k = setParachuteDrag / mass (24 / 100: about 6.4 m/s); closes on landing.
175 void open_parachute(int character, float k);
176 bool parachute_open(int character) const;
177 float take_landing(int character); // downward speed of the last landing (m/s), then 0
178 void character_steps(int character, unsigned& jumps, unsigned& landings) const; // running counts (landings above 3 m/s)
179 bool character_swimming(int character) const; // in water deep enough to swim
180 bool character_climbing(int character) const; // on a ladder, moving up or down
181 int character_ladder(int character) const; // 1 climbing up, -1 down, 2 holding on, 0 off a ladder
182 bool character_on_ground(int character) const;
183
184private:
185 struct Impl;
186 std::unique_ptr<Impl> p_;
187};
188
189} // namespace obf
Definition con.h:21
int add_vehicle(const VehicleDef &def, const Mat4 &world)
Definition physics.cpp:571
void drive(int vehicle, float throttle, float yaw, float pitch=0, float roll=0)
Definition physics.cpp:630
void explode(int id)
Definition physics.cpp:966
void add_ladder(const float lo[3], const float hi[3])
Definition physics.cpp:1061
void step(float dt)
Definition physics.cpp:1127
Mat4 joint(int vehicle, const std::string &name, int occurrence) const
Definition physics.cpp:827
std::vector< Hit > take_hits()
Definition physics.cpp:971
int fire(const float pos[3], const float vel[3], int material, int material2, float ttl, int ignore_vehicle=-1, int ignore_character=-1, int tag=0, float radius=0, bool bounce=false)
Definition physics.cpp:946
bool shot_position(int id, float out[3]) const
Definition physics.cpp:957
size_t add_scene(const std::vector< DrawItem > &items, const Vfs &vfs, const std::set< std::string > &ladders={})
Definition physics.cpp:537
void character_steps(int character, unsigned &jumps, unsigned &landings) const
Definition physics.cpp:1066
void set_velocity(int vehicle, float x, float y, float z)
Definition physics.cpp:636
bool parachute_open(int character) const
Definition physics.cpp:1060
void add_static(const std::string &key, const CollisionMesh &mesh, const Mat4 &world)
Definition physics.cpp:501
void character_position(int character, float out[3]) const
Definition physics.cpp:1120
float take_landing(int character)
Definition physics.cpp:1070
int character_ladder(int character) const
Definition physics.cpp:1063
void place_character(int character, float x, float y, float z)
Definition physics.cpp:1105
bool set_posture(int character, int posture)
Definition physics.cpp:1072
size_t ladders() const
Definition physics.cpp:1062
void projectile_ids(std::vector< int > &out) const
Definition physics.cpp:995
void look(int vehicle, float x, float y, int seat=0)
Definition physics.cpp:820
void walk(int character, float vx, float vz, bool jump)
Definition physics.cpp:1099
void set_character_active(int character, bool active)
Definition physics.cpp:1111
float speed(int vehicle) const
Definition physics.cpp:1149
void kill_character(int character)
Definition physics.cpp:1118
bool character_climbing(int character) const
Definition physics.cpp:1064
void open_parachute(int character, float k)
Definition physics.cpp:1059
int add_character(float x, float y, float z)
Definition physics.cpp:1087
void projectiles(std::vector< float > &out) const
Definition physics.cpp:990
Mat4 transform(int vehicle) const
Definition physics.cpp:1139
bool line_of_sight(const float a[3], const float b[3], int ignore_character, int target_character, int target_vehicle, int ignore_vehicle=-1) const
Definition physics.cpp:973
Physics(const Heightmap &hm, float world_size, float water_level=-1e9f)
Definition physics.cpp:480
bool character_swimming(int character) const
Definition physics.cpp:1065
bool character_on_ground(int character) const
Definition physics.cpp:1125
Definition vfs.h:11
Definition con.py:1
Definition anim.cpp:7
VehicleDef vehicle_def(const Con &con, const Vfs &vfs, const std::string &name)
Definition physics.cpp:348
WeaponDef weapon_def(const Con &con, const std::string &name)
Definition physics.cpp:304
Definition sm.h:18
Definition physics.h:36
float lift
Definition physics.h:38
float height
Definition physics.h:38
float pos[3]
Definition physics.h:37
Definition terrain.h:21
Definition physics.h:43
int seat
Definition physics.h:50
float mn[3]
Definition physics.h:46
int wheel
Definition physics.h:48
Input input[3]
Definition physics.h:45
float speed[3]
Definition physics.h:46
int wing
Definition physics.h:48
float sign[3]
Definition physics.h:46
bool reset
Definition physics.h:47
Input
Definition physics.h:44
@ Pitch
Definition physics.h:44
@ None
Definition physics.h:44
@ Yaw
Definition physics.h:44
@ LookX
Definition physics.h:44
@ LookY
Definition physics.h:44
@ Throttle
Definition physics.h:44
@ Roll
Definition physics.h:44
float mx[3]
Definition physics.h:46
bool propeller
Definition physics.h:49
Definition mat.h:8
static Mat4 identity()
Definition mat.h:10
Definition physics.h:139
int armor
Definition physics.h:143
float radius
Definition physics.h:147
float height
Definition physics.h:142
int material2
Definition physics.h:144
int material
Definition physics.h:144
int character
Definition physics.h:141
int vehicle
Definition physics.h:140
float pos[3]
Definition physics.h:146
int shot
Definition physics.h:148
int tag
Definition physics.h:145
Definition physics.cpp:419
Definition physics.h:74
std::string upper
Definition physics.h:81
std::string name
Definition physics.h:75
std::string lower
Definition physics.h:81
Mat4 at
Definition physics.h:79
int show
Definition physics.h:80
WeaponDef weapon
Definition physics.h:76
float exit[3]
Definition physics.h:77
float pos[3]
Definition physics.h:78
Definition physics.h:84
std::vector< WheelDef > wheels
Definition physics.h:102
std::vector< FloatDef > floats
Definition physics.h:92
bool ship
Definition physics.h:90
float mass
Definition physics.h:85
float drag
Definition physics.h:85
float differential
Definition physics.h:86
float exit[3]
Definition physics.h:94
std::vector< float > hull
Definition physics.h:103
std::vector< float > armor
Definition physics.h:97
float hitpoints
Definition physics.h:95
float thrust
Definition physics.h:91
WeaponDef weapon
Definition physics.h:99
int material
Definition physics.h:96
std::vector< WingDef > wings
Definition physics.h:101
std::map< std::pair< std::string, int >, JointDef > joints
Definition physics.h:93
std::vector< size_t > hull_split
Definition physics.h:106
bool tracked
Definition physics.h:87
std::vector< SeatDef > seats
Definition physics.h:100
bool plane
Definition physics.h:89
float torque
Definition physics.h:86
float turn_rate
Definition physics.h:88
std::vector< int > armor_material
Definition physics.h:98
Definition physics.h:54
float ttl
Definition physics.h:57
int ammo_type
Definition physics.h:62
std::string name
Definition physics.h:55
int material2
Definition physics.h:58
float interval
Definition physics.h:56
int mag_size
Definition physics.h:59
float reload
Definition physics.h:60
float radius
Definition physics.h:63
bool bounce
Definition physics.h:66
float muzzle_pos[3]
Definition physics.h:70
int material
Definition physics.h:58
bool heals
Definition physics.h:68
std::string end_effect
Definition physics.h:64
int mags
Definition physics.h:59
std::string projectile
Definition physics.h:65
float velocity
Definition physics.h:56
bool fire_once
Definition physics.h:61
float near
Definition physics.h:67
std::string muzzle
Definition physics.h:69
Definition physics.h:18
float steer_deg
Definition physics.h:22
float damping
Definition physics.h:21
bool driven
Definition physics.h:23
float radius
Definition physics.h:20
float steer_rate
Definition physics.h:22
float strength
Definition physics.h:21
float pos[3]
Definition physics.h:19
Definition physics.h:27
float range
Definition physics.h:32
int input
Definition physics.h:30
float flap
Definition physics.h:29
float rate
Definition physics.h:31
float lift
Definition physics.h:29
float sign
Definition physics.h:31
float normal[3]
Definition physics.h:28
float pos[3]
Definition physics.h:28
float regulate
Definition physics.h:29