source: flair-src/trunk/lib/FlairSimulator/src/Ball.cpp@ 395

Last change on this file since 395 was 361, checked in by Sanahuja Guillaume, 4 years ago

add ball for Julio

File size: 5.6 KB
Line 
1// %flair:license{
2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
4// %flair:license}
5// created: 2020/05/26
6// filename: Ball.cpp
7//
8// author: Guillaume Sanahuja
9// Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11// version: $Id: $
12//
13// purpose: classe chargeant une balle
14//
15/*********************************************************************/
16#ifdef GL
17
18#include "Ball.h"
19#include "Gui.h"
20#include "Euler.h"
21#include <ISceneManager.h>
22#include <IMeshSceneNode.h>
23#include <ITexture.h>
24#include <Tab.h>
25#include <DoubleSpinBox.h>
26#include <Vector3DSpinBox.h>
27#include <PushButton.h>
28#include "FollowMeCamera.h"
29
30#define G (float)9.81 // gravity ( N/(m/s²) )
31
32using namespace irr;
33using namespace irr::core;
34using namespace irr::scene;
35using namespace irr::video;
36using namespace flair::core;
37using namespace flair::gui;
38
39namespace flair {
40namespace simulator {
41
42Ball::Ball(std::string name,uint32_t modelId) : Model(name,modelId) {
43
44 node = getGui()->getSceneManager()->addSphereSceneNode(100,16,getSceneNode());
45
46 ITexture *texture = getGui()->getTexture("ball.jpg");
47 node->setMaterialTexture(0, texture);
48 node->setMaterialFlag(video::EMF_LIGHTING, false);
49
50 getFollowMeCamera()->setPositionOffset(vector3df(0, 0, 0));
51 getFollowMeCamera()->setTargetOffset(vector3df(0, 0, 0));
52
53 setTriangleSelector(getGui()->getSceneManager()->createTriangleSelector(node->getMesh(),node));
54
55 Tab *setup_tab = new Tab(GetTabWidget(), "model");
56 radius = new DoubleSpinBox(setup_tab->NewRow(), "radius :", "m",0, 1, 0.1,2,0.05);
57 weight = new DoubleSpinBox(setup_tab->NewRow(), "weight :", "Kg",0, 5, 0.1,2,0.5);
58 Km = new DoubleSpinBox(setup_tab->NewRow(), "Km :",0, 1, 0.1,2,0.05);
59
60 Tab *throw_tab = new Tab(GetTabWidget(), "throw");
61 velInit = new Vector3DSpinBox(throw_tab->NewRow(), "initial velocity :", -15, 15, 0.1);
62 throwButton = new PushButton(throw_tab->NewRow(), "throw");
63 resetButton = new PushButton(throw_tab->NewRow(), "reset position");
64
65 node->setScale(vector3df(radius->Value(), radius->Value(), radius->Value()));
66
67 throwing=false;
68
69 SetIsReady(true);
70}
71
72Ball::~Ball() {}
73
74void Ball::CalcModel(void) {
75 if(throwButton->Clicked()) {
76 throwing=true;
77 state[-1].Vel = velInit->Value();
78 state[-2].Vel = velInit->Value();
79 }
80 if(resetButton->Clicked()) {
81 throwing=false;
82 }
83
84 if(throwing) {
85 state[0].Pos = state[-1].Pos + dT() * state[-1].Vel + Vector3D<double>(0,0, weight->Value()*G*dT()*dT()/2);
86 state[0].Vel = state[-1].Vel + dT() * (state[-1].Vel * velInit->Value().GetNorm()+Vector3D<double>(-Km->Value(),-Km->Value(),-Km->Value()+weight->Value()*G) );
87 } else {
88 state[0].Pos=InitialPosition();
89 }
90 //Printf("pos %f %f %f\n",state[0].Pos.x,state[0].Pos.y,state[0].Pos.z);
91 //Printf("vel %f %f %f\n",state[0].Vel.x,state[0].Vel.y,state[0].Vel.z);
92 //From Julio:
93 /*
94 x(k+1) = x(k) + h*(vx(:,k));
95 y(k+1) = y(k) + h*(vy(:,k));
96 z(k+1) = z(k) + h*(vz(:,k)) - (g*h^2/2) ;
97
98 vx(k+1) = vx(k) + h*(vx(k)*v*-Km);
99 vy(k+1) = vy(k) + h*(vy(k)*v*-Km);
100 vz(k+1) = vz(k) + h*(vz(k)*v*-Km-g);
101*/
102
103}
104
105void Ball::AnimateModel(void){
106// adapt size
107 if (radius->ValueChanged() == true) {
108 node->setScale(vector3df(radius->Value(), radius->Value(), radius->Value()));
109 }
110}
111
112size_t Ball::dbtSize(void) const { return 6 * sizeof(float); }
113
114void Ball::WritedbtBuf(char *dbtbuf) { /*
115 float *buf=(float*)dbtbuf;
116 vector3df vect=node->getPosition();
117 memcpy(buf,&vect.X,sizeof(float));
118 buf++;
119 memcpy(buf,&vect.Y,sizeof(float));
120 buf++;
121 memcpy(buf,&vect.Z,sizeof(float));
122 buf++;
123 vect=node->getRotation();
124 memcpy(buf,&vect.X,sizeof(float));
125 buf++;
126 memcpy(buf,&vect.Y,sizeof(float));
127 buf++;
128 memcpy(buf,&vect.Z,sizeof(float));
129 buf++;*/
130}
131
132void Ball::ReaddbtBuf(char *dbtbuf) { /*
133 float *buf=(float*)dbtbuf;
134 vector3df vect;
135 memcpy(&vect.X,buf,sizeof(float));
136 buf++;
137 memcpy(&vect.Y,buf,sizeof(float));
138 buf++;
139 memcpy(&vect.Z,buf,sizeof(float));
140 buf++;
141 node->setPosition(vect);
142 memcpy(&vect.X,buf,sizeof(float));
143 buf++;
144 memcpy(&vect.Y,buf,sizeof(float));
145 buf++;
146 memcpy(&vect.Z,buf,sizeof(float));
147 buf++;
148 node->setRotation(vect);
149 node->setAnimationSpeed(2.f);*/
150}
151
152} // end namespace simulator
153} // end namespace flair
154#endif // GL
Note: See TracBrowser for help on using the repository browser.