1 #include <GLviewer/Scenes/SceneBase.hh>
3 #include <Base/Geometry/HomgPoint3D.hh>
4 #include <Base/Geometry/HomgPlane3D.hh>
5 #include <Base/Math/Random.hh>
11 double SceneBase::animationSpeed_ = 1.0;
13 SceneBase::SceneBase() :
14 IsActive_(true), ActiveChannel_(C_BothMono),camera_(NULL), velocity_(0,0,0) {
28 GLboolean maskHist[4];
29 glGetBooleanv(GL_COLOR_WRITEMASK, maskHist);
34 glColorMask(maskHist[0], maskHist[1],
35 maskHist[2], maskHist[3]);
41 std::list<BIAS::HomgPoint3D >
44 std::list<BIAS::HomgPoint3D > cornerlist;
46 cornerlist.push_back(bcur);
48 cornerlist.push_back(bcur);
50 cornerlist.push_back(bcur);
52 cornerlist.push_back(bcur);
55 cornerlist.push_back(bcur);
57 cornerlist.push_back(bcur);
59 cornerlist.push_back(bcur);
61 cornerlist.push_back(bcur);
65 std::list<BIAS::HomgPlane3D >
68 std::list<HomgPlane3D > facelist;
89 std::list<HomgPoint3D > thiscorners =
92 std::list<BIAS::HomgPlane3D> otherfaces =
99 std::list<BIAS::HomgPlane3D >::iterator itfaces;
100 for (itfaces=otherfaces.begin(); itfaces!=otherfaces.end(); itfaces++) {
102 std::list<BIAS::HomgPoint3D >::iterator itcorners;
103 for (itcorners=thiscorners.begin(); itcorners!=thiscorners.end();
105 if (itfaces->ScalarProduct(*itcorners)>=0.0) {
133 std::list<HomgPoint3D > thiscorners =
136 std::list<BIAS::HomgPlane3D> otherfaces =
139 std::list<HomgPoint3D > othercorners =
142 std::list<BIAS::HomgPlane3D> thisfaces =
147 std::list<BIAS::HomgPlane3D >::iterator itfaces;
148 double besttsofar = 1e100;
149 for (itfaces=otherfaces.begin(); itfaces!=otherfaces.end(); itfaces++) {
150 std::list<BIAS::HomgPoint3D >::iterator itcorners;
151 for (itcorners=thiscorners.begin(); itcorners!=thiscorners.end();
158 if (fabs(denominator)>1e-9) {
160 t = (-x.ScalarProduct(n) - (*itfaces)[3]) / denominator;
167 if (t>0.0 && t<besttsofar) {
171 if (cp[0]>=othermin[0] && cp[1]>=othermin[1] && cp[2]>=othermin[2]
172 && cp[0]<=othermax[0] && cp[1]<=othermax[1] && cp[2]<=othermax[2]){
174 contactnormal = itfaces->GetNormalVector();
182 for (itfaces=thisfaces.begin(); itfaces!=thisfaces.end(); itfaces++) {
183 std::list<BIAS::HomgPoint3D >::iterator itcorners;
184 for (itcorners=othercorners.begin(); itcorners!=othercorners.end();
191 if (fabs(denominator)>1e-9) {
193 t = (-x.ScalarProduct(n) - (*itfaces)[3]) / denominator;
200 if (t>0.0 && t<besttsofar) {
204 if (cp[0]>=othermin[0] && cp[1]>=othermin[1] && cp[2]>=othermin[2]
205 && cp[0]<=othermax[0] && cp[1]<=othermax[1] && cp[2]<=othermax[2]){
207 contactnormal = itfaces->GetNormalVector();
257 if (contact[0]!=-1e10) {
259 collisiondirection = contact;
262 if (collisiondirection.
NormL2()<1e-10) {
264 BIASWARN(
"collision only using velocities");
267 if (collisiondirection.
NormL2()<1e-10) {
270 BIASWARN(
"no collision direction could be detected, using x");
283 double m2 = otherscene->
mass_;
301 double v1elas = ((m1-m2)* v1 + 2.0*m2*v2)/(m1+m2);
302 double v2elas = ((m2-m1)* v2 + 2.0*m1*v1)/(m1+m2);
305 double v2plas = (m1*v1 + m2*v2) / (m1+m2);
306 double v1plas = v2plas;
316 + collisiondirection * (elasticity * v1elas + (1.0-elasticity)*v1plas);
321 elasticity * (otherscene->
velocity_ - v2 * collisiondirection)
322 + collisiondirection * (elasticity * v2elas + (1.0-elasticity)*v2plas);
328 const double& frictioncoefficient) {
335 velocity_ *= pow(frictioncoefficient, delta);
351 cout<<
"Scene type: "<<
typeid(*this).name()<<endl;
354 cout<<
"GLProjection: "<<
camera_<<endl;
357 cout<<
"mass: "<<
mass_<<endl;
365 std::list<BIAS::HomgPoint3D > listvertices =
368 vector< Vector3<double> > vertices;
369 vertices.resize(listvertices.size());
370 list<HomgPoint3D>::iterator it = listvertices.begin();
372 for(; it!=listvertices.end(); it++) {
373 vertices[i++] = it->GetEuclidian();
378 glVertex3dv(vertices[0].GetData());
379 glVertex3dv(vertices[1].GetData());
380 glVertex3dv(vertices[1].GetData());
381 glVertex3dv(vertices[2].GetData());
382 glVertex3dv(vertices[2].GetData());
383 glVertex3dv(vertices[3].GetData());
384 glVertex3dv(vertices[3].GetData());
385 glVertex3dv(vertices[0].GetData());
386 glVertex3dv(vertices[4].GetData());
387 glVertex3dv(vertices[5].GetData());
388 glVertex3dv(vertices[5].GetData());
389 glVertex3dv(vertices[6].GetData());
390 glVertex3dv(vertices[6].GetData());
391 glVertex3dv(vertices[7].GetData());
392 glVertex3dv(vertices[7].GetData());
393 glVertex3dv(vertices[4].GetData());
394 glVertex3dv(vertices[0].GetData());
395 glVertex3dv(vertices[4].GetData());
396 glVertex3dv(vertices[5].GetData());
397 glVertex3dv(vertices[1].GetData());
398 glVertex3dv(vertices[2].GetData());
399 glVertex3dv(vertices[6].GetData());
400 glVertex3dv(vertices[3].GetData());
401 glVertex3dv(vertices[7].GetData());
409 const double& friction) {
413 for (
unsigned int i=0; i<scenes.size(); i++) {
418 for (
unsigned int i=0; i<scenes.size(); i++) {
419 for (
unsigned int j=i+1; j<scenes.size(); j++) {
421 scenes[j]->GetGravityImmunity()) {
428 double timetocontact = 1.0;
436 double ttc = scenes[i]->TimeToContact(scenes[j], contactnormal);
437 if (ttc<0.0) ttc = 0.0;
441 scenes[i]->MoveScene(timetocontact);
442 scenes[j]->MoveScene(timetocontact);
444 if (isnan(contactnormal[0]) ||isnan(contactnormal[1])
445 ||isnan(contactnormal[2]) ) {
446 BIASWARN(
"contact normal is nan !");
452 scenes[i]->PerformCollision(scenes[j],contactnormal);
virtual void Render()
this is the entry point seen from GLProjectionParametersBase.
bool renderBoundingBox_
show the bounding box ?
virtual void MoveScene(const double &delta=1.0)
this moves the scene (no rotation yet)
static double animationSpeed_
how long does a single step in time take for animations ?
bool gravityImmunity_
if true, scene is not accelerated by gravity
void ScalarProduct(const Vector3< T > &argvec, T &result) const
scalar product (=inner product) of two vectors, storing the result in result
TRIANGLEMESH_DRAW_STYLE dTriangleMeshDrawStyle_
std::list< BIAS::HomgPlane3D > FacesFromBoundingBox(const BIAS::Vector3< double > &b1, const BIAS::Vector3< double > &b2)
virtual void PerformCollision(SceneBase *otherscene, const BIAS::Vector3< double > &contactnormal=BIAS::Vector3< double >(-1e10,-1e10,-1e10))
compute momentum of each scene after collision
virtual double TimeToContact(SceneBase *otherScene, BIAS::Vector3< double > &collisionpoint)
compute time to contact of bounding boxes
bool GetGravityImmunity() const
is this scene accelerated by gravity ?
virtual void Draw()=0
To do anything usefull, overload this method, assume context is ready and draw.
std::list< BIAS::HomgPoint3D > CornersFromBoundingBox(const BIAS::Vector3< double > &b1, const BIAS::Vector3< double > &b2)
void ApplyTimeStep(const double &delta=1.0, const BIAS::Vector3< double > &gravity=BIAS::Vector3< double >(0,-9.81, 0), const double &frictioncoefficient=0.999)
change position using a small time step of delta seconds and change velocity using gravity vector in ...
void DumpInfo() const
dump info about the scene
BIAS::Vector3< double > velocity_
scene velocity in m/s
double mass_
for collision:
POINT_DRAW_STYLE dPointDrawStyle_
enum EChannel ActiveChannel_
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
virtual bool HasBoundingBoxCollision(SceneBase *otherScene)
check if bounding boxes of two scenes overlap
Base class for all scenes.
static void AnimationAndCollision(std::vector< SceneBase * > scenes, const BIAS::Vector3< double > &gravity=BIAS::Vector3< double >(0, 10, 0), const double &friction=0.999)
call this method for a vector of scenes to solve the collision problem.
virtual void RenderBoundingBox()
render the bounding box of the scene using gl-lines
double elasticity_
the more elastic the less enegry is lost in collisions
virtual void GetBoundingBox(BIAS::Vector3< double > &themin, BIAS::Vector3< double > &themax)
Determine and return the bouning box of all elements of the scene.
Vector3< T > & Normalize()
normalize this vector to length 1
double NormL2() const
the L2 norm sqrt(a^2 + b^2 + c^2)
void SetColorMask(bool red=true, bool green=true, bool blue=true, bool alpha=true)
sets a color mask for the scene rendering
BIAS::GLProjectionParametersInterface * camera_
A homogeneous plane (in P^3) All points X on the plane p fulfill p ' * X = 0.