Sure
here are simplugin.hpp and simplugin.cpp.
Note that I'd be glad to remove all lines of code except those related with read write to variables..but when I cancelled them I got errors in compiling. I got the code from republic of texas and it works without problems when I spawn the missile (getting the plugin) from F3
#pragma once
#include <comm/intergen/ifc.h>
//wrap in special comments to inject into the generated interface header too
// can also use /*ifc{ ... }ifc*/ to include only in the client header
//ifc{
#include <ot/vehicle_physics.h>
#include "ot/explosions.h"
#include "ot/environment.h"
#include "ot/canvas.h"
//}ifc
///Plugin's base implementation class, exposing a xt::engine interface
class simplugin
: public policy_intrusive_base
{
public:
simplugin();
~simplugin() {};
///Interface declaration: [namespace::]name, path
ifc_class(xt::global_variables, "ifc/");
///Interface creator
ifc_fnx(get) static iref<simplugin> get()
{
return new simplugin;
}
//interface function examples
ifc_fn double readDat(int index);
ifc_fn bool writeDat(int index, double data);
ifc_fn void writeEcef(int index, double x, double y, double z, int id);
ifc_fn double readEcefx(int index);
ifc_fn double readEcefy(int index);
ifc_fn double readEcefz(int index);
ifc_fn int readEcefId(int index);
ifc_fn void writeQuat(int index, double x, double y, double z, double w, int id);
ifc_fn double readQuatx(int index);
ifc_fn double readQuaty(int index);
ifc_fn double readQuatz(int index);
ifc_fn double readQuatw(int index);
ifc_fn int readQuatId(int index);
ifc_fn void init_chassis(iref<ot::vehicle_physics> obj);
ifc_fn void init_vehicle(iref<ot::vehicle_physics> obj);
ifc_fn void update_vehicle(float dt, float throttle, float brake, float steer);
ifc_fn void firething();
ifc_fn void turretthing(float v, float dt);
ifc_fn void mantletthing(float v, float dt);
private:
iref<ot::geomob> m_geomob;
iref<ot::sndgrp> m_sndgrp;
iref<ot::explosions> m_explosion;
iref<ot::environment> m_environment;
iref<ot::canvas> m_canvas;
int _counter;
iref<ot::vehicle_physics> _vehicle;
};
#pragma once
#include "simplugin.hpp"
#include "v8/v8.h"
#include <ot/vehicle_physics.h>
#include <ot/vehicle_cfg.h>
#include <ot/explosions.h>
#include <ot/environment.h>
#include <ot/canvas.h>
#include <ot/dynamic_object.h>
#include <ot/static_object.h>
#include <glm/gtc/quaternion.hpp>
#include <ot/location_cfg.h>
#include <algorithm>
#include <string>
#include <array>
ot::wheel wheel_params;
ot::vehicle_params physics_params;
const double PI = 3.141592653589793;
std::array<double, 100> globalData;
std::array<double, 100> globalLat;
std::array<double, 100> globalLon;
std::array<int, 100> globalId;
std::array<double, 100> globalEcefx;
std::array<double, 100> globalEcefy;
std::array<double, 100> globalEcefz;
std::array<double, 100> globalQuatx;
std::array<double, 100> globalQuaty;
std::array<double, 100> globalQuatz;
std::array<double, 100> globalQuatw;
std::array<int, 100> globalQuatId;
uint fnt ;
uint img ;
uint img2 ;
std::array<double,2> screensize;
double simplugin::readDat(int index)
{
return globalData[index];
}
bool simplugin::writeDat(int index, double data)
{
globalData[index] = data;
return true;
}
void simplugin::writeEcef(int index, double x, double y, double z, int id)
{
globalEcefx[index] = x;
globalEcefy[index] = y;
globalEcefz[index] = z;
globalId[index] = id;
}
double simplugin::readEcefx(int index)
{
return globalEcefx[index];
}
double simplugin::readEcefy(int index)
{
return globalEcefy[index];
}
double simplugin::readEcefz(int index)
{
return globalEcefz[index];
}
int simplugin::readEcefId(int index)
{
return globalId[index];
}
void simplugin::writeQuat(int index, double x, double y, double z, double w, int id)
{
globalQuatx[index] = x;
globalQuaty[index] = y;
globalQuatz[index] = z;
globalQuatw[index] = w;
globalQuatId[index] = id;
}
double simplugin::readQuatx(int index)
{
return globalQuatx[index];
}
double simplugin::readQuaty(int index)
{
return globalQuaty[index];
}
double simplugin::readQuatz(int index)
{
return globalQuatz[index];
}
double simplugin::readQuatw(int index)
{
return globalQuatw[index];
}
int simplugin::readQuatId(int index)
{
return globalQuatId[index];
}
quat Quatmult(quat q, quat p) {
quat r = quat(0, 0, 0, 0);
r.x = q.x * p.w + q.y * p.z + q.w * p.x - q.z * p.y;
r.y = q.z * p.x + q.w * p.y + q.y * p.w - q.x * p.z;
r.z = q.w * p.z + q.z * p.w + q.x * p.y - q.y * p.x;
r.w = -(q.y * p.y + q.x * p.x + q.z * p.z - q.w * p.w);
return r;
}
double rad2deg(double rad)
{
return rad * (180.f / PI);
}
double deg2rad(double deg)
{
return deg * (PI / 180.f);
}
simplugin::simplugin() {
m_sndgrp = ot::sndgrp::create();
}
float axle2coef = 1.f;
glm::quat toQuatRot(float angle, glm::vec3 axis) {
glm::quat returnQuat = glm::quat(0, 0, 0, 0);
returnQuat.x = axis.x * glm::sin(angle / 2);
returnQuat.y = axis.y * glm::sin(angle / 2);
returnQuat.z = axis.z * glm::sin(angle / 2);
returnQuat.w = glm::cos(angle / 2);
return returnQuat;
}
void simplugin::init_chassis(iref<ot::vehicle_physics> obj)
{
iref<ot::geomob> geomob = obj->get_geomob(0);
wheel_params.radius1 = 0.7f;
wheel_params.width = 0.45f;
wheel_params.differential = true;
wheel_params.suspension_max = 0.2f;
wheel_params.suspension_min = -0.35f;
wheel_params.suspension_stiffness = 5.f;
wheel_params.damping_compression = 0.2f;
wheel_params.damping_relaxation = 0.12f;
wheel_params.grip = 1.f;
wheel_params.slip_lateral_coef = 3;
obj->add_wheel_swing("halfaxle_l0", "tire_l0", wheel_params);
obj->add_wheel_swing("halfaxle_r0", "tire_r0", wheel_params);
obj->add_wheel_swing("halfaxle_l1", "tire_l1", wheel_params);
obj->add_wheel_swing("halfaxle_r1", "tire_r1", wheel_params);
obj->add_wheel_swing("halfaxle_l2", "tire_l2", wheel_params);
obj->add_wheel_swing("halfaxle_r2", "tire_r2", wheel_params);
obj->add_wheel_swing("halfaxle_l3", "tire_l3", wheel_params);
obj->add_wheel_swing("halfaxle_r3", "tire_r3", wheel_params);
float a0 = geomob->get_joint_model_pos(geomob->get_joint("tire_l0")).y;
float a1 = geomob->get_joint_model_pos(geomob->get_joint("tire_l1")).y;
float a2 = geomob->get_joint_model_pos(geomob->get_joint("tire_l2")).y;
float a3 = geomob->get_joint_model_pos(geomob->get_joint("tire_l3")).y;
float m = 0.5f * (a2 + a3);
axle2coef = (a1 - m) / (a0 - m);
}
void simplugin::init_vehicle(iref<ot::vehicle_physics> obj)
{
m_geomob = obj->get_geomob(0);
m_explosion = ot::explosions::get();
m_environment = ot::environment::get();
m_canvas = ot::canvas::create("main");
fnt = m_canvas->load_font("ui/default.fnt");
img = m_canvas->load_image("ui/basic.imgset/airspeed");
img2 = m_canvas->load_image("ui/basic.imgset/airspeed_pointer");
screensize[0] = readDat(0); /* x */
screensize[1] = readDat(1); /* y */
_vehicle = obj;
float3 offsetPos = float3(1, 1, 1);
double3 offset = m_geomob->get_world_pos_offset(offsetPos);
glm::quat rotation = m_geomob->get_rot();
auto firstbullet = m_explosion->launch_combo(offset, float3(75), 10, float3(1, 1, 0), float3(0.3, 0.3, 0.3), 7, 0.1, 0.1, 20, true, true, true);
m_explosion->destroy_tracer(firstbullet);
}
coid::uint exps;
quat QuatConjug(quat QuatIn) {
quat Quat = quat(0, 0, 0, 0);
Quat.x = -QuatIn.x;
Quat.y = -QuatIn.y;
Quat.z = -QuatIn.z;
Quat.w = QuatIn.w;
return Quat;
}
float turret_axis = 0, mantlet_axis = 0;
float turret_rot = 0, mantlet_rot = 0, armor_pitch = 0;
void simplugin::turretthing(float v, float dt)
{
turret_axis = -v;
}
void simplugin::mantletthing(float v, float dt)
{
mantlet_axis = v;
}
unsigned int index;
void simplugin::firething()
{
glm::vec3 mdc = glm::vec3(0, 0, 7);
double3 offset = m_geomob->get_world_pos_offset(mdc);
//mantlet_rot = std::max(0.f, std::min(0.5f, mantlet_rot));
_vehicle->log(coid::token((std::to_string(mantlet_rot) + " Hio").c_str()));
float armor_pitch = mantlet_rot;
float armor_azimuth = -turret_rot;
glm::quat cannonQuat = m_geomob->get_rot();
glm::quat unitquat = glm::quat(0, 1, 0, 0); // default muzzle orientation
glm::quat pitchquat = toQuatRot(armor_pitch, glm::vec3(1, 0, 0));
glm::quat headquat = toQuatRot(armor_azimuth, glm::vec3(0, 0, 1));
glm::quat tempquat = Quatmult(pitchquat, Quatmult(unitquat, QuatConjug(pitchquat)));
tempquat = Quatmult(headquat, Quatmult(tempquat, QuatConjug(headquat)));
glm::quat plasmaquat = Quatmult(cannonQuat, Quatmult(tempquat, QuatConjug(cannonQuat)));
glm::quat bulletquat = Quatmult(cannonQuat, Quatmult(headquat, Quatmult(pitchquat, unitquat)));
auto bullet = ot::static_object::create("outerra/crate/crate", offset, bulletquat);
m_explosion->launch_tracer(offset, float3(827 * plasmaquat.x, 827 * plasmaquat.y, 827 * plasmaquat.z), 1, glm::vec3(1, 1, 0), 0.5, 0.2, 0, 0, 0, bullet->get_geomob(0)->get_eid(), 1);
}
float engine_force = 27000.f;
float brake_force = 28000.f;
float wheel_friction = 200.f;
void simplugin::update_vehicle(float dt, float throttle, float brake, float steer)
{
m_canvas->draw_text(fnt, screensize[0]/2, screensize[1] / 2, "ciao", uchar4(0, 255, 0, 255));
float speed_kmh = _vehicle->speed();
float applied_engine_force = engine_force * abs(throttle);
_vehicle->wheel_force(-1, applied_engine_force);
_vehicle->steer(0, steer);
_vehicle->steer(1, steer);
_vehicle->steer(2, steer * axle2coef);
_vehicle->steer(3, steer * axle2coef);
float applied_wheel_friction = brake_force * brake + wheel_friction;
_vehicle->wheel_brake(-1, applied_wheel_friction);
for (ot::impact_info i : m_explosion->landed_tracers())
{
m_explosion->make_crater(i.wpos, 10);
}
turret_rot += turret_axis * 0.4 * dt;
mantlet_rot += mantlet_axis * 0.1 * dt;
}