Commit iniziale: è presente una base del progetto con joint di base
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
#include<math.h>
|
||||
#include<vector>
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
#ifndef RB_H
|
||||
#define RB_H
|
||||
|
||||
namespace rb{
|
||||
typedef std::vector<float> Vector3;
|
||||
typedef std::vector<_Float16> Vector3_s;
|
||||
|
||||
class rigidbody
|
||||
{
|
||||
private:
|
||||
Vector3 vel = {0,0,0};
|
||||
Vector3 acc = {0,0,0};
|
||||
Vector3_s rot = {0,0,0};
|
||||
Vector3 tanAcc = {0,0,0};
|
||||
|
||||
_Float16 mass = 1;
|
||||
|
||||
Vector3 coords = {0,0,0};
|
||||
Vector3 centerOfMass = {0,0,0};
|
||||
|
||||
int64_t prevT = 0;
|
||||
|
||||
//funzioni
|
||||
void calcVel(const float Dtime);
|
||||
void calcRot(const time_t Dtime);
|
||||
void calcAcc(const Vector3 Dacc);
|
||||
void calcTanAcc(const Vector3 Dacc);
|
||||
void calcPos(const float Dtime);
|
||||
|
||||
|
||||
|
||||
public:
|
||||
rigidbody(){}
|
||||
rigidbody(Vector3 coords, Vector3 centerOfMass, _Float16 mass);
|
||||
~rigidbody();
|
||||
|
||||
|
||||
Vector3 getPos();
|
||||
Vector3_s getRot();
|
||||
void setPos(const Vector3 Npos);
|
||||
void setRot(const Vector3_s Nrot);
|
||||
void setVel(const Vector3 Nacc);
|
||||
void setAcc(const Vector3 Nvel);
|
||||
void step(const sf::Clock time);
|
||||
|
||||
//complesso, deve definire accelerazione e accelerazione tangenziale
|
||||
void appForce(Vector3 f, Vector3 pos);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
inline rb::Vector3 operator+(const rb::Vector3& v1, const rb::Vector3& v2) {
|
||||
if (v1.size() != 3 || v2.size() != 3) {
|
||||
throw std::invalid_argument("I vettori devono avere esattamente 3 elementi.");
|
||||
}
|
||||
return rb::Vector3{
|
||||
v1[0] + v2[0],
|
||||
v1[1] + v2[1],
|
||||
v1[2] + v2[2]
|
||||
};
|
||||
}
|
||||
|
||||
inline rb::Vector3 operator-(const rb::Vector3& v1, const rb::Vector3& v2) {
|
||||
if (v1.size() != 3 || v2.size() != 3) {
|
||||
throw std::invalid_argument("I vettori devono avere esattamente 3 elementi.");
|
||||
}
|
||||
return rb::Vector3{
|
||||
v1[0] - v2[0],
|
||||
v1[1] - v2[1],
|
||||
v1[2] - v2[2]
|
||||
};
|
||||
}
|
||||
|
||||
inline rb::Vector3_s operator+(const rb::Vector3_s& v1, const rb::Vector3_s& v2) {
|
||||
if (v1.size() != 3 || v2.size() != 3) {
|
||||
throw std::invalid_argument("I vettori devono avere esattamente 3 elementi.");
|
||||
}
|
||||
return rb::Vector3_s{
|
||||
v1[0] + v2[0],
|
||||
v1[1] + v2[1],
|
||||
v1[2] + v2[2]
|
||||
};
|
||||
}
|
||||
|
||||
inline rb::Vector3_s operator-(const rb::Vector3_s& v1, const rb::Vector3_s& v2) {
|
||||
if (v1.size() != 3 || v2.size() != 3) {
|
||||
throw std::invalid_argument("I vettori devono avere esattamente 3 elementi.");
|
||||
}
|
||||
return rb::Vector3_s{
|
||||
v1[0] - v2[0],
|
||||
v1[1] - v2[1],
|
||||
v1[2] - v2[2]
|
||||
};
|
||||
}
|
||||
/*
|
||||
inline bool operator!=(const rb::Vector3_s& v1, const rb::Vector3_s& v2) {
|
||||
if (v1.size() != 3 || v2.size() != 3) {
|
||||
throw std::invalid_argument("I vettori devono avere esattamente 3 elementi.");
|
||||
}
|
||||
return true;
|
||||
}*/
|
||||
#endif
|
||||
Executable
+100
@@ -0,0 +1,100 @@
|
||||
#include "../headers/rb.hpp"
|
||||
|
||||
|
||||
using namespace rb ;
|
||||
|
||||
rigidbody::rigidbody(Vector3 coords, Vector3 centerOfMass, _Float16 mass)
|
||||
{
|
||||
if (coords.size() != 3) throw "Coords must be 3";
|
||||
if (centerOfMass.size() != 3) throw "COM coords must be 3";
|
||||
|
||||
this->coords = coords;
|
||||
this->centerOfMass = centerOfMass;
|
||||
this->mass = mass;
|
||||
|
||||
}
|
||||
|
||||
rigidbody::~rigidbody()
|
||||
{
|
||||
}
|
||||
|
||||
Vector3 rigidbody::getPos(){
|
||||
return Vector3 {coords};
|
||||
}
|
||||
Vector3_s rigidbody::getRot(){
|
||||
return Vector3_s {rot};
|
||||
}
|
||||
|
||||
void rigidbody::setPos(Vector3 Npos){
|
||||
if (Npos.size() != 3) throw "Pos must be 3 in lenght!";
|
||||
|
||||
int i = 0;
|
||||
for (float axis : Npos){
|
||||
coords[i] = axis;
|
||||
i++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void rigidbody::setAcc(const Vector3 Nacc){
|
||||
if (Nacc.size() != 3) throw "Vel vector must be 3 in lenght!";
|
||||
|
||||
int i = 0;
|
||||
for (float axis : Nacc){
|
||||
acc[i] = axis;
|
||||
i++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void rigidbody::setRot(const Vector3_s Nrot){
|
||||
if (Nrot.size() != 3) throw "Vel vector must be 3 in lenght!";
|
||||
|
||||
int i = 0;
|
||||
for (float axis : Nrot){
|
||||
rot[i] = axis;
|
||||
i++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void rigidbody::calcVel(const float Dtime){
|
||||
Vector3 tmpVel;
|
||||
|
||||
for (float a : acc){
|
||||
//if (a>0.8 || a<-0.8)
|
||||
tmpVel.push_back( a*Dtime );
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
for (float nv : tmpVel){
|
||||
vel[i++] += nv;
|
||||
}
|
||||
}
|
||||
|
||||
void rigidbody::calcPos(const float Dtime){
|
||||
Vector3 tmpPos;
|
||||
|
||||
for (float v : vel){
|
||||
tmpPos.push_back( v*Dtime );
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
for (float np : tmpPos){
|
||||
coords[i++] += np *100;//(np* cos(float(rot[i]))) *100;
|
||||
}
|
||||
}
|
||||
|
||||
void rigidbody::step(const sf::Clock time){
|
||||
int64_t Dtime = time.getElapsedTime().asMicroseconds();
|
||||
if (prevT == 0) prevT = Dtime;
|
||||
|
||||
|
||||
float dt = (float(Dtime) / 1000000.0) - (float(prevT) / 1000000.0);
|
||||
prevT = Dtime;
|
||||
|
||||
calcPos(dt);
|
||||
calcVel(dt);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user